package com.zcloud.basic.info.command;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.exception.BizException;
import com.jjb.saas.system.client.dept.facade.DeptFacade;
import com.jjb.saas.system.client.tenant.facade.TenantFacade;
import com.jjb.saas.system.client.tenant.request.FacadeTenantAddCmd;
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.CorpInfoXgfGateway;
import com.zcloud.basic.info.domain.model.CorpFormE;
import com.zcloud.basic.info.domain.model.CorpInfoXgfE;
import com.zcloud.basic.info.dto.CorpInfoXgfAddCmd;
import com.zcloud.basic.info.dto.CorpInfoXgfItemCmd;
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;
import java.util.stream.Collectors;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-10-31 10:52:08
 */
@Component
@AllArgsConstructor
@Slf4j
public class CorpInfoXgfAddExe {
    private final CorpInfoXgfGateway corpInfoXgfGateway;
    private final CorpFormGateway CorpFormGateway;
    private final CorpInfoXgfCoConvertor corpInfoXgfCoConvertor;
    @DubboReference
    private TenantFacade tenantFacade;

    @Transactional(rollbackFor = Exception.class)
    public Long execute(CorpInfoXgfAddCmd cmd) {
        CorpInfoXgfE corpInfoXgfE = new CorpInfoXgfE();
        BeanUtils.copyProperties(cmd, corpInfoXgfE);
        Long corpInfoId ;
        try {
            corpInfoXgfE.setType(CorpTypeEnum.serviceProvider.getCode());
            corpInfoId = corpInfoXgfGateway.addInfo(corpInfoXgfE);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (corpInfoId == null) {
            throw new BizException("保存失败");
        }
        OtaTenantAddCmd otaTenantAddCmd = new OtaTenantAddCmd();
        otaTenantAddCmd.setAccount(corpInfoXgfE.getCorpName());
        otaTenantAddCmd.setTenantId(corpInfoId);
        otaTenantAddCmd.setTenantName(corpInfoXgfE.getCorpName());
        otaTenantAddCmd.setPassword("Aa@12345678");
        log.info("CorpInfoXgfAddExe,新增企业调用GBS请求:{}",otaTenantAddCmd.toString());
        Response response = tenantFacade.addOtaTenant(otaTenantAddCmd);
        log.info("CorpInfoXgfAddExe,新增企业调用GBS返回:{}",response.toString());

    /*    List<CorpInfoXgfItemCmd> mainDepartmentList = cmd.getMainDepartmentList();
        List<CorpFormE> corpFormEList  = corpInfoXgfCoConvertor.converItemToData(mainDepartmentList);
        corpFormEList.forEach(info->{
            info.setInfoId(corpInfoId);
            info.setType(CorpFormTypeEnum.MAINDEPARTMENT.getCode());
            info.setTypeName(CorpFormTypeEnum.MAINDEPARTMENT.getName());
        });
        List<CorpFormE> managerDepartmentList  = corpInfoXgfCoConvertor.converItemToData(cmd.getManagerDepartmentList());
        managerDepartmentList.forEach(info->{
            info.setInfoId(corpInfoId);
            info.setType(CorpFormTypeEnum.MANAGERDEPARTMENT.getCode());
            info.setTypeName(CorpFormTypeEnum.MANAGERDEPARTMENT.getName());
        });
        List<CorpFormE> competentDeptList  = corpInfoXgfCoConvertor.converItemToData(cmd.getCompetentDeptList());
        competentDeptList.forEach(info->{
            info.setInfoId(corpInfoId);
            info.setType(CorpFormTypeEnum.COMPETENTDEPT.getCode());
            info.setTypeName(CorpFormTypeEnum.COMPETENTDEPT.getName());
        });
        List<CorpFormE> superVisecorpDeptList  = corpInfoXgfCoConvertor.converItemToData(cmd.getSuperVisecorpDeptList());
        superVisecorpDeptList.forEach(info->{
            info.setInfoId(corpInfoId);
            info.setType(CorpFormTypeEnum.SUPERVISECORPDEPT.getCode());
            info.setTypeName(CorpFormTypeEnum.SUPERVISECORPDEPT.getName());
        });
        List<CorpFormE> corpTypeList  = corpInfoXgfCoConvertor.converItemToData(cmd.getCorpTypeList());
        corpTypeList.forEach(info->{
            info.setInfoId(corpInfoId);
            info.setType(CorpFormTypeEnum.CORP_TYPE.getCode());
            info.setTypeName(CorpFormTypeEnum.CORP_TYPE.getName());
        });
        List<CorpFormE> employmentformList  = corpInfoXgfCoConvertor.converItemToData(cmd.getEmploymentformList());
        employmentformList.forEach(info->{
            info.setInfoId(corpInfoId);
            info.setType(CorpFormTypeEnum.EMPLOYMENTFORM.getCode());
            info.setTypeName(CorpFormTypeEnum.EMPLOYMENTFORM.getName());
        });
        List<CorpFormE> selectfromList  = corpInfoXgfCoConvertor.converItemToData(cmd.getSelectfromList());
        selectfromList.forEach(info->{
            info.setInfoId(corpInfoId);
            info.setType(CorpFormTypeEnum.SELECTFROM.getCode());
            info.setTypeName(CorpFormTypeEnum.SELECTFROM.getName());
        });

        corpFormEList.addAll(managerDepartmentList);
        corpFormEList.addAll(competentDeptList);
        corpFormEList.addAll(superVisecorpDeptList);
        corpFormEList.addAll(corpTypeList);
        corpFormEList.addAll(employmentformList);
        corpFormEList.addAll(selectfromList);

        List<CorpFormE> filteredList = corpFormEList.stream()
                .filter(item -> item.getItemCode() != null && !item.getItemCode().trim().isEmpty())
                .collect(Collectors.toList());
        filteredList.forEach(info->{
            CorpFormGateway.add(info);
        });*/


        //处理多个
        return corpInfoId;
    }
}

