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.jjb.saas.system.client.tenant.facade.TenantFacade;
import com.jjb.saas.system.client.tenant.request.OtaTenantAddCmd;
import com.zcloud.basic.info.command.convertor.CorpFormCoConvertor;
import com.zcloud.basic.info.command.convertor.CorpInfoXgfCoConvertor;
import com.zcloud.basic.info.domain.enums.CorpFormTypeEnum;
import com.zcloud.basic.info.domain.enums.CorpTypeEnum;
import com.zcloud.basic.info.domain.gateway.CorpFormGateway;
import com.zcloud.basic.info.domain.gateway.CorpInfoGateway;
import com.zcloud.basic.info.domain.gateway.UserGateway;
import com.zcloud.basic.info.domain.model.*;
import com.zcloud.basic.info.dto.CorpInfoAddCmd;
import com.zcloud.basic.info.dto.DepartmentAddCmd;
import com.zcloud.basic.info.dto.UserAddCmd;
import com.zcloud.basic.info.persistence.dataobject.CorpFormDO;
import com.zcloud.basic.info.persistence.dataobject.CorpInfoDO;
import com.zcloud.basic.info.persistence.repository.*;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Tools;
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 org.springframework.util.ObjectUtils;

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


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:02:15
 */
@Component
@AllArgsConstructor
@Slf4j
public class CorpInfoAddExe {
    private final CorpInfoGateway corpInfoGateway;
    private final CorpInfoRepository corpInfoRepository;
    private final UserRepository userRepository;
    private final DepartmentRepository departmentRepository;
    private final UserEmploymentLogRepository userEmploymentLogRepository;
    private final CorpFormCoConvertor corpFormCoConvertor;
    private final CorpFormGateway corpFormGateway;
    private final CorpFormRepository corpFormRepository;


    @Transactional(rollbackFor = Exception.class)
    public Long execute(CorpInfoAddCmd cmd) {

        Long corpCount = corpInfoRepository.getCorpCountByCorpName(cmd.getCorpName());
        if(corpCount > 0){
            throw new BizException("企业名称已存在");
        }

        CorpInfoE examTypeE = new CorpInfoE();
        if (!ObjectUtils.isEmpty(cmd.getCode())){
            List<CorpInfoDO> corpinfoByCode = corpInfoRepository.getCorpinfoByCode(cmd.getCode());
            examTypeE.checkList(corpinfoByCode == null ? 0 : corpinfoByCode.size());
        }
        try {
            examTypeE.initPassWord(cmd.getType());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        BeanUtils.copyProperties(cmd, examTypeE);
        Long corpInfoId ;
        corpInfoId =  corpInfoGateway.add(examTypeE);
        if (corpInfoId == null) {
            throw new BizException("保存失败");
        }
        if(CollUtil.isNotEmpty(cmd.getSelectfromList())){
            //选取形式
            List<CorpFormE> selectfromList  = corpFormCoConvertor.converItemToData(cmd.getSelectfromList());
            selectfromList.forEach(info->{
                info.setInfoId(corpInfoId);
                info.setType(CorpFormTypeEnum.SELECTFROM.getCode());
                info.setTypeName(CorpFormTypeEnum.SELECTFROM.getName());
                info.setCorpFormId(Tools.get32UUID());
            });
            List<CorpFormE> filteredList = selectfromList.stream()
                    .filter(item -> item.getItemCode() != null && !item.getItemCode().trim().isEmpty())
                    .collect(Collectors.toList());
            List<CorpFormDO> convertToData = corpFormCoConvertor.convertToData(filteredList);
            corpFormRepository.addBatch(convertToData);
        }


        DepartmentE departmentE = new DepartmentE();
        departmentE.initCorpInfo(corpInfoId,cmd.getCorpName());

        Long departmentId = departmentRepository.addCorpDepartment(departmentE);


        UserE userE = new UserE();
        userE.initCorpInfo(examTypeE,departmentId,corpInfoId);
        userRepository.addCorpUser(userE);

        UserEmploymentLogE userEmploymentLogE = new UserEmploymentLogE();
        BeanUtils.copyProperties(userE, userEmploymentLogE);
        userEmploymentLogE.initAdd(userEmploymentLogE, cmd.getCorpName(), userE.getId());
        userEmploymentLogRepository.addCorpUserEmploymentLog(userEmploymentLogE);
        return corpInfoId;
    }
}

