package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TFacility;
import com.ruoyi.system.domain.TServiceManage;
import com.ruoyi.system.domain.TUserProfile;
import com.ruoyi.system.domain.dto.*;
import com.ruoyi.system.mapper.TFacilityMapper;
import com.ruoyi.system.mapper.TUserProfileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TDeviceListMapper;
import com.ruoyi.system.domain.TDeviceList;
import com.ruoyi.system.service.ITDeviceListService;

/**
 * 设备列Service业务层处理
 * 
 * @author zhengjie
 * @date 2023-08-30
 */
@Service
public class TDeviceListServiceImpl implements ITDeviceListService 
{
    @Autowired
    private TDeviceListMapper tDeviceListMapper;
    @Autowired
    private TUserProfileMapper tUserProfileMapper;
    @Autowired
    private TFacilityMapper tFacilityMapper;

    /**
     * 查询设备列
     * 
     * @param id 设备列主键
     * @return 设备列
     */
    @Override
    public TDeviceList selectTDeviceListById(Integer id)
    {
        return tDeviceListMapper.selectTDeviceListById(id);
    }

    /**
     * 查询设备列列表
     * 
     * @param tDeviceList 设备列
     * @return 设备列
     */
    @Override
    public List<TDeviceList> selectTDeviceListList(TDeviceList tDeviceList)
    {
        return tDeviceListMapper.selectTDeviceListList(tDeviceList);
    }

    @Override
    public List<RateDto> selectTDeviceByDeviceId(String device_id_code) {
        return tDeviceListMapper.selectTDeviceByDeviceId(device_id_code);
    }

    @Override
    public List<RateDto> selectTDeviceByBlood(String device_id_code) {
        return tDeviceListMapper.selectTDeviceByBlood(device_id_code);
    }

    @Override
    public List<RateDto> selectTDeviceByOxygen(String device_id_code) {
        return tDeviceListMapper.selectTDeviceByOxygen(device_id_code);
    }

    @Override
    public List<RateDto> selectTDeviceByTemp(String device_id_code) {
        return tDeviceListMapper.selectTDeviceByTemp(device_id_code);
    }

    @Override
    public List<RateDto> selectTDeviceByStep(String device_id_code) {
        return tDeviceListMapper.selectTDeviceByStep(device_id_code);
    }

    @Override
    public List<MessageDto> selectTDeviceByMessage(String device_id_code) {
        return tDeviceListMapper.selectTDeviceByMessage(device_id_code);
    }

    @Override
    public List<MessageLogDto> selectTDeviceByMessageLog(MessageLogDto messageLogDto) {
        return tDeviceListMapper.selectTDeviceByMessageLog(messageLogDto);
    }

    /**
     * 新增设备列
     * 
     * @param tDeviceList 设备列
     * @return 结果
     */
    @Override
    public int insertTDeviceList(TDeviceList tDeviceList)
    {
        TFacility tFacility = new TFacility();
        if (StringUtils.isNotNull(tDeviceList.getDeviceId())){
            tFacility.setCode(tDeviceList.getDeviceId());
        }else if (StringUtils.isNotNull(tDeviceList.getSimCode())){
            tFacility.setTelno(tDeviceList.getSimCode());
        }else if (StringUtils.isNotNull(tDeviceList.getSerialNumber())){
            tFacility.setIccid(tDeviceList.getSerialNumber());
        }
        tFacility.setCreateTime(DateUtils.getNowDate());
        tFacilityMapper.insertTFacility(tFacility);
        tDeviceList.setCreateTime(DateUtils.getNowDate());
        return tDeviceListMapper.insertTDeviceList(tDeviceList);
    }

    /**
     * 修改设备列
     * 
     * @param tDeviceList 设备列
     * @return 结果
     */
    @Override
    public int updateTDeviceList(TDeviceList tDeviceList)
    {
        return tDeviceListMapper.updateTDeviceList(tDeviceList);
    }

    /**
     * 批量删除设备列
     * 
     * @param ids 需要删除的设备列主键
     * @return 结果
     */
    @Override
    public int deleteTDeviceListByIds(Integer[] ids)
    {
        return tDeviceListMapper.deleteTDeviceListByIds(ids);
    }

    /**
     * 删除设备列信息
     * 
     * @param id 设备列主键
     * @return 结果
     */
    @Override
    public int deleteTDeviceListById(Integer id)
    {
        return tDeviceListMapper.deleteTDeviceListById(id);
    }

    @Override
    public String importTDeviceList(List<TDeviceList> tDeviceLists) {

        if (StringUtils.isNull(tDeviceLists) || tDeviceLists.size() == 0)
        {
            throw new ServiceException("导入设备列表信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TDeviceList tDeviceList : tDeviceLists)
        {
            try
            {

                if (checkManageData(tDeviceList))
                {
                    TUserProfile user = tUserProfileMapper.selectTUserProfileBySfzId(tDeviceList.getSfzid());
                    tDeviceList.setUserid(user.getId());
                    tDeviceList.setSfzid(user.getSfzid());
                    tDeviceList.setTelno(user.getTelOne());
                    this.insertTDeviceList(tDeviceList);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "设备列表信息" + " 导入成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "设备列表信息" + " 信息不完整");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "设备列表信息" + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();

    }

    public boolean checkManageData(TDeviceList tDeviceList){
        return StringUtils.isNotNull(tDeviceList.getName()) && StringUtils.isNotNull(tDeviceList.getSfzid()) &&
                StringUtils.isNotNull(tDeviceList.getTelno()) && StringUtils.isNotNull(tDeviceList.getDeviceId()) &&
                StringUtils.isNotNull(tDeviceList.getGrantType());
    }


    /**
     * 判断报警信息
     *
     * @param alarm_type 报警类型
     * @return 结果
     */
    @Override
    public String checkMessage(Integer alarm_type){
        switch (alarm_type){
            case 16:
                return "SOS报警";
            case 17:
                return "低电报警";
            case 20:
                return "手环拆除报警";
            case 21:
                return "跌倒报警";
            case 22:
                return "心率异常报警";
            case 23:
                return "血压异常报警";
            case 24:
                return "体温异常报警";
            case 25:
                return "血氧异常报警";
            default:
                return "无效";
        }
    }

    @Override
    public int deviceValue(CallPoliceDto callPoliceDto) {
        if (StringUtils.isNotNull(callPoliceDto)){
            if (StringUtils.isNotNull(callPoliceDto.getShouhuan_blood_high_max())){
                tDeviceListMapper.updateTSettingshouhuan_blood_high_max(callPoliceDto.getShouhuan_blood_high_max());
            }
            if (StringUtils.isNotNull(callPoliceDto.getShouhuan_blood_high_min())){
                tDeviceListMapper.updateTSettingshouhuan_blood_high_min(callPoliceDto.getShouhuan_blood_high_min());
            }
            if (StringUtils.isNotNull(callPoliceDto.getShouhuan_blood_low_max())){
                tDeviceListMapper.updateTSettingshouhuan_blood_low_max(callPoliceDto.getShouhuan_blood_low_max());
            }
            if (StringUtils.isNotNull(callPoliceDto.getShouhuan_blood_low_min())){
                tDeviceListMapper.updateTSettingshouhuan_blood_low_min(callPoliceDto.getShouhuan_blood_low_min());
            }
            if (StringUtils.isNotNull(callPoliceDto.getShouhuan_body_max())){
                tDeviceListMapper.updateTSettingshouhuan_body_max(callPoliceDto.getShouhuan_body_max());
            }
            if (StringUtils.isNotNull(callPoliceDto.getShouhuan_body_min())){
                tDeviceListMapper.updateTSettingshouhuan_body_min(callPoliceDto.getShouhuan_body_min());
            }
            if (StringUtils.isNotNull(callPoliceDto.getShouhuan_oxygen_max())){
                tDeviceListMapper.updateTSettingshouhuan_oxygen_max(callPoliceDto.getShouhuan_oxygen_max());
            }
            if (StringUtils.isNotNull(callPoliceDto.getShouhuan_oxygen_min())){
                tDeviceListMapper.updateTSettingshouhuan_oxygen_min(callPoliceDto.getShouhuan_oxygen_min());
            }
            if (StringUtils.isNotNull(callPoliceDto.getShouhuan_heart_max())){
                tDeviceListMapper.updateTSettingshouhuan_heart_max(callPoliceDto.getShouhuan_heart_max());
            }
            if (StringUtils.isNotNull(callPoliceDto.getShouhuan_heart_min())){
                tDeviceListMapper.updateTSettingshouhuan_heart_min(callPoliceDto.getShouhuan_heart_min());
            }
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public List<DeviceTypeDto> selectDeviceListDto() {
        return tDeviceListMapper.selectDeviceListDto();
    }

    @Override
    public Integer selectDeviceListCount() {
        return tDeviceListMapper.selectDeviceListCount();
    }
}
