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.Bulletin;
import com.trs.ggzyexchange.entity.GcBidcandidatePost;
import com.trs.ggzyexchange.entity.TenderDetailsVo;
import com.trs.ggzyexchange.entity.ZcPurchaseBulletin;
import com.trs.ggzyexchange.enums.ClassIfy;
import com.trs.ggzyexchange.service.BulletinService;
import com.trs.ggzyexchange.service.GcNoticeandfileService;
import com.trs.ggzyexchange.service.TenderDetailsVoService;
import com.trs.ggzyexchange.service.ZcPurchaseBulletinService;
import com.trs.ggzyexchange.utils.TenderDetailsVoUtils;
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-03-03
 */
@RestController
@RequestMapping("/zc-purchase-bulletin")
public class ZcPurchaseBulletinController extends BaseController {

    @Autowired
    private ZcPurchaseBulletinService zcPurchaseBulletinService;

    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;

    @Autowired
    private TenderDetailsVoService tenderDetailsVoService;

    @Autowired
    private BulletinService bulletinService;

    //@GetMapping(value = "/queryPageListAll")
    public Result<IPage<TenderDetailsVo>> queryPageListAll(
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
            HttpServletRequest req
    ){
        Page<ZcPurchaseBulletin> page = new Page<ZcPurchaseBulletin>(pageNo, pageSize);
        IPage<TenderDetailsVo> tenderDetailsVoIPage = zcPurchaseBulletinService.queryZcPurchaseBulletinAllList(page);
        for (TenderDetailsVo record : tenderDetailsVoIPage.getRecords()) {
            record.setNoticeTypeCode(ClassIfy.PREQUALIFICATION_ANNOUNCEMENT.getCode());
            record.setNoticeTypeName(ClassIfy.PREQUALIFICATION_ANNOUNCEMENT.getName());
            record.setTransactionTypeCode(ClassIfy.GOVERNMENT_PROCUREMENT.getCode());
            record.setTransactionTypeName(ClassIfy.GOVERNMENT_PROCUREMENT.getName());
            record = tenderDetailsVoService.getTenderDetailsVoAllOrther(record);
            TenderDetailsVoUtils.isNull(record);
        }
        return Result.OK(tenderDetailsVoIPage);
    }

    //@GetMapping(value = "/queryPageListUpdateEs")
    public Integer queryPageListUpdateEs() throws JsonProcessingException {
        int count = zcPurchaseBulletinService.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);
            tenderDetailsVos = tenderDetailsVoListAll;
            Boolean aBoolean = gcNoticeandfileService.parseTenderDetailsVo(tenderDetailsVoListAll);
        }
        return tenderDetailsVos.size();
    }


    public List<TenderDetailsVo> getTenderDetailsVoListAll(Integer pageNo,Integer pageSize){
        Page<ZcPurchaseBulletin> page = new Page<ZcPurchaseBulletin>(pageNo, pageSize);
        IPage<TenderDetailsVo> tenderDetailsVoIPage = zcPurchaseBulletinService.queryZcPurchaseBulletinAllList(page);
        for (TenderDetailsVo record : tenderDetailsVoIPage.getRecords()) {
            record.setNoticeTypeCode(ClassIfy.PREQUALIFICATION_ANNOUNCEMENT.getCode());
            record.setNoticeTypeName(ClassIfy.PREQUALIFICATION_ANNOUNCEMENT.getName());
            record.setTransactionTypeCode(ClassIfy.GOVERNMENT_PROCUREMENT.getCode());
            record.setTransactionTypeName(ClassIfy.GOVERNMENT_PROCUREMENT.getName());
            record = tenderDetailsVoService.getTenderDetailsVoAllOrther(record);
            TenderDetailsVoUtils.isNull(record);
        }
        return tenderDetailsVoIPage.getRecords();
    }


    //@GetMapping(value = "/queryBulletinUpdateEs")
    public Integer queryBulletinUpdateEs() throws JsonProcessingException {
        int count = bulletinService.count();
        Integer pageSize = 1000;
        Integer pageNum = (int)Math.ceil((double)count/pageSize);
        List<TenderDetailsVo> tenderDetailsVosList = new ArrayList<TenderDetailsVo>();
        for(int i = 1; i <= pageNum;i++){
            QueryWrapper<Bulletin> queryWrapper = new QueryWrapper<Bulletin>();
            Page<Bulletin> page = new Page<Bulletin>(i, pageSize);
            IPage<Bulletin> pageList = bulletinService.page(page, queryWrapper);
            List<TenderDetailsVo> tenderDetailsVos = new ArrayList<TenderDetailsVo>();
            for (Bulletin bulletin : pageList.getRecords()) {
                TenderDetailsVo tenderDetailsVo = new TenderDetailsVo();
                tenderDetailsVo.setSourceDataKey(bulletin.getGuid());
                tenderDetailsVo.setProjectCode(bulletin.getPurchaseprojectcode());
                tenderDetailsVo.setProjectName(bulletin.getPurchaseprojectname());
                tenderDetailsVo.setTenderProjectCode(bulletin.getGuid());
                tenderDetailsVo.setNoticeName(bulletin.getBulletintitle());
                tenderDetailsVo.setNoticeContent(bulletin.getBulletincontent());
                tenderDetailsVo.setNoticeSendTime(bulletin.getBulletinstarttime());
                tenderDetailsVo.setNoticeTypeCode(ClassIfy.PREQUALIFICATION_ANNOUNCEMENT.getCode());
                tenderDetailsVo.setNoticeTypeName(ClassIfy.PREQUALIFICATION_ANNOUNCEMENT.getName());
                tenderDetailsVo.setTransactionTypeCode(ClassIfy.GOVERNMENT_PROCUREMENT.getCode());
                tenderDetailsVo.setTransactionTypeName(ClassIfy.GOVERNMENT_PROCUREMENT.getName());
                tenderDetailsVo.setRegionCode(bulletin.getCode());
                tenderDetailsVo.setRegionName(bulletin.getCodePram());
                tenderDetailsVo.setDataSourceCode(bulletin.getJyptid());
                tenderDetailsVo.setDataSourceName(bulletin.getBulletinmedia());
                tenderDetailsVo.setBidOpeningTime(bulletin.getBulletinstarttime());
                tenderDetailsVo.setBidFinishTime(bulletin.getBulletinendtime());
                tenderDetailsVos.add(tenderDetailsVo);
                TenderDetailsVoUtils.isNull(tenderDetailsVo);
            }
            tenderDetailsVosList = tenderDetailsVos;
            Boolean aBoolean = gcNoticeandfileService.parseTenderDetailsVo(tenderDetailsVos);
        }
        return tenderDetailsVosList.size();
    }
}
