package com.njcn.kafka.event.service.impl;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.kafka.event.mapper.OracleRmpEventDetailPOMapper;
import com.njcn.kafka.event.mapper.PqLineMapper;
import com.njcn.kafka.event.pojo.dto.EventPushDTO;
import com.njcn.kafka.event.pojo.dto.LedgerBaseInfoDTO;
import com.njcn.kafka.event.pojo.param.PushParam;
import com.njcn.kafka.event.pojo.po.OracleRmpEventDetailPO;
import com.njcn.kafka.event.service.EventDataService;
import com.njcn.kafka.event.service.KafkaProducerService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * Description:
 * Date: 2025/05/28  上午 8:38【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class EventDataServiceImpl implements EventDataService {


    private final KafkaProducerService kafkaProducerService;
    private final OracleRmpEventDetailPOMapper oracleRmpEventDetailPOMapper;
    private final PqLineMapper pqLineMapper;


    @Override
    public void push(PushParam pushParam) throws Exception {
        LocalDateTime beginTime = pushParam.getBeginTime();
        LocalDateTime endTime = pushParam.getEndTime();
        List<OracleRmpEventDetailPO> oracleRmpEventDetailPOList = oracleRmpEventDetailPOMapper.selectList(
                new LambdaQueryWrapper<OracleRmpEventDetailPO>()
                        .between(OracleRmpEventDetailPO::getCreateTime,beginTime,endTime));
        if(!CollectionUtils.isEmpty(oracleRmpEventDetailPOList)){
            List<Integer> lineIds = oracleRmpEventDetailPOList.stream().map(OracleRmpEventDetailPO::getLineid).collect(Collectors.toList());
            List<LedgerBaseInfoDTO> baseLedger = pqLineMapper.getBaseLedger(lineIds, null);
            Map<Integer, LedgerBaseInfoDTO> ledgerBaseInfoDTOMap = baseLedger.stream().collect(Collectors.toMap(LedgerBaseInfoDTO::getLineId, Function.identity()));
            EventPushDTO eventPushDTO = new EventPushDTO();
            eventPushDTO.setTime(LocalDateTime.now());
            List<EventPushDTO.EventDTO> collect = oracleRmpEventDetailPOList.stream().map(temp -> {
                EventPushDTO.EventDTO eventDTO = new EventPushDTO.EventDTO();
                BeanUtils.copyProperties(temp,eventDTO);
                eventDTO.setEventdetail_index(temp.getEventdetailIndex());
                eventDTO.setBusname(ledgerBaseInfoDTOMap.get(temp.getLineid()).getBusBarName());
                eventDTO.setBdname(ledgerBaseInfoDTOMap.get(temp.getLineid()).getStationName());
                eventDTO.setPointname(ledgerBaseInfoDTOMap.get(temp.getLineid()).getLineName());
                eventDTO.setGdname(ledgerBaseInfoDTOMap.get(temp.getLineid()).getGdName());
                return eventDTO;
            }).collect(Collectors.toList());
            eventPushDTO.setBody(collect);
            String jsonStr = JSONUtil.toJsonStr(collect);

            kafkaProducerService.sendMessage("eventdata",jsonStr);
        }


    }
}
