package com.zcloud.basic.info.command;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.exception.BizException;
import com.jjb.saas.system.client.dept.facade.DeptFacade;
import com.jjb.saas.system.client.dept.request.FacadeUpdateDeptCmd;
import com.zcloud.basic.info.domain.enums.CommonFlagEnum;
import com.zcloud.basic.info.domain.gateway.DepartmentGateway;
import com.zcloud.basic.info.domain.gateway.DepartmentSuperviseGateway;
import com.zcloud.basic.info.domain.model.DepartmentE;
import com.zcloud.basic.info.domain.model.DepartmentSuperviseE;
import com.zcloud.basic.info.domain.model.FacadeDepartmentE;
import com.zcloud.basic.info.dto.DepartmentUpdateCmd;
import com.zcloud.basic.info.persistence.dataobject.DepartmentDO;
import com.zcloud.basic.info.persistence.repository.DepartmentRepository;
import com.zcloud.gbscommon.utils.Const;
import com.zcloud.gbscommon.utils.ZcloudRedisUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:03:21
 */
@Component
@AllArgsConstructor
@Slf4j
public class DepartmentUpdateExe {
    private final DepartmentGateway departmentGateway;
    private final DepartmentRepository departmentRepository;
    private final DepartmentSuperviseGateway departmentSuperviseGateway;
    @DubboReference
    private DeptFacade deptFacade;
//    @Autowired
//    private ZcloudRedisUtil zcloudRedisUtil;

    @Transactional(rollbackFor = Exception.class)
    public void execute(DepartmentUpdateCmd departmentUpdateCmd) {
        DepartmentDO departmentDO = departmentRepository.getById(departmentUpdateCmd.getId());
        if(departmentDO == null){
            throw new BizException("部门信息不存在");
        }
        //名字校验重复
        if (departmentRepository.existsByName(departmentUpdateCmd.getParentId(), departmentUpdateCmd.getName(), departmentUpdateCmd.getId())) {
            throw new BizException("部门名称已存在");
        }
        DepartmentE departmentE = new DepartmentE();
        BeanUtils.copyProperties(departmentUpdateCmd, departmentE);
        boolean res = departmentGateway.update(departmentE);
        if (!res) {
            throw new BizException("修改失败");
        }
        departmentSuperviseGateway.deletedDepartmentBySuperviseId(departmentUpdateCmd.getId());
        if(CollUtil.isNotEmpty(departmentUpdateCmd.getDepartmentIdsList())&& CommonFlagEnum.YES.getCode().equals(departmentUpdateCmd.getSuperviseFlag())){
            List<DepartmentSuperviseE> departmentSuperviseEList =departmentE.initAdd(departmentUpdateCmd.getDepartmentIdsList(), departmentUpdateCmd.getId());
            departmentSuperviseGateway.addbatch(departmentSuperviseEList);
        }


        FacadeDepartmentE deptE =FacadeDepartmentE.initUpdate(departmentE);

        FacadeUpdateDeptCmd deptCmd = new FacadeUpdateDeptCmd();
        BeanUtils.copyProperties(deptE, deptCmd);

        //删除redis数据
      //  zcloudRedisUtil.del(Const.REDIS_DEPT_PREFIX+departmentDO.getCorpinfoId()+":"+departmentUpdateCmd.getId());

     //   deptFacade.updateDept(deptCmd);
    }
}

