package com.chinaweal.aiccs.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.market.ai.AreaSliceClaimAI;
import com.chinaweal.aiccs.aiccs.market.entity.AddressGeoLog;
import com.chinaweal.aiccs.aiccs.market.entity.EntAddressGeoLog;
import com.chinaweal.aiccs.aiccs.market.service.AddressGeoLogService;
import com.chinaweal.aiccs.aiccs.market.service.IEntAddressGeoLogService;
import com.vividsolutions.jts.geom.Point;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * 企业地址分析定时任务
 *
 * @author lroyia
 * @since 2024/6/6 14:53
 **/
@Slf4j
@Configuration
public class EntAddressAnalysisSchedule {

    @Autowired
    private IEntAddressGeoLogService entAddressGeoLogService;
    @Autowired
    private AddressGeoLogService addressGeoLogService;
    @Autowired
    private AreaSliceClaimAI areaSliceClaimAI;

    /**
     * 分析地址
     *
     * @author lroyia
     * @since 2024年6月6日 15:23:16
     */
    @Scheduled(cron = "${scheduling.cron.entAddressAnalysisSchedule}")
    public void analysisAddress() {
        int limit = 1000;
        // 获取未分析地址的市场主体信息
        log.info("获取未分析住所经纬度的市场主体信息");
        List<EntAddressGeoLog> list = entAddressGeoLogService.getNotMatchEnt(null, limit);
        long sum = 0;
        while (list.size() == limit) {
            for (EntAddressGeoLog each : list) {
                Point point = areaSliceClaimAI.getPointByAddress(each.getAddress());
                if (point == null) {
                    continue;
                }
                saveBean(each);
            }
            sum += list.size();
            log.info("市场主体住所经纬度分析绑定完成数量：{}", sum);
            list = entAddressGeoLogService.getNotMatchEnt(list.get(list.size() - 1).getPripid(), limit);
        }
        if (CollectionUtils.isNotEmpty(list)) {
            for (EntAddressGeoLog each : list) {
                Point point = areaSliceClaimAI.getPointByAddress(each.getAddress());
                if (point == null) {
                    continue;
                }
                saveBean(each);
            }
            sum += list.size();
            log.info("市场主体住所经纬度分析绑定完成数量：{}", sum);
        }
    }

    /**
     * 保存信息
     *
     * @param bean 保存的bean
     * @author lroyia
     * @since 2024年6月6日 15:36:20
     */
    private void saveBean(EntAddressGeoLog bean) {
        AddressGeoLog one = addressGeoLogService.getOne(new LambdaQueryWrapper<AddressGeoLog>().eq(AddressGeoLog::getAddress, bean.getAddress()), false);
        if (one == null) {
            return;
        }
        bean.setPoy(one.getPoy())
                .setPox(one.getPox())
                .setLevelName(one.getLevelName())
                .setLevelWeight(one.getLevelWeight());
        if (entAddressGeoLogService.lambdaQuery().eq(EntAddressGeoLog::getPripid, bean.getPripid()).count() > 0) {
            entAddressGeoLogService.updateById(bean);
        } else {
            entAddressGeoLogService.save(bean);
        }
    }
}
