package com.mortals.xhx.module.device.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.module.device.dao.DeviceMessageStatDao;
import com.mortals.xhx.module.device.model.DeviceAlarmInfoQuery;
import com.mortals.xhx.module.device.model.DeviceMessageStatEntity;
import com.mortals.xhx.module.device.model.DeviceMessageStatQuery;
import com.mortals.xhx.module.device.service.DeviceAlarmInfoService;
import com.mortals.xhx.module.device.service.DeviceLogService;
import com.mortals.xhx.module.device.service.DeviceMessageStatService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * DeviceMessageStatService
 * 设备消息统计 service实现
 *
 * @author zxfei
 * @date 2024-03-26
 */
@Service("deviceMessageStatService")
@Slf4j
public class DeviceMessageStatServiceImpl extends AbstractCRUDServiceImpl<DeviceMessageStatDao, DeviceMessageStatEntity, Long> implements DeviceMessageStatService {


    @Autowired
    private DeviceAlarmInfoService deviceAlarmInfoService;
    @Autowired
    private DeviceLogService deviceLogService;
    @Autowired
    private ProductService productService;


    @Override
    public void deviceMessageStat(Long siteId, Context context) {
        //查询当天统计，如果有 则更新统计结果，否则新增
        try {
            List<ProductEntity> productEntities = productService.find(new ProductEntity());
            for (ProductEntity productEntity : productEntities) {
                Calendar calendar = Calendar.getInstance();
                DeviceMessageStatEntity deviceStatEntity = this.selectOne(new DeviceMessageStatQuery()
                        .siteId(siteId)
                        .productId(productEntity.getId())
                        .year(calendar.get(Calendar.YEAR))
                        .month(calendar.get(Calendar.MONTH) + 1)
                        .day(calendar.get(Calendar.DAY_OF_MONTH)));
                if (ObjectUtils.isEmpty(deviceStatEntity)) {
                    deviceStatEntity = new DeviceMessageStatEntity();
                    deviceStatEntity.initAttrValue();
                    deviceStatEntity.setSiteId(siteId);
                    deviceStatEntity.setProductId(productEntity.getId());
                    deviceStatEntity.setProductName(productEntity.getProductName());
                    deviceStatEntity.setProductCode(productEntity.getProductCode());
                    deviceStatEntity.setCreateTime(new Date());
                    deviceStatEntity.setYear(calendar.get(Calendar.YEAR));
                    deviceStatEntity.setMonth(calendar.get(Calendar.MONTH) + 1);
                    deviceStatEntity.setDay(calendar.get(Calendar.DAY_OF_MONTH));
                }

                //告警数量 按产品分组
                DeviceAlarmInfoQuery deviceAlarmInfoQuery = new DeviceAlarmInfoQuery();
                deviceAlarmInfoQuery.setCreateTimeStart(DateUtils.getCurrStrDate());
                deviceAlarmInfoQuery.setCreateTimeEnd(DateUtils.getCurrStrDate());
                deviceAlarmInfoQuery.setProductId(productEntity.getId());
                deviceAlarmInfoQuery.setSiteId(siteId);

                int alarmTotalCount = deviceAlarmInfoService.count(deviceAlarmInfoQuery, null);
                deviceStatEntity.setAlarmTotalCount(alarmTotalCount);

                if (deviceStatEntity.newEntity()) {
                    deviceStatEntity.setCreateTime(new Date());
                    this.save(deviceStatEntity);
                } else {
                    deviceStatEntity.setUpdateTime(new Date());
                    this.update(deviceStatEntity);
                }

            }

        } catch (Exception e) {
            log.error("统计异常", e);
            throw new AppException("统计异常!");
        }
    }


    @Override
    public Result<DeviceMessageStatEntity> find(DeviceMessageStatEntity query, PageInfo pageInfo, Context context) throws AppException {
        Result<DeviceMessageStatEntity> deviceStatEntityResult = new Result<>();
        List<DeviceMessageStatEntity> billInfos = this.getDao().getStatList(query, pageInfo);
        deviceStatEntityResult.setList(billInfos);
        deviceStatEntityResult.setPageInfo(pageInfo);
        return deviceStatEntityResult;
    }





}