package com.trs.ggzyexchange.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.trs.ggzyexchange.controller.AreaDataName;
import com.trs.ggzyexchange.entity.*;
import com.trs.ggzyexchange.enums.BulletinType;
import com.trs.ggzyexchange.service.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TenderDetailsVoServiceImpl implements TenderDetailsVoService {
    @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;
    @Autowired
    private GcBidpublicityService gcBidpublicityService;

    @Autowired
    private QtjyBidopenrecordService qtjyBidopenrecordService;

    @Autowired
    private ZcBidopenrecordService zcBidopenrecordService;

    public TenderDetailsVo getTenderDetailsVoAllOrther(TenderDetailsVo record){
        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();
            }
        }
        //获取行业信息代码
        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();
                    if(!ObjectUtils.isEmpty(name)){
                        record.setIndustriesTypeName(name);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        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){
                try {
                    String name = tRegionList.get(0).getName();
                    if(!ObjectUtils.isEmpty(name)){
                        record.setDataSourceName(name+"公共资源交易中心");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            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();
                    }
                }
            }
        }
        return record;
    }

    public TenderDetailsVo getTenderDetailsVoAllOrtherZc(TenderDetailsVo record){
        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();
            }
        }
        //获取行业信息代码
        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();
                    if(!ObjectUtils.isEmpty(name)){
                        record.setIndustriesTypeName(name);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        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){
                try {
                    String name = tRegionList.get(0).getName();
                    if(!ObjectUtils.isEmpty(name)){
                        record.setDataSourceName(name+"公共资源交易中心");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            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<ZcBidopenrecord> gcBidopenrecordQueryWrapper = new QueryWrapper<ZcBidopenrecord>();
                Page<ZcBidopenrecord> gcBidopenrecordPagezc = new Page<ZcBidopenrecord>(1, 1);
                gcBidopenrecordQueryWrapper.eq("BID_SECTION_CODES",splitBidSectionCode);
                List<ZcBidopenrecord> zcBidopenrecordRecords = zcBidopenrecordService.page(gcBidopenrecordPagezc, gcBidopenrecordQueryWrapper).getRecords();
                if(zcBidopenrecordRecords.size() >= 1){
                    try {
                        String bidOpeningAddress = zcBidopenrecordRecords.get(0).getBidOpeningAddress();
                        if(!ObjectUtils.isEmpty(bidOpeningAddress)){
                            record.setBidOpeningAddress(bidOpeningAddress);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    try {
                        Long bidOpeningTime = zcBidopenrecordRecords.get(0).getBidOpeningTime();
                        if(!ObjectUtils.isEmpty(bidOpeningTime)){
                            record.setBidOpeningTime(bidOpeningTime+"");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    try {
                        Long bidFinishTime = zcBidopenrecordRecords.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();
                    }
                }
            }
        }
        return record;
    }

    public TenderDetailsVo getTenderDetailsVoAllQtjyNoticeOrther(TenderDetailsVo record){
        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();
            }
        }
        //获取行业信息代码
        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();
                    if(!ObjectUtils.isEmpty(name)){
                        record.setIndustriesTypeName(name);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        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){
                try {
                    String name = tRegionList.get(0).getName();
                    if(!ObjectUtils.isEmpty(name)){
                        record.setDataSourceName(name+"公共资源交易中心");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(subSourceCode.equals("150401")){
                record.setDataSourceName("赤峰市公共资源交易中心");
            }
        }
        String tenderProjectCode = record.getTenderProjectCode();

        QueryWrapper<QtjyBidopenrecord> qtjyBidopenrecordQueryWrapper = new QueryWrapper<>();

        qtjyBidopenrecordQueryWrapper.eq("TENDER_PROJECT_CODE",tenderProjectCode);
        qtjyBidopenrecordQueryWrapper.orderByAsc("BID_OPENING_TIME");

        List<QtjyBidopenrecord> qtjyBidopenrecordList = qtjyBidopenrecordService.list(qtjyBidopenrecordQueryWrapper);

        if(qtjyBidopenrecordList.size() >=1){
            QtjyBidopenrecord qtjyBidopenrecord = qtjyBidopenrecordList.get(0);
            if(!ObjectUtils.isEmpty(qtjyBidopenrecord)){
                Long bidOpeningTime = qtjyBidopenrecord.getBidOpeningTime();
                if(!ObjectUtils.isEmpty(bidOpeningTime)){
                    record.setBidOpeningTime(bidOpeningTime.toString());
                }
                Long bidFinishTime = qtjyBidopenrecord.getBidFinishTime();
                if(!ObjectUtils.isEmpty(bidFinishTime)){
                    record.setBidFinishTime(bidFinishTime.toString());
                }
                String bidOpeningAddress = qtjyBidopenrecord.getBidOpeningAddress();
                record.setBidOpeningAddress(bidOpeningAddress);
            }
        }
        return record;
    }
}
