package com.sptg.ad.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.ad.api.entity.DeviceAdvertStatistics;
import com.sptg.ad.api.entity.EarlyWarning;
import com.sptg.ad.api.feigns.EarlyWarningFeign;
import com.sptg.ad.biz.service.DeviceAdvertStatisticsService;
import com.sptg.ad.biz.service.EarlyWarningService;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *            
 *                
 * description:
 */
@RestController
@RequiredArgsConstructor
public class EarlyWarningController implements EarlyWarningFeign {

    private final EarlyWarningService earlyWarningService;
    private final DeviceAdvertStatisticsService deviceAdvertStatisticsService;

    @Override
    @PostMapping(value = "/early_warning/pagingQuery")
    public Page<EarlyWarning> pagingQuery(@RequestBody PageBean<EarlyWarning, EarlyWarning> pageBean) {
        return this.earlyWarningService.pagingQuery(pageBean);
    }

    @Override
    @PostMapping(value = "/early_warning/add")
    public ResultBean<Boolean> addEntity(@RequestBody EarlyWarning earlyWarning) {
        List<DeviceAdvertStatistics> statisticsList = new ArrayList<>();
        Integer taskType = earlyWarning.getTaskType();
        //  获取设备信息列表
        earlyWarning.getDeviceIdList().forEach(deviceId -> {
            DeviceAdvertStatistics statistics = this.deviceAdvertStatisticsService.selectCurrentMonthAdvertStatistics(deviceId, taskType);
            statisticsList.add(statistics);
        });
        Assert.notEmpty(statisticsList,"设备不存在");

        List<EarlyWarning> warningList = new ArrayList<>();
        Date now = new Date();
        //  月预警
        if (null != earlyWarning.getMonthExpect()){
            //  获取下个月1号的日期
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(now);
            calendar.add(Calendar.MONTH, 1);
            calendar.set(Calendar.DATE, 1);
            Date endTime = calendar.getTime();
            //  创建对象
            statisticsList.forEach(statistics -> {
                EarlyWarning warning = new EarlyWarning(statistics, 1, taskType, earlyWarning.getMonthExpect(), now, endTime);
                warningList.add(warning);
                statistics.setEarlyWarningCount(statistics.getEarlyWarningCount() + 1);
            });
        }
        //  周预警
        if (null != earlyWarning.getWeekExpect()){
            //  获取下周一的日期
            Calendar calendar = Calendar.getInstance();
            int i = calendar.get(Calendar.DAY_OF_WEEK);
            calendar.add(Calendar.DATE, i == 1 ? i : 9 - i);
            Date endTime = calendar.getTime();
            //  获取当前的第几周
            int weekNum = calendar.get(Calendar.WEEK_OF_MONTH);
            //  创建对象
            statisticsList.forEach(statistics -> {
                EarlyWarning warning = new EarlyWarning(statistics, 2, taskType, earlyWarning.getWeekExpect(), now, endTime);
                warning.setWeekNum(weekNum);
                warningList.add(warning);
                statistics.setEarlyWarningCount(statistics.getEarlyWarningCount() + 1);
            });
        }
        //  保存预警记录
        if (this.earlyWarningService.insertBatch(warningList)){
            //  更新设备信息列表
            this.deviceAdvertStatisticsService.updateBatchById(statisticsList);
            return ResultBean.ok(true);
        }
        return ResultBean.ok(false);
    }
}
