package com.crm.manager.controller;

import com.crm.manager.dto.export.SalesRewardStatisticsExportDto;
import com.crm.manager.service.SelesRewardService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.sales.SalesRewardActivityDto;
import com.crm.rely.backend.core.dto.sales.SalesRewardStatisticsDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.sales.SelesRewardActivityEntity;
import com.crm.rely.backend.core.entity.sales.SelesRewardStatisticsEntity;
import com.crm.rely.backend.util.DateUtil;
import com.crm.rely.backend.util.ExportUtil;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Log4j
@RestController
@RequestMapping("/sales/reward")
public class SelesRewardController {

    @Autowired
    private SelesRewardService service;

    @PostMapping("/statistics")
    public ResultWithPagerDto<List<SalesRewardStatisticsDto>> salesRewardStatistics(@RequestBody SelesRewardStatisticsEntity entity, InfoEntity infoEntity) throws Exception {

        return service.salesRewardStatistics(entity, infoEntity);
    }

    @GetMapping("/statistics/export")
    public void searcherListExport(SelesRewardStatisticsEntity entity, HttpServletResponse response) throws Exception {
        List<SalesRewardStatisticsExportDto> dtos = service.searcherListExport(entity);
        ExportUtil.transferToResponse(genFileName("TRADE_REWARD_STATISTICS"), dtos,
                SalesRewardStatisticsExportDto.class, response);
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<List<SalesRewardActivityDto>> salesRewardStatistics(@RequestBody SelesRewardActivityEntity entity) throws Exception {

        return service.searchPageList(entity);
    }

    @PostMapping("/deduction")
    public BaseResultDto deductionAmount(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        service.deductionAmount(entity);
        return BaseResultDto.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Scheduled(cron = "0 20 0 * *  ?")
    //@Scheduled(cron = "0 58 20 * *  ?")
    public void salesRewardTask() throws Exception {
        try {
            log.info("销售激励活动任务开始 ---------------");
            service.salesRewardTask();
            log.info("销售激励活动任务结束 ---------------");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/salesRewardTask")
    public void manageSalesRewardTask(@RequestParam(name = "newTime") String newTime) throws Exception {
        try {
            log.info("销售激励活动任务开始 ---------------");
            service.salesRewardTask(DateUtil.parseDate(newTime));
            log.info("销售激励活动任务结束 ---------------");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Scheduled(cron = "0 30 0,12 * * ?")
    //@Scheduled(cron = "0 27 21 * *  ?")
    public void salesRewardStatisticsTask() throws Exception {
        try {
            log.info("销售激励统计任务开始 ---------------");
            service.salesRewardStatisticsTask();
            log.info("销售激励统计任务结束 ---------------");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }

    }

    @PostMapping("/salesRewardStatisticsTask")
    public void manageSalesRewardStatisticsTask() throws Exception {
        try {
            log.info("销售激励统计任务开始 ---------------");
            service.salesRewardStatisticsTask();
            log.info("销售激励统计任务结束 ---------------");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }

    }

    private String genFileName(String fileNamePrefix) {
        StringBuffer stringBuffer = new StringBuffer();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        stringBuffer.append(fileNamePrefix);
        stringBuffer.append("_");
        stringBuffer.append(sdf.format(new Date()));
        stringBuffer.append(".xlsx");
        String fileName = stringBuffer.toString();
        return fileName;
    }

}
