package com.mortals.xhx.module.device.web;

import cn.hutool.core.date.DateUtil;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.common.code.DaySelectEnum;
import com.mortals.xhx.common.keys.RedisCacheKeys;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.model.DeviceStatEntity;
import com.mortals.xhx.module.device.model.DeviceStatQuery;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.device.service.DeviceStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 设备统计
 *
 * @author zxfei
 * @date 2022-07-05
 */
@RestController
@RequestMapping("device/stat")
public class DeviceStatController extends BaseCRUDJsonBodyMappingController<DeviceStatService, DeviceStatEntity, Long> {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ICacheService cacheService;


    public DeviceStatController() {
        super.setModuleDesc("设备统计");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        super.init(model, context);
    }


    @Override
    protected void doListBefore(DeviceStatEntity query, Map<String, Object> model, Context context) throws AppException {
        super.doListBefore(query, model, context);

        if (!ObjectUtils.isEmpty(query.getSelected())) {
            //转换日期
            switch (DaySelectEnum.getByValue(query.getSelected())) {
                case 今天:
                    query.setCreateTimeStart(DateUtil.today());
                    break;
                case 近七天:
                    query.setCreateTimeStart(DateUtil.offsetDay(new Date(),-7).toDateStr());
                    break;
                case 近三十天:
                    query.setCreateTimeStart(DateUtil.offsetDay(new Date(),-30).toDateStr());
                    break;
                case 近三月:
                    query.setCreateTimeStart(DateUtil.offsetMonth(new Date(),-3).toDateStr());
                    break;
                case 今年:
                    query.setCreateTimeStart(DateUtil.beginOfYear(new Date()).toDateStr());
                    break;
                default:
                    break;
            }
        }

    }

    /**
     * 同步当前站点
     */
    @PostMapping(value = "syncDeviceStat")
    public Rest<Object> syncDeviceStat(@RequestBody DeviceQuery deviceQuery) throws Exception {
        try {
            if (ObjectUtils.isEmpty(deviceQuery) || ObjectUtils.isEmpty(deviceQuery.getSiteId()))
                throw new AppException("请求站点Id不能为空！");

            boolean bool = cacheService.setnx(RedisCacheKeys.getDeviceStatLockKey() + deviceQuery.getSiteId(), deviceQuery.getSiteId(), 120L);
            if (!bool) {
                throw new AppException("同步统计设备中，请稍后再统计！");
            }
            //刷新成功，返回列
            this.service.deviceStat(deviceQuery.getSiteId(),getContext());
            DeviceStatQuery deviceStatQuery = new DeviceStatQuery();
            Calendar calendar = Calendar.getInstance();
            deviceStatQuery.setYear(calendar.get(Calendar.YEAR));
            deviceStatQuery.setMonth(calendar.get(Calendar.MONTH) + 1);
            deviceStatQuery.setDay(calendar.get(Calendar.DAY_OF_MONTH));
            deviceStatQuery.setSiteId(deviceQuery.getSiteId());
            Rest<Object> resp = this.list(deviceStatQuery);
            return resp;
        } catch (Exception e) {
            log.error("刷新异常", e);
            return Rest.fail(KEY_RESULT_CODE, super.convertException(e));
        }
    }


    public static void main(String[] args) {
        System.out.println(DateUtils.getThisYear());
        System.out.println(DateUtils.getCurrMonth());
        System.out.println(DateUtils.getCurrMonth());
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
    }

}