package org.jeecg.modules.elasticsearch.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.jeecg.modules.elasticsearch.entity.ClassIfy;
import org.jeecg.modules.elasticsearch.entity.TenderDetailsVo;
import org.jeecg.modules.elasticsearch.service.TenderDetailsVoService;
import org.jeecg.modules.elasticsearch.utils.RestHighLevelClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class TenderDetailsVoServiceImpl implements TenderDetailsVoService {


    @Autowired
    private RestHighLevelClientUtils restHighLevelClientUtils;

    @Override
    public Boolean  releasTenderDetailsVo(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)){
                    noticeSendTime = "1970-10-24 10:20:00";
                }
                tenderDetailsVo.setDocpubtime(noticeSendTime);
                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 Boolean  revokeTenderDetailsVo(List<TenderDetailsVo> tenderDetailsVos) throws JsonProcessingException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("2m");
        ObjectMapper objectMapper = new ObjectMapper();
        DeleteResponse deleteResponse = null;
        try{
            for (TenderDetailsVo tenderDetailsVo : tenderDetailsVos) {
                String sourceDataKey = tenderDetailsVo.getSourceDataKey();
                if(!ObjectUtils.isEmpty(sourceDataKey)){
                    deleteResponse = restHighLevelClientUtils.deleteDoc(ClassIfy.ES_INDEX_SEARCH_PUBLIC_RESOURCE_DEAL.getCode(), String.valueOf(tenderDetailsVo.getSourceDataKey()));
                }else{
                   return false;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        boolean flag = false;
        try {
            flag =  deleteResponse.status().toString().equals("OK");
            flag = true;
        }catch (Exception e){
            flag = false;
        }
        return flag;
    }
}
