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.UserGroup;
import com.foreverwin.mesnac.meapi.service.UserGroupService;
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.util.List;
import java.util.Map;

/**
 *
 * @author pavel.Liu
 * @since 2021-06-18
 */
@RestController
@RequestMapping("/USER-GROUP")
public class UserGroupController {

    @Autowired
    public UserGroupService userGroupService;

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

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getUserGroupList(UserGroup userGroup){
        String site = CommonMethods.getSite();
        userGroup.setSite(site);
        List<UserGroup> result;
        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(userGroup);
        result = userGroupService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<UserGroup> frontPage, UserGroup userGroup){
        IPage result;
        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(userGroup);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(UserGroup::getHandle, frontPage.getGlobalQuery())
                .or().like(UserGroup::getSite, frontPage.getGlobalQuery())
                .or().like(UserGroup::getUserGroup, frontPage.getGlobalQuery())
                .or().like(UserGroup::getDescription, frontPage.getGlobalQuery())
                .or().like(UserGroup::getWorkstationBo, frontPage.getGlobalQuery())
    );
        }
        result = userGroupService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

    /**
     * 新增
     * @param userGroup  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping
    public R save(@RequestBody UserGroup userGroup) {
        return R.ok(userGroupService.save(userGroup));
    }

    /**
     * 修改
     * @param userGroup  传递的实体
     * @return  null 失败  实体成功
     */
    @PutMapping
    public R updateById(@RequestBody UserGroup userGroup) {
        return R.ok(userGroupService.updateById(userGroup));
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(userGroupService.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(userGroupService.removeByIds(ids));
    }


    @ResponseBody
    @GetMapping("/findAbnormalUserGroup")
    public R findAbnormalUserGroup(@RequestParam(required = false) Map<String,Object> param){
        try{
            return R.ok(userGroupService.findAbnormalUserGroup(param));
        }catch (Exception e){
            return R.failed("找寻用户组失败"+e.getMessage());
        }
    }
}












