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.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.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;

/**
 * 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.Custom)){
            emailParseQuery.setEndDate(DateUtils.dateTimeNow());
            emailParseQuery.setStartDate( DateUtil.RangeTime(emailParseQuery.getTimeRange().getFloatDateSymbel(),DateUtil.getTodayDate()));
        }else{
            //自定义时间，把结束时间延后一天，确保时间判断能查到结束日期当天的数据
            emailParseQuery.setEndDate(DateUtil.getAroundDate(DateUtils.parse(emailParseQuery.getEndDate(),DateUtils.YYYY_MM_DD),1));
        }
        List<EmailParseInfoVO> emailParseInfoDOList = emailParseInfoMapper.searchEmailList(emailParseQuery);
        long total = emailParseInfoMapper.countEmailList(emailParseQuery);
        return MybatisPage.of(total,emailParseInfoDOList);
    }

    @Override
    public Map<String, Integer> searchEmailCount(TimeRange timeRange) {
        Map<String, Integer> result = new HashMap<>();
        String parseDate = getParseDate(timeRange, DateUtils.getDate());
        Integer parseTotal = emailParseInfoMapper.searchEmailCount(parseDate,null);
        result.put("parseTotal",parseTotal);
        Integer parseSuccess = emailParseInfoMapper.searchEmailCount(parseDate, ParseStatus.Success.getCode());
        result.put("parseSuccess",parseSuccess);
        Integer parseFail = emailParseInfoMapper.searchEmailCount(parseDate, 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<EmailParseDetailVO> result = emailFileInfoMapper.searchEmailDetailById(emailFileQuery);
        long total = emailFileInfoMapper.countEmailDetailById(emailFileQuery);
        return MybatisPage.of(total,result);
    }
}
