package com.trs.ggzyexchange.service.impl;

import com.alibaba.fastjson.JSON;
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.entity.GcNoticeandfile;
import com.trs.ggzyexchange.entity.GcNoticeandfileVo;
import com.trs.ggzyexchange.entity.TenderDetailsVo;
import com.trs.ggzyexchange.enums.ClassIfy;
import com.trs.ggzyexchange.mapper.GcNoticeandfileMapper;
import com.trs.ggzyexchange.service.GcNoticeandfileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trs.ggzyexchange.utils.RestHighLevelClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 招标公告与资格预审公告 服务实现类
 * </p>
 *
 * @author trs
 * @since 2023-02-16
 */
@Slf4j
@Service
public class GcNoticeandfileServiceImpl extends ServiceImpl<GcNoticeandfileMapper, GcNoticeandfile> implements GcNoticeandfileService {

    @Autowired
    private GcNoticeandfileMapper gcNoticeandfileMapper;


    @Autowired
    private RestHighLevelClientUtils restHighLevelClientUtils;


    @Override
    public IPage<GcNoticeandfileVo> queryGcTenderProjectList(Page<GcNoticeandfile> page, GcNoticeandfile gcNoticeandfile) {
        return gcNoticeandfileMapper.queryGcTenderProjectList(
                page,
                gcNoticeandfile.getRegionCode(),
                gcNoticeandfile.getIndustriesType(),
                gcNoticeandfile.getNoticeName(),
                gcNoticeandfile.getPlatformCode(),
                gcNoticeandfile.getNoticeSendTimeStart(),
                gcNoticeandfile.getNoticeSendTimeEnd()
                );
    }

    @Override
    public IPage<TenderDetailsVo> queryGcTenderProjectAllList(Page<GcNoticeandfile> page, GcNoticeandfile gcNoticeandfile) {

        return gcNoticeandfileMapper.queryGcTenderProjectAllList(
                page,
                gcNoticeandfile.getRegionCode(),
                gcNoticeandfile.getIndustriesType(),
                gcNoticeandfile.getNoticeName(),
                gcNoticeandfile.getPlatformCode(),
                gcNoticeandfile.getNoticeSendTimeStart(),
                gcNoticeandfile.getNoticeSendTimeEnd()
        );
    }


    @Override
    public IPage<TenderDetailsVo> queryGcTenderProjectAllListGeTime(Page<GcNoticeandfile> page, GcNoticeandfile gcNoticeandfile, Long time) {
        return gcNoticeandfileMapper.queryGcTenderProjectAllListGeTime(
                page,
                gcNoticeandfile.getRegionCode(),
                gcNoticeandfile.getIndustriesType(),
                gcNoticeandfile.getNoticeName(),
                gcNoticeandfile.getPlatformCode(),
                gcNoticeandfile.getNoticeSendTimeStart(),
                gcNoticeandfile.getNoticeSendTimeEnd(),
                time
        );
    }

    @Override
    public Boolean  parseTenderDetailsVo(List<TenderDetailsVo> tenderDetailsVos) throws JsonProcessingException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("2m");
        ObjectMapper objectMapper = new ObjectMapper();
        IndexResponse info = null;
        try{
            for (TenderDetailsVo tenderDetailsVo : tenderDetailsVos) {
                String noticeSendTime = tenderDetailsVo.getNoticeSendTime();
                if(!ObjectUtils.isEmpty(noticeSendTime)){
                    tenderDetailsVo.setDocpubtime(noticeSendTime);
                }else {
                    noticeSendTime = "1970-10-24 10:20:00";
                }
                String bidSectionCodes = tenderDetailsVo.getBidSectionCodes();
                if(!ObjectUtils.isEmpty(bidSectionCodes) && 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(",");
                        flag = true;
                    }
                    if(i2 > 0){
                        splitBidSectionCode =  bidSectionCodes.split(";");
                        flag = true;
                    }
                    if(i3 > 0){
                        splitBidSectionCode =  bidSectionCodes.split("<br>");
                        flag = true;
                    }
                    if(!flag){
                        splitBidSectionCode = new String[]{bidSectionCodes};
                    }
                    if(!ObjectUtils.isEmpty(splitBidSectionCode)){
                        tenderDetailsVo.setBidSectionCodesArr(splitBidSectionCode);
                    }else {
                        tenderDetailsVo.setBidSectionCodesArr(new String[]{""});
                    }
                }
                String toJSONString = objectMapper.writeValueAsString(tenderDetailsVo);
                log.info("同步数据-主键："+tenderDetailsVo.getSourceDataKey());
                log.info("同步数据-公告标题："+tenderDetailsVo.getNoticeName());
                log.info("发布时间："+tenderDetailsVo.getNoticeSendTime());
                info = restHighLevelClientUtils.addDoc(ClassIfy.ES_INDEX_SEARCH_PUBLIC_RESOURCE_DEAL.getCode(), String.valueOf(tenderDetailsVo.getSourceDataKey()), toJSONString);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        boolean flag = false;
        try {
            flag =  info.status().toString().equals("OK");
            flag = true;
        }catch (Exception e){
            flag = false;
        }
        return flag;
    }


    @Override
    public Long maxDataTimeStamp() {
        Long aLong = gcNoticeandfileMapper.maxDataTimeStamp();
        try{
            if(!ObjectUtils.isEmpty(aLong)){
                if(aLong != 0){
                    String sLong = ((aLong+"").substring(0,8)) + "000000";
                    aLong = Long.parseLong(sLong);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return aLong;
    }


}
