package com.zcloud.basic.info.command;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.exception.BizException;
import com.zcloud.basic.info.command.convertor.DepartmentSuperviseCoConvertor;
import com.zcloud.basic.info.command.query.CorpInfoQueryExe;
import com.zcloud.basic.info.command.query.DepartmentQueryExe;
import com.zcloud.basic.info.domain.enums.CommonFlagEnum;
import com.zcloud.basic.info.domain.enums.CorpTypeEnum;
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.dto.DepartmentAddCmd;
import com.zcloud.basic.info.dto.DepartmentTreeQry;
import com.zcloud.basic.info.dto.clientobject.DepartmentTreeInfoCO;
import com.zcloud.basic.info.persistence.repository.DepartmentRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:03:19
 */
@Component
@AllArgsConstructor
@Slf4j
public class DepartmentAddExe {

    private final DepartmentGateway departmentGateway;
    private final DepartmentSuperviseGateway departmentSuperviseGateway;
    private final DepartmentRepository departmentRepository;
    private final CorpInfoQueryExe corpInfoQueryExe;
    private final DepartmentQueryExe departmentQueryExe;


    @Transactional(rollbackFor = Exception.class)
    public Long execute(DepartmentAddCmd cmd) {
        Boolean b = corpInfoQueryExe.verifyCorpInfo();
        if (!b) {
            throw new BizException("请先完善企业信息");
        }
        DepartmentE examTypeE = new DepartmentE();
      /*  //TODO 此处需要修改为部门下的名字不能重复
        DepartmentTreeQry departmentQry = new DepartmentTreeQry();
        departmentQry.setEqCorpinfoId(cmd.getCorpinfoId());
        departmentQry.setEqParentId(cmd.getParentId());
        List<DepartmentTreeInfoCO> departmentTreeInfoCOList = departmentQueryExe.listTree(departmentQry);*/
        //名字校验重复
        if (departmentRepository.existsByName(cmd.getParentId(), cmd.getName(), null)) {
            throw new BizException("部门名称已存在");
        }
        BeanUtils.copyProperties(cmd, examTypeE);
        Long id = departmentGateway.add(examTypeE);

        if (id ==null) {
            throw new BizException("保存失败");
        }
        if(CollUtil.isNotEmpty(cmd.getDepartmentIdsList())&& CommonFlagEnum.YES.getCode().equals(cmd.getSuperviseFlag())){
            List<DepartmentSuperviseE> departmentSuperviseEList =examTypeE.initAdd(cmd.getDepartmentIdsList(), id);
            departmentSuperviseGateway.addbatch(departmentSuperviseEList);
        }
        return id;
    }
}

