package com.zehong.system.service.impl;

import com.github.pagehelper.PageInfo;
import com.zehong.common.core.domain.entity.SysDictData;
import com.zehong.common.utils.DateUtils;
import com.zehong.common.utils.PageInfoUtil;
import com.zehong.common.utils.StringUtils;
import com.zehong.system.domain.TDeviceAlarm;
import com.zehong.system.domain.TDeviceInfo;
import com.zehong.system.domain.TDeviceReportData;
import com.zehong.system.domain.TPipe;
import com.zehong.system.domain.form.DeviceAlarmForm;
import com.zehong.system.domain.vo.DeviceAlarmVo;
import com.zehong.system.mapper.TDeviceAlarmMapper;
import com.zehong.system.mapper.TDeviceInfoMapper;
import com.zehong.system.mapper.TDeviceReportDataMapper;
import com.zehong.system.mapper.TPipeMapper;
import com.zehong.system.service.ISysDictTypeService;
import com.zehong.system.service.ITDeviceAlarmService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 报警信息Service业务层处理
 * 
 * @author zehong
 * @date 2021-07-21
 */
@Service
public class TDeviceAlarmServiceImpl implements ITDeviceAlarmService 
{
    @Autowired
    private TDeviceAlarmMapper tDeviceAlarmMapper;
    @Autowired
    private TDeviceInfoMapper tDeviceInfoMapper;
    @Autowired
    private TPipeMapper tPipeMapper;
    @Autowired
    private TDeviceReportDataMapper tDeviceReportDataMapper;

    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    /**
     * 查询报警信息
     * 
     * @param alarmId 报警信息ID
     * @return 报警信息
     */
    @Override
    public DeviceAlarmVo selectTDeviceAlarmById(int alarmId) throws Exception
    {
        DeviceAlarmVo deviceAlarmVo = new DeviceAlarmVo();
        TDeviceAlarm tDeviceAlarm = tDeviceAlarmMapper.selectTDeviceAlarmById(alarmId);
        BeanUtils.copyProperties(tDeviceAlarm, deviceAlarmVo);

        if (StringUtils.isNotEmpty(tDeviceAlarm.getAlarmType())) {
            List<SysDictData> sysDictDataList = iSysDictTypeService.selectDictDataByType("t_alarm_type");
            for (SysDictData sysDictData : sysDictDataList) {
                if (tDeviceAlarm.getAlarmType().equals(sysDictData.getDictValue())) {
                    deviceAlarmVo.setAlarmType(sysDictData.getDictLabel());
                }
            }
        }

        if (StringUtils.isNotEmpty(tDeviceAlarm.getDeviceType())) {
            List<SysDictData> sysDictDataList = iSysDictTypeService.selectDictDataByType("t_trouble_device_type");
            for (SysDictData sysDictData : sysDictDataList) {
                if (tDeviceAlarm.getDeviceType().equals(sysDictData.getDictValue())) {
                    deviceAlarmVo.setDeviceType(sysDictData.getDictLabel());
                }
            }
        }

        if("0".equals(tDeviceAlarm.getDeviceType())){
            TPipe pipe = tPipeMapper.selectTPipeById(tDeviceAlarm.getDeviceId());
            if(pipe != null) {
                deviceAlarmVo.setDeviceCode(pipe.getPipeCode());
                deviceAlarmVo.setDeviceName(pipe.getPipeName());

                List<TPipe> pipeList = new ArrayList<>();
                pipeList.add(pipe);
                deviceAlarmVo.setPipeList(pipeList);
            }
        } else {
            TDeviceInfo deviceInfo = tDeviceInfoMapper.selectTDeviceInfoById(tDeviceAlarm.getDeviceId());
            if(deviceInfo != null){
                deviceAlarmVo.setDeviceCode(deviceInfo.getDeviceCode());
                deviceAlarmVo.setDeviceName(deviceInfo.getDeviceName());

                List<TDeviceInfo> deviceList = new ArrayList<>();
                deviceList.add(deviceInfo);
                deviceAlarmVo.setDeviceList(deviceList);
            }

            // 查询当前设备开始报警时间前后一小时的数据
            TDeviceReportData tDeviceReportData = new TDeviceReportData();
            tDeviceReportData.setDeviceNum(deviceInfo.getIotNo());
            tDeviceReportData.setReportTime(tDeviceAlarm.getStartTime());

            List<TDeviceReportData> deviceReportDataList = tDeviceReportDataMapper.getAlarmDeviceReportDataList(tDeviceReportData);
            if(deviceReportDataList.size() != 0) {
                deviceAlarmVo.setDeviceReportDataList(deviceReportDataList);
            }
        }

        return deviceAlarmVo;
    }

    /**
     * 查询报警信息列表
     * 
     * @param deviceAlarmForm 报警信息
     * @return 报警信息
     */
    @Override
    public PageInfo<DeviceAlarmVo> selectTDeviceAlarmPage(DeviceAlarmForm deviceAlarmForm) throws Exception
    {
        PageInfo<TDeviceAlarm> deviceAlarmList = new PageInfo<>();

        if(deviceAlarmForm.getDeviceCode() != null){
            List<TDeviceAlarm> pipeList = tDeviceAlarmMapper.selectTDeviceAlarmByPipeCode(deviceAlarmForm);
            List<TDeviceAlarm> deviceList = tDeviceAlarmMapper.selectTDeviceAlarmByDeviceCode(deviceAlarmForm);
            setDeviceInfo(deviceAlarmList,pipeList);
            setDeviceInfo(deviceAlarmList,deviceList);
        } else {
            deviceAlarmList = new PageInfo(tDeviceAlarmMapper.selectTDeviceAlarmList(deviceAlarmForm));
        }
        PageInfo<DeviceAlarmVo> pageVo = PageInfoUtil.pageInfo2PageInfoDTO(deviceAlarmList,DeviceAlarmVo.class);
        if(pageVo.getList().size() != 0) {
            for (DeviceAlarmVo deviceAlarmVo : pageVo.getList()) {
                if ("0".equals(deviceAlarmVo.getDeviceType())) {
                    TPipe pipe = tPipeMapper.selectTPipeById(deviceAlarmVo.getDeviceId());
                    if (pipe != null) {
                        deviceAlarmVo.setDeviceCode(pipe.getPipeCode());
                        deviceAlarmVo.setDeviceName(pipe.getPipeName());

                        List<TPipe> pipeList = new ArrayList<>();
                        pipeList.add(pipe);
                        deviceAlarmVo.setPipeList(pipeList);
                    }
                } else {
                    TDeviceInfo deviceInfo = tDeviceInfoMapper.selectTDeviceInfoById(deviceAlarmVo.getDeviceId());
                    if (deviceInfo != null) {
                        deviceAlarmVo.setDeviceCode(deviceInfo.getDeviceCode());
                        deviceAlarmVo.setDeviceName(deviceInfo.getDeviceName());

                        List<TDeviceInfo> deviceList = new ArrayList<>();
                        deviceList.add(deviceInfo);
                        deviceAlarmVo.setDeviceList(deviceList);
                    }
                }

                if (StringUtils.isNotEmpty(deviceAlarmVo.getAlarmType())) {
                    List<SysDictData> sysDictDataList = iSysDictTypeService.selectDictDataByType("t_alarm_type");
                    for (SysDictData sysDictData : sysDictDataList) {
                        if (deviceAlarmVo.getAlarmType().equals(sysDictData.getDictValue())) {
                            deviceAlarmVo.setAlarmType(sysDictData.getDictLabel());
                        }
                    }
                }

                if (StringUtils.isNotEmpty(deviceAlarmVo.getDeviceType())) {
                    List<SysDictData> sysDictDataList = iSysDictTypeService.selectDictDataByType("t_trouble_device_type");
                    for (SysDictData sysDictData : sysDictDataList) {
                        if (deviceAlarmVo.getDeviceType().equals(sysDictData.getDictValue())) {
                            deviceAlarmVo.setDeviceType(sysDictData.getDictLabel());
                        }
                    }
                }
            }
        }

        return pageVo;
    }

    private void setDeviceInfo(PageInfo<TDeviceAlarm> deviceAlarmList,List<TDeviceAlarm> list){
        if(list.size() != 0){
            if(null != deviceAlarmList.getList() && !deviceAlarmList.getList().isEmpty()){
                deviceAlarmList.getList().addAll(list);
                deviceAlarmList.setTotal(deviceAlarmList.getTotal() + list.size());
            }else{
                deviceAlarmList.setList(list);
                deviceAlarmList.setTotal(list.size());
            }
        }
    }

    /**
     * 新增报警信息
     * 
     * @param tDeviceAlarm 报警信息
     * @return 结果
     */
    @Override
    public int insertTDeviceAlarm(TDeviceAlarm tDeviceAlarm)
    {
        tDeviceAlarm.setCreateTime(DateUtils.getNowDate());
        return tDeviceAlarmMapper.insertTDeviceAlarm(tDeviceAlarm);
    }

    /**
     * 修改报警信息
     * 
     * @param tDeviceAlarm 报警信息
     * @return 结果
     */
    @Override
    public int updateTDeviceAlarm(TDeviceAlarm tDeviceAlarm)
    {
        tDeviceAlarm.setUpdateTime(DateUtils.getNowDate());
        return tDeviceAlarmMapper.updateTDeviceAlarm(tDeviceAlarm);
    }

    /**
     * 批量删除报警信息
     * 
     * @param alarmIds 需要删除的报警信息ID
     * @return 结果
     */
    @Override
    public int deleteTDeviceAlarmByIds(int[] alarmIds)
    {
        return tDeviceAlarmMapper.deleteTDeviceAlarmByIds(alarmIds);
    }

    /**
     * 删除报警信息信息
     * 
     * @param alarmId 报警信息ID
     * @return 结果
     */
    @Override
    public int deleteTDeviceAlarmById(int alarmId)
    {
        return tDeviceAlarmMapper.deleteTDeviceAlarmById(alarmId);
    }

    /**
     * 查询报警设备
     * @return TDeviceAlarm
     */
    @Override
    public List<TDeviceAlarm> selectAlarmDevice (){
        return tDeviceAlarmMapper.selectAlarmDevice();
    }
}
