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

import cn.hutool.core.util.ObjectUtil;
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.mapper.FsStoreOrderMapper;
import com.fs.his.mapper.FsStorePaymentMapper;
import com.fs.hisStore.domain.FsStoreOrderScrm;
import com.fs.hisStore.domain.FsStorePaymentScrm;
import com.fs.hisStore.mapper.FsStoreOrderScrmMapper;
import com.fs.hisStore.mapper.FsStorePaymentScrmMapper;
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 ShopExpressToWxService extends ExpressToWxService {
    @Autowired
    private FsStoreOrderScrmMapper fsStoreOrderMapper;
    @Autowired
    private FsStorePaymentScrmMapper fsStorePaymentMapper;

    private FsStoreOrderScrm fsStoreOrder;
    private FsStorePaymentScrm fsStorePayment;

    @Override
    public void setOrderCode(String orderCode) {
        super.setOrderCode(orderCode);
        this.fsStoreOrder = fsStoreOrderMapper.selectFsStoreOrderByOrderCode(getOrderCode());
        if(ObjectUtil.isNull(fsStoreOrder)) {
            throw new IllegalArgumentException(String.format("该订单 %s 未找到！",getOrderCode()));
        }
        List<FsStorePaymentScrm> fsStorePayments = fsStorePaymentMapper.selectFsStorePaymentByOrderId(fsStoreOrder.getId());
        if(CollectionUtils.isEmpty(fsStorePayments)){
            throw new IllegalArgumentException(String.format("该订单 %s 未找到对应支付记录!", getOrderCode()));
        }
        fsStorePayment = fsStorePayments.get(0);
    }


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

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

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

    @Override
    public String getExpressCompany() {

        return fsStoreOrder.getDeliverySn();
    }

    @Override
    public String getExpressNo() {

        return fsStoreOrder.getDeliveryId();
    }

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


    /**
     * 获取订单商品信息
     * @return
     */
    private String getOrderGoodsInfo(FsStoreOrderScrm 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 {
            String itemJson = order.getItemJson();
            com.alibaba.fastjson.JSONArray arrays = JSON.parseArray(itemJson);
            for(int i=0;i<arrays.size();i++){
                JSONObject jsonObject = arrays.getJSONObject(i);
                String jsonInfo = jsonObject.getString("jsonInfo");
                JSONObject jsonObject1 = JSON.parseObject(jsonInfo);
                String productName = jsonObject1.getString("productName");
                title.append(productName).append("\n");
            }
//        }
        return title.toString();
    }
}
