package com.uas.eis.serviceImpl;

import com.uas.eis.dao.BaseDao;
import com.uas.eis.dao.EdiLogDao;
import com.uas.eis.dao.EdiSuccessLogDao;
import com.uas.eis.service.ProductService;
import com.uas.eis.utils.Constant;
import com.uas.eis.utils.HttpUtil;
import com.uas.eis.utils.JacksonUtil;
import com.uas.eis.utils.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by zdw
 * 2018-05-15 08:29.
 */
@Service
public class ProductServiceImpl implements ProductService{

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String sendProductToYundingUrl = "http://dc.oclean.com/putstorespec?sign";

    @Autowired
    BaseDao baseDao;
    @Autowired
    EdiLogDao ediLogDao;
    @Autowired
    EdiSuccessLogDao ediSuccessLogDao;


    @Override
    public Map<String, Object> postProductToYunding(Integer id) {
        Map<String,Object> data = getMainData(id);
        List<Map<String,Object>> datas = new LinkedList<Map<String,Object>>();
        datas.add(data);

        String timestamp = String.valueOf(System.currentTimeMillis()/1000);
        String outerNo = String.valueOf(data.get("goods_no"));

        Map<String,Object> postMapData = new HashMap<>();
        postMapData.put("app_key", Constant.yundingAppKey);
        postMapData.put("time_stamp",timestamp);
        postMapData.put("spec_list",datas);

        return postData(outerNo,postMapData,timestamp);
    }

    private Map<String,Object> postData(String outerNo,Map<String,Object> data,String timestamp){
        Map<String,Object> res = new HashMap<>();
        String jsonData = JacksonUtil.toSortJson(data);
        System.out.println(jsonData);
        try{

            HttpUtil.Response response = HttpUtil.doPostToYunding(sendProductToYundingUrl,jsonData,timestamp);

            String responseText = response.getResponseText();

            logger.info("responseText:" + responseText);
            logger.info("responseCode:" + response.getStatusCode());

            if(response.getStatusCode()!=200){
                throw new RuntimeException("yunding server error:" + responseText);
            }

            Map<String,Object> responseMap = JacksonUtil.fromJson(response.getResponseText());

            if(!"0".equals(String.valueOf(responseMap.get("code")))){
                res.put("success",false);
                res.put("remark",responseMap.get("message"));
            }else{
                Map<String,Object> msgJson = (Map<String,Object>) responseMap.get("msg_json");
                List<String> successCodes = (List<String>)msgJson.get("s_no");
                if(successCodes.contains(outerNo)){
                    res.put("success",true);
                    ediLogDao.save(LogUtil.getSendSuccessLog(outerNo,jsonData,"send success"));
                }else{
                    res.put("success",false);
                    res.put("remark","云顶数据中心处理失败!");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("error",e);

            res.put("success",false);
            res.put("remark",e.getMessage());
        }
        return res;
    }

    private Map<String,Object> getMainData(Integer id){
        Map<String,Object> data = new HashMap<>();
        SqlRowSet rs = baseDao.getJdbcTemplate().queryForRowSet("select pr_code,pr_detail,pr_brand,pr_unit,pr_code,pr_spec,pr_description from Product where pr_id=" + id);
        if(rs.next()){
            data.put("goods_no",rs.getString("pr_code"));
            data.put("goods_name",rs.getString("pr_detail"));
            data.put("goods_type",1);
            data.put("class_name","");
            data.put("brand_name",rs.getString("pr_brand"));
            data.put("unit_name",rs.getString("pr_unit"));
            data.put("spec_no",rs.getString("pr_code"));
            data.put("spec_code","");
            data.put("spec_name",rs.getString("pr_spec"));
            data.put("bar_code","");
            data.put("price","");
            data.put("guarantee","");
            data.put("description",rs.getString("pr_description"));
        }
        return data;
    }





}
