package com.zcloud.basic.info.gatewayimpl;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.exception.BizException;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.system.client.tenant.facade.TenantFacade;
import com.jjb.saas.system.client.tenant.request.OtaTenantAddCmd;
import com.zcloud.basic.info.domain.gateway.CorpInfoGateway;
import com.zcloud.basic.info.domain.model.CorpInfoE;
import com.zcloud.basic.info.persistence.dataobject.CorpInfoDO;
import com.zcloud.basic.info.persistence.repository.CorpInfoRepository;
import com.zcloud.gbscommon.utils.Tools;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:02:16
 */
@Service
@AllArgsConstructor
@Slf4j
public class CorpInfoGatewayImpl implements CorpInfoGateway {
    private final CorpInfoRepository corpInfoRepository;
    @DubboReference
    private TenantFacade tenantFacade;

    @Override
    public Long add(CorpInfoE corpInfoE) {
        CorpInfoDO d = new CorpInfoDO();


        BeanUtils.copyProperties(corpInfoE, d);
        if(StringUtils.isEmpty(d.getCorpinfoId())){
            d.setCorpinfoId(Tools.get32UUID());
        }
        if(!CorpInfoE.isCorp()){
            d.setAccountContactId(AuthContext.getUserId());
            d.setAccountContactName(AuthContext.getName());

        }
        d.setTenantName(AuthContext.getCurrentUser().getTenantName());

        corpInfoRepository.saveCorp(d);
//        corpInfoRepository.save(d);

//        OtaTenantAddCmd otaTenantAddCmd = new OtaTenantAddCmd();
//        otaTenantAddCmd.setAccount(corpInfoE.getCorpName());
//        otaTenantAddCmd.setTenantId(d.getId());
//        otaTenantAddCmd.setTenantName(corpInfoE.getCorpName());
//        otaTenantAddCmd.setPassword("Aa@123456789");
//        log.info("CorpInfoXgfAddExe,新增企业调用GBS请求:{}",otaTenantAddCmd.toString());
//        //TODO 企业推送gbs报错解密失败
//          Response response = tenantFacade.addOtaTenant(otaTenantAddCmd);
//        log.info("CorpInfoXgfAddExe,新增企业调用GBS返回:{}",response.toString());
        return d.getId();
    }

    @Override
    public Boolean update(CorpInfoE corpInfoE) {
        CorpInfoDO d = new CorpInfoDO();
        BeanUtils.copyProperties(corpInfoE, d);
        corpInfoRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedCorpInfoById(Long id) {
        return corpInfoRepository.remove(id);
    }

    @Override
    public Boolean deletedCorpInfoByIds(Long[] ids) {
        return corpInfoRepository.removeByIds(Collections.singletonList(ids));
    }

    @Override
    public void changeCompanyName(CorpInfoE corpInfoE) {
//        CorpInfoDO corpInfo = corpInfoRepository.getById(corpInfoE.getId());
//        corpInfo.setCorpName(corpInfoE.getCorpName());
        CorpInfoDO corpInfo = new CorpInfoDO();
        BeanUtils.copyProperties(corpInfoE, corpInfo);
        corpInfoRepository.updateCorpName(corpInfo);
//        corpInfoRepository.updateById(corpInfo);
    }

}

