package com.bonus.canteen.core.kitchen.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.util.StringUtils;
import com.bonus.canteen.core.kitchen.domain.constants.DeviceSensorResultEnum;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.kitchen.mapper.KitchenDeviceSensorRecordMapper;
import com.bonus.canteen.core.kitchen.domain.KitchenDeviceSensorRecord;
import com.bonus.canteen.core.kitchen.service.IKitchenDeviceSensorRecordService;

/**
 * 厨房后场设备传感器记录Service业务层处理
 * 
 * @author xsheng
 * @date 2025-06-16
 */
@Service
public class KitchenDeviceSensorRecordServiceImpl implements IKitchenDeviceSensorRecordService {
    @Autowired
    private KitchenDeviceSensorRecordMapper kitchenDeviceSensorRecordMapper;

    /**
     * 查询厨房后场设备传感器记录
     * 
     * @param recordId 厨房后场设备传感器记录主键
     * @return 厨房后场设备传感器记录
     */
    @Override
    public KitchenDeviceSensorRecord selectKitchenDeviceSensorRecordByRecordId(Long recordId) {
        return kitchenDeviceSensorRecordMapper.selectKitchenDeviceSensorRecordByRecordId(recordId);
    }

    /**
     * 查询厨房后场设备传感器记录列表
     * 
     * @param kitchenDeviceSensorRecord 厨房后场设备传感器记录
     * @return 厨房后场设备传感器记录
     */
    @Override
    public List<KitchenDeviceSensorRecord> selectKitchenDeviceSensorRecordList(KitchenDeviceSensorRecord kitchenDeviceSensorRecord) {
        List<KitchenDeviceSensorRecord> kitchenDeviceSensorRecordList = kitchenDeviceSensorRecordMapper
                .selectKitchenDeviceSensorRecordList(kitchenDeviceSensorRecord);
        if(CollUtil.isNotEmpty(kitchenDeviceSensorRecordList)) {
            kitchenDeviceSensorRecordList.forEach(record -> {
                if("temperature".equals(record.getMeasureCode()) && StringUtils.isNotBlank(record.getMeasureData())) {
                    if(new BigDecimal(record.getMeasureData()).compareTo(record.getMeasureMaxValue()) > 0) {
                        record.setMeasureData(DeviceSensorResultEnum.TEMPERATURE_HIGH.getDesc());
                    }else if(new BigDecimal(record.getMeasureData()).compareTo(record.getMeasureMaxValue()) < 0) {
                        record.setMeasureData(DeviceSensorResultEnum.TEMPERATURE_LOW.getDesc());
                    } else {
                        record.setMeasureData(DeviceSensorResultEnum.NORMAL.getDesc());
                    }
                }
                if("humidity".equals(record.getMeasureCode()) && StringUtils.isNotBlank(record.getMeasureData())) {
                    if(new BigDecimal(record.getMeasureData()).compareTo(record.getMeasureMaxValue()) > 0) {
                        record.setMeasureData(DeviceSensorResultEnum.HUMIDITY_HIGH.getDesc());
                    }else if(new BigDecimal(record.getMeasureData()).compareTo(record.getMeasureMaxValue()) < 0) {
                        record.setMeasureData(DeviceSensorResultEnum.HUMIDITY_LOW.getDesc());
                    } else {
                        record.setMeasureData(DeviceSensorResultEnum.NORMAL.getDesc());
                    }
                }
                if("smoke".equals(record.getMeasureCode()) && StringUtils.isNotBlank(record.getMeasureData())) {
                    if(new BigDecimal(record.getMeasureData()).compareTo(record.getMeasureMaxValue()) > 0) {
                        record.setMeasureData(DeviceSensorResultEnum.SMOKE_HIGH.getDesc());
                    } else {
                        record.setMeasureData(DeviceSensorResultEnum.NORMAL.getDesc());
                    }
                }
            });
        }

        return kitchenDeviceSensorRecordList;
    }

    /**
     * 新增厨房后场设备传感器记录
     * 
     * @param kitchenDeviceSensorRecord 厨房后场设备传感器记录
     * @return 结果
     */
    @Override
    public int insertKitchenDeviceSensorRecord(KitchenDeviceSensorRecord kitchenDeviceSensorRecord) {
        kitchenDeviceSensorRecord.setCreateTime(DateUtils.getNowDate());
        try {
            return kitchenDeviceSensorRecordMapper.insertKitchenDeviceSensorRecord(kitchenDeviceSensorRecord);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改厨房后场设备传感器记录
     * 
     * @param kitchenDeviceSensorRecord 厨房后场设备传感器记录
     * @return 结果
     */
    @Override
    public int updateKitchenDeviceSensorRecord(KitchenDeviceSensorRecord kitchenDeviceSensorRecord) {
        kitchenDeviceSensorRecord.setUpdateTime(DateUtils.getNowDate());
        try {
            return kitchenDeviceSensorRecordMapper.updateKitchenDeviceSensorRecord(kitchenDeviceSensorRecord);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除厨房后场设备传感器记录
     * 
     * @param recordIds 需要删除的厨房后场设备传感器记录主键
     * @return 结果
     */
    @Override
    public int deleteKitchenDeviceSensorRecordByRecordIds(Long[] recordIds) {
        return kitchenDeviceSensorRecordMapper.deleteKitchenDeviceSensorRecordByRecordIds(recordIds);
    }

    /**
     * 删除厨房后场设备传感器记录信息
     * 
     * @param recordId 厨房后场设备传感器记录主键
     * @return 结果
     */
    @Override
    public int deleteKitchenDeviceSensorRecordByRecordId(Long recordId) {
        return kitchenDeviceSensorRecordMapper.deleteKitchenDeviceSensorRecordByRecordId(recordId);
    }
}
