package com.zd.netty.service.impl;

import com.zd.bottle.feign.service.RemoteBottleService;
import com.zd.common.core.constant.HttpStatus;
import com.zd.common.core.domain.R;
import com.zd.netty.service.IFridService;
import com.zd.netty.service.ISendService;
import com.zd.system.api.airbottle.RemoteAirBottleService;
import com.zd.system.api.chemical.RemoteStockService;
import com.zd.system.api.domain.InventoryTag;
import com.zd.system.api.laboratory.domain.RemoteLabHardware;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import static com.zd.netty.constant.RfidConstants.*;

/**
 * @author Administrator
 */
@Slf4j
@Service(value = "sendService")
public class SendServiceImpl implements ISendService {

    @Resource
    private RemoteStockService remoteStockService;
    @Resource
    private RemoteAirBottleService bottleService;

    @Resource
    private RemoteBottleService remoteBottleService;

    @Resource
    private RedisTemplate<String,RemoteLabHardware> redisTemplate;

    @Override
    public boolean send(InventoryTag tag) {
        String serialNumber = tag.getSerialNumber();
        RemoteLabHardware hardware = tag.getRemoteLabHardware();
        if (StringUtils.hasLength(serialNumber)){
            log.info("===============》设备编码：{}",serialNumber);
            hardware = redisTemplate.opsForValue().get(serialNumber);
        }
        if (hardware!=null){
            log.info("=========ip:{},设备编码：{}=========",hardware.getIpAddress(),serialNumber);
            int antId = tag.getAntId();
            String uniformPower = hardware.getUniformPower();
            String[] uniformPowers = uniformPower.split(",");
            return handleTag(tag, antId, uniformPowers);
        }
        log.info("=========未获取到设备信息=========");
        return sendTag(tag,null);
    }

    private boolean handleTag(InventoryTag tag, int antId, String[] uniformPowers) {
        // 判断有设备配置和触发的天线编码等与配置天线编号
        if (uniformPowers.length > 0) {
            String tempUniformPower = uniformPowers[antId - 1];
            if (StringUtils.hasLength(tempUniformPower)) {
                String[] tempUniformPowers = tempUniformPower.split(":");
                if (tempUniformPowers.length > 1) {
                    String temp = tempUniformPowers[1];
                    return sendTag(tag, temp);
                } else {
                    return sendTag(tag, null);
                }
            }
        }
        return false;
    }

    private boolean sendTag(InventoryTag tag, String temp) {
        if (temp == null) {
            boolean b = sendBottle(tag);
            if (b){
                return true;
            }
            return sendStock(tag);
        }
        if (ALL_TYPE.equals(temp)) {
            boolean b = sendBottle(tag);
            if (b){
                return true;
            }
            return sendStock(tag);
        }
        if (BOTTLE_TYPE.equals(temp)) {
            return sendBottle(tag);
        }
        if (CHEMICAL_TYPE.equals(temp)) {
            return sendStock(tag);
        }
        return false;
    }

    private boolean sendStock(InventoryTag tag) {
        try {
            R<Boolean> result = remoteStockService.rfidCheck(tag);
            log.info("化学品服务调用：" + result.getData());
            if (result.getCode() == HttpStatus.SUCCESS) {
                return result.getData();
            }
        } catch (Exception e) {
            log.info("化学品服务异常：" + e.getMessage());
        }
        return false;
    }

    private boolean sendBottle(InventoryTag tag) {
        R<Boolean> result;
        try {
            result = bottleService.remoteAdd(tag);
            log.info("气瓶服务调用：" + result.getData());
            if (result.getCode() == HttpStatus.SUCCESS) {
               return result.getData();
            }
        } catch (Exception e) {
            log.info("气瓶服务异常：" + e.getMessage());
        }
        try {
            result = remoteBottleService.remoteAdd(tag);
            log.info("气瓶服务调用：" + result.getData());
            if (result.getCode() == HttpStatus.SUCCESS) {
                return result.getData();
            }
        } catch (Exception e) {
            log.info("新气瓶服务异常：" + e.getMessage());
        }
        return false;
    }

    @Override
    public void sendError(String msg) {
        log.info(msg);
    }
}
