package com.zd.laboratory.service.impl;

import com.zd.common.core.constant.CacheConstants;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.laboratory.domain.LabAbnormal;
import com.zd.laboratory.domain.LabHardware;
import com.zd.laboratory.domain.LabSensor;
import com.zd.laboratory.domain.vo.LabAbnormalVO;
import com.zd.laboratory.domain.vo.LabHardwareVO;
import com.zd.laboratory.domain.vo.LabSensorVO;
import com.zd.laboratory.mapper.LabAbnormalMapper;
import com.zd.laboratory.mapper.LabHardwareMapper;
import com.zd.laboratory.mapper.LabSensorMapper;
import com.zd.laboratory.mapper.LabSubjectHardwarePositionMapper;
import com.zd.laboratory.service.ILabAbnormalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 异常设备Service业务层处理
 *
 * @author zd
 * @date 2021-09-07
 */
@Service
public class LabAbnormalServiceImpl implements ILabAbnormalService {

    private Logger logger = LoggerFactory.getLogger(LabAbnormalServiceImpl.class);
    @Autowired
    private LabAbnormalMapper labAbnormalMapper;

    @Autowired
    private LabHardwareMapper labHardwareMapper;

    @Autowired
    private LabSensorMapper labSensorMapper;

    @Autowired
    private LabSubjectHardwarePositionMapper labSubjectHardwarePostionMapper;

    /**
     * 查询异常设备
     *
     * @param id 异常设备主键
     * @return 异常设备
     */
    @Override
    public LabAbnormal selectLabAbnormalById(Long id) {
        return labAbnormalMapper.selectLabAbnormalById(id);
    }

    /**
     * 查询异常设备列表
     *
     * @param labAbnormal 异常设备
     * @return 异常设备
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<LabAbnormalVO> selectLabAbnormalList(LabAbnormalVO labAbnormal) {
        LabAbnormalVO abnormal = new LabAbnormalVO();
        if (labAbnormal.getEquipmentType() == 1) {
            abnormal.setHardwareId(labAbnormal.getId());
        } else if (labAbnormal.getEquipmentType() == 2) {
            abnormal.setSensorId(labAbnormal.getId());
        }
        return labAbnormalMapper.selectLabAbnormalList(abnormal);
    }

    /**
     * 查询设备列表
     *
     * @param labAbnormal 设备
     * @return 设备
     */
    @Override
    @DataScope(deptAlias = "xx", permi = PerPrefix.LABORATORY_ABNORMAL)
    public List<LabAbnormalVO> getEquipmentList(LabAbnormalVO labAbnormal) {
        List<LabAbnormalVO> labAbnormalList = labAbnormalMapper.getEquipmentList(labAbnormal);
        for(LabAbnormalVO labAbnormalVO:labAbnormalList){
            LabAbnormalVO labAbnormalVO1 = new LabAbnormalVO();
            if(labAbnormalVO.getEquipmentType()==1){
                labAbnormalVO1.setHardwareId(labAbnormalVO.getId());
            }else{
                labAbnormalVO1.setSensorId(labAbnormalVO.getId());
            }
            LabAbnormalVO newLabAbnormalVO = labAbnormalMapper.getEquipmentVo(labAbnormalVO1);
            labAbnormalVO.setAbnormalReason(newLabAbnormalVO.getAbnormalReason());
            labAbnormalVO.setLastAbnormalTime(newLabAbnormalVO.getCreateTime());
        }
        return labAbnormalList;
    }

    /**
     * 新增异常设备
     *
     * @param labAbnormal 异常设备
     * @return 结果
     */
    @Override
    public int insertLabAbnormal(LabAbnormal labAbnormal) {
        labAbnormal.setCreateTime(DateUtils.getNowDate());
        return labAbnormalMapper.insertLabAbnormal(labAbnormal);
    }

    /**
     * 修改异常设备
     *
     * @param labAbnormal 异常设备
     * @return 结果
     */
    @Override
    public int updateLabAbnormal(LabAbnormal labAbnormal) {
        labAbnormal.setUpdateTime(DateUtils.getNowDate());
        return labAbnormalMapper.updateLabAbnormal(labAbnormal);
    }

    /**
     * 批量删除异常设备
     *
     * @param ids 需要删除的异常设备主键
     * @return 结果
     */
    @Override
    public int deleteLabAbnormalByIds(Long[] ids) {
        return labAbnormalMapper.deleteLabAbnormalByIds(ids);
    }

    /**
     * 删除异常设备信息
     *
     * @param id 异常设备主键
     * @return 结果
     */
    @Override
    public int deleteLabAbnormalById(Long id) {
        return labAbnormalMapper.deleteLabAbnormalById(id);
    }


    /**
     * 删除设备和异常信息
     *
     * @param labAbnormal 设备对象
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delEquipment(LabAbnormalVO labAbnormal) {
        LabAbnormal abnormal = new LabAbnormal();
        int flag = 0;
        if (labAbnormal.getEquipmentType() == 1) {
            labSubjectHardwarePostionMapper.deleteLabSubjectHardwarePositionByHardId(labAbnormal.getId());
            labHardwareMapper.deleteLabHardwareById(labAbnormal.getId());
            abnormal.setHardwareId(labAbnormal.getId());
            flag = labAbnormalMapper.delEquipment(abnormal);
        } else if (labAbnormal.getEquipmentType() == 2) {
            labSensorMapper.deleteLabSensorById(labAbnormal.getId());
            abnormal.setSensorId(labAbnormal.getId());
            flag = labAbnormalMapper.delEquipment(abnormal);
        }
        return flag;
    }


    /**
     * 获取设备统计异常信息
     *
     * @param labAbnormal 设备对象
     * @return 异常设备
     */
    @Override
    public LabAbnormalVO getAbnormalCount(LabAbnormalVO labAbnormal) {
        LabAbnormal abnormal = new LabAbnormal();
        if (labAbnormal.getEquipmentType() == 1) {
            abnormal.setHardwareId(labAbnormal.getId());
        } else if (labAbnormal.getEquipmentType() == 2) {
            abnormal.setSensorId(labAbnormal.getId());
        }
        return labAbnormalMapper.getAbnormalCount(abnormal);
    }

    /**
     * 继电器 异常设备
     */
    @Override
    public void insertRelayAbnormal(String expiredKey) {

        logger.warn("继电器上报超时：" + expiredKey + " --  上报时长超过10分钟");

        LabHardware labHardware = new LabHardware();
        labHardware.setRelayCode(expiredKey);
        List<LabHardwareVO> list = labHardwareMapper.selectLabHardwareList(labHardware);

        List<LabAbnormal> abnormals = new ArrayList<>();
        LabAbnormal abnormal;
        for (LabHardwareVO labHardwareVO : list) {
            abnormal = new LabAbnormal();
            abnormal.setName(labHardwareVO.getName());
            abnormal.setType(1);
            abnormal.setAbnormalReason("继电器中断10分钟");
            abnormal.setHardwareId(labHardwareVO.getId());
            abnormal.setDeptId(labHardwareVO.getDeptId());
            abnormal.setDeptName(labHardwareVO.getDeptName());
            abnormal.setCreateBy(labHardwareVO.getCreateBy());
            abnormal.setCreateTime(DateUtils.getNowDate());
            abnormals.add(abnormal);
        }

        if(abnormals.size() > 0){
            labAbnormalMapper.insertBatch(abnormals);
        }
    }

    /**
     * 传感器 异常设备
     */
    @Override
    public void insertSensorAbnormal(String expiredKey) {

        logger.warn("传感器/采集器：" + expiredKey + " --  上报时长超过1分40秒");

        LabSensor labSensor = new LabSensor();
        //labSensor.setGatewayId(expiredKey.replace(CacheConstants.SENSOR_LIFE, ""));
        labSensor.setGatewayId(expiredKey);
        List<LabSensorVO> list = labSensorMapper.selectLabSensorList(labSensor);

        List<LabAbnormal> abnormals = new ArrayList<>();
        LabAbnormal abnormal;
        for (LabSensorVO labSensorVO : list) {
            abnormal = new LabAbnormal();
            abnormal.setName(labSensorVO.getName());
            abnormal.setType(2);
            abnormal.setAbnormalReason("云桥/采集器上报超过1分40秒");
            abnormal.setSensorId(labSensorVO.getId());
            abnormal.setDeptId(labSensorVO.getDeptId());
            abnormal.setDeptName(labSensorVO.getDeptName());
            abnormal.setCreateBy(labSensorVO.getCreateBy());
            abnormal.setCreateTime(DateUtils.getNowDate());

            abnormals.add(abnormal);
        }

        if(abnormals.size() > 0){
            labAbnormalMapper.insertBatch(abnormals);
        }
    }
}
