package com.dsk.monitor.job;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dsk.common.core.domain.R;
import com.dsk.common.helper.LoginHelper;
import com.dsk.common.utils.redis.RedisUtils;
import com.dsk.cscec.mapper.DCustomerMapper;
import com.dsk.monitor.domain.vo.PushMonitorCompanyAddVo;
import com.dsk.monitor.domain.vo.UpdateMonitorCompanyVo;
import com.dsk.search.service.BusinessOpportunityRadarService;
import com.dsk.system.utils.DskOpenApiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lc
 * @date 2024/5/13 17:40
 */
@Component
@Slf4j
public class MonitorScheduledTask {

    @Resource
    private DCustomerMapper customerMapper;

    @Autowired
    private BusinessOpportunityRadarService opportunityRadarService;

    @Resource
    private DskOpenApiUtil dskOpenApiUtil;

    /**
     * 每天3点更新已监控企业
     */
//    @Scheduled(cron = "0 0 0/2 * * ?")
    @Scheduled(cron = "0 0 3 * * ?")
    public void updateMonitorCompany(){
        // 中建服务  查找当前供应商名单 传到监控服务
        //获取所有供应商企业名称
        List<String> list = customerMapper.allSearchCompanyNameList();
        List<Integer> newList = new ArrayList<>();
        for (String companyName : list) {
            //循环匹配 得到能关联上的企业id
            Map<String, Object> map = opportunityRadarService.enterpriseByName(companyName);
            if (!ObjectUtils.isEmpty(map.get("data"))) {
                Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
                Integer companyId = MapUtil.getInt(data, "jskEid");
                newList.add(companyId);
            }
        }
        //如果没有上一次的企业id数据则直接返回
        if (!RedisUtils.hasKey("monitorCompanyList")){
            RedisUtils.setCacheList("monitorCompanyList",newList);
            //设置过期时间两天
            RedisUtils.expire("monitorCompanyList",172800);
            log.info("redis没有上一次供应商数据  无需进行更新！");
            return;
        }
        List<Integer> oldList = new ArrayList<>();
        oldList = RedisUtils.getCacheList("monitorCompanyList");
        RedisUtils.setCacheList("monitorCompanyList",newList);
        //设置过期时间两天
        RedisUtils.expire("monitorCompanyList",172800);

        List<Integer> oldListCopy = oldList;
        //被删除的
        oldList.removeAll(newList);
        //新旧list完全相同  无需更新
        if (oldList.size()==0 && oldListCopy.size()==newList.size()){
            log.info("供应商数据没有变化  无需更新监控！");
            return;
        }
        //新增加的
        newList.removeAll(oldListCopy);
        UpdateMonitorCompanyVo updateVo = new UpdateMonitorCompanyVo();
        updateVo.setAddList(newList);
        updateVo.setDropList(oldList);
        updateVo.setSystemCode("ZJYJ");
        log.info("中建一局用户更新默认监控企业！");
        dskOpenApiUtil.requestBody("/operate/monitor/company/updateDefaultCompany", BeanUtil.beanToMap(updateVo, false, false));

    }
}
