package com.bweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.SecRole;
import com.hfkj.model.ResponseData;
import com.hfkj.service.sec.SecRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: SecRoleController
 * @author: HuRui
 * @date: 2024/3/27
 **/
@Controller
@RequestMapping(value="/secRole")
@Api(value="系统用户角色管理")
public class SecRoleController {

    Logger log = LoggerFactory.getLogger(SecUserController.class);

    @Resource
    private SecRoleService secRoleService;

    @RequestMapping(value="/editRole",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑角色")
    public ResponseData editRole(@RequestBody SecRole body) {
        try {
            if (body == null || StringUtils.isBlank(body.getRoleName())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            SecRole secRole;
            if (body.getId() != null) {
                // 查询角色
                secRole = secRoleService.getDetail(body.getId());
                if (secRole == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            } else  {
                secRole = new SecRole();
                secRole.setStatus(1);
            }
            secRole.setRoleName(body.getRoleName());
            secRole.setRoleDesc(body.getRoleDesc());
            secRoleService.editData(secRole);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/delRole",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除角色")
    public ResponseData delRole(@RequestBody SecRole body) {
        try {
            if (body == null || body.getId() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            secRoleService.delete(body.getId());

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/queryDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询详情")
    public ResponseData queryDetail(@RequestParam(value = "roleId" , required = true) Long roleId) {
        try {

            secRoleService.delete(roleId);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/queryList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData queryList(@RequestParam(value = "roleName" , required = false) String roleName,
                                  @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                  @RequestParam(value = "pageSize" , required = true) Integer pageSize) {
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("roleName", roleName);

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(secRoleService.getList(param)));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/queryAllRole",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询全部角色")
    public ResponseData queryAllRole() {
        try {

            return ResponseMsgUtil.success(secRoleService.getList(new HashMap<>()));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }


}
