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.OtaPlatformUserUpdateCmd;
import com.zcloud.basic.info.command.convertor.CorpInfoXgfCoConvertor;
import com.zcloud.basic.info.domain.enums.CorpFormTypeEnum;
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.CorpInfoXgfItemCmd;
import com.zcloud.basic.info.dto.CorpInfoXgfUpdateCmd;
import com.zcloud.basic.info.persistence.dataobject.CorpInfoDO;
import com.zcloud.basic.info.persistence.repository.CorpInfoRepository;
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.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-10-31 10:52:10
 */
@Component
@AllArgsConstructor
@Slf4j
public class CorpInfoXgfUpdateExe {
    private final CorpInfoXgfGateway corpInfoXgfGateway;
    private final CorpInfoXgfCoConvertor corpInfoXgfCoConvertor;
    private final CorpFormGateway CorpFormGateway;
    private final CorpInfoRepository corpInfoRepository;
    @DubboReference
    private TenantFacade tenantFacade;
    @Transactional(rollbackFor = Exception.class)
    public void execute(CorpInfoXgfUpdateCmd corpInfoXgfUpdateCmd) {

        CorpInfoDO corpInfoDO = corpInfoRepository.getById(corpInfoXgfUpdateCmd.getId());
        if(corpInfoDO == null) {
            throw new BizException("企业信息不存在");
        }

        CorpInfoXgfE corpInfoXgfE = new CorpInfoXgfE();
        BeanUtils.copyProperties(corpInfoXgfUpdateCmd, corpInfoXgfE);
        boolean res = corpInfoXgfGateway.update(corpInfoXgfE);
        if (!res) {
            throw new BizException("修改失败");
        }
        OtaPlatformUserUpdateCmd otaUpdateCmd = new OtaPlatformUserUpdateCmd();
        otaUpdateCmd.setAccount(corpInfoXgfE.getCorpName());
        otaUpdateCmd.setTenantName(corpInfoXgfE.getCorpName());
        otaUpdateCmd.setOriginalAccount(corpInfoDO.getCorpName());
        otaUpdateCmd.setPassword(corpInfoXgfUpdateCmd.getPassWord());
        log.info("CorpInfoXgfUpdateExe,修改企业信息调用GBS请求:{}",otaUpdateCmd.toString());
        Response response = tenantFacade.updateOtaPlatformUser(otaUpdateCmd);
        log.info("CorpInfoXgfUpdateExe,修改企业信息调用GBS返回:{}",response.toString());

      /*  Long corpInfoId = corpInfoXgfUpdateCmd.getId();
        List<CorpFormE> corpFormEList = new ArrayList<>();
        //股份主管部门
        List<CorpInfoXgfItemCmd> mainDepartmentList = corpInfoXgfUpdateCmd.getMainDepartmentList();
        if(CollUtil.isNotEmpty(mainDepartmentList)){
            CorpFormGateway.deletedCorpFormByType(corpInfoId,CorpFormTypeEnum.MAINDEPARTMENT.getCode());
             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(corpInfoXgfUpdateCmd.getManagerDepartmentList());
        if(CollUtil.isNotEmpty(managerDepartmentList)){
            CorpFormGateway.deletedCorpFormByType(corpInfoId,CorpFormTypeEnum.MANAGERDEPARTMENT.getCode());
            managerDepartmentList.forEach(info->{
                info.setInfoId(corpInfoId);
                info.setType(CorpFormTypeEnum.MANAGERDEPARTMENT.getCode());
                info.setTypeName(CorpFormTypeEnum.MANAGERDEPARTMENT.getName());
            });
            corpFormEList.addAll(managerDepartmentList);
        }
        //基层单位监管部门
        List<CorpFormE> competentDeptList  = corpInfoXgfCoConvertor.converItemToData(corpInfoXgfUpdateCmd.getCompetentDeptList());
        if(CollUtil.isNotEmpty(competentDeptList)){
            CorpFormGateway.deletedCorpFormByType(corpInfoId,CorpFormTypeEnum.COMPETENTDEPT.getCode());
            competentDeptList.forEach(info->{
                info.setInfoId(corpInfoId);
                info.setType(CorpFormTypeEnum.COMPETENTDEPT.getCode());
                info.setTypeName(CorpFormTypeEnum.COMPETENTDEPT.getName());
            });
            corpFormEList.addAll(competentDeptList);
        }
        //基层单位主管部门
        List<CorpFormE> superVisecorpDeptList  = corpInfoXgfCoConvertor.converItemToData(corpInfoXgfUpdateCmd.getSuperVisecorpDeptList());
        if(CollUtil.isNotEmpty(superVisecorpDeptList)){
            CorpFormGateway.deletedCorpFormByType(corpInfoId,CorpFormTypeEnum.SUPERVISECORPDEPT.getCode());
            superVisecorpDeptList.forEach(info->{
                info.setInfoId(corpInfoId);
                info.setType(CorpFormTypeEnum.SUPERVISECORPDEPT.getCode());
                info.setTypeName(CorpFormTypeEnum.SUPERVISECORPDEPT.getName());
            });
            corpFormEList.addAll(superVisecorpDeptList);
        }
        //集团单位
        List<CorpFormE> corpTypeList  = corpInfoXgfCoConvertor.converItemToData(corpInfoXgfUpdateCmd.getCorpTypeList());
        if(CollUtil.isNotEmpty(corpTypeList)){
            CorpFormGateway.deletedCorpFormByType(corpInfoId,CorpFormTypeEnum.CORP_TYPE.getCode());
            corpTypeList.forEach(info->{
                info.setInfoId(corpInfoId);
                info.setType(CorpFormTypeEnum.CORP_TYPE.getCode());
                info.setTypeName(CorpFormTypeEnum.CORP_TYPE.getName());
            });
            corpFormEList.addAll(corpTypeList);
        }
        //用工形式
        List<CorpFormE> employmentformList  = corpInfoXgfCoConvertor.converItemToData(corpInfoXgfUpdateCmd.getEmploymentformList());
        if(CollUtil.isNotEmpty(employmentformList)){
            CorpFormGateway.deletedCorpFormByType(corpInfoId,CorpFormTypeEnum.EMPLOYMENTFORM.getCode());
            employmentformList.forEach(info->{
                info.setInfoId(corpInfoId);
                info.setType(CorpFormTypeEnum.EMPLOYMENTFORM.getCode());
                info.setTypeName(CorpFormTypeEnum.EMPLOYMENTFORM.getName());
            });
            corpFormEList.addAll(employmentformList);

        }
        //选取形式
        List<CorpFormE> selectfromList  = corpInfoXgfCoConvertor.converItemToData(corpInfoXgfUpdateCmd.getSelectfromList());
        if(CollUtil.isNotEmpty(selectfromList)){
            CorpFormGateway.deletedCorpFormByType(corpInfoId,CorpFormTypeEnum.SELECTFROM.getCode());
            selectfromList.forEach(info->{
                info.setInfoId(corpInfoId);
                info.setType(CorpFormTypeEnum.SELECTFROM.getCode());
                info.setTypeName(CorpFormTypeEnum.SELECTFROM.getName());
            });
            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);
        });*/


    }
}

