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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.receiver.farm.dao.entity.FarmDeviceEntity;
import com.huimv.receiver.farm.dao.repo.FarmDeviceRepo;
import com.huimv.receiver.farm.service.ICenterDevice;
import com.huimv.receiver.utils.TextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Service
@Slf4j
public class CenterDeviceImpl implements ICenterDevice {
    @Autowired
    private FarmDeviceRepo deviceRepo;
    @Autowired
    private TextUtil textUtil;

    @Override
    public Result syncAddFarmDevice(String deviceData) throws UnsupportedEncodingException {
        //转义符号处理
        JSONObject deviceDataJo = JSON.parseObject(textUtil.translateSymbol(deviceData));
        FarmDeviceEntity deviceEntity = new FarmDeviceEntity();
        deviceEntity.setDataId(deviceDataJo.getLong("dataId"));
        deviceEntity.setDeviceCode(deviceDataJo.getString("deviceCode"));
        deviceEntity.setDeviceName(deviceDataJo.getString("deviceName"));
        deviceEntity.setDeviceType(deviceDataJo.getString("deviceType"));
        deviceEntity.setUpTime(new Timestamp(new  java.util.Date().getTime()));
        deviceEntity.setFactory(deviceDataJo.getString("factory"));
        deviceEntity.setFarmId(deviceDataJo.getInteger("farmId"));
        deviceEntity.setLastTime(new Timestamp(new  java.util.Date().getTime()));
        deviceEntity.setLockinState(deviceDataJo.getInteger("lockinState"));
        deviceEntity.setMainParams(deviceDataJo.getString("mainParams"));
        deviceEntity.setRecord(deviceDataJo.getString("record"));
        deviceEntity.setState(deviceDataJo.getInteger("state"));
        deviceEntity.setWorker(deviceDataJo.getString("worker"));
        FarmDeviceEntity addEntity = deviceRepo.saveAndFlush(deviceEntity);
        log.info("同步添加数据中心端设备数据>>"+addEntity);
        return new Result(ResultCode.SUCCESS,addEntity);
    }

    @Override
    public Result syncEditFarmDevice(String deviceData) throws UnsupportedEncodingException {
        //转义符号处理
        JSONObject deviceDataJo = JSON.parseObject(textUtil.translateSymbol(deviceData));
        //查找已经存在的记录
        Optional<FarmDeviceEntity> optional = deviceRepo.getByDataId(deviceDataJo.getLong("dataId"));
        if(!optional.isPresent()){
            String ERR_INFO_NOEXIST = "该记录不存在.";
            log.error(ERR_INFO_NOEXIST);
            return new Result(10001,ERR_INFO_NOEXIST,false);
        }
        //更新数据
        FarmDeviceEntity deviceEntity = optional.get();
        deviceEntity.setDeviceCode(deviceDataJo.getString("deviceCode"));
        deviceEntity.setDeviceName(deviceDataJo.getString("deviceName"));
        deviceEntity.setDeviceType(deviceDataJo.getString("deviceType"));
        deviceEntity.setUpTime(new Timestamp(new  java.util.Date().getTime()));
        deviceEntity.setFactory(deviceDataJo.getString("factory"));
        deviceEntity.setFarmId(deviceDataJo.getInteger("farmId"));
        deviceEntity.setLastTime(new Timestamp(new  java.util.Date().getTime()));
        deviceEntity.setLockinState(deviceDataJo.getInteger("lockinState"));
        deviceEntity.setMainParams(deviceDataJo.getString("mainParams"));
        deviceEntity.setRecord(deviceDataJo.getString("record"));
        deviceEntity.setState(deviceDataJo.getInteger("state"));
        deviceEntity.setWorker(deviceDataJo.getString("worker"));
        FarmDeviceEntity editEntity = deviceRepo.saveAndFlush(deviceEntity);
        log.info("同步更新数据中心端设备数据>>"+editEntity);
        return new Result(ResultCode.SUCCESS,editEntity);
    }

    @Override
    public Result syncRemoveDevice(String deviceData) throws UnsupportedEncodingException {
        //转义符号处理
        Long dataId = Long.parseLong(textUtil.translateSymbol(deviceData));
        //查询目标设备数据
        List<Object[]> existList = deviceRepo.getDevice(dataId);
        if(existList.size()>0) {
            Object[] object = existList.get(0);
            Integer id = (Integer) object[0];
            deviceRepo.deleteById(id);
            log.info("同步删除牧场端设备数据.");
            return new Result(ResultCode.SUCCESS);
        }else{
            String ERR_INFO="暂无需要删除的设备数据.";
            log.info(ERR_INFO);
            return new Result(10001,ERR_INFO,false);
        }
    }
}
