package com.iamberry.wechat.service.efast;

import com.iamberry.app.tool.log.RatFWLogger;
import com.iamberry.wechat.face.efast.EfastRentService;
import com.iamberry.wechat.face.porduct.ProductColorService;
import com.iamberry.wechat.tools.DateTimeUtil;
import com.iamberry.wechat.tools.HttpClient431Util;
import com.iamberry.wechat.tools.NameUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/4/21.
 */
@Service
public class EfastRentServiceImpl implements EfastRentService {

    @Autowired
    private RatFWLogger logger;
    @Autowired
    private ProductColorService productColorService;

    @Override
    public Map<String,Object> getEfastSendOrderInfo(String oid,String type) {
        // TODO Auto-generated method stub

        // 组装数据
        Map<String, String> requestData = new HashMap<String, String>();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("status", false);
        requestData.put("app_nick", NameUtils.getConfig("app_nick"));
        requestData.put("app_key", NameUtils.getConfig("app_key"));
        requestData.put("app_secret", NameUtils.getConfig("app_secret"));
        requestData.put("timestamp", DateTimeUtil.format(new Date(),"yyyyMMddHHmmss"));
        requestData.put("app_act", "efast.trade.detail.get");
        requestData.put("oid", oid);
        requestData.put("type", type);
        requestData.put("feilds", NameUtils.getConfig("order_field"));
        try {
            String resultJson = HttpClient431Util.doPost(requestData, NameUtils.getConfig("efast_base_url"));
            logger.info("订单号为:" + oid + "的订单，百胜返回信息:" + resultJson);
            resultJson = StringEscapeUtils.unescapeJava(resultJson); // unicode
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(resultJson);
            if (m.find()) {
                resultJson = m.replaceAll("");
            }
            logger.info("get-订单号为:" + oid + "的订单，百胜返回信息:" + resultJson);
            JSONObject jsonObject = JSONObject.fromObject(resultJson);

            // default HTTP SUCCESS
            JSONObject respData = JSONObject.fromObject(jsonObject.get("resp_data"));
            // 发货状态 : shipping_status
            String shippingStatus = respData.getString("shipping_status");
            if ("1".equals(shippingStatus)) {   //已发货
                map.put("postFirm", respData.getString("shipping_name"));
                map.put("postNum", respData.getString("invoice_no"));
                map.put("status", true);
            }else if ("0".equals(shippingStatus)){   //未发货
                map.put("result", "该订单未发货！");
            }else if ("3".equals(shippingStatus)){    //备货中
                map.put("result", "备货中！");
            }
            logger.error(this, "get-订单号为:" + oid + "的订单，百胜返回信息:" + resultJson);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error(this, "get-订单号为:" + oid + "的订单，发生错误:" + e.getMessage());
            map.put("result", "查询订单失败！");
        }
        return map;
    }

}
