package com.zanxiang.game.module.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.module.base.pojo.params.SendMsgTaskResultParam;
import com.zanxiang.game.module.base.pojo.vo.SendMsgResultVO;
import com.zanxiang.game.module.base.util.PageUtil;
import com.zanxiang.game.module.manage.service.ICpSendMsgResultService;
import com.zanxiang.game.module.mybatis.entity.CpSendMsgResult;
import com.zanxiang.game.module.mybatis.mapper.CpSendMsgResultMapper;
import com.zanxiang.module.util.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : lingfeng
 * @time : 2024-03-14
 * @description : CP消息发送结果
 */
@Slf4j
@Service
public class CpSendMsgResultServiceImpl extends ServiceImpl<CpSendMsgResultMapper, CpSendMsgResult> implements ICpSendMsgResultService {


    @Override
    public PageUtil<SendMsgResultVO> getSendMsgTaskResultList(SendMsgTaskResultParam param) {
        Page<CpSendMsgResult> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()),
                new LambdaQueryWrapper<CpSendMsgResult>()
                        .eq(CpSendMsgResult::getTaskId, param.getTaskId())
                        .eq(StringUtils.isNotEmpty(param.getSendStatus()), CpSendMsgResult::getSendStatus, param.getSendStatus())
                        .orderByDesc(CpSendMsgResult::getCreateTime)
        );
        List<CpSendMsgResult> cpSendMsgResults = page.getRecords();
        List<SendMsgResultVO> sendMsgVos = toVo(cpSendMsgResults);
        return new PageUtil<>(sendMsgVos, page.getTotal(), param.getPageSize(), param.getPageNum(), 0);
    }

    private List<SendMsgResultVO> toVo(List<CpSendMsgResult> cpSendMsgResults) {
        if (Objects.isNull(cpSendMsgResults) || cpSendMsgResults.isEmpty()) {
            return null;
        }
        return cpSendMsgResults.stream()
                .map(task -> BeanUtil.copy(task, SendMsgResultVO.class))
                .collect(Collectors.toList());
    }
}
