package com.zcxk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.dto.UserGroupDto;
import com.zcxk.entity.UserEntity;
import com.zcxk.entity.UserGroup;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.UserGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户组(UserGroup)控制层
 *
 * @author makejava
 * @since 2020-10-27 09:13:56
 */
@RestController
@RequestMapping("/userGroup")
@Api(tags = "用户组")
public class UserGroupController {
    /**
     * 服务对象
     */
    @Autowired
    private UserGroupService userGroupService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.POST)
    @ApiOperation(value = "查询用户组")
    public AjaxMessage<UserGroup> selectOne(
            @ApiParam(value = "设置配置", required = true)@RequestParam Integer id) {
        UserGroup result = userGroupService.selectById(id);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 新增一条数据
     *
     * @param userGroup 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ApiOperation(value = "插入用户组")
    public AjaxMessage<Integer> insert(@ApiParam(value = "设置配置", required = true) @RequestBody UserGroup userGroup) {
        int result = userGroupService.insert(userGroup);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 修改一条数据
     *
     * @param userGroup 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "更新用户组")
    public AjaxMessage<Integer> update(@ApiParam(value = "设置配置", required = true) @RequestBody UserGroup userGroup) {
        int result = userGroupService.update(userGroup);
        if(result==-1){
            return new AjaxMessage<>(ResultStatus.USERGROUP_HAS_USER);
        }
        return new AjaxMessage<>(ResultStatus.OK, result);

    }



    /**
     * 分页查询
     *
     * @param pageNum  偏移
     * @param pageSize 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.POST)
    @ApiOperation(value = "查询用户组列表")
    public AjaxMessage<Pagination<UserGroup>> selectPage(Integer pageNum, Integer pageSize,String name) {
        UserGroup userGroup = new UserGroup();
        userGroup.setGroupName(name);
        IPage<UserGroup> iPage = new Page<>(pageNum, pageSize);
        iPage = userGroupService.selectPage(userGroup, iPage);
        Pagination<UserGroup> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }
    @RequestMapping(value = "findUsersByUserGroup", method = RequestMethod.POST)
    @ApiOperation(value = "查询用户组对应user")
    AjaxMessage<List<UserEntity>>  findUsersByUserGroup(@ApiParam(value = "用户组id", required = true)@RequestParam  Integer id){
        UserGroup userGroup=new UserGroup();
        userGroup.setId(id);
        return new AjaxMessage<>(ResultStatus.OK,
                userGroupService.findUsersByUserGroup(userGroup));
    }
    @RequestMapping(value = "updateRolesByUserGroup", method = RequestMethod.POST)
    @ApiOperation(value = "绑定用户组")
    AjaxMessage  updateRolesByUserGroup(@ApiParam(value = "用户组绑定信息", required = true) @RequestBody
                                                UserGroupDto userGroupDto){

        userGroupService.updateRolesByUserGroup(userGroupDto);
        return new AjaxMessage<>(ResultStatus.OK);
    }
    @RequestMapping(value = "deleteAll", method = RequestMethod.POST)
    @ApiOperation(value = "删除用户组")
    AjaxMessage<List<String>> deleteAll(@ApiParam(value = "用户组id", required = true)@RequestBody List<Integer> ids){

        return new AjaxMessage<>(ResultStatus.OK,
                userGroupService.deleteAll(ids));
    }


}