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.mapper.mst.RoleFunMapper;
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 RoleFunService extends BaseService<RoleFun> {

    @Override
    public BaseMapper<RoleFun> getRepository() {
        return roleFunMapper;
    }

    @Autowired
    private RoleFunMapper roleFunMapper;

    @Autowired
    private RoleMapper roleMapper;
    /**
     * @desc : 获取角色权限
     * @author : 于继渤
     * @date : 2024/2/26 10:36
     */
    public ResponseResultVO<Map<String, Object>> getRoleFun(Map<String, Object> param) {
        // 获取系统基础数据
        List<Map<String, Object>> list = roleFunMapper.getRoleFun(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<?> saveRoleFun(List<RoleFun> roleFun) {
        roleFunMapper.insertBatch(roleFun);
        return ResponseResultUtil.success();
    }

    /**
     * @desc : 删除功能权限
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    public ResponseResultVO<Boolean> deleteRoleId(String id) {
        return ResponseResultUtil.success(roleFunMapper.deleteRoleId(new RoleFun().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.getRoleFunList() != null) {
            //删除历史功能权限
            deleteRoleId(roleVO.getRoleId());
        }
        if(roleVO.getRoleFunList() != null && roleVO.getRoleFunList().size() > 0){
            for (RoleFun roleFun : roleVO.getRoleFunList()) {
                roleFun.setRoleId(roleVO.getRoleId());
                roleFun.setCpId(roleVO.getCpId());
            }
            //批量新建
            roleFunMapper.insertBatch(roleVO.getRoleFunList());
        }

        return ResponseResultUtil.success();
    }

}
