package com.jeequan.jeepay.pay.service.payorderpush.impl;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.components.mq.model.PushSpeakerMQ;
import com.jeequan.jeepay.core.interfaces.device.IPrinterService;
import com.jeequan.jeepay.core.model.device.PayOrderInfo4Device;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.db.entity.DeviceProvideConfig;
import com.jeequan.jeepay.db.entity.MemberAccountHistory;
import com.jeequan.jeepay.db.entity.PayOrder;
import com.jeequan.jeepay.pay.service.payorderpush.AbstractPushService;
import com.jeequan.jeepay.service.impl.MchStoreDeviceService;
import com.jeequan.jeepay.service.impl.MemberAccountHistoryService;
import com.jeequan.jeepay.service.impl.PayOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/*
* 支付成功 发送微信公众号消息 service
*
* @author zx
*
* @date 2021/6/8 17:43
*/
@Slf4j
@Service
public class PushPrinterService extends AbstractPushService {

    @Autowired private PayOrderService payOrderService;
    @Autowired private MchStoreDeviceService mchStoreDeviceService;
    @Autowired private MemberAccountHistoryService memberAccountHistoryService;

    public void send(String orderId, Byte orderType){

        log.info("云打印，打印订单类型为：{}", getOrderType(orderType));

        PayOrderInfo4Device printInfo; // 打印内容对象

        if (orderType == PushSpeakerMQ.MsgPayload.ORDER_TYPE_PAY) {
            // 查询订单、订单状态
            PayOrder payOrder = payOrderService.getById(orderId);
            if (!preCheck(payOrder)) {
                return;
            }

            printInfo = getPrintInfo(payOrder);
        }else  if (orderType == PushSpeakerMQ.MsgPayload.ORDER_TYPE_MEMBER) {
            // 查询订单、订单状态
            MemberAccountHistory accountHistory = memberAccountHistoryService.getById(orderId);
            if (accountHistory == null || accountHistory.getBizType() != MemberAccountHistory.BIZ_CONSUME) {
                return;
            }

            printInfo = getOrderInfo(accountHistory);
        }else {
            log.info("云喇叭播报失败，orderType错误");
            return;
        }

        // 查询设备
        List<JSONObject> deviceList = mchStoreDeviceService.selectAvailableDeviceList(printInfo.getMchNo(), printInfo.getStoreId(), Arrays.asList(DeviceProvideConfig.DEVICE_TYPE_PRINTER));
        if (CollectionUtils.isEmpty(deviceList)) {
            return;
        }

        // 循环调用单个设备
        for (JSONObject deviceParams : deviceList) {

            try {
                // 设备类型
                String provider = deviceParams.getString("provider");
                IPrinterService printerService = SpringBeansUtil.getBean(provider + "PrinterService", IPrinterService.class);
                if (null == printerService) {
                    continue;
                }

                printerService.send(deviceParams, printInfo);
                log.info("云打印打印成功，设备厂商provider={}, {}orderId={}", provider, getOrderType(orderType), orderId);
            } catch (Exception e) {
                log.error("error", e);
            }
        }
    }

}
