package com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.result.XlwResultData;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.EventDetailMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.EventDetailDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.EventMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.*;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.feign.XlwClientFeign;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zws
 * @date 2024/8/21
 */
@Component
public class XlwEventMapper implements EventMapper {

    @Resource
    private XlwClientFeign xlwClientFeign;
    @Resource
    private EventDetailMapper eventDetailMapper;
    @Override
    public List<EventDetailDO> syncEventDetailList(Date startDateTime, Date endDateTime) {
        XlwEventListSearchDTO xlwEventListSearchDTO = new XlwEventListSearchDTO();
        xlwEventListSearchDTO.setPage(1);
        xlwEventListSearchDTO.setPageSize(10);
        XlwResultData<XlwPageDTO<XlwEventListDTO>> resultData = xlwClientFeign.allEventList(xlwEventListSearchDTO);
        XlwPageDTO<XlwEventListDTO> xlwPageDTO = resultData.getData();
        long total = xlwPageDTO.getTotal();
        List<XlwEventListDTO> xlwEventListDTOList = new ArrayList<>();
        if(total > 1000){
            int pageSize = 1000;
            int maxPageNo = ((int)total/pageSize) + 1;
            for(int i = 1; i <= maxPageNo; i++){
                xlwEventListSearchDTO.setPage(i);
                xlwEventListSearchDTO.setPageSize(pageSize);
                resultData = xlwClientFeign.allEventList(xlwEventListSearchDTO);
                xlwPageDTO = resultData.getData();
                xlwEventListDTOList.addAll(xlwPageDTO.getRows());
            }
        }else{
            xlwEventListSearchDTO.setPageSize(Integer.valueOf(total+""));
            resultData = xlwClientFeign.allEventList(xlwEventListSearchDTO);
            xlwPageDTO = resultData.getData();
            xlwEventListDTOList = xlwPageDTO.getRows();
        }
        LambdaQueryWrapper<EventDetailDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(EventDetailDO::getThirdPartyId);
        queryWrapper.ge(EventDetailDO::getEventStartTime,startDateTime);
        queryWrapper.le(EventDetailDO::getEventStartTime,endDateTime);
        List<EventDetailDO> eventDetailDOList = eventDetailMapper.selectList(queryWrapper);
        List<String> thirdPartyIdList = eventDetailDOList.stream().map(EventDetailDO::getThirdPartyId).toList();
        List<String> eventIdList = xlwEventListDTOList.stream().map(XlwEventListDTO::getGuid).toList();
        //取差集需要同步的event
        List<String> syncEventIdList = CollUtil.subtractToList(eventIdList,thirdPartyIdList);
        if(CollUtil.isEmpty(eventIdList)){
            return List.of();
        }
        xlwEventListDTOList = xlwEventListDTOList.stream().filter(event-> syncEventIdList.contains(event.getGuid())).toList();
        List<EventDetailDO> detailDOList = XlwEventListDTO.convert2DO(xlwEventListDTOList);
        Map<String,List<XlwEventImageDTO>> eventIdImageListMap = new HashMap<>();
        for(XlwEventListDTO xlwEventListDTO : xlwEventListDTOList){
            XlwEventIdDTO xlwEventIdDTO = new XlwEventIdDTO(xlwEventListDTO.getGuid());
            XlwResultData<List<XlwEventImageDTO>> imageResultData = xlwClientFeign.getEventImageById(xlwEventIdDTO);
            List<XlwEventImageDTO> imageList = imageResultData.getData();
            eventIdImageListMap.put(xlwEventListDTO.getGuid(),imageList);
        }

        for(EventDetailDO eventDetailDO : detailDOList){
            List<XlwEventImageDTO> imageList = eventIdImageListMap.get(eventDetailDO.getThirdPartyId());
            List<String> imagePathList = new ArrayList<>();
            if(Objects.nonNull(imageList)){
                imageList.forEach(eventImage->{
                    if(StrUtil.isNotBlank(eventImage.getHeadImage())){
                        imagePathList.add(eventImage.getHeadImage());
                    }

                    if(StrUtil.isNotBlank(eventImage.getTailImage())){
                        imagePathList.add(eventImage.getTailImage());
                    }
                });
            }
            eventDetailDO.setEventImagesPath(imagePathList);
        }
        return detailDOList;
    }
}
