package com.trs.ggzyexchange.schedul;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.trs.ggzyexchange.api.vo.Result;
import com.trs.ggzyexchange.controller.AreaDataName;
import com.trs.ggzyexchange.controller.BaseController;
import com.trs.ggzyexchange.entity.*;
import com.trs.ggzyexchange.enums.ClassIfy;
import com.trs.ggzyexchange.enums.JobLock;
import com.trs.ggzyexchange.service.GcNoticeandfileService;
import com.trs.ggzyexchange.service.ScheduledTashService;
import com.trs.ggzyexchange.service.TenderDetailsVoService;
import com.trs.ggzyexchange.service.ZrzyTdBidwinnoticeService;
import com.trs.ggzyexchange.utils.TenderDetailsVoUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 成交结果公告 前端控制器
 * </p>
 *
 * @author trs
 * @since 2023-02-24
 */
@Component
public class ZrzyTdBidwinnoticeSchedul   implements Job {

    private Logger logger = LoggerFactory.getLogger(ZrzyTdBidwinnoticeSchedul.class);

    @Autowired
    private ZrzyTdBidwinnoticeService zrzyTdBidwinnoticeService;

    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;

    @Autowired
    private TenderDetailsVoService tenderDetailsVoService;

    @Autowired
    private ScheduledTashService scheduledTashService;

    public void queryPageListUpdateEs() throws JsonProcessingException {
        //查询当前在定时任务表当中的时间
        QueryWrapper<ScheduledTash> scheduledTashQueryWrapper = new QueryWrapper<ScheduledTash>();
        scheduledTashQueryWrapper.eq("table_name_value","zrzy_td_bidwinnotice");
        Page<ScheduledTash> pageScheduledTash = new Page<ScheduledTash>(1, 1);
        Page<ScheduledTash> page = scheduledTashService.page(pageScheduledTash, scheduledTashQueryWrapper);
        List<ScheduledTash> records = page.getRecords();
        ScheduledTash scheduledTash = null;
        if(records.size() > 0){
            scheduledTash = records.get(0);
        }
        Long time = null;
        if(ObjectUtils.isEmpty(scheduledTash)){
            try {
                //查询表gcNoticeandfile表当中的最新时间
                time = zrzyTdBidwinnoticeService.maxDataTimeStamp();
                ScheduledTash scheduledTashTash = new ScheduledTash();
                scheduledTashTash.setTime(time);
                scheduledTashTash.setTableNameValue("zrzy_td_bidwinnotice");
                scheduledTashTash.setUpdateTime(new Date());
                scheduledTashTash.setCreateTime(new Date());
                scheduledTashService.saveOrUpdate(scheduledTashTash,scheduledTashQueryWrapper);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            time = scheduledTash.getTime();
        }
        try {
            //查询表gcNoticeandfile表当中的最新时间
            Long newTime = zrzyTdBidwinnoticeService.maxDataTimeStamp();
            ScheduledTash scheduledTashTash = new ScheduledTash();
            scheduledTashTash.setTime(newTime);
            scheduledTashTash.setTableNameValue("zrzy_td_bidwinnotice");
            scheduledTashTash.setUpdateTime(new Date());
            scheduledTashService.saveOrUpdate(scheduledTashTash,scheduledTashQueryWrapper);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(!ObjectUtils.isEmpty(time)){
            QueryWrapper<ZrzyTdBidwinnotice> gcPerformanceQueryWrapper = new QueryWrapper<ZrzyTdBidwinnotice>();
            gcPerformanceQueryWrapper.gt("DATA_TIMESTAMP",time);
            int count = zrzyTdBidwinnoticeService.count(gcPerformanceQueryWrapper);
            Integer pageSize = 1000;
            Integer pageNum = (int)Math.ceil((double)count/pageSize);
            for(int i = 1; i <= pageNum;i++){
                List<TenderDetailsVo> tenderDetailsVoListAll = getTenderDetailsVoListAll(i, pageSize,time);
                gcNoticeandfileService.parseTenderDetailsVo(tenderDetailsVoListAll);
            }
        }
    }
    public List<TenderDetailsVo> getTenderDetailsVoListAll(Integer pageNo,Integer pageSize,Long time){
        QueryWrapper<ZrzyTdBidwinnotice> gcPerformanceQueryWrapper = new QueryWrapper<ZrzyTdBidwinnotice>();
        gcPerformanceQueryWrapper.gt("DATA_TIMESTAMP",time);
        Page<ZrzyTdBidwinnotice> page = new Page<ZrzyTdBidwinnotice>(pageNo, pageSize);
        Page<ZrzyTdBidwinnotice> page1 = zrzyTdBidwinnoticeService.page(page,gcPerformanceQueryWrapper);
        List<ZrzyTdBidwinnotice> records = page1.getRecords();
        List<TenderDetailsVo> tenderDetailsVos = new ArrayList<TenderDetailsVo>();
        for (ZrzyTdBidwinnotice record : records) {
            TenderDetailsVo tenderDetailsVo = new TenderDetailsVo();
            String sourceDataKey = record.getSourceDataKey();
            if(!ObjectUtils.isEmpty(sourceDataKey)){
                tenderDetailsVo.setSourceDataKey(sourceDataKey);
            }
            String dealLandCode = record.getDealLandCode();
            if(!ObjectUtils.isEmpty(dealLandCode)){
                tenderDetailsVo.setProjectCode(dealLandCode);
            }
            String announcementTitle = record.getAnnouncementTitle();
            if(!ObjectUtils.isEmpty(announcementTitle)){
                tenderDetailsVo.setNoticeName(announcementTitle);
            }
            String landProjectName = record.getLandProjectName();
            if(!ObjectUtils.isEmpty(landProjectName)){
                tenderDetailsVo.setProjectName(landProjectName);
            }
            String dealAnnouncementGuid = record.getDealAnnouncementGuid();
            if(!ObjectUtils.isEmpty(dealAnnouncementGuid)){
                tenderDetailsVo.setTenderProjectCode(dealAnnouncementGuid);
            }
            String announcementConnect = record.getAnnouncementConnect();
            if(!ObjectUtils.isEmpty(announcementConnect)){
                tenderDetailsVo.setNoticeContent(announcementConnect);
            }
            Long publishingTime = record.getPublishingTime();
            if(!ObjectUtils.isEmpty(publishingTime)){
                tenderDetailsVo.setNoticeSendTime(publishingTime.toString());
                tenderDetailsVo.setDocGetStartTime(publishingTime.toString());
            }
            String regionCode = record.getRegionCode();
            if(!ObjectUtils.isEmpty(regionCode)){
                tenderDetailsVo.setRegionCode(regionCode);
            }
            Long dealTime = record.getDealTime();
            if(!ObjectUtils.isEmpty(dealTime)){
                tenderDetailsVo.setDocGetEndTime(dealTime.toString());
            }
            String dataSourceCode = record.getDataSourceCode();
            if(!ObjectUtils.isEmpty(dataSourceCode)){
                tenderDetailsVo.setDataSourceCode(dataSourceCode);
            }
            tenderDetailsVos.add(tenderDetailsVo);
        }
        for (TenderDetailsVo record : tenderDetailsVos) {
           try {
               record.setNoticeTypeCode(ClassIfy.TRANSACTION_PARCEL.getCode());
               record.setNoticeTypeName(ClassIfy.TRANSACTION_PARCEL.getName());
               record.setTransactionTypeCode(ClassIfy.NATURAL_RESOURCES.getCode());
               record.setTransactionTypeName(ClassIfy.NATURAL_RESOURCES.getName());
               record = tenderDetailsVoService.getTenderDetailsVoAllOrther(record);
               TenderDetailsVoUtils.isNull(record);
           }catch (Exception e){
               e.printStackTrace();
           }
        }
        return tenderDetailsVos;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            if(JobLock.ZrzyTdBidwinnoticeFlag){
                JobLock.ZrzyTdBidwinnoticeFlag = false;
                logger.info("执行ZrzyTdBidwinnotice", new SimpleDateFormat().format(new Date()));
                try {
                    queryPageListUpdateEs();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JobLock.ZrzyTdBidwinnoticeFlag = true;
            }else{
                logger.info("不执行ZrzyTdBidwinnotice", new SimpleDateFormat().format(new Date()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
