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.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.model.DeviceStatQuery;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.sitestat.model.SitestatQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.device.model.DeviceStatEntity;
import com.mortals.xhx.module.device.service.DeviceStatService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

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

import com.alibaba.fastjson.JSONObject;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.mortals.framework.ap.SysConstains.*;

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

    @Autowired
    private DeviceService deviceService;



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

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


    /**
     * 同步当前站点
     */
    @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不能为空！");
            //刷新成功，返回列
            //deviceService.deviceStat(deviceQuery.getSiteId(),getContext());
            DeviceStatQuery deviceStatQuery = new DeviceStatQuery();
            deviceStatQuery.setYear(DateUtil.year(new Date()));
            deviceStatQuery.setMonth(DateUtil.month(new Date()) + 1);
            deviceStatQuery.setDay(DateUtil.dayOfMonth(new Date()));
            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));
        }
    }

}