package com.trs.ggzyexchange.controller;


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.trs.ggzyexchange.api.vo.Result;
import com.trs.ggzyexchange.entity.GcAttachment;
import com.trs.ggzyexchange.entity.GcBidcandidatePost;
import com.trs.ggzyexchange.entity.GcTenderbidfile;
import com.trs.ggzyexchange.entity.TenderDetailsVo;
import com.trs.ggzyexchange.enums.ClassIfy;
import com.trs.ggzyexchange.service.GcAttachmentService;
import com.trs.ggzyexchange.service.GcNoticeandfileService;
import com.trs.ggzyexchange.service.GcTenderbidfileService;
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-02-16
 */
@RestController
@RequestMapping("/gc-tenderbidfile")
public class GcTenderbidfileController extends BaseController {

    @Autowired
    private GcTenderbidfileService gcTenderbidfileService;

    @Autowired
    private AreaDataName areaDataName;

    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;

    @Autowired
    private TenderDetailsVoService tenderDetailsVoService;

    @Autowired
    private GcAttachmentService gcAttachmentService;


   // @GetMapping(value = "/queryPageListAll")
    public Result<IPage<TenderDetailsVo>> queryPageListAll(
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
            HttpServletRequest req
    ){
        Page<GcTenderbidfile> page = new Page<GcTenderbidfile>(pageNo, pageSize);
        IPage<TenderDetailsVo> tenderDetailsVoIPage = gcTenderbidfileService.queryGcTenderbidfileAllList(page);
        for (TenderDetailsVo record : tenderDetailsVoIPage.getRecords()) {
            String projectName = record.getProjectName();
            if(!ObjectUtils.isEmpty(projectName)){
                record.setNoticeName(projectName+" 招标文件");
            }
            record.setNoticeTypeCode(ClassIfy.BIDDING_DOCUMENTS.getCode());
            record.setNoticeTypeName(ClassIfy.BIDDING_DOCUMENTS.getName());
            record.setTransactionTypeCode(ClassIfy.ENGINEERING_CONSTRUCTION.getCode());
            record.setTransactionTypeName(ClassIfy.ENGINEERING_CONSTRUCTION.getName());
            record = tenderDetailsVoService.getTenderDetailsVoAllOrther(record);
            String bidSectionCodes = record.getBidSectionCodes();
            int i1 = bidSectionCodes.indexOf(",");
            int i2 = bidSectionCodes.indexOf(";");
            int i3 = bidSectionCodes.indexOf("<br>");
            String splitBidSectionCode = null;
            boolean flag = false;
            String[] split = null;
            if(i1 > 0){
                split =  bidSectionCodes.split(",");
                flag = true;
            }
            if(i2 > 0){
                split =  bidSectionCodes.split(";");
                flag = true;
            }
            if(i3 > 0){
                split =  bidSectionCodes.split("<br>");
                flag = true;
            }

            if(!flag){
                split = new String[]{bidSectionCodes};
            }
            List<GcAttachment> attachmentList = new ArrayList<GcAttachment>();
            if(!ObjectUtils.isEmpty(split)){
                if(split.length >=1){
                    for (String bidSectionCode : split) {
                        //通过标包编号查询对应的附件信息
                        QueryWrapper<GcAttachment> gcAttachmentQueryWrapper = new QueryWrapper<GcAttachment>();
                        gcAttachmentQueryWrapper.eq("BID_SECTION_CODE",bidSectionCode);
                        gcAttachmentQueryWrapper.eq("ATTACHMENT_SET_CODE","TENDER_FILE");
                        List<GcAttachment> list = gcAttachmentService.list(gcAttachmentQueryWrapper);
                        if(!ObjectUtils.isEmpty(list)){
                            attachmentList.addAll(list);
                        }
                    }
                }
            }
            String file = "";
            for (GcAttachment gcAttachment : attachmentList) {
                String attachmentName = gcAttachment.getAttachmentName();
                String url = gcAttachment.getUrl();
                file += "<a href='"+ url +"'>"+ attachmentName +"</a><br/>";
            }
            record.setNoticeContent(file);
            TenderDetailsVoUtils.isNull(record);
        }
        return Result.OK(tenderDetailsVoIPage);
    }

   // @GetMapping(value = "/queryPageListUpdateEs")
    public Integer queryPageListUpdateEs() throws JsonProcessingException {
        int count = gcTenderbidfileService.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<GcTenderbidfile> page = new Page<GcTenderbidfile>(pageNo, pageSize);
        IPage<TenderDetailsVo> tenderDetailsVoIPage = gcTenderbidfileService.queryGcTenderbidfileAllList(page);
        for (TenderDetailsVo record : tenderDetailsVoIPage.getRecords()) {
            String projectName = record.getProjectName();
            if(!ObjectUtils.isEmpty(projectName)){
                record.setNoticeName(projectName);
            }
            record.setNoticeTypeCode(ClassIfy.BIDDING_DOCUMENTS.getCode());
            record.setNoticeTypeName(ClassIfy.BIDDING_DOCUMENTS.getName());
            record.setTransactionTypeCode(ClassIfy.ENGINEERING_CONSTRUCTION.getCode());
            record.setTransactionTypeName(ClassIfy.ENGINEERING_CONSTRUCTION.getName());
            record = tenderDetailsVoService.getTenderDetailsVoAllOrther(record);
            String bidSectionCodes = record.getBidSectionCodes();
            int i1 = bidSectionCodes.indexOf(",");
            int i2 = bidSectionCodes.indexOf(";");
            int i3 = bidSectionCodes.indexOf("<br>");
            String splitBidSectionCode = null;
            boolean flag = false;
            String[] split = null;
            if(i1 > 0){
                split =  bidSectionCodes.split(",");
                flag = true;
            }
            if(i2 > 0){
                split =  bidSectionCodes.split(";");
                flag = true;
            }
            if(i3 > 0){
                split =  bidSectionCodes.split("<br>");
                flag = true;
            }
            if(!flag){
                split = new String[]{bidSectionCodes};
            }
            List<GcAttachment> attachmentList = new ArrayList<GcAttachment>();
            if(!ObjectUtils.isEmpty(split)){
                if(split.length >=1){
                    for (String bidSectionCode : split) {
                        //通过标包编号查询对应的附件信息
                        QueryWrapper<GcAttachment> gcAttachmentQueryWrapper = new QueryWrapper<GcAttachment>();
                        gcAttachmentQueryWrapper.eq("BID_SECTION_CODE",bidSectionCode);
                        List<GcAttachment> list = gcAttachmentService.list(gcAttachmentQueryWrapper);
                        if(!ObjectUtils.isEmpty(list)){
                            attachmentList.addAll(list);
                        }
                    }
                }
            }
            String file = "";
            for (GcAttachment gcAttachment : attachmentList) {
                String attachmentName = gcAttachment.getAttachmentName();
                String url = gcAttachment.getUrl();
                file += "<a href='"+ url +"'>"+ attachmentName +"</a><br/>";
            }
            record.setNoticeContent(file);
            TenderDetailsVoUtils.isNull(record);
        }
        return tenderDetailsVoIPage.getRecords();
    }
}
