package com.simuwang.manage.service.impl;

import com.simuwang.base.common.enums.ParseStatus;
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.EmailFileInfoMapper;
import com.simuwang.base.mapper.EmailParseInfoMapper;
import com.simuwang.base.mapper.EmailTypeRuleMapper;
import com.simuwang.base.pojo.dos.EmailFileInfoDO;
import com.simuwang.base.pojo.dos.EmailParseDetailDO;
import com.simuwang.base.pojo.dos.EmailParseInfoDO;
import com.simuwang.base.pojo.dos.EmailTypeRuleDO;
import com.simuwang.base.pojo.dto.query.EmailFileQuery;
import com.simuwang.base.pojo.dto.query.EmailParseQuery;
import com.simuwang.base.pojo.vo.EmailFileInfoVO;
import com.simuwang.base.pojo.vo.EmailParseDetailVO;
import com.simuwang.base.pojo.vo.EmailParseInfoVO;
import com.simuwang.base.pojo.vo.EmailTypeRuleVO;
import com.simuwang.manage.service.ParseEmailService;
import com.smppw.common.pojo.enums.TimeRange;
import com.smppw.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * FileName: ParseEmailServiceImpl
 * Author:   chenjianhua
 * Date:     2024/9/10 11:26
 * Description: ${DESCRIPTION}
 */
@Service
public class ParseEmailServiceImpl implements ParseEmailService {

    @Autowired
    private EmailTypeRuleMapper emailTypeRuleMapper;

    @Autowired
    private EmailParseInfoMapper emailParseInfoMapper;

    @Autowired
    private EmailFileInfoMapper emailFileInfoMapper;
    @Override
    public void saveEmailType(EmailTypeRuleVO emailTypeRuleVO) {
        EmailTypeRuleDO  ruleDO = new EmailTypeRuleDO();
        ruleDO.setId(emailTypeRuleVO.getId());
        ruleDO.setNav(emailTypeRuleVO.getNav());
        ruleDO.setReport(emailTypeRuleVO.getReport());
        ruleDO.setValuation(emailTypeRuleVO.getValuation());
        ruleDO.setUpdateTime(new Date());
        if(ruleDO.getId() == null){
            ruleDO.setCreateTime(new Date());
            emailTypeRuleMapper.saveEmailType(ruleDO);
        }else{
            emailTypeRuleMapper.updateEmailType(ruleDO);
        }
    }

    @Override
    public EmailTypeRuleVO searchEmailType() {
        EmailTypeRuleVO emailTypeRuleVO = new EmailTypeRuleVO();
        EmailTypeRuleDO emailTypeRuleDO = emailTypeRuleMapper.searchEmailType();
        if(emailTypeRuleDO == null){
            return emailTypeRuleVO;
        }
        emailTypeRuleVO.setId(emailTypeRuleDO.getId());
        emailTypeRuleVO.setNav(emailTypeRuleDO.getNav());
        emailTypeRuleVO.setValuation(emailTypeRuleDO.getValuation());
        emailTypeRuleVO.setReport(emailTypeRuleDO.getReport());
        return emailTypeRuleVO;
    }

    @Override
    public MybatisPage<EmailParseInfoVO> searchEmailList(EmailParseQuery emailParseQuery){
        if(emailParseQuery.getTimeRange().equals(TimeRange.FromSetup)){
            emailParseQuery.setEndDate(DateUtil.getTodayDate());
        }
        if(!emailParseQuery.getTimeRange().equals(TimeRange.Custom) && !emailParseQuery.getTimeRange().equals(TimeRange.FromSetup)){
            emailParseQuery.setEndDate(DateUtils.getAroundToday(1));
            emailParseQuery.setStartDate( DateUtil.RangeTime(emailParseQuery.getTimeRange().getFloatDateSymbel(),DateUtil.getTodayDate()));
        }else{
            //自定义时间，把结束时间延后一天，确保时间判断能查到结束日期当天的数据
            emailParseQuery.setEndDate(DateUtil.getAroundDate(DateUtils.parse(emailParseQuery.getEndDate(),DateUtils.YYYY_MM_DD),1));
        }
        List<EmailParseInfoDO> emailParseInfoDOList = emailParseInfoMapper.searchEmailList(emailParseQuery);
        List<EmailParseInfoVO> emailParseInfoVOList = emailParseInfoDOList.stream()
                .map(EmailParseInfoDO::toVO).collect(Collectors.toList());
        for(EmailParseInfoVO vo : emailParseInfoVOList){
            List<EmailFileInfoDO> emailFileInfoList = emailFileInfoMapper.getEmailFileByEmailId(vo.getId());
            List<EmailFileInfoVO> emailFileInfoVOList = emailFileInfoList.stream().map(EmailFileInfoDO::toVO).collect(Collectors.toList());
            vo.setEmailFileInfoList(emailFileInfoVOList);
        }
        long total = emailParseInfoMapper.countEmailList(emailParseQuery);
        return MybatisPage.of(total,emailParseInfoVOList);
    }

    @Override
    public Map<String, Integer> searchEmailCount(TimeRange timeRange,String startDate,String endDate) {
        if(timeRange.equals(TimeRange.FromSetup)){
            endDate= DateUtil.getTodayDate();
        }
        if(!timeRange.equals(TimeRange.Custom) && !timeRange.equals(TimeRange.FromSetup)){
            endDate = DateUtils.getAroundToday(1);
            startDate = DateUtil.RangeTime(timeRange.getFloatDateSymbel(),DateUtil.getTodayDate());
        }else{
            //自定义时间，把结束时间延后一天，确保时间判断能查到结束日期当天的数据
            endDate = DateUtil.getAroundDate(DateUtils.parse(endDate,DateUtils.YYYY_MM_DD),1);
        }
        Map<String, Integer> result = new HashMap<>();
        Integer parseTotal = emailParseInfoMapper.searchEmailCount(startDate,endDate,null);
        result.put("parseTotal",parseTotal);
        Integer parseSuccess = emailParseInfoMapper.searchEmailCount(startDate,endDate, ParseStatus.Success.getCode());
        result.put("parseSuccess",parseSuccess);
        Integer parseFail = emailParseInfoMapper.searchEmailCount(startDate,endDate, ParseStatus.Fail.getCode());
        result.put("parseFail",parseFail);
        return result;
    }

    private String getParseDate(TimeRange timeRange, String date) {
        String preDate =DateUtil.RangeTime(timeRange.getFloatDateSymbel(),date);
        return preDate;
    }

    @Override
    public EmailFileInfoVO getEmailFileById(Integer fileId) {
        EmailFileInfoDO emailFileInfoDO = emailFileInfoMapper.getEmailFileById(fileId);
        EmailFileInfoVO fileInfoVO = new EmailFileInfoVO();
        if(emailFileInfoDO != null){
            fileInfoVO.setId(emailFileInfoDO.getId());
            fileInfoVO.setEmailId(emailFileInfoDO.getEmailId());
            fileInfoVO.setFilePath(emailFileInfoDO.getFilePath());
            fileInfoVO.setFileName(emailFileInfoDO.getFileName());
        }
        return fileInfoVO;
    }

    @Override
    public MybatisPage<EmailParseDetailVO> searchEmailDetailById(EmailFileQuery emailFileQuery) {
        //根据邮件ID获取附件信息
        List<EmailParseDetailDO> dataList = emailFileInfoMapper.searchEmailDetailById(emailFileQuery);
        List<EmailParseDetailVO> result = dataList.stream().filter(e -> StringUtil.isNotNull(e)).map(EmailParseDetailDO::toVO).collect(Collectors.toList());
        long total = emailFileInfoMapper.countEmailDetailById(emailFileQuery);
        return MybatisPage.of(total,result);
    }
}
