package com.casic.missiles.modular.alarm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.core.util.EhcacheConstant;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.alarm.service.IAlarmContentTypeService;
import com.casic.missiles.modular.system.dao.AlarmContentTypeMapper;
import com.casic.missiles.modular.system.model.AlarmContentType;
import org.springframework.stereotype.Service;

import java.nio.channels.AcceptPendingException;
import java.util.List;
import java.util.Map;

@Service
public class AlarmContentTypeServiceImpl extends ServiceImpl<AlarmContentTypeMapper, AlarmContentType> implements IAlarmContentTypeService {


    @Override
    public ResponseData selectListPage(String alarmType, String deviceType, String alarmContent) {
        ResponseData responseData = new ResponseData();
        try {
            List<Map<String, Object>> alarmContentTypeList = this.baseMapper.selectListPage(
                    Long.valueOf(StrUtil.isEmpty(alarmType) ? "-1" : alarmType),
                    Long.valueOf(StrUtil.isEmpty(deviceType) ? "-1" : deviceType), alarmContent);
            supplementData(alarmContentTypeList);
            responseData.setData(alarmContentTypeList);
            responseData.setMessage("success");
            responseData.setCode(200);
        } catch (AcceptPendingException ex) {
            log.error("主题:报警内容类型查询异常，异常信息{}", ex);
            responseData.setMessage("查询异常");
            responseData.setCode(500);
        }
        return responseData;
    }

    @Override
    public ResponseData add(AlarmContentType AlarmContentType) {
        ResponseData responseData = new ResponseData();
        try {
            responseData.setData(this.save(AlarmContentType));
            responseData.setMessage("success");
            responseData.setCode(200);
            return responseData;
        } catch (AcceptPendingException ex) {
            log.error("主题:报警内容类型插入异常，异常信息{}", ex);
            return ResponseData.error("插入异常");
        }
    }

    @Override
    public ResponseData update(AlarmContentType AlarmContentType) {
        ResponseData responseData = new ResponseData();
        try {
            responseData.setData(this.updateById(AlarmContentType));
            responseData.setMessage("success");
            responseData.setCode(200);
            return responseData;
        } catch (AcceptPendingException ex) {
            log.error("主题:报警内容类型更新异常，异常信息{}", ex);
            return ResponseData.error("插入异常");
        }
    }

    @Override
    public ResponseData deleteById(Long id) {
        ResponseData responseData = new ResponseData();
        try {
            responseData.setData(this.removeById(id));
            responseData.setMessage("success");
            responseData.setCode(200);
            return responseData;
        } catch (AcceptPendingException ex) {
            log.error("主题:报警内容类型删除异常，异常信息{}", ex);
            return ResponseData.error("插入异常");
        }
    }

    private void supplementData(List<Map<String, Object>> alarmContentTypeList) {
        alarmContentTypeList.forEach(
                alarmType -> {
                    alarmType.put("alarmTypeName", EhcacheConstant.retBean().getAlarmTypeNameByCodeNum(alarmType.get("alarmType").toString()));
                    alarmType.put("deviceTypeName", EhcacheConstant.retBean().getDeviceTypeNameById(alarmType.get("deviceType").toString()));
                }
        );
    }
}
