package com.simuwang.manage.task;

import cn.hutool.core.date.DateUtil;
import com.simuwang.base.common.enums.DeletionType;
import com.simuwang.base.common.enums.DistributeType;
import com.simuwang.base.common.enums.Frequency;
import com.simuwang.base.common.enums.TaskType;
import com.simuwang.base.common.util.DateUtils;
import com.simuwang.base.common.util.StringUtil;
import com.simuwang.base.mapper.*;
import com.simuwang.base.pojo.dos.*;
import com.simuwang.base.pojo.dto.DeletionDownParam;
import com.simuwang.base.pojo.dto.MailboxInfoDTO;
import com.simuwang.daq.service.EmailParseApiServiceImpl;
import com.simuwang.manage.service.DeletionService;
import com.simuwang.shiro.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * FileName: FundDeletionTask
 * Author:   chenjianhua
 * Date:     2024/9/18 22:43
 * Description: ${DESCRIPTION}
 */
//@EnableScheduling
@Component
public class FundDeletionTask {
    private static final Logger log = LoggerFactory.getLogger(FundDeletionTask.class);
    @Autowired
    private NavMapper navMapper;
    @Autowired
    private DeletionService deletionService;
    @Autowired
    private EmailTaskInfoMapper emailTaskInfoMapper;
//    @Scheduled(cron = "0 0 5,12,19 * * ?")
    public void computeDeletion(){
        EmailTaskInfoDO emailTaskInfoDO = startEmailTask(null, 1);
        List<String> fundIdList = navMapper.getAllFundId();
        try{
            for(String fundId : fundIdList){
                DeletionDownParam deletionDownParam = new DeletionDownParam();
                deletionDownParam.setFundId(fundId);
                deletionService.computeDeletion(deletionDownParam);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            endEmailTask(emailTaskInfoDO.getId(),-1);
            return;
        }
        endEmailTask(emailTaskInfoDO.getId(),2);
    }
    private void endEmailTask(Integer id, Integer taskStatus) {
        try{
            EmailTaskInfoDO emailTaskInfoDO = new EmailTaskInfoDO();
            emailTaskInfoDO.setId(id);
            emailTaskInfoDO.setTaskStatus(taskStatus);
            emailTaskInfoDO.setUpdateTime(DateUtils.getNowDate());
            emailTaskInfoDO.setEndTime(DateUtils.getNowDate());
            emailTaskInfoMapper.updateTaskStatusById(emailTaskInfoDO);
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }

    private EmailTaskInfoDO startEmailTask(String email,Integer taskStatus) {
        EmailTaskInfoDO  emailTaskInfoDO = new EmailTaskInfoDO();
        try{
            emailTaskInfoDO.setTaskName(TaskType.DELETION_COMPUTE.getInfo());
            emailTaskInfoDO.setTaskType(TaskType.DELETION_COMPUTE.getType());
            emailTaskInfoDO.setTaskStatus(taskStatus);
            emailTaskInfoDO.setStartTime(DateUtils.getNowDate());
            emailTaskInfoDO.setIsvalid(1);
            emailTaskInfoDO.setEmail(email);
            emailTaskInfoDO.setCreateTime(DateUtils.getNowDate());
            emailTaskInfoDO.setUpdateTime(DateUtils.getNowDate());
            try{
                emailTaskInfoDO.setCreatorId(UserUtils.getLoginUser().getUserId());
                emailTaskInfoDO.setUpdaterId(UserUtils.getLoginUser().getUserId());
            }catch (Exception e){
                emailTaskInfoDO.setCreatorId(1);
                emailTaskInfoDO.setUpdaterId(1);
            }
            emailTaskInfoMapper.insert(emailTaskInfoDO);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return emailTaskInfoDO;
    }
}
