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

import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.device.model.DeviceAlarmInfoEntity;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.service.DeviceAlarmInfoService;
import com.mortals.xhx.module.device.service.DeviceService;
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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 设备告警日志
 *
 * @author zxfei
 * @date 2022-06-27
 */
@RestController
@RequestMapping("device/alarm/info")
public class DeviceAlarmInfoController extends BaseCRUDJsonBodyMappingController<DeviceAlarmInfoService, DeviceAlarmInfoEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private DeviceService deviceService;

    public DeviceAlarmInfoController() {
        super.setModuleDesc("设备告警日志");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "alarmType", paramService.getParamBySecondOrganize("DeviceAlarmInfo", "alarmType"));
        this.addDict(model, "alarmLevel", paramService.getParamBySecondOrganize("DeviceAlarmInfo", "alarmLevel"));
        this.addDict(model, "alarmStatus", paramService.getParamBySecondOrganize("DeviceAlarmInfo", "alarmStatus"));
        this.addDict(model, "alarmDevice", deviceService.find(new DeviceQuery()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getDeviceName())));
        super.init(model, context);
    }


    @Override
    protected void doListBefore(DeviceAlarmInfoEntity query, Map<String, Object> model, Context context) throws AppException {
        super.doListBefore(query, model, context);
        if (ObjectUtils.isEmpty(query.getOrderColList())) {
            query.setOrderColList(Arrays.asList(new OrderCol("createTime", OrderCol.DESCENDING)));
        }
    }

    @Override
    protected int doListAfter(DeviceAlarmInfoEntity query, Map<String, Object> model, Context context) throws AppException {
        List<DeviceAlarmInfoEntity> alarmInfoEntityList = this.service.find(query, context);
        Map<Integer, Long> collect = alarmInfoEntityList.stream().collect(Collectors.groupingBy(DeviceAlarmInfoEntity::getAlarmLevel, Collectors.counting()));
        model.put("totalCount", alarmInfoEntityList.size());
        model.put("dangerCount", collect.getOrDefault(0, 0L));
        model.put("subCount", collect.getOrDefault(1, 0L));
        model.put("normalCount", collect.getOrDefault(2, 0L));
        return super.doListAfter(query, model, context);
    }


    /**
     * 设备告警信息统计
     */
    @PostMapping(value = "stats")
    public Rest<DeviceAlarmInfoEntity> alarmStats(@RequestBody DeviceAlarmInfoEntity deviceAlarmInfo) {
        String busiDesc = this.getModuleDesc() + "设备告警统计";
        try {
            Rest<DeviceAlarmInfoEntity> rest = this.service.alarmStats(deviceAlarmInfo, getContext());
            recordSysLog(request, busiDesc + " 【成功】");
            return rest;
        } catch (Exception e) {
            log.error("设备告警信息统计", e);
            return Rest.fail(super.convertException(e));
        }
    }

    /**
     * 设备告警信息统计
     */
    @PostMapping(value = "statlist")
    public Rest<List<DeviceAlarmInfoEntity>> alarmStatList(@RequestBody DeviceAlarmInfoEntity deviceAlarmInfo) {
        String busiDesc = this.getModuleDesc() + "设备告警统计";
        try {
            Rest<List<DeviceAlarmInfoEntity>> rest = this.service.alarmStatList(deviceAlarmInfo, getContext());
            recordSysLog(request, busiDesc + " 【成功】");
            return rest;
        } catch (Exception e) {
            log.error("设备告警信息统计", e);
            return Rest.fail(super.convertException(e));
        }
    }

}