package com.zd.chemical.service.impl;

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

import cn.hutool.core.date.DateUtil;
import com.zd.chemical.domain.vo.AioAlarmRecordVo;
import com.zd.chemical.domain.vo.HxpAlarmRecordSearch;
import com.zd.chemical.domain.vo.HxpAlarmRecordVo;
import com.zd.chemical.mapper.HxpUserecordMapper;
import com.zd.chemical.util.SmsSydUtil;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.datascope.annotation.DataScope;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.chemical.mapper.HxpAlarmRecordMapper;
import com.zd.chemical.domain.HxpAlarmRecord;
import com.zd.chemical.service.IHxpAlarmRecordService;

/**
 * 化学品报警记录Service业务层处理
 *
 * @author liubo
 * @date 2022-06-16
 */
@Slf4j
@Service
public class HxpAlarmRecordServiceImpl implements IHxpAlarmRecordService {
    @Autowired
    private HxpAlarmRecordMapper hxpAlarmRecordMapper;
    @Autowired
    private HxpUserecordMapper hxpUserecordMapper;
    @Autowired
    private SmsSydUtil smsSydUtil;

    /**
     * 查询化学品报警记录
     *
     * @param id 化学品报警记录主键
     * @return 化学品报警记录
     */
    @Override
    public HxpAlarmRecord selectHxpAlarmRecordById(Long id) {
        return hxpAlarmRecordMapper.selectHxpAlarmRecordById(id);
    }

    /**
     * 查询化学品报警记录列表
     *
     * @param hxpAlarmRecordSearch 化学品报警记录
     * @return 化学品报警记录
     */
    @Override
    @DataScope(deptAlias = "st", permi = PerPrefix.HXP_ALARMRECORD)
    public List<HxpAlarmRecordVo> selectHxpAlarmRecordList(HxpAlarmRecordSearch hxpAlarmRecordSearch) {
        return hxpAlarmRecordMapper.selectHxpAlarmRecordList(hxpAlarmRecordSearch);
    }

    /**
     * 新增化学品报警记录
     *
     * @param hxpAlarmRecord 化学品报警记录
     * @return 结果
     */
    @Override
    public int insertHxpAlarmRecord(HxpAlarmRecord hxpAlarmRecord) {
        return hxpAlarmRecordMapper.insertHxpAlarmRecord(hxpAlarmRecord);
    }

    /**
     * 修改化学品报警记录
     *
     * @param hxpAlarmRecord 化学品报警记录
     * @return 结果
     */
    @Override
    public int updateHxpAlarmRecord(HxpAlarmRecord hxpAlarmRecord) {
        HxpAlarmRecord oldAlarmRecord = hxpAlarmRecordMapper.selectHxpAlarmRecordById(hxpAlarmRecord.getId());
        if(oldAlarmRecord == null){
            throw new ServiceException("参数无效，请刷新重试！");
        }
        if(oldAlarmRecord.getAlarmMode() != 3){
            throw new ServiceException("类型不符，操作失败！");
        }
        hxpAlarmRecord.setHandlingTime(DateUtils.getNowDate());
        hxpAlarmRecord.setHandlingUserId(SecurityUtils.getUserId());
        int count = hxpAlarmRecordMapper.updateHxpAlarmRecord(hxpAlarmRecord);

        if(hxpAlarmRecord.getHandlingStatus() == 2){
            hxpAlarmRecordMapper.updateOtherByParentId(hxpAlarmRecord.getId());
        }

        return count;
    }

    /**
     * 批量删除化学品报警记录
     *
     * @param ids 需要删除的化学品报警记录主键
     * @return 结果
     */
    @Override
    public int deleteHxpAlarmRecordByIds(Long[] ids) {
        return hxpAlarmRecordMapper.deleteHxpAlarmRecordByIds(ids);
    }

    /**
     * 删除化学品报警记录信息
     *
     * @param id 化学品报警记录主键
     * @return 结果
     */
    @Override
    public int deleteHxpAlarmRecordById(Long id) {
        return hxpAlarmRecordMapper.deleteHxpAlarmRecordById(id);
    }

    @Override
    public List<AioAlarmRecordVo> queryAlarmRecordList(AioAlarmRecordVo aioAlarmRecordVo) {
        return hxpAlarmRecordMapper.queryAlarmRecordList(aioAlarmRecordVo);
    }

    @Override
    public AioAlarmRecordVo queryAlarmRecordDetail(Long id) {
        return hxpAlarmRecordMapper.queryAlarmRecordDetail(id);
    }

    /**
     * 手动处理报警记录
     *
     * @param hxpAlarmRecord 手动处理报警记录
     * @return 结果
     */
    @Override
    public int handleProcessing(HxpAlarmRecord hxpAlarmRecord) {
        HxpAlarmRecord oldAlarmRecord = hxpAlarmRecordMapper.selectHxpAlarmRecordById(hxpAlarmRecord.getId());
        if(oldAlarmRecord == null){
            throw new ServiceException("参数无效，请刷新重试！");
        }
        hxpAlarmRecord.setHandlingTime(DateUtils.getNowDate());
        hxpAlarmRecord.setHandlingUserId(SecurityUtils.getUserId());

        int count = hxpAlarmRecordMapper.updateHxpAlarmRecord(hxpAlarmRecord);

        if(hxpAlarmRecord.getHandlingStatus() == 2){
            hxpAlarmRecordMapper.updateOtherByParentId(hxpAlarmRecord.getId());
        }
        return count;
    }

    @Override
    public void sendPhoneBySyd() {

        String subName;

        HxpAlarmRecord alarmRecord = new HxpAlarmRecord();
        alarmRecord.setAlarmMode(3);
        alarmRecord.setHasValid(0);
        alarmRecord.setHandlingStatus(1);
        List<HxpAlarmRecord> list = hxpAlarmRecordMapper.queryListByNOValid(alarmRecord);

        Map<String,Object> subInfo;
        String content;
        for (HxpAlarmRecord hxpAlarmRecord : list) {

            subInfo = hxpUserecordMapper.selectSubInfoById(hxpAlarmRecord.getSubId());
            if(subInfo != null){
                subName = subInfo.get("name")==null?"":String.valueOf(subInfo.get("name"));
                content = "【实验室安全系统】" + subName + "-实验人员未领用即携带化学品离开房间，发生时间："
                        + DateUtil.format(DateUtil.date(), DateUtils.YYYY_MM_DD_HH_MM_SS)
                        + "，请尽快确认。";// 短信回复“CL”确认已处理。
                try {
                    smsSydUtil.sendSydSms(content, 1, null, new String[]{hxpAlarmRecord.getLiableUserIds()});
                } catch (Exception e) {
                    log.error("四医大阶梯通知发送异常：" + e.getMessage());
                }
            }
        }
    }
}
