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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.device.model.DeviceMessageStatEntity;
import com.mortals.xhx.module.device.service.DeviceMessageStatService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.model.ProductQuery;
import com.mortals.xhx.module.product.service.ProductService;
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 java.util.*;
import java.util.stream.Collectors;

/**
 * 设备消息统计
 *
 * @author zxfei
 * @date 2024-03-26
 */
@RestController
@RequestMapping("device/message/stat")
public class DeviceMessageStatController extends BaseCRUDJsonBodyMappingController<DeviceMessageStatService, DeviceMessageStatEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private ProductService productService;

    public DeviceMessageStatController() {
        super.setModuleDesc("设备消息统计");
    }


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


    @Override
    protected int doListAfter(DeviceMessageStatEntity query, Map<String, Object> model, Context context) throws AppException {
        List<DeviceMessageStatEntity> statEntities = (List<DeviceMessageStatEntity>) model.get(KEY_RESULT_DATA);
        //重新分组返回  [{date:2024-8}]
        if (!ObjectUtils.isEmpty(statEntities)) {


            //按月分组
            Map<Integer, Map<String, List<DeviceMessageStatEntity>>> collect = statEntities.stream()
                    .collect(Collectors.groupingBy(DeviceMessageStatEntity::getMonth, Collectors.groupingBy(DeviceMessageStatEntity::getProductName,
                            Collectors.toList())));

            Map<Integer, Integer> monthStat = statEntities.stream().collect(Collectors.groupingBy(DeviceMessageStatEntity::getMonth, Collectors.summingInt(x -> x.getAlarmTotalCount())));
            List<ProductEntity> productEntities = new ArrayList<ProductEntity>();
            if (!ObjectUtils.isEmpty(query.getProductCodeList())) {
                ProductQuery productQuery = new ProductQuery();
                productQuery.setProductCodeList(query.getProductCodeList());
                productEntities = productService.find(productQuery);
            } else {
                productEntities = productService.find(new ProductEntity());
            }

            String month = query.getGroupList().stream().filter(f -> "month".equals(f)).findFirst().orElseGet(() -> null);
            if (!ObjectUtils.isEmpty(month)) {
                for (int i = 1; i <= 12; i++) {
                    if (!collect.containsKey(i)) {
                        Map<String, List<DeviceMessageStatEntity>> map = new HashMap<>();
                        for (ProductEntity productEntity : productEntities) {
                            DeviceMessageStatEntity deviceMessageStatEntity = new DeviceMessageStatEntity();
                            deviceMessageStatEntity.setAlarmTotalCount(0);
                            map.put(productEntity.getProductName(), Arrays.asList(deviceMessageStatEntity));
                        }
                        collect.put(i, map);
                    }
                    if (!monthStat.containsKey(i)) {
                        monthStat.put(i, 0);
                    }
                }
            }
            model.put("stat", collect);
            model.put("monthStat", monthStat);

            model.put(KEY_RESULT_DATA, null);
        }
        return super.doListAfter(query, model, context);
    }
}