package com.liangjian.dataplatform.member.service.impl;

import com.liangjian.dataplatform.member.jobUtil.EsUtils;
import com.liangjian.dataplatform.member.service.IMemOrderproductsService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import org.springframework.stereotype.Service;

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

@Service
public class MemOrderproductsService implements IMemOrderproductsService {
    @Override
    public Map<String, Object> ProductsListByOderCode(Map<String, Object> data) {

        Map<String, Object> ret = new HashMap<>();

        List<String> ordersCode = null;
        int start = 0;
        String sql = null;
        if (data != null)
        {
            ordersCode = (List<String>) data.get("query");
            sql = "select * from mem_orderproducts where   OrdersCode='"+ ordersCode.get(0) + "'" ;
        }


        sql = EsUtils.query(sql, "_sql/translate?format=json", true);
        String from = "\"from\":" + start + ",";
        StringBuilder sb = new StringBuilder(sql);
        sb.insert(1,from);

        String str = EsUtils.query(sb.toString(), "mem_orderproducts/_search?format=json", false);

        JSONObject jsonObject = JSONObject.fromObject(str);
        JSONObject hits = jsonObject.getJSONObject("hits");
        JSONArray hitsArr = hits.getJSONArray("hits");

        List<Map<String, Object>> maps = new ArrayList<>();
        for (Object o : hitsArr) {
            Map<String, Object> map = (Map<String, Object>)o;
            Map<String, Object> fields = (Map<String, Object>) map.get("fields");
            Object source = map.get("_source");
            String id = (String) map.get("_id");
            if (null != source && !"null".equals(source.toString())){
                Map<String, Object> sourceMap = (Map<String, Object>)source;
                fields.put("ProductName",sourceMap.get("ProductName"));
                fields.put("Manufacturer",sourceMap.get("Manufacturer"));
                fields.put("IntegralAmount",sourceMap.get("IntegralAmount"));
                fields.put("Id",id);
            }

            fields.put("_id",id);
            List actualPrice = (List) fields.get("ActualPrice");
            Integer num = (Integer) actualPrice.get(0);
//            actualPrice.add(0,num > 0 ? num / 100:0);
            fields.put("ActualPrice",num > 0 ? num / 100:0);

            maps.add(fields);
        }

        sql ="select count(1) from mem_orderproducts  where  OrdersCode='"+ ordersCode.get(0) + "'" ;

        String query = EsUtils.query(sql, "_sql?format=json", true);
        JSONObject jsonObject1 = JSONObject.fromObject(query);
        JSONArray rows = jsonObject1.getJSONArray("rows");
        List<Integer> list = (List<Integer>) rows.get(0);

        ret.put("items", maps);
        ret.put("totalCount", list.get(0));
        ret.put("success", true);

        String s = JSONObject.fromObject(ret).toString();
        System.out.println(s);

        return ret;
    }
}
