package com.fs.wx.order.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.his.domain.FsStoreOrder;
import com.fs.his.domain.FsStorePayment;
import com.fs.his.domain.FsStoreProduct;
import com.fs.his.mapper.FsStoreOrderMapper;
import com.fs.his.mapper.FsStorePaymentMapper;
import com.fs.live.domain.LiveOrder;
import com.fs.live.domain.LiveOrderPayment;
import com.fs.live.mapper.LiveOrderMapper;
import com.fs.live.mapper.LiveOrderPaymentMapper;
import com.fs.live.service.ILiveOrderService;
import com.fs.wx.order.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 java.util.List;

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

@Service
@Scope(value = SCOPE_PROTOTYPE)
public class LiveExpressToWxService extends ExpressToWxService {
    @Autowired
    private LiveOrderMapper liveOrderMapper;
    @Autowired
    private LiveOrderPaymentMapper liveOrderPaymentMapper;

    private LiveOrder liveOrder;
    private LiveOrderPayment liveOrderPayment ;

    @Override
    public void setOrderCode(String orderCode) {
        super.setOrderCode(orderCode);
        this.liveOrder = liveOrderMapper.selectLiveOrderByOrderCode(getOrderCode());
        if(ObjectUtil.isNull(liveOrder)) {
            throw new IllegalArgumentException(String.format("该订单 %s 未找到！",getOrderCode()));
        }
        this.liveOrderPayment = liveOrderPaymentMapper.selectLiveOrderLatestPayByOrderId(liveOrder.getOrderId());
        if(ObjectUtil.isNull(liveOrderPayment)){
            throw new IllegalArgumentException(String.format("该订单 %s 未找到对应支付记录!", getOrderCode()));
        }
    }


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

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

    @Override
    public String getOrderGoodsInfo() {
        return getOrderGoodsInfo(liveOrder);
    }

    @Override
    public String getExpressCompany() {

        return liveOrder.getDeliveryCode();
    }

    @Override
    public String getExpressNo() {

        return liveOrder.getDeliverySn();
    }

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


    /**
     * 获取订单商品信息
     * @return
     */
    private String getOrderGoodsInfo(LiveOrder order){
        StringBuilder title = new StringBuilder();
        // 如果是套餐
//        if(ObjectUtil.equal(order.getIsPackage(),1)){
//            String packageJson = order.getPackageJson();
//            JSONObject jsonObject = JSON.parseObject(packageJson);
//            title = new StringBuilder(jsonObject.getString("title"));
//        } else {

            FsStoreProduct product = JSONUtil.toBean(order.getItemJson(), FsStoreProduct.class);

            String productName = product.getProductName();
            title.append(productName).append("\n");

//        }
        return title.toString();
    }
}
