package com.trs.ggzyexchange.controller;
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.entity.*;
import com.trs.ggzyexchange.enums.ClassIfy;
import com.trs.ggzyexchange.service.GcBidcandidatePostService;
import com.trs.ggzyexchange.service.GcNoticeandfileService;
import com.trs.ggzyexchange.service.TenderDetailsVoService;
import com.trs.ggzyexchange.service.ZrzyTdCedeNoticeService;
import com.trs.ggzyexchange.utils.TenderDetailsVoUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 出让公告信息 前端控制器
 * </p>
 *
 * @author trs
 * @since 2023-02-23
 */
@RestController
@RequestMapping("/zrzy-td-cede-notice")
public class ZrzyTdCedeNoticeController extends BaseController {

    @Autowired
    private ZrzyTdCedeNoticeService zrzyTdCedeNoticeService;

    @Autowired
    private TenderDetailsVoService tenderDetailsVoService;

    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;

    @Autowired
    private AreaDataName areaDataName;

    //@GetMapping(value = "/list")
    public Result<List<TenderDetailsVo>> queryPageList(ZrzyTdCedeNotice zrzyTdCedeNotice,
                                                           @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                           @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                           HttpServletRequest req) {
        Page<ZrzyTdCedeNotice> page = new Page<ZrzyTdCedeNotice>(pageNo, pageSize);
        Page<ZrzyTdCedeNotice> pageList = zrzyTdCedeNoticeService.page(page);
        List<TenderDetailsVo> tenderDetailsVoList = new ArrayList<TenderDetailsVo>();
        for (ZrzyTdCedeNotice record : pageList.getRecords()) {
            TenderDetailsVo tenderDetailsVo = new TenderDetailsVo();
            tenderDetailsVo.setSourceDataKey(record.getSourceDataKey());
            tenderDetailsVo.setDataSourceCode(record.getSourceDataCode());
            tenderDetailsVo.setProjectCode(record.getAnnouncementCode());
            tenderDetailsVo.setProjectName(record.getLandProjectName());
            tenderDetailsVo.setTenderProjectCode(record.getAnnouncementGuid());
            tenderDetailsVo.setNoticeName(record.getAnnouncementTitle());
            tenderDetailsVo.setNoticeContent(record.getAnnouncementConnect());
            Long publishingTime = record.getPublishingTime();
            if(!ObjectUtils.isEmpty(publishingTime)){
                tenderDetailsVo.setNoticeSendTime(record.getPublishingTime().toString());
            }
            tenderDetailsVo.setRegionCode(record.getLandDistrict());
            tenderDetailsVo.setBulletinDuty(record.getContactPerson());
            tenderDetailsVo.setDataSourceCode(record.getDataSourceCode());
            Long announcementStartTime = record.getAnnouncementStartTime();
            if(!ObjectUtils.isEmpty(announcementStartTime)){
                tenderDetailsVo.setDocGetStartTime(announcementStartTime.toString());
            }
            Long announcementDeadline = record.getAnnouncementDeadline();
            if(!ObjectUtils.isEmpty(announcementDeadline)){
                tenderDetailsVo.setDocGetEndTime(announcementDeadline.toString());
                tenderDetailsVo.setBidDocReferEndTime(announcementDeadline.toString());
            }
            tenderDetailsVoList.add(tenderDetailsVo);
        }
        for (TenderDetailsVo record : tenderDetailsVoList) {
            record.setNoticeTypeCode(ClassIfy.TRANSFER_ANNOUNCEMENT.getCode());
            record.setNoticeTypeName(ClassIfy.TRANSFER_ANNOUNCEMENT.getName());
            record.setTransactionTypeCode(ClassIfy.NATURAL_RESOURCES.getCode());
            record.setTransactionTypeName(ClassIfy.NATURAL_RESOURCES.getName());
            record = tenderDetailsVoService.getTenderDetailsVoAllOrther(record);
            TenderDetailsVoUtils.isNull(record);
        }
        return Result.OK(tenderDetailsVoList);
    }


    //@GetMapping(value = "/queryPageListUpdateEs")
    public Integer queryPageListUpdateEs() throws JsonProcessingException {
        int count = zrzyTdCedeNoticeService.count();
        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);
            Boolean aBoolean = gcNoticeandfileService.parseTenderDetailsVo(tenderDetailsVoListAll);
        }
        return tenderDetailsVos.size();
    }
    public List<TenderDetailsVo> getTenderDetailsVoListAll(Integer pageNo,Integer pageSize){
        Page<ZrzyTdCedeNotice> page = new Page<ZrzyTdCedeNotice>(pageNo, pageSize);
        Page<ZrzyTdCedeNotice> pageList = zrzyTdCedeNoticeService.page(page);
        List<TenderDetailsVo> tenderDetailsVoList = new ArrayList<TenderDetailsVo>();
        for (ZrzyTdCedeNotice record : pageList.getRecords()) {
            TenderDetailsVo tenderDetailsVo = new TenderDetailsVo();
            tenderDetailsVo.setSourceDataKey(record.getSourceDataKey());
            tenderDetailsVo.setDataSourceCode(record.getSourceDataCode());
            tenderDetailsVo.setProjectCode(record.getAnnouncementCode());
            tenderDetailsVo.setProjectName(record.getLandProjectName());
            tenderDetailsVo.setTenderProjectCode(record.getAnnouncementGuid());
            tenderDetailsVo.setNoticeName(record.getAnnouncementTitle());
            tenderDetailsVo.setNoticeContent(record.getAnnouncementConnect());
            Long publishingTime = record.getPublishingTime();
            if(!ObjectUtils.isEmpty(publishingTime)){
                tenderDetailsVo.setNoticeSendTime(record.getPublishingTime().toString());
            }
            tenderDetailsVo.setRegionCode(record.getLandDistrict());
            tenderDetailsVo.setBulletinDuty(record.getContactPerson());
            tenderDetailsVo.setDataSourceCode(record.getDataSourceCode());
            Long announcementStartTime = record.getAnnouncementStartTime();
            if(ObjectUtils.isEmpty(announcementStartTime)){
                tenderDetailsVo.setDocGetStartTime(announcementStartTime.toString());
            }
            Long announcementDeadline = record.getAnnouncementDeadline();
            if(!ObjectUtils.isEmpty(announcementDeadline)){
                tenderDetailsVo.setDocGetEndTime(announcementDeadline.toString());
                tenderDetailsVo.setBidDocReferEndTime(announcementDeadline.toString());
            }
            tenderDetailsVoList.add(tenderDetailsVo);
        }
        for (TenderDetailsVo record : tenderDetailsVoList) {
            record.setNoticeTypeCode(ClassIfy.TRANSFER_ANNOUNCEMENT.getCode());
            record.setNoticeTypeName(ClassIfy.TRANSFER_ANNOUNCEMENT.getName());
            record.setTransactionTypeCode(ClassIfy.NATURAL_RESOURCES.getCode());
            record.setTransactionTypeName(ClassIfy.NATURAL_RESOURCES.getName());
            record = tenderDetailsVoService.getTenderDetailsVoAllOrther(record);
            TenderDetailsVoUtils.isNull(record);
        }
        return tenderDetailsVoList;
    }

}
