package com.foreverwin.mesnac.meapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.meapi.model.NwaUser;
import com.foreverwin.mesnac.meapi.service.NwaUserService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author robert
 * @since 2021-06-16
 */
@RestController
@RequestMapping("/Z-NWA-USER")
public class NwaUserController {

    @Autowired
    public NwaUserService nwaUserService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getNwaUserById(@PathVariable String id)
    {
        return R.ok( nwaUserService.getById(id));
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getNwaUserList(NwaUser nwaUser){
        List<NwaUser> result;
        QueryWrapper<NwaUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(nwaUser);
        result = nwaUserService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<NwaUser> frontPage, NwaUser nwaUser){
        IPage result;
        QueryWrapper<NwaUser> queryWrapper = new QueryWrapper<>();
        String site = CommonMethods.getSite();
        nwaUser.setSite(site);
        queryWrapper.setEntity(nwaUser);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(NwaUser::getHandle, frontPage.getGlobalQuery())
                .or().like(NwaUser::getUserName, frontPage.getGlobalQuery())
                .or().like(NwaUser::getFirstName, frontPage.getGlobalQuery())
                .or().like(NwaUser::getLastName, frontPage.getGlobalQuery())
                .or().like(NwaUser::getFullName, frontPage.getGlobalQuery())
                .or().like(NwaUser::getPassword, frontPage.getGlobalQuery())
                .or().like(NwaUser::getSalt, frontPage.getGlobalQuery())
                .or().like(NwaUser::getPhone, frontPage.getGlobalQuery())
                .or().like(NwaUser::getAvatar, frontPage.getGlobalQuery())
                .or().like(NwaUser::getDeptId, frontPage.getGlobalQuery())
                .or().like(NwaUser::getLockFlag, frontPage.getGlobalQuery())
                .or().like(NwaUser::getDelFlag, frontPage.getGlobalQuery())
                .or().like(NwaUser::getSite, frontPage.getGlobalQuery())
                .or().like(NwaUser::getDeptDesc, frontPage.getGlobalQuery())
                .or().like(NwaUser::getJobTitle, frontPage.getGlobalQuery())
                .or().like(NwaUser::getEmail, frontPage.getGlobalQuery())
    );
        }
        result = nwaUserService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

    /**
     * 新增
     * @param nwaUser  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping
    public R save(@RequestBody NwaUser nwaUser) {
        LocalDateTime now = LocalDateTime.now();
        nwaUser.setCreateTime(now);
        nwaUser.setUpdateTime(now);
        return R.ok(nwaUserService.save(nwaUser));
    }

    /**
     * 修改
     * @param nwaUser  传递的实体
     * @return  null 失败  实体成功
     */
    @PutMapping
    public R updateById(@RequestBody NwaUser nwaUser) {
		LocalDateTime now = LocalDateTime.now();
        nwaUser.setUpdateTime(now);
        return R.ok(nwaUserService.updateById(nwaUser));
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(nwaUserService.removeById(id));
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/delete-batch")
    public R removeByIds(List<String> ids){
        return R.ok(nwaUserService.removeByIds(ids));
    }


    /**
     * 不分页查询
     * @return
     */
    @GetMapping("findAllUser")
    public R findAllUser(){
        return R.ok(nwaUserService.findAllUser(CommonMethods.getSite()));
    }
}