package com.zcloud.basic.info.command;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jjb.saas.system.client.dept.facade.DeptFacade;
import com.zcloud.basic.info.domain.gateway.DepartmentGateway;
import com.zcloud.basic.info.domain.gateway.DepartmentSuperviseGateway;
import com.zcloud.basic.info.dto.clientobject.DepartmentCO;
import com.zcloud.basic.info.persistence.dataobject.DepartmentDO;
import com.zcloud.basic.info.persistence.repository.DepartmentRepository;
import com.zcloud.basic.info.persistence.repository.PostRepository;
import com.zcloud.basic.info.persistence.repository.UserRepository;
import com.zcloud.gbscommon.utils.Const;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.ZcloudRedisUtil;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:03:20
 */
@Component
@AllArgsConstructor
public class DepartmentRemoveExe {
    private final DepartmentGateway departmentGateway;
    private final DepartmentRepository departmentRepository;
    private final UserRepository userRepository;
    private final DepartmentSuperviseGateway departmentSuperviseGateway;
    private final PostRepository postRepository;
    @DubboReference
    private DeptFacade deptFacade;
//    @Autowired
//    private ZcloudRedisUtil zcloudRedisUtil;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long id) {
        DepartmentDO departmentDO = departmentRepository.getById(id);
        if(departmentDO == null){
            throw new BizException("部门信息不存在");
        }
//        departmentGateway
        Map<String, Object> parmas =new HashMap<>();
        parmas.put("eqParentId", id);
        Long departmentCount = departmentRepository.countByParams(parmas);
        if(departmentCount>0){
            throw new BizException("请先删除下级部门");
        }
        //部门下是否有岗位
        Boolean postFlag = postRepository.checkPostDepartment(id);
        if(postFlag){
            throw new BizException("部门有相应岗位，请解除相关岗位信息后删除");
        }
        Boolean b = userRepository.checkUserDepartment(id);
        if(b){
            throw new BizException("部门有相应人员，请解除相关部门信息后删除");
        }
        boolean res = departmentGateway.deletedDepartmentById(id);
        if (!res) {
            throw new BizException("删除失败");
        }
        departmentSuperviseGateway.deletedDepartmentBySuperviseId(id);

        deptFacade.deleteById( id);
        //删除redis数据
     //   zcloudRedisUtil.del(Const.REDIS_DEPT_PREFIX+id);
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return true;
        }

        Boolean b = userRepository.checkUserDepartment(ids);
        if(b){
            throw new BizException("部门有相应人员，请解除相关部门信息后删除");
        }
        boolean res = departmentGateway.deletedDepartmentByIds(ids);
        if (!res) {
            throw new BizException("删除失败");
        }
        // 批量删除 Redis 缓存 - 循环处理每个ID
//        for (Long id : ids) {
//            deptFacade.deleteById( id);
//            zcloudRedisUtil.del(Const.REDIS_DEPT_PREFIX + id);
//        }
        //ids转成成list
        departmentSuperviseGateway.deletedDepartmentBySuperviseIdList(Arrays.asList(ids));

        return true;
    }
}

