package com.dk.mdm.service.mst;

import com.dk.common.infrastructure.annotaiton.Pagination;
import com.dk.common.infrastructure.constant.Constant;
import com.dk.common.infrastructure.enums.ErrorCodeEnum;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseCodeEnum;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.mst.RoleConvert;
import com.dk.mdm.infrastructure.util.AuthUtils;
import com.dk.mdm.model.pojo.mst.DictionaryData;
import com.dk.mdm.model.pojo.mst.Role;
import com.dk.mdm.mapper.mst.RoleMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.pojo.mst.RoleFun;
import com.dk.mdm.model.pojo.mst.RoleSensitive;
import com.dk.mdm.model.query.mst.RoleQuery;
import com.dk.mdm.model.response.mst.RoleResponse;
import com.dk.mdm.model.vo.mst.RoleVo;
import com.dk.mdm.service.common.CommonService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service
@Transactional
public class RoleService extends BaseService<Role> {

    @Override
    public BaseMapper<Role> getRepository() {
        return roleMapper;
    }

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleFunService roleFunService;

    @Autowired
    private RoleSensitiveService roleSensitiveService;

    @Autowired
    private RoleConvert roleConvert;

    @Autowired
    private CommonService commonService;

    @Autowired
    private AuthUtils authUtils;

    /**
     * @desc : 重写主键
     * @author : 周兴
     * @date : 2024/2/29 20:29
     */
    @Override
    public String getPrimaryKey() {
        return "role_id";
    }

    /**
     * @desc : 查询角色
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Pagination
    public ResponseResultVO<PageList<RoleResponse>> selectByCond(RoleQuery roleQuery) {
        return super.mergeListWithCount(roleQuery, roleMapper.selectByCond(roleQuery),
                roleMapper.countByCond(roleQuery));
    }

    /**
     * @desc : 新建角色
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> saveRole(RoleVo roleVO) {
        Role role = roleConvert.convertToPo(roleVO);
        // 获取显示顺序
        role.setDisplayNo(commonService.getMaxDisplayNo(Constant.DisplayNoTable.ROLE));
        //设置编码
        Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.ROLE.getName(), true);
        role.setRoleId(uniqueNoteCode.get("outId").toString());
        role.setRoleCode(uniqueNoteCode.get("outNote").toString());
        //新建
        roleMapper.insert(role);
        //设置角色权限明细
        setRoleItem(roleVO, role);

        return ResponseResultUtil.success();
    }
    /**
     * @desc : 设置功能权限敏感数据
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    private void setRoleItem(RoleVo roleVO, Role role) {
        if (roleVO.getRoleFunList() != null && roleVO.getRoleFunList().size() > 0 && roleVO.getRoleFunList().get(0).getFunUuid() != null) {
            //新建角色功能权限
            for (RoleFun roleFun : roleVO.getRoleFunList()) {
                roleFun.setRoleId(role.getRoleId());
                roleFun.setCpId(role.getCpId());
            }
            roleFunService.saveRoleFun(roleVO.getRoleFunList());
        }
        if (roleVO.getRoleSensitiveList() != null && roleVO.getRoleSensitiveList().size() > 0 && roleVO.getRoleSensitiveList().get(0).getSenItemCode()!= null) {
            //新建角色敏感信息权限
            for (RoleSensitive roleSensitive : roleVO.getRoleSensitiveList()) {
                roleSensitive.setRoleId(role.getRoleId());
                roleSensitive.setCpId(role.getCpId());
            }
            roleSensitiveService.saveRoleSensitive(roleVO.getRoleSensitiveList());
        }
    }

    /**
     * @desc : 编辑角色
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> updateRole(RoleVo roleVO) {

        Role role1 = roleMapper.selectById(roleVO.getRoleId());
        if (role1.getSysCode() != null) {
            //系统预制 无法编辑
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.SYSTEM_PRESET_DATA_UNABLE_TO_OPERATE.getMessage());
        }
        Role role = roleConvert.convertToPo(roleVO);
        updateByUuid(role);
        if (roleVO.getRoleFunList() != null) {
            //删除历史功能权限
            roleFunService.deleteRoleId(roleVO.getRoleId());
        }
        if (roleVO.getRoleSensitiveList() != null) {
            //删除历史敏感信息权限
            roleSensitiveService.deleteRoleId(roleVO.getRoleId());
        }

        //设置角色权限明细
        setRoleItem(roleVO, role);
        return ResponseResultUtil.success();
    }

    /**
     * @desc   : 保存方法
     * @author : 常皓宁
     * @date   : 2024/3/7 8:38
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> insert(RoleVo roleVO) {
        Role role = roleConvert.convertToPo(roleVO);
        // 获取显示顺序
        Integer displayNo =commonService.getMaxDisplayNo(Constant.DisplayNoTable.ROLE);
        if (displayNo != null) {
            //插入序号
            role.setDisplayNo(displayNo);
        } else {
            role.setDisplayNo(0);
        }
        role.setCpId(authUtils.getStaff().getCpId());
        //设置编码
        Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.ROLE.getName(), true);
        role.setRoleId(uniqueNoteCode.get("outId").toString());
        role.setRoleCode(uniqueNoteCode.get("outNote").toString());
        //新建
        roleMapper.insert(role);
        return ResponseResultUtil.success();
    }

    /**
     * @desc   : 编辑角色
     * @author : 常皓宁
     * @date   : 2024/3/7 8:47
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> update(RoleVo roleVO) {
        Role role1 = roleMapper.selectById(roleVO.getRoleId());
        if (role1.getSysCode() != null) {
            //系统预制 无法编辑
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.SYSTEM_PRESET_DATA_UNABLE_TO_OPERATE.getMessage());
        }
        Role role = roleConvert.convertToPo(roleVO);
        updateByUuid(role);
        return ResponseResultUtil.success();
    }


    /**
     * @desc : 停用
     * @author : 于继渤
     * @date :  2023/1/4 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<Boolean> disable(String id) {
        Role role = roleMapper.selectById(id);
        if (role.getSysCode() != null) {
            //系统预制 无法编辑
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.SYSTEM_PRESET_DATA_UNABLE_TO_OPERATE.getMessage());
        }
        super.disable(id);

        return ResponseResultUtil.success();
    }

    /**
     * @desc : 启用
     * @author : 于继渤
     * @date :  2023/1/4 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<Boolean> enable(String id) {
        Role role = roleMapper.selectById(id);
        if (role.getSysCode() != null) {
            //系统预制 无法编辑
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.SYSTEM_PRESET_DATA_UNABLE_TO_OPERATE.getMessage());
        }
        super.enable(id);

        return ResponseResultUtil.success();
    }


}
