package com.fs.erp.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fs.erp.domain.ErpOrder;
import com.fs.erp.domain.ErpRefundOrder;
import com.fs.erp.dto.*;
import com.fs.erp.service.IErpOrderService;
import com.fs.erp.utils.CommonUtils;
import com.fs.his.config.FsSysConfig;
import com.fs.his.domain.FsStoreOrder;
import com.fs.his.utils.ConfigUtil;
import com.fs.hisStore.domain.FsStoreOrderScrm;
import com.fs.live.domain.LiveOrder;
import com.fs.live.mapper.LiveOrderMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ErpOrderServiceImpl implements IErpOrderService
{
    public final static Logger LOGGER = LoggerFactory.getLogger(ErpOrderServiceImpl.class);
    @Autowired
    ConfigUtil configUtil;
    @Autowired
    private LiveOrderMapper liveOrderMapper;
    @Override
    public ErpOrderResponse addOrder(ErpOrder order) {
        FsSysConfig sysConfig = configUtil.getSysConfig();
        JSONObject param = JSONUtil.createObj();
        param.set("appkey", sysConfig.getErpAppKey());
        param.set("sessionkey", sysConfig.getErpSessionKey());
        param.set("method", "gy.erp.trade.add");
        param.set("shop_code", order.getShop_code());
        param.set("vip_code", order.getVip_code());
        param.set("platform_code", order.getPlatform_code());
        param.set("warehouse_code", order.getWarehouse_code());
        param.set("express_code", order.getExpress_code());
        param.set("post_fee", order.getPost_fee());
        param.set("cod", order.getCod());
        param.set("seller_memo", order.getSeller_memo());
        param.set("order_type_code", order.getOrder_type_code());
        param.set("cod_fee", order.getCod_fee());
        param.set("buyer_memo", order.getBuyer_memo());
        param.set("details", order.getDetails());
        param.set("payments",order.getPayments());
        param.set("receiver_name", order.getReceiver_name());
        param.set("receiver_mobile", order.getReceiver_mobile());
        param.set("receiver_province", order.getReceiver_province());
        param.set("receiver_city", order.getReceiver_city());
        param.set("receiver_district", order.getReceiver_district());
        param.set("receiver_address", order.getReceiver_address());
        param.set("deal_datetime", order.getDeal_datetime());
        param.set("sign",CommonUtils.sign(param.toString(),sysConfig.getErpSecret()));

        String result = HttpUtil.post(sysConfig.getErpUrl(), param.toString());
        System.out.println("erp-order:"+param.toString());
        LOGGER.info("erp-order:"+param.toString());
        LOGGER.error("erp-order-result:"+result);
        ErpOrderResponse response=JSONUtil.toBean(result, ErpOrderResponse.class);
        return response;
    }

    @Override
    public ErpOrderResponse addOrderScrm(ErpOrder order) {
        return addOrder( order);
    }

    @Override
    public ErpOrderResponse addLiveOrder(ErpOrder order) {
        return addOrder(order);
    }

    @Override
    public ErpOrderResponse refundOrder(ErpRefundOrder order) {
        FsSysConfig sysConfig = configUtil.getSysConfig();
        JSONObject param = JSONUtil.createObj();
        param.set("appkey", sysConfig.getErpAppKey());
        param.set("sessionkey", sysConfig.getErpSessionKey());
        param.set("method", "gy.erp.trade.return.add");
        param.set("type_code", order.getType_code());
        param.set("shop_code", order.getShop_code());
        param.set("vip_code", order.getVip_code());
        // param.set("trade_code", order.getTrade_platform_code());
        param.set("trade_platform_code", order.getTrade_platform_code());
        param.set("item_detail", order.getDetails());
        param.set("sign",CommonUtils.sign(param.toString(),sysConfig.getErpSecret()));
        String result = HttpUtil.post(sysConfig.getErpUrl(), param.toString());
        ErpOrderResponse response=JSONUtil.toBean(result, ErpOrderResponse.class);
        return response;
    }

    @Override
    public ErpOrderResponse refundOrderScrm(ErpRefundOrder order) {
        return null;
    }

    @Override
    public ErpOrderResponse refundLiveOrder(ErpRefundOrder order) {
        return null;
    }

    @Override
    public ErpDeliverysResponse getDeliver(ErpDeliverysRequest request) {
        FsSysConfig sysConfig = configUtil.getSysConfig();
        JSONObject param = JSONUtil.createObj();
        param.set("appkey", sysConfig.getErpAppKey());
        param.set("sessionkey", sysConfig.getErpSessionKey());
        param.set("method", "gy.erp.trade.deliverys.get");
        param.set("code", request.getCode());
        param.set("page_no", request.getPage_no());
        param.set("page_size", request.getPage_size());
        param.set("detail_flag", request.getDetail_flag());

        param.set("sign",CommonUtils.sign(param.toString(),sysConfig.getErpSecret()));
        String result = HttpUtil.post(sysConfig.getErpUrl(), param.toString());
        ErpDeliverysResponse response=JSONUtil.toBean(result, ErpDeliverysResponse.class);
        return response;
    }

    @Override
    public ErpOrderQueryResponse getOrder(ErpOrderQueryRequert requert) {
        FsSysConfig sysConfig = configUtil.getSysConfig();
        JSONObject param = JSONUtil.createObj();
        param.set("appkey", sysConfig.getErpAppKey());
        param.set("sessionkey", sysConfig.getErpSessionKey());
        param.set("method", "gy.erp.trade.get");
        param.set("code", requert.getCode());
        param.set("page_no", requert.getPage_no());
        param.set("page_size", requert.getPage_size());
        param.set("platform_code", requert.getPlatform_code());
        param.set("sign",CommonUtils.sign(param.toString(),sysConfig.getErpSecret()));
        String result = HttpUtil.post(sysConfig.getErpUrl(), param.toString());
        ErpOrderQueryResponse response=JSONUtil.toBean(result, ErpOrderQueryResponse.class);
        return response;
    }

    @Override
    public ErpOrderQueryResponse getScrmOrder(ErpOrderQueryRequert param) {
        return null;
    }

    @Override
    public ErpOrderQueryResponse getLiveOrder(ErpOrderQueryRequert param) {
        return null;
    }

    @Override
    public BaseResponse refundUpdate(ErpRefundUpdateRequest request) {
        FsSysConfig sysConfig = configUtil.getSysConfig();
        JSONObject param = JSONUtil.createObj();
        param.set("appkey", sysConfig.getErpAppKey());
        param.set("sessionkey", sysConfig.getErpSessionKey());
        param.set("method", "gy.erp.trade.refund.update");
        param.set("tid", request.getTid());
        param.set("oid", request.getOid());
        param.set("refund_state", request.getRefund_state());
        param.set("sign",CommonUtils.sign(param.toString(),sysConfig.getErpSecret()));
        String result = HttpUtil.post(sysConfig.getErpUrl(), param.toString());
        ErpOrderResponse response=JSONUtil.toBean(result, ErpOrderResponse.class);
        return response;
    }

    @Override
    public BaseResponse refundUpdateScrm(ErpRefundUpdateRequest param) {
        return null;
    }

    @Override
    public BaseResponse refundUpdateLive(ErpRefundUpdateRequest param) {
        return null;
    }

    @Override
    public ErpOrderResponse finishOrder(ErpOrder order) {
        return null;
    }

    @Override
    public void getOrderDeliveryStatus(FsStoreOrder order) {

    }

    @Override
    public void getOrderScrmDeliveryStatus(FsStoreOrderScrm order) {

    }

    @Override
    public void getOrderLiveDeliveryStatus(LiveOrder order) {

    }
}
