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.jjb.saas.system.client.tenant.request.TenantUpdateStatusCmd;
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.gateway.CorpFormGateway;
import com.zcloud.basic.info.domain.gateway.CorpInfoGateway;
import com.zcloud.basic.info.domain.model.CorpFormE;
import com.zcloud.basic.info.domain.model.CorpInfoE;
import com.zcloud.basic.info.dto.CorpInfoUpdateCmd;
import com.zcloud.basic.info.dto.CorpInfoUpdateUseFlagCmd;
import com.zcloud.basic.info.persistence.dataobject.CorpFormDO;
import com.zcloud.basic.info.persistence.dataobject.CorpInfoDO;
import com.zcloud.basic.info.persistence.dataobject.DepartmentDO;
import com.zcloud.basic.info.persistence.dataobject.UserDO;
import com.zcloud.basic.info.persistence.repository.CorpFormRepository;
import com.zcloud.basic.info.persistence.repository.CorpInfoRepository;
import com.zcloud.basic.info.persistence.repository.DepartmentRepository;
import com.zcloud.basic.info.persistence.repository.UserRepository;
import com.zcloud.gbscommon.utils.Const;
import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.gbscommon.utils.ZcloudRedisUtil;
import lombok.AllArgsConstructor;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

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

/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:02:17
 */
@Component
@AllArgsConstructor
@Slf4j
public class CorpInfoUpdateExe {
    private final CorpInfoGateway corpInfoGateway;
    private final CorpInfoRepository corpInfoRepository;
    private final DepartmentRepository departmentRepository;
    private final UserRepository userRepository;
    @DubboReference
    private TenantFacade tenantFacade;
    private final CorpFormCoConvertor corpFormCoConvertor;
    private final CorpFormGateway corpFormGateway;
    private final CorpFormRepository corpFormRepository;
//    @Autowired
//    private ZcloudRedisUtil zcloudRedisUtil;

    @Transactional(rollbackFor = Exception.class)
    public void execute(CorpInfoUpdateCmd corpInfoUpdateCmd) {
        CorpInfoE corpInfoE = new CorpInfoE();
        if (!ObjectUtils.isEmpty(corpInfoUpdateCmd.getCode())){
            List<CorpInfoDO> corpinfoByCodeAndId = corpInfoRepository.getCorpinfoByCodeAndId(corpInfoUpdateCmd.getCode(), corpInfoUpdateCmd.getId());
            corpInfoE.checkList(corpinfoByCodeAndId == null ? 0 : corpinfoByCodeAndId.size());
        }

        BeanUtils.copyProperties(corpInfoUpdateCmd, corpInfoE);
        boolean res = corpInfoGateway.update(corpInfoE);
        if (!res) {
            throw new BizException("修改失败");
        }
        //修改多选
        //选取形式
        if (CollUtil.isNotEmpty(corpInfoUpdateCmd.getSelectfromList())) {
            List<CorpFormE> selectfromList = corpFormCoConvertor.converItemToData(corpInfoUpdateCmd.getSelectfromList());
            corpFormGateway.deletedCorpFormByType(corpInfoUpdateCmd.getId(), CorpFormTypeEnum.SELECTFROM.getCode());
            selectfromList.forEach(info -> {
                info.setInfoId(corpInfoUpdateCmd.getId());
                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);
        }

    }

    public void changeCompanyName(CorpInfoUpdateCmd corpInfoUpdateCmd) {
//        CorpInfoDO corpInfoDO = corpInfoRepository.getById(corpInfoUpdateCmd.getId());

        CorpInfoE corpInfoE = new CorpInfoE();
        BeanUtils.copyProperties(corpInfoUpdateCmd, corpInfoE);
        corpInfoGateway.changeCompanyName(corpInfoE);
        DepartmentDO departmentDO = new DepartmentDO();
        departmentDO.setName(corpInfoE.getCorpName());
        departmentDO.setId(corpInfoE.getId());
        departmentRepository.updateInfoById(departmentDO);
        UserDO userDO = new UserDO();
        userDO.setName(corpInfoE.getCorpName());
        userDO.setId(corpInfoE.getId());
        userRepository.updateUserName(userDO);
        //删除redis数据
        //  zcloudRedisUtil.del(Const.REDIS_CORP_PREFIX+corpInfoUpdateCmd.getId());

//        OtaPlatformUserUpdateCmd otaUpdateCmd = new OtaPlatformUserUpdateCmd();
//        otaUpdateCmd.setAccount(corpInfoE.getCorpName());
//        otaUpdateCmd.setTenantName(corpInfoE.getCorpName());
//        otaUpdateCmd.setOriginalAccount(corpInfoE.getCorpName());
//        log.info("CorpInfoXgfUpdateExe,修改企业信息调用GBS请求:{}",otaUpdateCmd.toString());
//        Response response = tenantFacade.updateOtaPlatformUser(otaUpdateCmd);
//        log.info("CorpInfoXgfUpdateExe,修改企业信息调用GBS返回:{}",response.toString());
//
    }

    public void changeEnableFlag(CorpInfoUpdateUseFlagCmd corpInfoUpdateUseFlagCmd) {
        CorpInfoDO corpInfoDO = new CorpInfoDO();
        BeanUtils.copyProperties(corpInfoUpdateUseFlagCmd, corpInfoDO);
        corpInfoRepository.updateById(corpInfoDO);
        // 通知GBS修改禁用启用标识
        TenantUpdateStatusCmd tenantUpdateStatusCmd = new TenantUpdateStatusCmd();
        tenantUpdateStatusCmd.setId(corpInfoUpdateUseFlagCmd.getId());
        if (corpInfoUpdateUseFlagCmd.getUseFlag() == 1) {
            tenantUpdateStatusCmd.setStatusEnum("TRUE");
        } else if (corpInfoUpdateUseFlagCmd.getUseFlag() == 0) {
            tenantUpdateStatusCmd.setStatusEnum("FALSE");
        }
        tenantFacade.updateStatusEnum(tenantUpdateStatusCmd);
    }
}

