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.GcBidpublicity;
import com.trs.ggzyexchange.entity.GcBidpublicityVo;
import com.trs.ggzyexchange.entity.TenderDetailsVo;
import com.trs.ggzyexchange.enums.ClassIfy;
import com.trs.ggzyexchange.service.GcBidpublicityService;
import com.trs.ggzyexchange.service.GcNoticeandfileService;
import com.trs.ggzyexchange.service.TenderDetailsVoService;
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-02-16
 */
@RestController
@RequestMapping("/gc-bidpublicity")
public class GcBidpublicityController extends BaseController {

    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;

    @Autowired
    private AreaDataName areaDataName;

    @Autowired
    private GcBidpublicityService gcBidpublicityService;

    @Autowired
    private TenderDetailsVoService tenderDetailsVoService;

    //@GetMapping(value = "/list")
    public Result<IPage<GcBidpublicityVo>> queryPageList(GcBidpublicity gcBidpublicity,
                                                         @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                         @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                         HttpServletRequest req) {
        Page<GcBidpublicity> page = new Page<GcBidpublicity>(pageNo, pageSize);
        IPage<GcBidpublicityVo> pageList = gcBidpublicityService.queryGcBidpublicityList(page, gcBidpublicity);
        List<GcBidpublicityVo> records = pageList.getRecords();
        for (GcBidpublicityVo record : records) {
            String regionCode = record.getRegionCode();
            String platformCode = record.getPlatformCode();
            String areaDataName = this.areaDataName.getAreaDataName(regionCode, platformCode);
            record.setArea(areaDataName);
        }
        pageList.setRecords(records);
        return Result.OK(pageList);
    }
    //@GetMapping(value = "/queryPageListAll")
    public Result<IPage<TenderDetailsVo>> queryPageListAll(
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
            HttpServletRequest req
    ) throws JsonProcessingException {
        Page<GcBidpublicity> page = new Page<GcBidpublicity>(pageNo, pageSize);
        IPage<TenderDetailsVo> pageList = gcBidpublicityService.queryGcBidpublicityAllList(page);
        for (TenderDetailsVo record : pageList.getRecords()) {
            record.setNoticeTypeCode(ClassIfy.ANNOUNCEMENT_OF_BID_WINNING_RESULTS.getCode());
            record.setNoticeTypeName(ClassIfy.ANNOUNCEMENT_OF_BID_WINNING_RESULTS.getName());
            record.setTransactionTypeCode(ClassIfy.ENGINEERING_CONSTRUCTION.getCode());
            record.setTransactionTypeName(ClassIfy.ENGINEERING_CONSTRUCTION.getName());
            record = tenderDetailsVoService.getTenderDetailsVoAllOrther(record);
            TenderDetailsVoUtils.isNull(record);
        }
        return Result.OK(pageList);
    }
    //@GetMapping(value = "/queryPageListUpdateEs")
    public Integer queryPageListUpdateEs() throws JsonProcessingException {
        int count = gcNoticeandfileService.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<GcBidpublicity> page = new Page<GcBidpublicity>(pageNo, pageSize);
        IPage<TenderDetailsVo> pageList = gcBidpublicityService.queryGcBidpublicityAllList(page);
        for (TenderDetailsVo record : pageList.getRecords()) {
            record.setNoticeTypeCode(ClassIfy.ANNOUNCEMENT_OF_BID_WINNING_RESULTS.getCode());
            record.setNoticeTypeName(ClassIfy.ANNOUNCEMENT_OF_BID_WINNING_RESULTS.getName());
            record.setTransactionTypeCode(ClassIfy.ENGINEERING_CONSTRUCTION.getCode());
            record.setTransactionTypeName(ClassIfy.ENGINEERING_CONSTRUCTION.getName());
            record = tenderDetailsVoService.getTenderDetailsVoAllOrther(record);
            TenderDetailsVoUtils.isNull(record);
        }
        return pageList.getRecords();
    }
}
