package com.crm.manager.controller;

import com.crm.manager.service.SelesPerformanceService;
import com.crm.rely.backend.util.DateUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Log4j
@RestController
@RequestMapping("/sales/performance")
public class SelesPerformanceController {
    @Autowired
    private SelesPerformanceService service;

    @Transactional(rollbackFor = Exception.class)
    @Scheduled(cron = "0 25 0 * * ?")
    public void salesPerformanceTask() throws Exception {
        try {
            log.info("销售业绩统计任务开始 ---------------");
            service.salesPerformanceTask(DateUtil.addDate(new Date(), -1),null);
            log.info("销售业绩统计任务结束 ---------------");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }

    @PostMapping("/salesPerformanceTask")
    public void salesPerformance(@RequestParam(name = "startDate") String startDate) throws Exception {
        try {
            log.info("销售业绩统计任务开始 ---------------");
            service.salesPerformanceTask(DateUtil.addDate(DateUtil.parseDate(startDate), -1),null);
            log.info("销售业绩统计任务结束 ---------------");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Scheduled(cron = "0 45 0  * * 1")
    public void salesPerformanceWeekSendEmailTask() throws Exception {
        try {
            log.info("销售业绩周统计发送邮件任务开始 ---------------");
            Date startDate = DateUtil.addDate(new Date(), -7);
            Date endDate = DateUtil.addDate(new Date(), -1);

            String startMonth = DateUtil.format("yyyy-MM",startDate);
            String endMonth = DateUtil.format("yyyy-MM",endDate);
            if(startMonth.equals(endMonth)){
                service.salesPerformanceSendEmailTask(startDate,endDate);
            }else{
                Date startMonthDate = DateUtil.parseDate(endMonth+"-01");

                Date endMonthDate = DateUtil.addDate(startMonthDate, -1);
                log.info(String.format("startDate:%s,endDate:%s", DateUtil.formatDate(startDate), DateUtil.formatDate(endMonthDate)));
                service.salesPerformanceSendEmailTask(startDate,endMonthDate);

                log.info(String.format("startDate:%s,endDate:%s", DateUtil.formatDate(startMonthDate), DateUtil.formatDate(endDate)));
                service.salesPerformanceSendEmailTask(startMonthDate,endDate);
            }
            log.info("售业绩周统计发送邮件任务结束 ---------------");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Scheduled(cron = "0 0 1 1 * ?")
    public void salesPerformanceMonthSendEmailTask() throws Exception {
        try {
            log.info("销售业绩月统计发送邮件任务开始 ---------------");
            Date startDate = DateUtil.addMonth(new Date(), -1);
            Date endDate = DateUtil.addDate(new Date(), -1);
            service.salesPerformanceSendEmailTask(startDate,endDate);
            log.info("销售业绩月统计发送邮件任务结束 ---------------");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/salesPerformanceSendEmailTask")
    public void salesPerformanceSendEmail(@RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate) throws Exception {
        try {
            service.salesPerformanceSendEmailTask(DateUtil.parseDate(startDate),DateUtil.parseDate(endDate));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }

}
