package com.dxhy.core.service.erpBigb.impl;

import com.alibaba.fastjson.JSON;
import com.dxhy.api.model.config.sys.SysGroupConfigEntity;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.HttpUtils;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.core.model.erpBigb.JxBaseInfoData;
import com.dxhy.core.model.erpBigb.JxBaseInfoResp;
import com.dxhy.core.service.erpBigb.ErpBigbService;
import com.dxhy.jxpt.baseService.config.dao.SysGroupConfigDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description 调大B接口定时任务service
 * @Author wangruwei
 * @Date 2024/12/5 16:32
 **/
@Service("erpBigbService")
@Slf4j

public class ErpBigbServiceImpl extends MpBaseServiceImpl<SysGroupConfigDao, SysGroupConfigEntity> implements ErpBigbService {

    @Value("${common.serverUrl.bigBUrl}")
    private String bigBUrl;

    /**
     * @Description 调大B获取基础配置信息
     * @Author wangruwei
     * @Date 2024/12/5 16:32
     **/
    @Override
    public void getGroupConfig() {
        try {
            log.info("调大B获取基础配置信息:url:{}",bigBUrl + "/sd/paramConfig/get");
            String result = HttpUtils.doPost(bigBUrl + "/sd/paramConfig/get", "");
            log.info("大B返回数据:{}",result);
            JxBaseInfoResp jxBaseInfoResp = JSON.parseObject(result, JxBaseInfoResp.class);
            if("0000".equals(jxBaseInfoResp.getCode())){
                JxBaseInfoData jxBaseInfoData = jxBaseInfoResp.getData();
                String aceId = jxBaseInfoData.getAceId();
                String aceKey = jxBaseInfoData.getAceKey();
                String createUser = jxBaseInfoData.getCreateUser();
                String updateUser = jxBaseInfoData.getUpdateUser();
                Date createTime = StringUtils.isNotBlank(jxBaseInfoData.getCreateTime())? DateUtils.strToDate(jxBaseInfoData.getCreateTime()):null;
                Date updateTime = StringUtils.isNotBlank(jxBaseInfoData.getUpdateTime())? DateUtils.strToDate(jxBaseInfoData.getUpdateTime()):null;
                if(StringUtils.isEmpty(aceId)||StringUtils.isEmpty(aceKey)){
                    log.error("返回的数据里没有aceid或acekey，不做处理");
                }else{
                    SysGroupConfigEntity updateDto = new SysGroupConfigEntity();
                    List<SysGroupConfigEntity> sysGroupConfigEntities = this.baseMapper.selectList(null);
                    if(sysGroupConfigEntities.size()>0){
                        SysGroupConfigEntity dbGroupConfig = sysGroupConfigEntities.get(0);
                        //判断是否一致
                        if(aceId.equals(dbGroupConfig.getAceId())&&aceKey.equals(dbGroupConfig.getAceKey())){
                            log.info("aceId和aceKey都一致，不做更改");
                        }else{
                            log.info("返回aceid:{},acekey:{}",aceId,aceKey);
                            log.info("数据库里aceid:{},acekey:{}", dbGroupConfig.getAceId(),dbGroupConfig.getAceKey());
                            log.info("不一致，做更新操作");
                            updateDto.setAceId(aceId);
                            updateDto.setAceKey(aceKey);
                            updateDto.setCzrMc("系统推送");
                            updateDto.setCzrId(createUser);
                            updateDto.setUpdateTime(new Date());
                            this.baseMapper.update(updateDto, null);
                        }
                    }else{
                        //没有，入库
                        log.info("sys_group_config没有数据，运维手动初始化。不做处理");
                    }
                }
            }
        } catch (Exception e) {
            log.info("调大B出错{}",e);
        }
    }
}
