package com.sckw.system.controller;

import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.system.model.vo.req.EditRoleReqVo;
import com.sckw.system.model.vo.req.UserBindRoleReqVo;
import com.sckw.system.service.KwsRoleService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 角色
 *
 * @author zk
 * @date 2023-05-31
 */
@RestController
@RequestMapping("/kwsRole")
public class KwsRoleController {

    @Autowired
    private KwsRoleService kwsRoleService;

    /**
     * @param id 主键ID
     * @return
     * @description 根据主键查询
     * @author zk
     * @date 2023/5/30
     **/
    @GetMapping("/detail")
    public HttpResult selectByKey(Long id) {
        return HttpResult.ok(kwsRoleService.selectByKey(id));
    }

    /**
     * 查询当前企业拥有的角色列表
     *
     * @return
     */
    @GetMapping("/list")
    public HttpResult list(Long entId) {
        return HttpResult.ok(kwsRoleService.queryList(entId));
    }

    /**
     * @param params {page：页数、pageSize：每页条数、。。。}
     * @return
     * @description 分页查询
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/select")
    public HttpResult select(@RequestBody HashMap params) {
        return HttpResult.ok(kwsRoleService.findPage(params));
    }

    /**
     * @param reqVo {name:角色名称}
     * @return
     * @description 新增
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/add")
    public HttpResult add(@Valid @RequestBody EditRoleReqVo reqVo) {
        reqVo.setAdminFlag(2);
        kwsRoleService.add(reqVo);
        return HttpResult.ok(HttpStatus.MSG_003);
    }

    /**
     * @param reqVo {id：主键ID、name:角色名称}
     * @return
     * @description 更新
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/update")
    public HttpResult update(@Valid @RequestBody EditRoleReqVo reqVo) {
        kwsRoleService.update(reqVo);
        return HttpResult.ok(HttpStatus.MSG_005);
    }

    /**
     * @param ids {ids：主键ID（多个以逗号隔开）}
     * @return
     * @description 更新
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/dels")
    public HttpResult del(@RequestParam String ids) {
        kwsRoleService.deleteByKey(ids);
        return HttpResult.ok(HttpStatus.MSG_008);
    }

    /**
     * @param reqVo 多个角色用","隔开
     * @return HttpResult
     * @desc: 用戶分配角色
     * @author: czh
     * @date: 2023/6/13
     */
    @PostMapping("userBindRole")
    public HttpResult userBindRole(@RequestBody UserBindRoleReqVo reqVo) {
        kwsRoleService.userBindRole(reqVo);
        return HttpResult.ok(HttpStatus.MSG_010);
    }

    @PostMapping(value = "export", produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@RequestBody HashMap params, HttpServletResponse response) {
        kwsRoleService.export(params, response);
    }

}
