package com.trs.ggzyexchange.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.trs.ggzyexchange.api.vo.Result;
import com.trs.ggzyexchange.entity.*;
import com.trs.ggzyexchange.enums.CommonConstant;
import com.trs.ggzyexchange.service.*;
import com.trs.ggzyexchange.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
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 java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author trs
 * @since 2023-12-20
 */
@RestController
@RequestMapping("/jg-field-apply")
public class JgFieldApplyController extends BaseController {

    @Autowired
    private JgFieldApplyService jgFieldApplyService;

    @Autowired
    private SourceCodeNameService sourceCodeNameService;

    @Autowired
    private JgSiteInformationService jgSiteInformationService;

    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;

    @Autowired
    private GcTenderprojectService gcTenderprojectService;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping(value = "/list")
    public Result<IPage<JgFieldApplyVo>> queryPageList(
            @RequestParam(name="sourceCode",required=false) String sourceCode,
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize)
    {
        if(pageSize > 50){
            return Result.error("参数有误");
        }
        if(pageNo > 100){
            return Result.error("参数有误");
        }
        if(pageSize < 0){
            return Result.error("参数有误");
        }
        if(pageNo < 0){
            return Result.error("参数有误");
        }
        String redisKey = CommonConstant.JG_FIELD_APPLY_REDIS;
        if(!ObjectUtils.isEmpty(sourceCode) && sourceCode != ""){
            redisKey += redisKey + sourceCode;
        }
        if(!ObjectUtils.isEmpty(pageNo)){
            redisKey += redisKey + pageNo;
        }
        if(!ObjectUtils.isEmpty(pageSize)){
            redisKey += redisKey + pageSize;
        }
        Object JgFieldApplyDataObj = redisUtil.get(redisKey);
        if(!ObjectUtils.isEmpty(JgFieldApplyDataObj)){
            try {
                JSONObject jsonObject = JSONObject.parseObject(JgFieldApplyDataObj.toString());
                if(!ObjectUtils.isEmpty(jsonObject)){
                    Object current = jsonObject.get("current");
                    Object total = jsonObject.get("total");
                    Object pages = jsonObject.get("pages");
                    Object size = jsonObject.get("size");
                    Object records = jsonObject.get("records");
                    List<JgFieldApplyVo> jgFieldApplyVoListLL = null;
                    if(!ObjectUtils.isEmpty(records)){
                        jgFieldApplyVoListLL = JSONArray.parseArray(records.toString(), JgFieldApplyVo.class);
                    }
                    if(!ObjectUtils.isEmpty(jgFieldApplyVoListLL)){
                        IPage<JgFieldApplyVo> JgFieldApplyVoListLL = new Page<JgFieldApplyVo>();
                        if(!ObjectUtils.isEmpty(current)){
                            long LLcurrent = Long.parseLong(current.toString());
                            JgFieldApplyVoListLL.setCurrent(LLcurrent);
                        }
                        if(!ObjectUtils.isEmpty(total)){
                            long LLtotal = Long.parseLong(total.toString());
                            JgFieldApplyVoListLL.setTotal(LLtotal);
                        }
                        if(!ObjectUtils.isEmpty(pages)){
                            long LLpages = Long.parseLong(pages.toString());
                            JgFieldApplyVoListLL.setPages(LLpages);
                        }
                        if(!ObjectUtils.isEmpty(size)){
                            long LLsize = Long.parseLong(size.toString());
                            JgFieldApplyVoListLL.setSize(LLsize);
                        }
                        JgFieldApplyVoListLL.setRecords(jgFieldApplyVoListLL);
                        return Result.OK(JgFieldApplyVoListLL);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        QueryWrapper<JgFieldApply> queryWrapper = new QueryWrapper<JgFieldApply>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(new Date());
        String startTime = format+"000000";
        String endTime = format+"235959";
        queryWrapper.gt("BID_OPENING_TIME",startTime);
        queryWrapper.le("BID_OPENING_TIME",endTime);
        queryWrapper.eq("MEETING_TYPE","1");
        if(!ObjectUtils.isEmpty(sourceCode) && sourceCode != ""){
            queryWrapper.eq("source_data_code",sourceCode);
        }
        queryWrapper.orderByDesc("BID_OPENING_TIME");
        queryWrapper.orderByAsc("source_data_code");
        Page<JgFieldApply> page = new Page<JgFieldApply>(pageNo, pageSize);
        IPage<JgFieldApply> pageList = jgFieldApplyService.page(page, queryWrapper);
        IPage<JgFieldApplyVo> JgFieldApplyVoList = new Page<JgFieldApplyVo>();
        List<JgFieldApplyVo> applyVoList = new ArrayList<JgFieldApplyVo>();
        for (JgFieldApply jgFieldApply : pageList.getRecords()) {
            JgFieldApplyVo jgFieldApplyVo = new JgFieldApplyVo();
            try {
                String unifiedDealCodes = jgFieldApply.getUnifiedDealCodes();
                if(!ObjectUtils.isEmpty(unifiedDealCodes)){
                    int i1 = unifiedDealCodes.indexOf(",");
                    int i2 = unifiedDealCodes.indexOf(";");
                    int i3 = unifiedDealCodes.indexOf("<br>");
                    String splitUnifiedDealCodes = null;
                    boolean flag = false;
                    if(i1 >= 0){
                        splitUnifiedDealCodes =  unifiedDealCodes.split(",")[0];
                        flag = true;
                    }
                    if(i2 >= 0){
                        splitUnifiedDealCodes =  unifiedDealCodes.split(";")[0];
                        flag = true;
                    }
                    if(i3 >= 0){
                        splitUnifiedDealCodes =  unifiedDealCodes.split("<br>")[0];
                        flag = true;
                    }
                    if(!flag){
                        splitUnifiedDealCodes = unifiedDealCodes;
                    }
                    if(!ObjectUtils.isEmpty(splitUnifiedDealCodes)){
                        String sourceDataKey = getSourceDataKey(splitUnifiedDealCodes);
                        if(!ObjectUtils.isEmpty(sourceDataKey)){
                            jgFieldApplyVo.setSourceDataKey(sourceDataKey);
                        }
                        /*else{
                            String projectCode = jgFieldApply.getProjectCode();
                            if (!ObjectUtils.isEmpty(projectCode)){
                                String sourceDataKeyByProjectCode = getSourceDataKeyByProjectCode(projectCode);
                                jgFieldApplyVo.setSourceDataKey(sourceDataKeyByProjectCode);
                            }
                        }*/
                    }
                }
                try {
                    String sourceDataCode = jgFieldApply.getSourceDataCode();
                    if(!ObjectUtils.isEmpty(sourceDataCode)){
                        String sourceDataCodeName = getSourceDataCodeName(sourceDataCode);
                        jgFieldApplyVo.setSourceDataCodeName(sourceDataCodeName);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                String projectName = jgFieldApply.getProjectName();
                if(!ObjectUtils.isEmpty(projectName)){
                    jgFieldApplyVo.setProjectName(projectName);
                }
                String venueCode = jgFieldApply.getVenueCode();
                if(!ObjectUtils.isEmpty(venueCode)){
                    try {
                        String venueAddressName = getVenueAddressName(venueCode);
                        if(!ObjectUtils.isEmpty(venueAddressName) && venueAddressName != null && venueAddressName != ""){
                            jgFieldApplyVo.setVenueAddressName(venueAddressName);
                        }else{
                            jgFieldApplyVo.setVenueAddressName(venueCode);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                try {
                    SimpleDateFormat formatyyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
                    Long bidOpeningTime = jgFieldApply.getBidOpeningTime();
                    String bidOpeningTimeStr = bidOpeningTime+"";
                    if(!ObjectUtils.isEmpty(bidOpeningTimeStr) && bidOpeningTimeStr != ""){
                        Date parse = formatyyyyMMddHHmmss.parse(bidOpeningTimeStr);
                        if(!ObjectUtils.isEmpty(parse)){
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            String format1 = sdf.format(parse);
                            jgFieldApplyVo.setBidOpeningTime(format1);
                        }
                    }
                    Long bidEndTime = jgFieldApply.getBidEndTime();
                    String bidEndTimeStr = bidEndTime+"";
                    if(!ObjectUtils.isEmpty(bidEndTimeStr) && bidEndTimeStr != ""){
                        Date parse = formatyyyyMMddHHmmss.parse(bidOpeningTimeStr);
                        if(!ObjectUtils.isEmpty(parse)){
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            String format1 = sdf.format(parse);
                            jgFieldApplyVo.setBidEndTime(format1);
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            applyVoList.add(jgFieldApplyVo);
        }
        JgFieldApplyVoList.setRecords(applyVoList);
        JgFieldApplyVoList.setCurrent(pageList.getCurrent());
        JgFieldApplyVoList.setPages(pageList.getPages());
        JgFieldApplyVoList.setSize(pageList.getSize());
        JgFieldApplyVoList.setTotal(pageList.getTotal());
        redisUtil.set(redisKey, JSONObject.toJSONString(JgFieldApplyVoList),CommonConstant.REDIS_SAVE_TIME_TEN_MINUTES);
        return Result.OK(JgFieldApplyVoList);
    }
    public String getSourceDataKey (String unifiedDealCodes){
        String sourceDataKeyStr = null;
        if(!ObjectUtils.isEmpty(unifiedDealCodes) && unifiedDealCodes != ""){
            QueryWrapper<GcNoticeandfile> queryWrapper = new QueryWrapper<GcNoticeandfile>();
            queryWrapper.like("UNIFIED_DEAL_CODES",unifiedDealCodes);
            Page<GcNoticeandfile> page = new Page<GcNoticeandfile>(1, 1);
            IPage<GcNoticeandfile> pageList = gcNoticeandfileService.page(page, queryWrapper);
            try {
                List<GcNoticeandfile> records = pageList.getRecords();
                if(!ObjectUtils.isEmpty(records)){
                    if(records.size() > 0){
                        GcNoticeandfile sourceCodeName = pageList.getRecords().get(0);
                        String sourceDataKey = sourceCodeName.getSourceDataKey();
                        sourceDataKeyStr = sourceDataKey;
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return sourceDataKeyStr;
    }


    public String getSourceDataKeyByProjectCode (String projectCode){
        String sourceDataKeyStr = null;
        if(!ObjectUtils.isEmpty(projectCode) && projectCode != ""){
            QueryWrapper<GcTenderproject> queryWrapperGcTenderproject = new QueryWrapper<GcTenderproject>();
            queryWrapperGcTenderproject.eq("PROJECT_CODE",projectCode);
            Page<GcTenderproject> pageGcTenderproject = new Page<GcTenderproject>(1, 1);
            IPage<GcTenderproject> pageListGcTenderproject = gcTenderprojectService.page(pageGcTenderproject, queryWrapperGcTenderproject);
            try {
                List<GcTenderproject> GcTenderprojectRecords = pageListGcTenderproject.getRecords();
                if(!ObjectUtils.isEmpty(GcTenderprojectRecords)){
                    if(GcTenderprojectRecords.size() > 0){
                        GcTenderproject gcTenderproject = GcTenderprojectRecords.get(0);
                        if(!ObjectUtils.isEmpty(gcTenderproject)){
                            String tenderProjectCode = gcTenderproject.getTenderProjectCode();
                            if(!ObjectUtils.isEmpty(tenderProjectCode)){
                                QueryWrapper<GcNoticeandfile> queryWrapper = new QueryWrapper<GcNoticeandfile>();
                                queryWrapper.like("TENDER_PROJECT_CODE",tenderProjectCode);
                                Page<GcNoticeandfile> page = new Page<GcNoticeandfile>(1, 1);
                                IPage<GcNoticeandfile> pageList = gcNoticeandfileService.page(page, queryWrapper);
                                try {
                                    List<GcNoticeandfile> records = pageList.getRecords();
                                    if(!ObjectUtils.isEmpty(records)){
                                        if(records.size() > 0){
                                            GcNoticeandfile sourceCodeName = pageList.getRecords().get(0);
                                            String sourceDataKey = sourceCodeName.getSourceDataKey();
                                            sourceDataKeyStr = sourceDataKey;
                                        }
                                    }
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return sourceDataKeyStr;
    }

    public String getSourceDataCodeName(String sourceDataCode){
        String sourceNameStr = null;
        if(!ObjectUtils.isEmpty(sourceDataCode) && sourceDataCode != ""){
            QueryWrapper<SourceCodeName> queryWrapper = new QueryWrapper<SourceCodeName>();
            queryWrapper.eq("SOURCE_CODE",sourceDataCode);
            Page<SourceCodeName> page = new Page<SourceCodeName>(1, 1);
            IPage<SourceCodeName> pageList = sourceCodeNameService.page(page, queryWrapper);
            try {
                List<SourceCodeName> records = pageList.getRecords();
                if(!ObjectUtils.isEmpty(records)){
                    if(records.size() > 0){
                        SourceCodeName sourceCodeName = pageList.getRecords().get(0);
                        String sourceName = sourceCodeName.getSourceName();
                        sourceNameStr = sourceName;
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return sourceNameStr;
    }
    public String getVenueAddressName(String venueCode){
        String venueCameraName = null;
        if(!ObjectUtils.isEmpty(venueCode) && venueCode != ""){
            QueryWrapper<JgSiteInformation> queryWrapper = new QueryWrapper<JgSiteInformation>();
            queryWrapper.eq("VENUE_CODE",venueCode);
            Page<JgSiteInformation> page = new Page<JgSiteInformation>(1, 1);
            IPage<JgSiteInformation> pageList = jgSiteInformationService.page(page, queryWrapper);
            try {
                List<JgSiteInformation> records = pageList.getRecords();
                if(!ObjectUtils.isEmpty(records)){
                    if(records.size() > 0){
                        JgSiteInformation jgSiteInformation = pageList.getRecords().get(0);
                        String venueAddressNameStr = jgSiteInformation.getVenueAddressName();
                        venueCameraName = venueAddressNameStr;
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return venueCameraName;
    }
}
