package com.dk.mdm.service.mst;

import com.dk.common.infrastructure.enums.ErrorCodeEnum;
import com.dk.common.response.ResponseCodeEnum;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.mapper.mst.RoleMapper;
import com.dk.mdm.model.pojo.mst.Role;
import com.dk.mdm.model.pojo.mst.RoleFun;
import com.dk.mdm.model.pojo.mst.RoleSensitive;
import com.dk.mdm.mapper.mst.RoleSensitiveMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.vo.mst.RoleVo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleSensitiveService extends BaseService<RoleSensitive> {

    @Override
    public BaseMapper<RoleSensitive> getRepository() {
        return roleSensitiveMapper;
    }

    @Autowired
    private RoleSensitiveMapper roleSensitiveMapper;

    @Autowired
    private RoleMapper roleMapper;
    /**
     * @desc : 获取角色敏感信息权限
     * @author : 于继渤
     * @date : 2024/2/26 10:36
     */
    public ResponseResultVO<Map<String, Object>> getRoleSensitive(Map<String, Object> param) {
        // 获取系统基础数据
        List<Map<String, Object>> list = roleSensitiveMapper.getRoleSensitive(param);
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("list", list);
        return ResponseResultUtil.success(objectObjectHashMap);
    }

    /**
     * @desc : 保存敏感信息
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> saveRoleSensitive(List<RoleSensitive> roleSensitives) {
        roleSensitiveMapper.insertBatch(roleSensitives);
        return ResponseResultUtil.success();
    }

    /**
     * @desc : 删除角色关联数据
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    public ResponseResultVO<Boolean> deleteRoleId(String id) {
        return ResponseResultUtil.success(roleSensitiveMapper.deleteRoleId(new RoleSensitive().setRoleId(id)) > 0);
    }

    /**
     * @desc : 批量修改敏感信息
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<Boolean> updateBatch(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());
        }
        if (roleVO.getRoleSensitiveList() != null) {
            //删除历史功能权限
            deleteRoleId(roleVO.getRoleId());
        }
        if(roleVO.getRoleSensitiveList() != null && roleVO.getRoleSensitiveList().size() > 0){
            //新建角色敏感信息权限
            for (RoleSensitive roleSensitive : roleVO.getRoleSensitiveList()) {
                roleSensitive.setRoleId(roleVO.getRoleId());
                roleSensitive.setCpId(roleVO.getCpId());
            }
            //批量新建
            roleSensitiveMapper.insertBatch(roleVO.getRoleSensitiveList());
        }

        return ResponseResultUtil.success();
    }

}
