package com.hzs.system.sys.controller.manage;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.ENormalDisable;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.system.sys.SysRole;
import com.hzs.common.domain.system.sys.SysRoleScope;
import com.hzs.system.sys.param.SysRoleParam;
import com.hzs.system.sys.param.SysRoleQueryParam;
import com.hzs.system.sys.service.ISysRoleScopeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hzs.common.core.constant.UserConstants;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.sys.service.ISysRoleService;

/**
 * 角色信息
 *
 * @author hzs
 */
@RestController
@RequestMapping("/manage/role")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService iSysRoleService;
    @Autowired
    private ISysRoleScopeService iSysRoleScopeService;

    /**
     * 列表
     *
     * @param param
     * @return
     */
    @AccessPermissions("role")
    @Log(module = EOperationModule.ROLE_CONFIG, business = EOperationBusiness.ROLE_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(SysRoleQueryParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        startPage();
        List<SysRole> list = iSysRoleService.selectRoleList(param);
        return getDataTable(list);
    }

    /**
     * 导出
     *
     * @param response
     * @param param
     */
    @Log(module = EOperationModule.ROLE_CONFIG, business = EOperationBusiness.ROLE_CONFIG, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRoleQueryParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        List<SysRole> list = iSysRoleService.selectRoleList(param);
        ExcelUtil<SysRole> util = new ExcelUtil<>(SysRole.class);
        util.exportExcel(response, list, "角色导出");
    }

    /**
     * 新增角色
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.ROLE_CONFIG, business = EOperationBusiness.ROLE_CONFIG, method = EOperationMethod.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRoleParam param) {
        if (UserConstants.NOT_UNIQUE.equals(iSysRoleService.checkRoleNameUnique(param))) {
            return AjaxResult.error("新增角色'" + param.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(iSysRoleService.checkRoleKeyUnique(param))) {
            return AjaxResult.error("新增角色'" + param.getRoleName() + "'失败，角色权限已存在");
        }
        return toAjax(iSysRoleService.insertRole(param, SecurityUtils.getUserId()));
    }

    /**
     * 修改角色
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.ROLE_CONFIG, business = EOperationBusiness.ROLE_CONFIG, method = EOperationMethod.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRoleParam param) {
        if (null == param.getRoleId()) {
            return AjaxResult.error("缺少参数");
        }
        iSysRoleService.checkRoleAllowed(param.getRoleId());
        if (UserConstants.NOT_UNIQUE.equals(iSysRoleService.checkRoleNameUnique(param))) {
            return AjaxResult.error("修改角色'" + param.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(iSysRoleService.checkRoleKeyUnique(param))) {
            return AjaxResult.error("修改角色'" + param.getRoleName() + "'失败，角色权限已存在");
        }
        return toAjax(iSysRoleService.updateRole(param, SecurityUtils.getUserId()));
    }

    /**
     * 获取角色详情
     */
    @GetMapping(value = "/{roleId}")
    public AjaxResult getInfo(@PathVariable Long roleId) {
        SysRole sysRole = iSysRoleService.selectRoleById(roleId);

        List<SysRoleScope> sysRoleScopeList = iSysRoleScopeService.queryScopeByRole(roleId);
        if (CollectionUtil.isNotEmpty(sysRoleScopeList)) {
            List<Long> scopeIdList = sysRoleScopeList.stream().map(SysRoleScope::getScopeId).collect(Collectors.toList());
            sysRole.setScopeIdList(scopeIdList);
        }

        return AjaxResult.success(sysRole);
    }

    /**
     * 删除角色
     */
    @Log(module = EOperationModule.ROLE_CONFIG, business = EOperationBusiness.ROLE_CONFIG, method = EOperationMethod.DELETE)
    @DeleteMapping("/{roleIds}")
    public AjaxResult remove(@PathVariable Long[] roleIds) {
        return toAjax(iSysRoleService.deleteRoleByIds(roleIds, SecurityUtils.getUserId()));
    }

    /**
     * 全部角色
     *
     * @return
     */
    @GetMapping("/all")
    public AjaxResult all() {
        SysRoleQueryParam sysRoleQueryParam = new SysRoleQueryParam();
        sysRoleQueryParam.setStatus(ENormalDisable.NORMAL.getValue());
        return AjaxResult.success(iSysRoleService.selectRoleList(sysRoleQueryParam));
    }

}
