package com.trs.ggzyexchange.controller;


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.GcNoticeandfileService;
import com.trs.ggzyexchange.service.TenderDetailsVoService;
import com.trs.ggzyexchange.service.ZrzyTdBidwinnoticeService;
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-24
 */
@RestController
@RequestMapping("/zrzy-td-bidwinnotice")
public class ZrzyTdBidwinnoticeController extends BaseController {
    @Autowired
    private ZrzyTdBidwinnoticeService zrzyTdBidwinnoticeService;

    @Autowired
    private AreaDataName areaDataName;

    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;

    @Autowired
    private TenderDetailsVoService tenderDetailsVoService;

    //@GetMapping(value = "/list")
    public Result<IPage<ZrzyTdBidwinnoticeVo>> queryPageList(ZrzyTdBidwinnotice zrzyTdCedeNotice,
                                                             @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                             @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                             HttpServletRequest req) {
        Page<ZrzyTdBidwinnotice> page = new Page<ZrzyTdBidwinnotice>(pageNo, pageSize);
        IPage<ZrzyTdBidwinnoticeVo> pageList = zrzyTdBidwinnoticeService.queryZrzyTdBidwinnoticeList(page, zrzyTdCedeNotice);
        List<ZrzyTdBidwinnoticeVo> records = pageList.getRecords();
        for (ZrzyTdBidwinnoticeVo record : records) {
            String regionCode = record.getRegionCode();
            String areaDataName = this.areaDataName.getAreaDataName(regionCode, null);
            record.setArea(areaDataName);
        }
        pageList.setRecords(records);
        return Result.OK(pageList);
    }
    // @GetMapping(value = "/queryPageList")
    public Result<List<TenderDetailsVo>> queryPageListZrzyTdBidwinnoticeVo(ZrzyTdBidwinnotice zrzyTdCedeNotice,
                                                             @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                             @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                             HttpServletRequest req) {
        Page<ZrzyTdBidwinnotice> page = new Page<ZrzyTdBidwinnotice>(pageNo, pageSize);
         Page<ZrzyTdBidwinnotice> page1 = zrzyTdBidwinnoticeService.page(page);

         List<ZrzyTdBidwinnotice> records = page1.getRecords();

         List<TenderDetailsVo> tenderDetailsVos = new ArrayList<TenderDetailsVo>();
         for (ZrzyTdBidwinnotice record : records) {
             TenderDetailsVo tenderDetailsVo = new TenderDetailsVo();
             String sourceDataKey = record.getSourceDataKey();
             tenderDetailsVo.setSourceDataKey(sourceDataKey);
             String dealLandCode = record.getDealLandCode();
             tenderDetailsVo.setProjectCode(dealLandCode);
             String announcementTitle = record.getAnnouncementTitle();
             String landProjectName = record.getLandProjectName();

             tenderDetailsVo.setProjectName(landProjectName);
             String dealAnnouncementGuid = record.getDealAnnouncementGuid();
             tenderDetailsVo.setTenderProjectCode(dealAnnouncementGuid);
             tenderDetailsVo.setNoticeName(announcementTitle);
             String announcementConnect = record.getAnnouncementConnect();
             tenderDetailsVo.setNoticeContent(announcementConnect);
             Long publishingTime = record.getPublishingTime();
             if(!ObjectUtils.isEmpty(publishingTime)){
                 tenderDetailsVo.setNoticeSendTime(publishingTime.toString());
                 tenderDetailsVo.setDocGetStartTime(publishingTime.toString());
             }
             String regionCode = record.getRegionCode();
             tenderDetailsVo.setRegionCode(regionCode);

             Long dealTime = record.getDealTime();
             if(!ObjectUtils.isEmpty(dealTime)){
                 tenderDetailsVo.setDocGetEndTime(dealTime.toString());
             }
             String dataSourceCode = record.getDataSourceCode();
             tenderDetailsVo.setDataSourceCode(dataSourceCode);
             tenderDetailsVos.add(tenderDetailsVo);
         }
         for (TenderDetailsVo record : tenderDetailsVos) {
             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);
         }
        return Result.OK(tenderDetailsVos);
    }

    //@GetMapping(value = "/queryPageListUpdateEs")
    public Integer queryPageListUpdateEs() throws JsonProcessingException {
        int count = zrzyTdBidwinnoticeService.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<ZrzyTdBidwinnotice> page = new Page<ZrzyTdBidwinnotice>(pageNo, pageSize);
        Page<ZrzyTdBidwinnotice> page1 = zrzyTdBidwinnoticeService.page(page);
        List<ZrzyTdBidwinnotice> records = page1.getRecords();
        List<TenderDetailsVo> tenderDetailsVos = new ArrayList<TenderDetailsVo>();
        for (ZrzyTdBidwinnotice record : records) {
            TenderDetailsVo tenderDetailsVo = new TenderDetailsVo();
            String sourceDataKey = record.getSourceDataKey();
            tenderDetailsVo.setSourceDataKey(sourceDataKey);
            String dealLandCode = record.getDealLandCode();
            tenderDetailsVo.setProjectCode(dealLandCode);
            String announcementTitle = record.getAnnouncementTitle();
            String landProjectName = record.getLandProjectName();
            tenderDetailsVo.setProjectName(landProjectName);
            String dealAnnouncementGuid = record.getDealAnnouncementGuid();
            tenderDetailsVo.setTenderProjectCode(dealAnnouncementGuid);
            tenderDetailsVo.setNoticeName(announcementTitle);
            String announcementConnect = record.getAnnouncementConnect();
            tenderDetailsVo.setNoticeContent(announcementConnect);
            Long publishingTime = record.getPublishingTime();
            if(!ObjectUtils.isEmpty(publishingTime)){
                tenderDetailsVo.setNoticeSendTime(publishingTime.toString());
                tenderDetailsVo.setDocGetStartTime(publishingTime.toString());
            }
            String regionCode = record.getRegionCode();
            tenderDetailsVo.setRegionCode(regionCode);
            Long dealTime = record.getDealTime();
            if(!ObjectUtils.isEmpty(dealTime)){
                tenderDetailsVo.setDocGetEndTime(dealTime.toString());
            }
            String dataSourceCode = record.getDataSourceCode();
            tenderDetailsVo.setDataSourceCode(dataSourceCode);
            tenderDetailsVos.add(tenderDetailsVo);
        }
        for (TenderDetailsVo record : tenderDetailsVos) {
            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);
        }
        return tenderDetailsVos;
    }
}
