package com.njcn.csdevice.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.csdevice.mapper.CsEventSendMsgMapper;
import com.njcn.csdevice.pojo.po.CsEventSendMsg;
import com.njcn.csdevice.pojo.vo.CsEventSendMsgVO;
import com.njcn.csdevice.service.ICsEventSendMsgService;
import com.njcn.system.api.EpdFeignClient;
import com.njcn.system.pojo.po.EleEpdPqd;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.po.User;
import com.njcn.web.pojo.param.BaseParam;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 事件推送日志表 服务实现类
 * </p>
 *
 * @author xuyang
 * @since 2023-09-18
 */
@Service
@RequiredArgsConstructor
public class CsEventSendMsgServiceImpl extends ServiceImpl<CsEventSendMsgMapper, CsEventSendMsg> implements ICsEventSendMsgService {
    private final EpdFeignClient epdFeignClient;
    private final UserFeignClient userFeignClient;

    @Override
    public Page<CsEventSendMsgVO> queryPage(BaseParam baseParam) {
        Page<CsEventSendMsgVO> returnpage = new Page<> (baseParam.getPageNum ( ), baseParam.getPageSize ( ));

        Page<CsEventSendMsgVO> result = this.getBaseMapper().selectPages(returnpage,baseParam);
        List<String> collect1 = result.getRecords().stream().map(CsEventSendMsgVO::getUserId).distinct().collect(Collectors.toList());
        Map<String, String> collect2;
        if(!CollectionUtils.isEmpty(collect1)){
            List<User> userTem = userFeignClient.appuserByIdList(collect1).getData();
            collect2 = userTem.stream().collect(Collectors.toMap(User::getId, User::getName, (e1, e2) -> e1 + "," + e2));

        } else {
            collect2 = new HashMap<>();
        }
        result.getRecords().forEach(temp->{
            EleEpdPqd data = epdFeignClient.findByName(temp.getTag()).getData();
            temp.setShowName(data.getShowName());
            temp.setUserName(collect2.get(temp.getUserId()));
        });


        return result;
    }
}
