package com.yanzhu.jh.video.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.enums.PublicStateEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MultipartFileUtils;
import com.yanzhu.jh.video.domain.DevAiProjectConfig;
import com.yanzhu.jh.video.domain.DevAiProjectDataVO;
import com.yanzhu.jh.video.mapper.DevAiProjectConfigMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.video.mapper.DevAiProjectDataMapper;
import com.yanzhu.jh.video.domain.DevAiProjectData;
import com.yanzhu.jh.video.service.IDevAiProjectDataService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备数据Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-09-27
 */
@Service
public class DevAiProjectDataServiceImpl implements IDevAiProjectDataService 
{
    private static final Logger logger = LoggerFactory.getLogger("DevAiProjectDataServiceImpl");

    @Autowired
    private DevAiProjectDataMapper devAiProjectDataMapper;

    @Autowired
    private DevAiProjectConfigMapper devAiProjectConfigMapper;

    /**
     * 查询设备数据
     * 
     * @param id 设备数据主键
     * @return 设备数据
     */
    @Override
    public DevAiProjectData selectDevAiProjectDataById(Long id)
    {
        return devAiProjectDataMapper.selectDevAiProjectDataById(id);
    }

    /**
     * 查询设备数据列表
     * 
     * @param devAiProjectData 设备数据
     * @return 设备数据
     */
    @Override
    public List<DevAiProjectData> selectDevAiProjectDataList(DevAiProjectData devAiProjectData)
    {
        return devAiProjectDataMapper.selectDevAiProjectDataList(devAiProjectData);
    }

    /**
     * 新增设备数据
     * 
     * @param devAiProjectData 设备数据
     * @return 结果
     */
    @Override
    public int insertDevAiProjectData(DevAiProjectData devAiProjectData)
    {
        devAiProjectData.setCreateTime(DateUtils.getNowDate());
        return devAiProjectDataMapper.insertDevAiProjectData(devAiProjectData);
    }

    /**
     * 新增设备数据
     *
     * @param devAiProjectDataVO 设备数据
     * @return 结果
     */
    @Override
    public void insertHttpDevAiProjectData(DevAiProjectDataVO devAiProjectDataVO)
    {
        logger.info("AiBox消息解析成功！！开始转换实体！！！");
        DevAiProjectData devAiProjectData = new DevAiProjectData();
        devAiProjectData.setDeviceId(devAiProjectDataVO.getDeviceId());
        /** 设备名称 */
        devAiProjectData.setDeviceName(devAiProjectDataVO.getDeviceName());
        /** 设备网口 0 IP 地址 */
        devAiProjectData.setDeviceAddr0(devAiProjectDataVO.getDeviceAddr0());
        /** 设备网口 1 IP 地址 */
        devAiProjectData.setDeviceAddr1(devAiProjectDataVO.getDeviceAddr1());
        /** 设备的序列号 */
        devAiProjectData.setSerialNumber(devAiProjectDataVO.getSerialNumber());
        /** 相机的序列号 */
        devAiProjectData.setIpcSerialNum(devAiProjectDataVO.getIPCSerialNum());
        /** 相机的 IP 地址 */
        devAiProjectData.setIpcAddr(devAiProjectDataVO.getIPCAddr());
        /** 通道号，用来唯一标识任务 ID */
        devAiProjectData.setChannelId(devAiProjectDataVO.getChannelId());
        /** 通道名称 */
        devAiProjectData.setChannelName(devAiProjectDataVO.getChannelName());
        /** 报警类型 */
        devAiProjectData.setAlarmType(devAiProjectDataVO.getAlarmType());
        /** 告警 ID */
        devAiProjectData.setAlarmId(devAiProjectDataVO.getAlarmId());
        /** 上报间隔 */
        devAiProjectData.setReportRate(devAiProjectDataVO.getReportRate());
        /** 采集时间 */
        devAiProjectData.setTimestamp(new Date(devAiProjectDataVO.getTimestamp()*1000));
        /** 左上角 X 坐标 */
        devAiProjectData.setLeftTopX(devAiProjectDataVO.getLeftTopX());
        /** 左上角 Y 坐标 */
        devAiProjectData.setLeftTopY(devAiProjectDataVO.getLeftTopY());
        /** 右下角 X 坐标 */
        devAiProjectData.setRightBtmX(devAiProjectDataVO.getRightBtmX());
        /** 右下角 Y 坐标 */
        devAiProjectData.setRightBtmY(devAiProjectDataVO.getRightBtmY());
        /** 当前告警区域人数 */
        devAiProjectData.setPersonNum(devAiProjectDataVO.getPersonNum());
        /** 绊线进入人数数量 */
        devAiProjectData.setInNum(devAiProjectDataVO.getInNum());
        /** 绊线离开人数数量 */
        devAiProjectData.setOutNum(devAiProjectDataVO.getOutNum());
        /** 车牌号 */
        devAiProjectData.setPlateNo(devAiProjectDataVO.getPlateNo());
        /** ValueType 为 1 时，代表明火检测；ValueType 为 2 时，代表烟雾检测； */
        devAiProjectData.setValueType(devAiProjectDataVO.getValueType());
        /** 告警短视频的地址 */
        devAiProjectData.setAlarmVideourl(devAiProjectDataVO.getAlarmVideourl());
        /** 告警短视频的名称 */
        devAiProjectData.setAlarmVideoName(devAiProjectDataVO.getAlarmVideoName());
        /** 人脸比对结果 */
        devAiProjectData.setCompareResult(devAiProjectDataVO.getCompareResult());
        logger.info("实体转换成功！！Base64开始转换图片！！！");
        String objImg64 = devAiProjectDataVO.getBigImage().get("Image").toString();
        try {
            MultipartFile file = MultipartFileUtils.base64ToMultipartFile("data:image/png;base64,"+objImg64);
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String profileFile = FileUploadUtils.upload(filePath, file);
            devAiProjectData.setImageUrl(profileFile);
            logger.info("Base64上传图片成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        devAiProjectData.setCreateTime(DateUtils.getNowDate());
        devAiProjectData.setIsDel(PublicStateEnum.OK.getCode());
        //根据设备配置查询设备绑定的项目和单位信息
        DevAiProjectConfig devAiProjectConfig = new DevAiProjectConfig();
        devAiProjectConfig.setSerialNumber(devAiProjectData.getSerialNumber());
        List<DevAiProjectConfig> devAiProjectConfigList = devAiProjectConfigMapper.selectDevAiProjectConfigList(devAiProjectConfig);
        if(devAiProjectConfigList!=null && devAiProjectConfigList.size()>0){
            devAiProjectData.setProjectId(devAiProjectConfigList.get(0).getProjectId());
            devAiProjectData.setDeptId(devAiProjectConfigList.get(0).getDeptId());
        }
        logger.info("新增AI设备数据成功");
        devAiProjectDataMapper.insertDevAiProjectData(devAiProjectData);
    }

    /**
     * 修改设备数据
     * 
     * @param devAiProjectData 设备数据
     * @return 结果
     */
    @Override
    public int updateDevAiProjectData(DevAiProjectData devAiProjectData)
    {
        return devAiProjectDataMapper.updateDevAiProjectData(devAiProjectData);
    }

    /**
     * 批量删除设备数据
     * 
     * @param ids 需要删除的设备数据主键
     * @return 结果
     */
    @Override
    public int deleteDevAiProjectDataByIds(Long[] ids)
    {
        return devAiProjectDataMapper.deleteDevAiProjectDataByIds(ids);
    }

    /**
     * 删除设备数据信息
     * 
     * @param id 设备数据主键
     * @return 结果
     */
    @Override
    public int deleteDevAiProjectDataById(Long id)
    {
        return devAiProjectDataMapper.deleteDevAiProjectDataById(id);
    }

    /**
     * 设备数据统计
     *
     * @param devAiProjectData 查询条件
     * @return 结果
     */
    public List<Map<String, Object>> groupCountByAlarmType(DevAiProjectData devAiProjectData) {
        return devAiProjectDataMapper.groupCountByAlarmType(devAiProjectData);
    }

}
