package com.simuwang.manage.service.impl;

import com.simuwang.base.common.support.MybatisPage;
import com.simuwang.base.mapper.CompanyEmailConfigMapper;
import com.simuwang.base.mapper.CompanyEmailSendHistoryMapper;
import com.simuwang.base.pojo.dos.CompanyEmailSendHistoryDO;
import com.simuwang.base.pojo.dos.sys.SysRoleUserDO;
import com.simuwang.base.pojo.dto.query.CompanyEmailHistoryPageQuery;
import com.simuwang.base.pojo.dto.query.CompanyEmailPageQuery;
import com.simuwang.base.pojo.vo.CompanyEmailConfigVO;
import com.simuwang.base.pojo.vo.CompanyEmailSendHistoryVO;
import com.simuwang.manage.service.CompanyEmailSendHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * FileName: CompanyEmailSendHistoryServiceImpl
 * Author:   chenjianhua
 * Date:     2024/9/9 10:06
 * Description: ${DESCRIPTION}
 */
@Service
public class CompanyEmailSendHistoryServiceImpl implements CompanyEmailSendHistoryService {
    @Autowired
    private CompanyEmailSendHistoryMapper companyEmailSendHistoryMapper;

    @Autowired
    private CompanyEmailConfigMapper companyEmailConfigMapper;
    @Override
    public MybatisPage<CompanyEmailSendHistoryVO> searchCompanyEmail(CompanyEmailPageQuery companyEmailPageQuery) {
        List<CompanyEmailSendHistoryDO> dataList = companyEmailSendHistoryMapper.searchCompanyEmailList(companyEmailPageQuery);
        List<CompanyEmailSendHistoryVO> voList = dataList.stream().map(CompanyEmailSendHistoryDO::toVo).collect(Collectors.toList());
        for(CompanyEmailSendHistoryVO vo : voList){
            vo.setEmail(vo.getEmail().replaceAll(",",";"));
        }
        long total = companyEmailSendHistoryMapper.countCompanyEmailList(companyEmailPageQuery);
        return MybatisPage.of(total, voList);
    }

    @Override
    public MybatisPage<CompanyEmailSendHistoryVO> searchEmailHistory(CompanyEmailHistoryPageQuery companyEmailHistoryPageQuery) {
        List<CompanyEmailSendHistoryDO> dataList = companyEmailSendHistoryMapper.searchEmailHistory(companyEmailHistoryPageQuery);
        List<CompanyEmailSendHistoryVO> voList = dataList.stream().map(CompanyEmailSendHistoryDO::toVo).collect(Collectors.toList());
        long total = companyEmailSendHistoryMapper.countCompanyEmailhistory(companyEmailHistoryPageQuery);
        return MybatisPage.of(total, voList);
    }

    @Override
    @Transactional
    public void deleteEmail(List<String> companyIdList) {
        for(String companyId : companyIdList){
            List<String> emailList = companyEmailConfigMapper.searchEmailByCompanyId(companyId);
            //删除配置
            companyEmailConfigMapper.deleteCompanyEmailConfig(companyId);
            //删除历史
            if(emailList.size() > 0){
                companyEmailSendHistoryMapper.deleteEmailHistory(emailList);
            }
        }
    }

    @Override
    public void deleteEmailHistory(List<Integer> idList) {
        companyEmailSendHistoryMapper.deleteEmailHistoryByIds(idList);
    }

    @Override
    public MybatisPage<CompanyEmailConfigVO> searchEmailConfig(CompanyEmailHistoryPageQuery companyEmailHistoryPageQuery) {
        List<CompanyEmailConfigVO> emailList = companyEmailConfigMapper.searchEmailConfig(companyEmailHistoryPageQuery);
        long total = companyEmailConfigMapper.countEmailConfig(companyEmailHistoryPageQuery);
        return MybatisPage.of(total,emailList);
    }
}
