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.CqSwIssueBulletin;
import com.trs.ggzyexchange.entity.GcPerformance;
import com.trs.ggzyexchange.entity.TenderDetailsVo;
import com.trs.ggzyexchange.enums.ClassIfy;
import com.trs.ggzyexchange.service.CqSwIssueBulletinService;
import com.trs.ggzyexchange.service.GcNoticeandfileService;
import com.trs.ggzyexchange.service.TenderDetailsVoService;
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-03-03
 */
@RestController
@RequestMapping("/cq-sw-issue-bulletin")
public class CqSwIssueBulletinController extends BaseController {

    @Autowired
    private CqSwIssueBulletinService cqSwIssueBulletinService;

    @Autowired
    private TenderDetailsVoService tenderDetailsVoService;

    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;


    //@GetMapping(value = "/queryPageListAll")
    public Result< List<TenderDetailsVo>> queryPageListAll(
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
            HttpServletRequest req
    ){
        Page<CqSwIssueBulletin> page = new Page<CqSwIssueBulletin>(pageNo, pageSize);

        IPage<CqSwIssueBulletin> GcPerformancePage = cqSwIssueBulletinService.page(page);

        List<TenderDetailsVo> tenderDetailsVos = new ArrayList<TenderDetailsVo>();
        for (CqSwIssueBulletin record : GcPerformancePage.getRecords()) {
            System.out.println("=================="+record);
            TenderDetailsVo tenderDetailsVo = new TenderDetailsVo();
            tenderDetailsVo.setSourceDataKey(record.getSourceDataKey());
            tenderDetailsVo.setProjectCode(record.getProjectCode());
            tenderDetailsVo.setProjectName(record.getProjectName());
            tenderDetailsVo.setDataSourceCode(record.getSourceDataCode());
            tenderDetailsVo.setNoticeName(record.getProjectName());
            tenderDetailsVo.setNoticeContent(record.getContentData());
            try {
                tenderDetailsVo.setNoticeSendTime(record.getPublishDate().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setDocGetStartTime(record.getSignupOpenTime().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setDocGetEndTime(record.getSignupEndTime().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setBidDocReferEndTime(record.getSignupEndTime().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setBidOpenTime(record.getSignupOpenTime().toString());

            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setRegionCode(record.getRegionCode());

            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setPlatformCode(record.getPlatformCode());

            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setValidPeriod(record.getPublishDuration().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setBidOpeningTime(record.getSignupOpenTime().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setBidFinishTime(record.getSignupEndTime().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            tenderDetailsVos.add(tenderDetailsVo);
        }
        for (TenderDetailsVo record : tenderDetailsVos) {
            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);
        }
        return Result.OK(tenderDetailsVos);
    }
    //@GetMapping(value = "/queryPageListUpdateEs")
    public Integer queryPageListUpdateEs() throws JsonProcessingException {
        int count = cqSwIssueBulletinService.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<CqSwIssueBulletin> page = new Page<CqSwIssueBulletin>(pageNo, pageSize);

        IPage<CqSwIssueBulletin> GcPerformancePage = cqSwIssueBulletinService.page(page);

        List<TenderDetailsVo> tenderDetailsVos = new ArrayList<TenderDetailsVo>();
        for (CqSwIssueBulletin record : GcPerformancePage.getRecords()) {
            TenderDetailsVo tenderDetailsVo = new TenderDetailsVo();
            tenderDetailsVo.setSourceDataKey(record.getSourceDataKey());
            tenderDetailsVo.setProjectCode(record.getProjectCode());
            tenderDetailsVo.setProjectName(record.getProjectName());
            tenderDetailsVo.setDataSourceCode(record.getSourceDataCode());
            tenderDetailsVo.setNoticeName(record.getProjectName());
            tenderDetailsVo.setNoticeContent(record.getContentData());
            try {
                tenderDetailsVo.setNoticeSendTime(record.getPublishDate().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setDocGetStartTime(record.getSignupOpenTime().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setDocGetEndTime(record.getSignupEndTime().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setBidDocReferEndTime(record.getSignupEndTime().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setBidOpenTime(record.getSignupOpenTime().toString());

            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setRegionCode(record.getRegionCode());

            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setPlatformCode(record.getPlatformCode());

            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setValidPeriod(record.getPublishDuration().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setBidOpeningTime(record.getSignupOpenTime().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                tenderDetailsVo.setBidFinishTime(record.getSignupEndTime().toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            tenderDetailsVos.add(tenderDetailsVo);
        }
        for (TenderDetailsVo record : tenderDetailsVos) {
            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);
        }
        return tenderDetailsVos;
    }

}
