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.BaseController;
import com.trs.ggzyexchange.entity.CqSwBargainResult;
import com.trs.ggzyexchange.entity.CqSwIssueBulletin;
import com.trs.ggzyexchange.entity.ScheduledTash;
import com.trs.ggzyexchange.entity.TenderDetailsVo;
import com.trs.ggzyexchange.enums.ClassIfy;
import com.trs.ggzyexchange.enums.JobLock;
import com.trs.ggzyexchange.service.CqSwIssueBulletinService;
import com.trs.ggzyexchange.service.GcNoticeandfileService;
import com.trs.ggzyexchange.service.ScheduledTashService;
import com.trs.ggzyexchange.service.TenderDetailsVoService;
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-03-03
 */
@Component
public class CqSwIssueBulletinSchedul  implements Job {

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

    @Autowired
    private CqSwIssueBulletinService cqSwIssueBulletinService;

    @Autowired
    private TenderDetailsVoService tenderDetailsVoService;

    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;

    @Autowired
    private ScheduledTashService scheduledTashService;

    @GetMapping(value = "/queryPageListUpdateEs")
    public void queryPageListUpdateEs() throws JsonProcessingException {

        //查询当前在定时任务表当中的时间
        QueryWrapper<ScheduledTash> scheduledTashQueryWrapper = new QueryWrapper<ScheduledTash>();
        scheduledTashQueryWrapper.eq("table_name_value","cq_sw_issue_bulletin");
        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 = cqSwIssueBulletinService.maxDataTimeStamp();
                ScheduledTash scheduledTashTash = new ScheduledTash();
                scheduledTashTash.setTime(time);
                scheduledTashTash.setTableNameValue("cq_sw_issue_bulletin");
                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 = cqSwIssueBulletinService.maxDataTimeStamp();
            ScheduledTash scheduledTashTash = new ScheduledTash();
            scheduledTashTash.setTime(newTime);
            scheduledTashTash.setTableNameValue("cq_sw_issue_bulletin");
            scheduledTashTash.setUpdateTime(new Date());
            scheduledTashService.saveOrUpdate(scheduledTashTash,scheduledTashQueryWrapper);
        }catch (Exception e){
            e.printStackTrace();
        }

        if(!ObjectUtils.isEmpty(time)){
            QueryWrapper<CqSwIssueBulletin> gcBidcandidatePostQueryWrapper = new QueryWrapper<CqSwIssueBulletin>();
            gcBidcandidatePostQueryWrapper.gt("DATA_TIMESTAMP",time);
            int count = cqSwIssueBulletinService.count(gcBidcandidatePostQueryWrapper);
            Integer pageNo = 1;
            Integer pageSize = 1000;
            Integer pageNum = (int)Math.ceil((double)count/pageSize);
            List<TenderDetailsVo> tenderDetailsVos = new ArrayList<TenderDetailsVo>();
            for(int i = 1; i <= pageNum;i++){
                List<TenderDetailsVo> tenderDetailsVoListAll = getTenderDetailsVoListAll(i, pageSize,time);
                Boolean aBoolean = gcNoticeandfileService.parseTenderDetailsVo(tenderDetailsVoListAll);
            }
        }

    }
    public List<TenderDetailsVo> getTenderDetailsVoListAll(Integer pageNo,Integer pageSize,Long time){
        Page<CqSwIssueBulletin> page = new Page<CqSwIssueBulletin>(pageNo, pageSize);
        QueryWrapper<CqSwIssueBulletin> gcBidcandidatePostQueryWrapper = new QueryWrapper<CqSwIssueBulletin>();
        gcBidcandidatePostQueryWrapper.gt("DATA_TIMESTAMP",time);
        IPage<CqSwIssueBulletin> GcPerformancePage = cqSwIssueBulletinService.page(page,gcBidcandidatePostQueryWrapper);
        List<TenderDetailsVo> tenderDetailsVos = new ArrayList<TenderDetailsVo>();
        for (CqSwIssueBulletin record : GcPerformancePage.getRecords()) {
            TenderDetailsVo tenderDetailsVo = new TenderDetailsVo();
            if(!ObjectUtils.isEmpty(record.getSourceDataKey())){
                tenderDetailsVo.setSourceDataKey(record.getSourceDataKey());
            }
            if(!ObjectUtils.isEmpty(record.getProjectCode())){
                tenderDetailsVo.setProjectCode(record.getProjectCode());
            }
            if(!ObjectUtils.isEmpty(record.getProjectName())){
                tenderDetailsVo.setProjectName(record.getProjectName());
            }
            if(!ObjectUtils.isEmpty(record.getSourceDataCode())){
                tenderDetailsVo.setDataSourceCode(record.getSourceDataCode());
            }
            if(!ObjectUtils.isEmpty(record.getProjectName())){
                tenderDetailsVo.setNoticeName(record.getProjectName());
            }
            if(!ObjectUtils.isEmpty(record.getContentData())){
                tenderDetailsVo.setNoticeContent(record.getContentData());
            }
            if(!ObjectUtils.isEmpty(record.getPublishDate())){
                tenderDetailsVo.setNoticeSendTime(record.getPublishDate().toString());

            }
            if(!ObjectUtils.isEmpty(record.getSignupOpenTime())){
                tenderDetailsVo.setDocGetStartTime(record.getSignupOpenTime().toString());
            }
            if(!ObjectUtils.isEmpty(record.getSignupEndTime())){
                tenderDetailsVo.setDocGetEndTime(record.getSignupEndTime().toString());
            }
            if(!ObjectUtils.isEmpty(record.getSignupEndTime())){
                tenderDetailsVo.setBidDocReferEndTime(record.getSignupEndTime().toString());
            }
            if(!ObjectUtils.isEmpty(record.getSignupOpenTime())){
                tenderDetailsVo.setBidOpenTime(record.getSignupOpenTime().toString());
            }
            if(!ObjectUtils.isEmpty(record.getRegionCode())){
                tenderDetailsVo.setRegionCode(record.getRegionCode());
            }
            if(!ObjectUtils.isEmpty(record.getPlatformCode())){
                tenderDetailsVo.setPlatformCode(record.getPlatformCode());
            }
            if(!ObjectUtils.isEmpty(record.getPublishDuration())){
                tenderDetailsVo.setValidPeriod(record.getPublishDuration().toString());
            }
            if(!ObjectUtils.isEmpty(record.getSignupOpenTime())){
                tenderDetailsVo.setBidOpeningTime(record.getSignupOpenTime().toString());
            }
            if(!ObjectUtils.isEmpty(record.getSignupEndTime())){
                tenderDetailsVo.setBidFinishTime(record.getSignupEndTime().toString());
            }
            tenderDetailsVos.add(tenderDetailsVo);
        }
        for (TenderDetailsVo record : tenderDetailsVos) {
            try {
                record.setNoticeTypeCode(ClassIfy.LISTING_DISCLOSURE.getCode());
                record.setNoticeTypeName(ClassIfy.LISTING_DISCLOSURE.getName());
                record.setTransactionTypeCode(ClassIfy.STATE_OWNED_PROPERTY_RIGHTS.getCode());
                record.setTransactionTypeName(ClassIfy.STATE_OWNED_PROPERTY_RIGHTS.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.CqSwIssueBulletinFlag){
                JobLock.CqSwIssueBulletinFlag = false;
                logger.info("执行CqSwIssueBulletin", new SimpleDateFormat().format(new Date()));
                try {
                    queryPageListUpdateEs();
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                JobLock.CqSwIssueBulletinFlag = true;
            }else{
                logger.info("不执行CqSwIssueBulletin", new SimpleDateFormat().format(new Date()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
