package com.prolog.eis.service.led.impl;

import com.prolog.eis.dto.led.LedReqDTO;
import com.prolog.eis.service.led.LedService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


/**
 * @author liubaolin
 * @date 2021/6/3 10:57
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LedServiceImpl implements LedService {
    private final LedApiService ledApiService;


    public static final String LED_DELIMITER = ";";

    @Async
    @Override
    public void sendInboundSuccessMsg(String locationNo, String containerNo) {
        try {
            sendLedMsg(locationNo,
                    String.format("%s入库成功", containerNo)
            );
        } catch (Exception e) {
            log.error("发送入库成功led消息失败：" + e.getMessage(), e);
        }
    }

    @Async
    @Override
    public void sendInboundErrMsg(String locationNo, String containerNo, String errMsg) {
        try {
            sendLedMsg(locationNo, containerNo + errMsg
            );
        } catch (Exception e) {
            log.error("发送入库失败led消息失败：" + e.getMessage(), e);
        }
    }

    @Async
    @Override
    public void sendOutboundMsg(String locationNo, String containerNo) {
        try {
            sendLedMsg(locationNo,
                    String.format("%s出库成功", containerNo)
            );
        } catch (Exception e) {
            log.error("发送出库led消息失败：" + e.getMessage(), e);
        }
    }

    private void sendLedMsg(String areaNo, String msg) {
        ledApiService.execute(LedReqDTO.builder()
                .areaNo(areaNo)
                .message(msg)
                .build());
    }
}
