package com.aucma.dms.service.impl;

import com.aucma.common.constant.DmsConstants;
import com.aucma.common.constant.SystemConstants;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.dms.domain.DmsRecordAlarmInfo;
import com.aucma.dms.mapper.DmsRecordAlarmInfoMapper;
import com.aucma.dms.service.IDmsRecordAlarmInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.ServerException;
import java.util.HashMap;
import java.util.List;

/**
 * 设备报警记录Service业务层处理
 *
 * @author Yinq
 * @date 2024-08-13
 */
@Service
public class DmsRecordAlarmInfoServiceImpl implements IDmsRecordAlarmInfoService {
    @Autowired
    private DmsRecordAlarmInfoMapper dmsRecordAlarmInfoMapper;

/*    @Autowired
    private RemoteSysCommonService remoteSysCommonService;*/

    /**
     * 查询设备报警记录
     *
     * @param alarmId 设备报警记录主键
     * @return 设备报警记录
     */
    @Override
    public DmsRecordAlarmInfo selectDmsRecordAlarmInfoByAlarmId(Long alarmId) {
        return dmsRecordAlarmInfoMapper.selectDmsRecordAlarmInfoByAlarmId(alarmId);
    }

    /**
     * 查询设备报警记录列表
     *
     * @param dmsRecordAlarmInfo 设备报警记录
     * @return 设备报警记录
     */
    @Override
    public List<DmsRecordAlarmInfo> selectDmsRecordAlarmInfoList(DmsRecordAlarmInfo dmsRecordAlarmInfo) {
        return dmsRecordAlarmInfoMapper.selectDmsRecordAlarmInfoList(dmsRecordAlarmInfo);
    }

    /**
     * 新增设备报警记录
     *
     * @param dmsRecordAlarmInfo 设备报警记录
     * @return 结果
     */
    @Override
    public int insertDmsRecordAlarmInfo(DmsRecordAlarmInfo dmsRecordAlarmInfo) {
        dmsRecordAlarmInfo.setCreateTime(DateUtils.getNowDate());
        return dmsRecordAlarmInfoMapper.insertDmsRecordAlarmInfo(dmsRecordAlarmInfo);
    }

    /**
     * 修改设备报警记录
     *
     * @param dmsRecordAlarmInfo 设备报警记录
     * @return 结果
     */
    @Override
    public int updateDmsRecordAlarmInfo(DmsRecordAlarmInfo dmsRecordAlarmInfo) {
        dmsRecordAlarmInfo.setUpdateTime(DateUtils.getNowDate());
        return dmsRecordAlarmInfoMapper.updateDmsRecordAlarmInfo(dmsRecordAlarmInfo);
    }

    /**
     * 批量删除设备报警记录
     *
     * @param alarmIds 需要删除的设备报警记录主键
     * @return 结果
     */
    @Override
    public int deleteDmsRecordAlarmInfoByAlarmIds(Long[] alarmIds) {
        return dmsRecordAlarmInfoMapper.deleteDmsRecordAlarmInfoByAlarmIds(alarmIds);
    }

    /**
     * 删除设备报警记录信息
     *
     * @param alarmId 设备报警记录主键
     * @return 结果
     */
    @Override
    public int deleteDmsRecordAlarmInfoByAlarmId(Long alarmId) {
        return dmsRecordAlarmInfoMapper.deleteDmsRecordAlarmInfoByAlarmId(alarmId);
    }

    /**
     * 设备报警人工处理
     *
     * @param dmsRecordAlarmInfo
     * @return
     */
    @Override
    public int handleDmsRecordAlarmInfo(DmsRecordAlarmInfo dmsRecordAlarmInfo) {
        dmsRecordAlarmInfo.setAlarmStatus(DmsConstants.DMS_ALARM_STATUS_ARTIFICIAL_HANDLE);
        dmsRecordAlarmInfo.setHandleUser(SecurityUtils.getLoginUser()==null ? SecurityUtils.getUsername() :SecurityUtils.getLoginUser().getUser().getNickName());
        dmsRecordAlarmInfo.setHandleTime(DateUtils.getNowDate());
        dmsRecordAlarmInfo.setUpdateBy(SecurityUtils.getLoginUser()==null ? SecurityUtils.getUserId() :SecurityUtils.getLoginUser().getUser().getUserId());
        dmsRecordAlarmInfo.setUpdateTime(DateUtils.getNowDate());
        return dmsRecordAlarmInfoMapper.updateDmsRecordAlarmInfo(dmsRecordAlarmInfo);
    }

    /**
     * 设备报警消息通知
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = ServerException.class)
    public int alarmNotification() {
        DmsRecordAlarmInfo selectAlarmInfo = new DmsRecordAlarmInfo();
        selectAlarmInfo.setNoticeType(SystemConstants.DMS_NOTICE_TYPE_WEB);
        selectAlarmInfo.setNoticeStatus(SystemConstants.DMS_NOTICE_STATUS_FALSE);
        List<DmsRecordAlarmInfo> alarmInfoList = dmsRecordAlarmInfoMapper.selectDmsRecordAlarmInfoList(selectAlarmInfo);
        if (alarmInfoList.size() == 0) {
            return 1;
        }
/*        List<SysPointRouter> webRouterList = new ArrayList<>();
        Gson gson = new Gson();*/
        HashMap<String, Object> map = new HashMap<>();
        //trigger写此逻辑
//        for (DmsRecordAlarmInfo alarmInfo : alarmInfoList) {
//            SysPointRouter pointRouter = new SysPointRouter();
//            pointRouter.setModuleCode(SystemConstants.DMS);
//            pointRouter.setPointType(SystemConstants.SYS_ROUTER_POINT_TYPE_ALARM);
//            pointRouter.setRouterAddress(SystemConstants.SYS_ROUTER_DMS_ALARM_URL);
//            pointRouter.setProcessActivityId(SystemConstants.PROCESS_ACTIVITY_ID_QUALITY_CHECK_ABNORMAL);
//            map.put("alarmId", alarmInfo.getAlarmId());
//            String json = gson.toJson(map);
//            pointRouter.setRouterAddressDetail(json);
//            pointRouter.setRemark(alarmInfo.getAlarmReason());
//            webRouterList.add(pointRouter);
//        }
//        //WEB通知
//        remoteSysCommonService.insertSysPointRouterPort(webRouterList, SecurityConstants.INNER);
        for (DmsRecordAlarmInfo alarmInfo : alarmInfoList) {
            DmsRecordAlarmInfo recordAlarmInfo = new DmsRecordAlarmInfo();
            recordAlarmInfo.setAlarmId(alarmInfo.getAlarmId());
            recordAlarmInfo.setNoticeStatus(SystemConstants.DMS_NOTICE_STATUS_TRUE);
            this.updateDmsRecordAlarmInfo(recordAlarmInfo);
        }

        //公众号通知
        return 1;
    }

}
