package com.fs.store.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.live.domain.LiveOrder;
import com.fs.live.domain.LiveOrderItem;
import com.fs.live.domain.LiveOrderPayment;
import com.fs.live.mapper.LiveOrderItemMapper;
import com.fs.live.mapper.LiveOrderMapper;
import com.fs.live.mapper.LiveOrderPaymentMapper;
import com.fs.store.domain.FsStoreDelivers;
import com.fs.store.mapper.FsStoreDeliversMapper;
import com.fs.store.service.ExpressToWxService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

@Service
@Scope(value = SCOPE_PROTOTYPE)
public class LiveStreamShopExpressToWxService extends ExpressToWxService {

    @Autowired
    private LiveOrderMapper liveOrderMapper;
    @Autowired
    private LiveOrderPaymentMapper liveOrderPaymentMapper;
    @Autowired
    private LiveOrderItemMapper liveOrderItemMapper;
    @Autowired
    private FsStoreDeliversMapper fsStoreDeliversMapper;

    private LiveOrder liveOrder;
    private LiveOrderPayment liveOrderPayment;



    @Override
    public void setOrderCode(String orderCode) {
        super.setOrderCode(orderCode);
        this.liveOrder = liveOrderMapper.selectLiveOrderByOrderCode(getOrderCode());
        if(ObjectUtil.isNull(this.liveOrder)){
            throw new IllegalArgumentException(String.format("订单%s未找到!", this.getOrderCode()));
        }
        LiveOrderPayment liveOrderPayments = liveOrderPaymentMapper.selectLiveOrderLatestPayByOrderId(liveOrder.getOrderId());
        if(ObjectUtil.isNotNull(liveOrderPayments)){
            liveOrderPayment = liveOrderPayments;
        }
    }

    @Override
    public String getTransactionId() {
        return liveOrderPayment.getBankTransactionId();
    }

    @Override
    public String getUserPhone() {
        return liveOrder.getUserPhone();
    }

    @Override
    public String getOrderGoodsInfo() {
        List<LiveOrderItem> liveOrderItems = liveOrderItemMapper.selectLiveOrderItemByOrderId(liveOrder.getOrderId());
        StringBuffer sb = new StringBuffer();
        for (LiveOrderItem liveOrderItem : liveOrderItems) {
            JSONObject jsonObject = JSON.parseObject(liveOrderItem.getJsonInfo());
            String productName = jsonObject.getString("productName");
            sb.append(productName).append("\n");
        }
        return sb.toString();
    }

    @Override
    public String getExpressCompany() {
        List<FsStoreDelivers> byOrderId = fsStoreDeliversMapper.findByOrderIdWithType(liveOrder.getOrderId(),1);
        if(CollectionUtils.isEmpty(byOrderId)){
            throw new IllegalArgumentException(String.format("当前订单 %s 无物流记录",getOrderCode()));
        }
        return byOrderId.get(0).getDeliverSn();
    }

    @Override
    public String getExpressNo() {
        List<FsStoreDelivers> byOrderId = fsStoreDeliversMapper.findByOrderIdWithType(liveOrder.getOrderId(),1);
        if(CollectionUtils.isEmpty(byOrderId)){
            throw new IllegalArgumentException(String.format("当前订单 %s 无物流记录",getOrderCode()));
        }
        return byOrderId.get(0).getDeliverId();
    }

    @Override
    public boolean support(Integer type) {
        return 1==type;
    }


}
