package com.simuwang.manage.service.impl;

import com.simuwang.base.common.support.MybatisPage;
import com.simuwang.base.common.util.DateUtils;
import com.simuwang.base.common.util.StringUtil;
import com.simuwang.base.mapper.EmailTaskInfoMapper;
import com.simuwang.base.pojo.dos.EmailTaskInfoDO;
import com.simuwang.base.pojo.dto.query.TaskPageQuery;
import com.simuwang.base.pojo.vo.EmailTaskInfoVO;
import com.simuwang.base.pojo.vo.IdListVO;
import com.simuwang.manage.service.TaskLogService;
import com.simuwang.shiro.utils.UserUtils;
import com.smppw.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * FileName: TaskLogServiceImpl
 * Author:   chenjianhua
 * Date:     2024/10/8 14:08
 * Description: ${DESCRIPTION}
 */
@Service
public class TaskLogServiceImpl implements TaskLogService {

    @Autowired
    private EmailTaskInfoMapper emailTaskInfoMapper;

    @Override
    public MybatisPage<EmailTaskInfoVO> searchTaskList(TaskPageQuery taskPageQuery) {
        if(StringUtil.isNotEmpty(taskPageQuery.getEndDate())){
            taskPageQuery.setEndDate(DateUtil.getAroundDate(DateUtils.parse(taskPageQuery.getEndDate(),DateUtils.YYYY_MM_DD),1));
        }
        List<EmailTaskInfoDO> emailTaskInfoDOList = emailTaskInfoMapper.searchTaskList(taskPageQuery);
        List<EmailTaskInfoVO> emailTaskInfoVOList = emailTaskInfoDOList.stream().map(EmailTaskInfoDO::toVO).collect(Collectors.toList());
        long total = emailTaskInfoMapper.countTaskList(taskPageQuery);
        return MybatisPage.of(total,emailTaskInfoVOList);
    }

    @Override
    public void deleteTaskLog(IdListVO idListVO) {
        emailTaskInfoMapper.deleteTaskLog(idListVO.getIdList(), UserUtils.getLoginUser().getUserId());
    }
}
