package com.njcn.device.pq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.biz.enums.DeviceResponseEnum;
import com.njcn.device.pq.mapper.AlarmStrategyMapper;
import com.njcn.device.pq.pojo.param.AlarmStrategyParam;
import com.njcn.device.pq.pojo.po.AlarmStrategy;
import com.njcn.device.pq.pojo.vo.AlarmStrategyVO;
import com.njcn.device.pq.service.AlarmStrategyService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.pojo.po.DictData;
import com.njcn.web.utils.RequestUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @version 1.0.0
 * @author: chenchao
 * @date: 2022/06/23 13:24
 */
@Service
@AllArgsConstructor
public class AlarmStrategyServiceImpl extends ServiceImpl<AlarmStrategyMapper, AlarmStrategy> implements AlarmStrategyService {

    private final AlarmStrategyMapper alarmStrategyMapper;

    private final DicDataFeignClient dicDataFeignClient;

    /**
     * 获取告警策略列表
     *
     * @return
     */
    @Override
    public List<AlarmStrategyVO> getAlarmStrategyData() {
        List<AlarmStrategyVO> alarmStrategyVOS = alarmStrategyMapper.getAlarmStrategyData();
        return alarmStrategyVOS;
    }

    @Override
    public boolean updateAlarmStrategyById(AlarmStrategyParam alarmStrategyParam) {
        AlarmStrategy alarmStrategy = new AlarmStrategy();
        alarmStrategy.setId(alarmStrategyParam.getId());
        alarmStrategy.setIntegrityValue(alarmStrategyParam.getIntegrityValue());
        alarmStrategy.setOnlineValue(alarmStrategyParam.getOnlineValue());
        alarmStrategy.setOfftimeValue(alarmStrategyParam.getOffTimeValue());
        alarmStrategy.setWarnValue(alarmStrategyParam.getWarnValue());
        alarmStrategy.setUpdateBy(RequestUtil.getUserIndex());
        alarmStrategy.setUpdateTime(LocalDateTime.now());
        alarmStrategy.setState(DataStateEnum.ENABLE.getCode());
        alarmStrategyMapper.updateById(alarmStrategy);
        return true;
    }

    @Override
    public boolean addAlarmStrategyById(AlarmStrategyParam alarmStrategyParam) {
        DictData result = dicDataFeignClient.getDicDataById(alarmStrategyParam.getId()).getData();
        if (Objects.isNull(result)) {
            throw new BusinessException(DeviceResponseEnum.LINE_GRADE_INDEX_ERR);
        }

        LambdaQueryWrapper<AlarmStrategy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AlarmStrategy::getId,alarmStrategyParam.getId());
        int count = this.count(lambdaQueryWrapper);
        if(count>0){
            throw new BusinessException(DeviceResponseEnum.SAME_ALARM_STRATEGY);
        }

        AlarmStrategy alarmStrategy = new AlarmStrategy();
        alarmStrategy.setId(alarmStrategyParam.getId());
        alarmStrategy.setIntegrityValue(alarmStrategyParam.getIntegrityValue());
        alarmStrategy.setOnlineValue(alarmStrategyParam.getOnlineValue());
        alarmStrategy.setOfftimeValue(alarmStrategyParam.getOffTimeValue());
        alarmStrategy.setWarnValue(alarmStrategyParam.getWarnValue());
        alarmStrategy.setUpdateBy(RequestUtil.getUserIndex());
        alarmStrategy.setUpdateTime(LocalDateTime.now());
        alarmStrategy.setState(DataStateEnum.ENABLE.getCode());
        alarmStrategyMapper.insert(alarmStrategy);
        return true;

    }

    @Override
    public boolean removeAlarmStrategyData(String id) {
        alarmStrategyMapper.updateAlarmStrategyState(id);
        return true;
    }

}
