package com.huimv.receiver.farm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huimv.receiver.farm.dao.entity.BaseWarningInfoEntity;
import com.huimv.receiver.farm.dao.repo.BaseWarningInfoRepo;
import com.huimv.receiver.farm.service.IAlarm;
import com.huimv.receiver.utils.Const;
import com.huimv.receiver.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Service
public class AlarmImpl implements IAlarm {
    @Autowired
    private BaseWarningInfoRepo warningInfoRepo;
    @Autowired
    private DateUtil dateUtil;

    @Override
    public void handleEnvironAlarm(String alarmInfo){
        JSONObject alarmJo = JSON.parseObject(alarmInfo);
        BaseWarningInfoEntity warningInfoEntity  = new BaseWarningInfoEntity();
        warningInfoEntity.setFarmId(alarmJo.getInteger("farmId"));
        warningInfoEntity.setAlarmType(alarmJo.getInteger("alarmType"));
        warningInfoEntity.setMsg(alarmJo.getString("info"));
        warningInfoEntity.setLevel(alarmJo.getInteger("level"));
        warningInfoEntity.setLevelName(alarmJo.getString("levelName"));
        warningInfoEntity.setWarningName(alarmJo.getString("warningName"));
        warningInfoEntity.setWarningTime(new Timestamp(alarmJo.getLong("alarmTime")));
        warningInfoEntity.setUploadTime(new Timestamp(new Date().getTime()));
        //
        warningInfoRepo.saveAndFlush(warningInfoEntity);
    }

    @Override
    public void handleEnvironAlarm(String alarmInfo, Integer typeAlarm,String farmId) throws ParseException {
        BaseWarningInfoEntity warningInfoEntity  = new BaseWarningInfoEntity();
        warningInfoEntity.setFarmId(Integer.parseInt(farmId));
        warningInfoEntity.setAlarmType(typeAlarm);
        warningInfoEntity.setMsg(alarmInfo);
        warningInfoEntity.setLevel(Integer.parseInt(Const.INFO_FIRST_LEVEL));
        warningInfoEntity.setLevelName(Const.INFO_FIRST_LEVEL_ALARM);
        warningInfoEntity.setWarningName(Const.INFO_ALARM_TYPE_NAME);
        warningInfoEntity.setWarningTime(new Timestamp(dateUtil.parseDateTimeLong(dateUtil.getTodayText())));
        warningInfoEntity.setUploadTime(new Timestamp(new Date().getTime()));
        //
        warningInfoRepo.saveAndFlush(warningInfoEntity);
    }
}
