package com.simuwang.manage.service.impl;

import com.simuwang.base.common.enums.ParseStatus;
import com.simuwang.base.mapper.EmailParseInfoMapper;
import com.simuwang.base.mapper.EmailTypeRuleMapper;
import com.simuwang.base.pojo.dos.EmailParseInfoDO;
import com.simuwang.base.pojo.dos.EmailTypeRuleDO;
import com.simuwang.base.pojo.vo.EmailParseInfoVO;
import com.simuwang.base.pojo.vo.EmailTypeRuleVO;
import com.simuwang.manage.service.ParseEmailService;
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;
    @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 List<EmailParseInfoVO> searchEmailList(String emailTitle, Integer emailType, Integer parseStatus) {
        List<EmailParseInfoVO> emailParseInfoDOList = emailParseInfoMapper.searchEmailList(emailTitle,emailType,parseStatus);
        return emailParseInfoDOList;
    }

    @Override
    public EmailParseInfoVO searchEmailById(Integer id) {
        return emailParseInfoMapper.searchEmailById(id);
    }

    @Override
    public Map<String, Integer> searchEmailCount(String parseDate) {
        Map<String, Integer> result = new HashMap<>();
        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;
    }
}
