package com.trs.ggzyexchange.controller;


import com.alibaba.fastjson.JSON;
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.CqGqIssueBulletin;
import com.trs.ggzyexchange.entity.CqSwBargainResult;
import com.trs.ggzyexchange.entity.TenderDetailsVo;
import com.trs.ggzyexchange.enums.ClassIfy;
import com.trs.ggzyexchange.service.CqGqIssueBulletinService;
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-04
 */
@RestController
@RequestMapping("/cq-gq-issue-bulletin")
public class CqGqIssueBulletinController extends BaseController {


    @Autowired
    private CqGqIssueBulletinService cqGqIssueBulletinService;

    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;

    @Autowired
    private TenderDetailsVoService tenderDetailsVoService;

    //@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<CqGqIssueBulletin> page = new Page<CqGqIssueBulletin>(pageNo, pageSize);
        Page<CqGqIssueBulletin> page1 = cqGqIssueBulletinService.page(page);
        List<TenderDetailsVo> tenderDetailsVoList = new ArrayList<TenderDetailsVo>();
        for (CqGqIssueBulletin record : page1.getRecords()) {
            TenderDetailsVo tenderDetailsVo = new TenderDetailsVo();
            String sourceDataKey = record.getSourceDataKey();
            tenderDetailsVo.setSourceDataKey(sourceDataKey);
            String projectCode = record.getProjectCode();
            tenderDetailsVo.setProjectCode(projectCode);
            String projectName = record.getProjectName();
            tenderDetailsVo.setProjectName(projectName);
            String projectClassifyCode = record.getProjectClassifyCode();
            tenderDetailsVo.setTenderProjectCode(projectClassifyCode);
            tenderDetailsVo.setNoticeName(projectName);
            tenderDetailsVo.setNoticeContent(JSON.toJSONString(record));
            Long publishDate = record.getPublishDate();
            if(!ObjectUtils.isEmpty(publishDate)){
                tenderDetailsVo.setNoticeSendTime(publishDate.toString());
            }
            String regionCode = record.getRegionCode();
            tenderDetailsVo.setRegionCode(regionCode);
            String platformCode = record.getPlatformCode();
            tenderDetailsVo.setPlatformCode(platformCode);
            tenderDetailsVoList.add(tenderDetailsVo);
        }
        for (TenderDetailsVo record : tenderDetailsVoList) {
            record.setNoticeTypeCode(ClassIfy.LISTING_DISCLOSURE_SHAREHOLDING.getCode());
            record.setNoticeTypeName(ClassIfy.LISTING_DISCLOSURE_SHAREHOLDING.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(tenderDetailsVoList);
    }
    //@GetMapping(value = "/queryPageListUpdateEs")
    public Integer queryPageListUpdateEs() throws JsonProcessingException {
        int count = cqGqIssueBulletinService.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<CqGqIssueBulletin> page = new Page<CqGqIssueBulletin>(pageNo, pageSize);
        Page<CqGqIssueBulletin> page1 = cqGqIssueBulletinService.page(page);
        List<TenderDetailsVo> tenderDetailsVoList = new ArrayList<TenderDetailsVo>();
        for (CqGqIssueBulletin record : page1.getRecords()) {
            TenderDetailsVo tenderDetailsVo = new TenderDetailsVo();
            String sourceDataKey = record.getSourceDataKey();
            tenderDetailsVo.setSourceDataKey(sourceDataKey);
            String projectCode = record.getProjectCode();
            tenderDetailsVo.setProjectCode(projectCode);
            String projectName = record.getProjectName();
            tenderDetailsVo.setProjectName(projectName);
            String projectClassifyCode = record.getProjectClassifyCode();
            tenderDetailsVo.setTenderProjectCode(projectClassifyCode);
            tenderDetailsVo.setNoticeName(projectName);
            tenderDetailsVo.setNoticeContent(JSON.toJSONString(record));
            Long publishDate = record.getPublishDate();
            if(!ObjectUtils.isEmpty(publishDate)){
                tenderDetailsVo.setNoticeSendTime(publishDate.toString());
            }
            String regionCode = record.getRegionCode();
            tenderDetailsVo.setRegionCode(regionCode);
            String platformCode = record.getPlatformCode();
            tenderDetailsVo.setPlatformCode(platformCode);
            tenderDetailsVo.setPlatformName("呼和浩特市");
            tenderDetailsVoList.add(tenderDetailsVo);
        }
        for (TenderDetailsVo record : tenderDetailsVoList) {
            record.setNoticeTypeCode(ClassIfy.LISTING_DISCLOSURE_SHAREHOLDING.getCode());
            record.setNoticeTypeName(ClassIfy.LISTING_DISCLOSURE_SHAREHOLDING.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 tenderDetailsVoList;
    }

}
