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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.model.response.SuccessResponseData;
import com.casic.missiles.modular.alarm.model.AlarmLevelParam;
import com.casic.missiles.modular.alarm.service.IAlarmLevelService;
import com.casic.missiles.modular.system.dao.AlarmLevelMapper;
import com.casic.missiles.modular.system.model.AlarmLevel;
import org.apache.ibatis.annotations.Param;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 123
 * @since 2019-10-15
 */
@Service
public class AlarmLevelServiceImpl extends ServiceImpl<AlarmLevelMapper, AlarmLevel> implements IAlarmLevelService {
    @Override
    public Map<String, Object> selectByNameAndTenGroup(Long tenantId, String name) {
        return this.baseMapper.selectByNameAndTenGroup(tenantId, name);
    }

    @Override
    public List<AlarmLevel> selectByNameAndTenList(Long tenantId, String name) {
        return this.baseMapper.selectByNameAndTenList(tenantId, name);
    }

    @Override
    public AlarmLevel selectByNameAndTenAndLevel(Long tenantId, String name, Integer level) {
        return this.baseMapper.selectByNameAndTenAndLevel(tenantId, name, level);
    }


    @Override
    public AlarmLevel selectByID(Long Id) {
        return this.baseMapper.selectByID(Id);
    }

    @Override
    public ResponseData deleteByCondition(String deviceType, String name, String tenantId) {
        ResponseData responseData = new ResponseData();
        try {
            Long deviceTypes = StrUtil.isEmpty(deviceType) ? null : Long.valueOf(deviceType);
            Long tenantIds = StrUtil.isEmpty(tenantId) ? null : Long.valueOf(tenantId);
            this.baseMapper.deleteByCondition(deviceTypes, name, tenantIds);
            responseData.setMessage("删除成功！");
            responseData.setCode(200);
        } catch (DataAccessException ex) {
            log.error("主题:报警等级配置新增出现异常，异常信息{}", ex);
            responseData.setCode(500);
            responseData.error("删除异常");
        }
        return responseData;
    }

    @Override
    @Transactional
    public ResponseData addAlarmLevel(AlarmLevelParam alarmLevelParam) {
        ResponseData resultData = new ResponseData();
        try {
            this.saveAlarmLevel(alarmLevelParam);
            resultData.setMessage("新增成功！");
            resultData.setCode(200);
        } catch (DataAccessException ex) {
            log.error("主题:报警等级配置新增出现异常，异常信息{}", ex);
            resultData.setCode(500);
            resultData.error("插入异常");
        }
        return resultData;
    }

    @Override
    @Transactional
    public ResponseData updateAlarmLevel(AlarmLevelParam alarmLevelParam) {
        ResponseData resultData = new ResponseData();
        try {
            saveAlarmLevel(alarmLevelParam);
            resultData.setCode(200);
            resultData.setMessage("更新成功！");
            return ResponseData.success();
        } catch (DataAccessException ex) {
            log.error("主题:报警等级配置更新出现异常，异常信息{}", ex);
            resultData.setCode(500);
        }
        return ResponseData.error("更新异常");
    }

    private void saveAlarmLevel(AlarmLevelParam alarmLevelParam) throws DataAccessException {
        QueryWrapper<AlarmLevel> queryWrapper = new QueryWrapper<AlarmLevel>()
                .eq("NAME", alarmLevelParam.getName())
                .eq("DEVICE_YPE", alarmLevelParam.getDeviceType())
                .eq("TENANT_ID", alarmLevelParam.getTenantId());
        this.baseMapper.delete(queryWrapper);
        AlarmLevel alarmLevel = new AlarmLevel();
        alarmLevel.setName(alarmLevelParam.getName());
        alarmLevel.setOpenJob(alarmLevelParam.getOpenJob());
        alarmLevel.setTenantId(Long.valueOf(StrUtil.isEmpty(alarmLevelParam.getTenantId()) ? "-1" : alarmLevelParam.getTenantId()));
        alarmLevel.setDeviceType(Long.valueOf(StrUtil.isEmpty(alarmLevelParam.getDeviceType()) ? "-1" : alarmLevelParam.getDeviceType()));
        alarmLevelParam.getLevelList().forEach(
                alarmLevels -> {
                    alarmLevel.setLevel(Integer.valueOf(StrUtil.isEmpty(alarmLevels.toString()) ? "-1" : alarmLevels.toString()));
                    this.save(alarmLevel);
                }
        );
    }
}
