package com.trs.ggzyexchange.controller;


import cn.hutool.core.util.StrUtil;
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.fasterxml.jackson.databind.ObjectMapper;
import com.trs.ggzyexchange.api.vo.Result;
import com.trs.ggzyexchange.entity.*;
import com.trs.ggzyexchange.enums.BulletinType;
import com.trs.ggzyexchange.enums.ClassIfy;
import com.trs.ggzyexchange.service.*;
import com.trs.ggzyexchange.utils.TenderDetailsVoUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.elasticsearch.client.RestHighLevelClient;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 招标公告与资格预审公告 前端控制器
 * </p>
 * bid_finish_time
 * @author trs
 * @since 2023-02-16
 */
@Slf4j
@RestController
@RequestMapping("/gc-noticeandfile")
public class GcNoticeandfileController extends BaseController {

    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;

    @Autowired
    private AreaDataName areaDataName;

    @Autowired
    private TRegionService tRegionService;

    @Autowired
    private AreaPlatformService areaPlatformService;

    @Autowired
    private GcTenderprojectService gcTenderprojectService;

    @Autowired
    private GcTenderbidfileService gcTenderbidfileService;

    @Autowired
    private GcBidopenrecordService gcBidopenrecordService;

    @Autowired
    private CIndustriesTypeService cIndustriesTypeService;


    //@GetMapping(value = "/list")
    public Result<IPage<GcNoticeandfileVo>> queryPageList(GcNoticeandfile gcNoticeandfile,
                                                          @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                          HttpServletRequest req) {
        Page<GcNoticeandfile> page = new Page<GcNoticeandfile>(pageNo, pageSize);
        IPage<GcNoticeandfileVo> pageList = gcNoticeandfileService.queryGcTenderProjectList(page, gcNoticeandfile);
        List<GcNoticeandfileVo> records = pageList.getRecords();
        for (GcNoticeandfileVo 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);
    }
    /**
     * 通过id查询
     *
     * queryPageListAll
     *
     * @param gcNoticeandfile
     * @return
     */
    //@GetMapping(value = "/queryPageListAll")
    public Result<IPage<TenderDetailsVo>> queryPageListAll(
            GcNoticeandfile gcNoticeandfile,
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
            HttpServletRequest req
    ) throws JsonProcessingException {
        Page<GcNoticeandfile> page = new Page<GcNoticeandfile>(pageNo, pageSize);
        IPage<TenderDetailsVo> pageList = gcNoticeandfileService.queryGcTenderProjectAllList(page, gcNoticeandfile);
        for (TenderDetailsVo record : pageList.getRecords()) {
             String escaped = StringEscapeUtils.unescapeHtml3(record.getNoticeContent());
            record.setNoticeContent(escaped);
            //获取国标区域名称
            QueryWrapper<TRegion> tRegionQueryWrapper = new QueryWrapper<TRegion>();
            tRegionQueryWrapper.eq("code",record.getRegionCode()+"000000");
            Page<TRegion> pageTRegion = new Page<TRegion>(1, 1);
            List<TRegion> records = tRegionService.page(pageTRegion, tRegionQueryWrapper).getRecords();
            if(records.size() >= 1){
                try {
                    String name = records.get(0).getName();
                    if(!ObjectUtils.isEmpty(name)){
                        record.setRegionName(name);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            //获取区域编号
            QueryWrapper<AreaPlatform> areaPlatformQueryWrapper = new QueryWrapper<AreaPlatform>();
            Page<AreaPlatform> areaPlatformPage = new Page<AreaPlatform>(1, 1);
            areaPlatformQueryWrapper.eq("PLATFORM_CODE",record.getPlatformCode());
            List<AreaPlatform> recordsAreaPlatform = areaPlatformService.page(areaPlatformPage, areaPlatformQueryWrapper).getRecords();
            if(recordsAreaPlatform.size() >= 1){
                try {
                    String regionName = recordsAreaPlatform.get(0).getRegionName();
                    if(!ObjectUtils.isEmpty(regionName)){
                        record.setPlatformName(regionName);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            record.setTransactionTypeCode(ClassIfy.ENGINEERING_CONSTRUCTION.getCode());
            record.setTransactionTypeName(ClassIfy.ENGINEERING_CONSTRUCTION.getName());
            //获取行业信息代码
            String industriesTypeCode = record.getIndustriesTypeCode();
            if(!ObjectUtils.isEmpty(industriesTypeCode)){
                try {
                    QueryWrapper<CIndustriesType> cIndustriesTypeQueryWrapper = new QueryWrapper<CIndustriesType>();
                    cIndustriesTypeQueryWrapper.eq("code",industriesTypeCode);
                    CIndustriesType cIndustriesType = cIndustriesTypeService.getOne(cIndustriesTypeQueryWrapper);
                    if(!ObjectUtils.isEmpty(cIndustriesType)){
                        String name = cIndustriesType.getName();
                        record.setIndustriesTypeName(name);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            record.setNoticeTypeCode(ClassIfy.TENDER_ANNOUNCEMENT.getCode());
            record.setNoticeTypeName(ClassIfy.TENDER_ANNOUNCEMENT.getName());
            String bulletinTypeCode = record.getBulletinTypeCode();
            if(!ObjectUtils.isEmpty(bulletinTypeCode)){
                try {
                    String bulletinTypeName = BulletinType.getBulletinTypeName(bulletinTypeCode);
                    if(!ObjectUtils.isEmpty(bulletinTypeName)){
                        record.setBulletinTypeName(bulletinTypeName);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            String dataSourceCode = record.getDataSourceCode();
            if(!ObjectUtils.isEmpty(dataSourceCode)){
                QueryWrapper<TRegion> tRegionQueryWrapperQ = new QueryWrapper<TRegion>();
                String subSourceCode = dataSourceCode.substring(1, dataSourceCode.length());
                tRegionQueryWrapperQ.eq("code",subSourceCode+"000000");
                List<TRegion> tRegionList = tRegionService.page(pageTRegion, tRegionQueryWrapperQ).getRecords();
                if(tRegionList.size() >= 1){
                    String name = tRegionList.get(0).getName();
                    if(!ObjectUtils.isEmpty(name)){
                        record.setDataSourceName(name+"公共资源交易中心");
                    }
                }
                if(subSourceCode.equals("150401")){
                    record.setDataSourceName("赤峰市公共资源交易中心");
                }
            }
            String bidSectionCodes = record.getBidSectionCodes();
            if(!ObjectUtils.isEmpty(bidSectionCodes)){
                int i1 = bidSectionCodes.indexOf(",");
                int i2 = bidSectionCodes.indexOf(";");
                int i3 = bidSectionCodes.indexOf("<br>");
                String splitBidSectionCode = null;
                boolean flag = false;
                if(i1 > 0){
                    splitBidSectionCode =  bidSectionCodes.split(",")[0];
                    flag = true;
                }
                if(i2 > 0){
                    splitBidSectionCode =  bidSectionCodes.split(";")[0];
                    flag = true;
                }
                if(i3 > 0){
                    splitBidSectionCode =  bidSectionCodes.split("<br>")[0];
                    flag = true;
                }
                if(!flag){
                    splitBidSectionCode = bidSectionCodes;
                }
                //获取标包编号
                if(!ObjectUtils.isEmpty(splitBidSectionCode)){
                    QueryWrapper<GcTenderbidfile> gcTenderbidfileQueryWrapper = new QueryWrapper<GcTenderbidfile>();
                    Page<GcTenderbidfile> gcTenderbidfilePage = new Page<GcTenderbidfile>(1, 1);
                    gcTenderbidfileQueryWrapper.eq("BID_SECTION_CODE",splitBidSectionCode);
                    List<GcTenderbidfile> gcTenderbidfileRecords = gcTenderbidfileService.page(gcTenderbidfilePage, gcTenderbidfileQueryWrapper).getRecords();
                    if(gcTenderbidfileRecords.size() >= 1){
                        try {
                            Integer validPeriod = gcTenderbidfileRecords.get(0).getValidPeriod();
                            if(!ObjectUtils.isEmpty(validPeriod)){
                                record.setValidPeriod(validPeriod+"");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        try {
                            Long clarifyTime = gcTenderbidfileRecords.get(0).getClarifyTime();
                            if(!ObjectUtils.isEmpty(clarifyTime)){
                                record.setClarifyTime(clarifyTime+"");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        try {
                            Long submitTime = gcTenderbidfileRecords.get(0).getSubmitTime();
                            if(!ObjectUtils.isEmpty(submitTime)){
                                record.setSubmitTime(submitTime+"");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    QueryWrapper<GcBidopenrecord> gcBidopenrecordQueryWrapper = new QueryWrapper<GcBidopenrecord>();
                    Page<GcBidopenrecord> gcBidopenrecordPage = new Page<GcBidopenrecord>(1, 1);
                    gcBidopenrecordQueryWrapper.eq("BID_SECTION_CODES",splitBidSectionCode);
                    List<GcBidopenrecord> gcBidopenrecordRecords = gcBidopenrecordService.page(gcBidopenrecordPage, gcBidopenrecordQueryWrapper).getRecords();
                    if(gcBidopenrecordRecords.size() >= 1){
                        try {
                            String bidOpeningAddress = gcBidopenrecordRecords.get(0).getBidOpeningAddress();
                            if(!ObjectUtils.isEmpty(bidOpeningAddress)){
                                record.setBidOpeningAddress(bidOpeningAddress);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        try {
                            Long bidOpeningTime = gcBidopenrecordRecords.get(0).getBidOpeningTime();
                            if(!ObjectUtils.isEmpty(bidOpeningTime)){
                                record.setBidOpeningTime(bidOpeningTime+"");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        try {
                            Long bidFinishTime = gcBidopenrecordRecords.get(0).getBidFinishTime();
                            if(!ObjectUtils.isEmpty(bidFinishTime)){
                                record.setBidFinishTime(bidFinishTime+"");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    String noticeNature = record.getNoticeNature();
                    if(!ObjectUtils.isEmpty(noticeNature)){
                        try {
                            String noticeNatureTypeName = BulletinType.getNoticeNatureTypeName(noticeNature);
                            if(!ObjectUtils.isEmpty(noticeNatureTypeName)){
                                record.setNoticeNatureName(noticeNatureTypeName);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
            TenderDetailsVoUtils.isNull(record);
        }
        //Boolean aBoolean = gcNoticeandfileService.parseTenderDetailsVo(pageList.getRecords());
        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){
        GcNoticeandfile gcNoticeandfile = new GcNoticeandfile();
        Page<GcNoticeandfile> page = new Page<GcNoticeandfile>(pageNo, pageSize);
        IPage<TenderDetailsVo> pageList = gcNoticeandfileService.queryGcTenderProjectAllList(page, gcNoticeandfile);
        for (TenderDetailsVo record : pageList.getRecords()) {
            String escaped = StringEscapeUtils.unescapeHtml3(record.getNoticeContent());
            record.setNoticeContent(escaped);
            //获取国标区域名称
            QueryWrapper<TRegion> tRegionQueryWrapper = new QueryWrapper<TRegion>();
            tRegionQueryWrapper.eq("code",record.getRegionCode()+"000000");
            Page<TRegion> pageTRegion = new Page<TRegion>(1, 1);
            List<TRegion> records = tRegionService.page(pageTRegion, tRegionQueryWrapper).getRecords();
            if(records.size() >= 1){
                try {
                    String name = records.get(0).getName();
                    if(!ObjectUtils.isEmpty(name)){
                        record.setRegionName(name);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            //获取区域编号
            QueryWrapper<AreaPlatform> areaPlatformQueryWrapper = new QueryWrapper<AreaPlatform>();
            Page<AreaPlatform> areaPlatformPage = new Page<AreaPlatform>(1, 1);
            areaPlatformQueryWrapper.eq("PLATFORM_CODE",record.getPlatformCode());
            List<AreaPlatform> recordsAreaPlatform = areaPlatformService.page(areaPlatformPage, areaPlatformQueryWrapper).getRecords();
            if(recordsAreaPlatform.size() >= 1){
                try {
                    String regionName = recordsAreaPlatform.get(0).getRegionName();
                    if(!ObjectUtils.isEmpty(regionName)){
                        record.setPlatformName(regionName);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            record.setTransactionTypeCode(ClassIfy.ENGINEERING_CONSTRUCTION.getCode());
            record.setTransactionTypeName(ClassIfy.ENGINEERING_CONSTRUCTION.getName());
            //获取行业信息代码
            String industriesTypeCode = record.getIndustriesTypeCode();
            if(!ObjectUtils.isEmpty(industriesTypeCode)){
                try {
                    QueryWrapper<CIndustriesType> cIndustriesTypeQueryWrapper = new QueryWrapper<CIndustriesType>();
                    cIndustriesTypeQueryWrapper.eq("code",industriesTypeCode);
                    CIndustriesType cIndustriesType = cIndustriesTypeService.getOne(cIndustriesTypeQueryWrapper);
                    if(!ObjectUtils.isEmpty(cIndustriesType)){
                        String name = cIndustriesType.getName();
                        record.setIndustriesTypeName(name);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            record.setNoticeTypeCode(ClassIfy.TENDER_ANNOUNCEMENT.getCode());
            record.setNoticeTypeName(ClassIfy.TENDER_ANNOUNCEMENT.getName());
            String bulletinTypeCode = record.getBulletinTypeCode();
            if(!ObjectUtils.isEmpty(bulletinTypeCode)){
                try {
                    String bulletinTypeName = BulletinType.getBulletinTypeName(bulletinTypeCode);
                    if(!ObjectUtils.isEmpty(bulletinTypeName)){
                        record.setBulletinTypeName(bulletinTypeName);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            String dataSourceCode = record.getDataSourceCode();
            if(!ObjectUtils.isEmpty(dataSourceCode)){
                QueryWrapper<TRegion> tRegionQueryWrapperQ = new QueryWrapper<TRegion>();
                String subSourceCode = dataSourceCode.substring(1, dataSourceCode.length());
                tRegionQueryWrapperQ.eq("code",subSourceCode+"000000");
                List<TRegion> tRegionList = tRegionService.page(pageTRegion, tRegionQueryWrapperQ).getRecords();
                if(tRegionList.size() >= 1){
                    String name = tRegionList.get(0).getName();
                    if(!ObjectUtils.isEmpty(name)){
                        record.setDataSourceName(name+"公共资源交易中心");
                    }
                }
                if(subSourceCode.equals("150401")){
                    record.setDataSourceName("赤峰市公共资源交易中心");
                }
            }
            String bidSectionCodes = record.getBidSectionCodes();
            if(!ObjectUtils.isEmpty(bidSectionCodes)){
                int i1 = bidSectionCodes.indexOf(",");
                int i2 = bidSectionCodes.indexOf(";");
                int i3 = bidSectionCodes.indexOf("<br>");
                String splitBidSectionCode = null;
                boolean flag = false;
                if(i1 > 0){
                    splitBidSectionCode =  bidSectionCodes.split(",")[0];
                    flag = true;
                }
                if(i2 > 0){
                    splitBidSectionCode =  bidSectionCodes.split(";")[0];
                    flag = true;
                }
                if(i3 > 0){
                    splitBidSectionCode =  bidSectionCodes.split("<br>")[0];
                    flag = true;
                }
                if(!flag){
                    splitBidSectionCode = bidSectionCodes;
                }
                //获取标包编号
                if(!ObjectUtils.isEmpty(splitBidSectionCode)){
                    QueryWrapper<GcTenderbidfile> gcTenderbidfileQueryWrapper = new QueryWrapper<GcTenderbidfile>();
                    Page<GcTenderbidfile> gcTenderbidfilePage = new Page<GcTenderbidfile>(1, 1);
                    gcTenderbidfileQueryWrapper.eq("BID_SECTION_CODE",splitBidSectionCode);
                    List<GcTenderbidfile> gcTenderbidfileRecords = gcTenderbidfileService.page(gcTenderbidfilePage, gcTenderbidfileQueryWrapper).getRecords();
                    if(gcTenderbidfileRecords.size() >= 1){
                        try {
                            Integer validPeriod = gcTenderbidfileRecords.get(0).getValidPeriod();
                            if(!ObjectUtils.isEmpty(validPeriod)){
                                record.setValidPeriod(validPeriod+"");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        try {
                            Long clarifyTime = gcTenderbidfileRecords.get(0).getClarifyTime();
                            if(!ObjectUtils.isEmpty(clarifyTime)){
                                record.setClarifyTime(clarifyTime+"");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        try {
                            Long submitTime = gcTenderbidfileRecords.get(0).getSubmitTime();
                            if(!ObjectUtils.isEmpty(submitTime)){
                                record.setSubmitTime(submitTime+"");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    QueryWrapper<GcBidopenrecord> gcBidopenrecordQueryWrapper = new QueryWrapper<GcBidopenrecord>();
                    Page<GcBidopenrecord> gcBidopenrecordPage = new Page<GcBidopenrecord>(1, 1);
                    gcBidopenrecordQueryWrapper.eq("BID_SECTION_CODES",splitBidSectionCode);
                    List<GcBidopenrecord> gcBidopenrecordRecords = gcBidopenrecordService.page(gcBidopenrecordPage, gcBidopenrecordQueryWrapper).getRecords();
                    if(gcBidopenrecordRecords.size() >= 1){
                        try {
                            String bidOpeningAddress = gcBidopenrecordRecords.get(0).getBidOpeningAddress();
                            if(!ObjectUtils.isEmpty(bidOpeningAddress)){
                                record.setBidOpeningAddress(bidOpeningAddress);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        try {
                            Long bidOpeningTime = gcBidopenrecordRecords.get(0).getBidOpeningTime();
                            if(!ObjectUtils.isEmpty(bidOpeningTime)){
                                record.setBidOpeningTime(bidOpeningTime+"");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        try {
                            Long bidFinishTime = gcBidopenrecordRecords.get(0).getBidFinishTime();
                            if(!ObjectUtils.isEmpty(bidFinishTime)){
                                record.setBidFinishTime(bidFinishTime+"");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    String noticeNature = record.getNoticeNature();
                    if(!ObjectUtils.isEmpty(noticeNature)){
                        try {
                            String noticeNatureTypeName = BulletinType.getNoticeNatureTypeName(noticeNature);
                            if(!ObjectUtils.isEmpty(noticeNatureTypeName)){
                                record.setNoticeNatureName(noticeNatureTypeName);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
            TenderDetailsVoUtils.isNull(record);
        }
        return pageList.getRecords();
    }

}
