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

import com.liangjian.dataplatform.member.entity.po.secondary.FiltersData;
import com.liangjian.dataplatform.member.entity.po.secondary.FiltersUtils;
import com.liangjian.dataplatform.member.jobUtil.EsUtils;
import com.liangjian.dataplatform.member.service.IPrescriptionproductService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

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

@Service
public class PrescriptionproductService implements IPrescriptionproductService {


    @Override
    public Map<String, Object> index(FiltersData data) {

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

        String code = "";
        int start = 0;
        String filter = "";
        String query = null;
        if (data != null){
            List<FiltersUtils> filters = data.getFilters();
            filter = EsUtils.getFilterString(filters);
            int pageIndex = data.getPageIndex();
            int pageSize = data.getPageSize();
            if (pageIndex == 1){
                start = 0;
            }else{
                start = ((pageIndex - 1) * pageSize) + 1;
            }
            code = getPrescriptionOderscode(data, filter);
            //根据会员电话号码，先找到他的所有订单
            if (code == "") {
                ret.put("items",new ArrayList());
                ret.put("totalCount",0);
                return ret;
            }
//            //找到属于处方产品的订单
            query = "select * from mem_prescription where  OrdersCode in(" + code + ") " +
                " order by OrdersCode " +
                " limit "+pageSize ;
        }
        String sql = EsUtils.query(query, "_sql/translate?format=json", true);
        String from = "\"from\":" + start + ",";
        StringBuilder sb = new StringBuilder(sql);
        sql = sb.insert(1, from).toString();
        String str = EsUtils.query(sql, "mem_prescription/_search?format=json", false);

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


        long totalcount = 0;
        String countdata = "select count(1) from mem_prescription where   OrdersCode in(" + code + ") " ;
        String count = EsUtils.query(countdata, "_sql?format=json", true);
        List<List<String>> rows = (List<List<String>>) JSONObject.fromObject(count).get("rows");


        ret.put("items",jsonArray);
        ret.put("totalCount",rows.get(0).get(0));
        return ret;
    }

    public static String getPrescriptionOderscode(FiltersData data, String filter){
        String code = "";

        String query = "select OrdersCode from mem_order where  1=1 " + filter + "" ;
        String orderstr = EsUtils.query(query,"_sql?format=json", true);

        JSONObject jsonObject = JSONObject.fromObject(orderstr);
        List<List<String>> rows = (List<List<String>>) jsonObject.get("rows");

        for (int i = rows.size()-1; i >= 0; i--) {
            List<String> number = rows.get(i);
            if (i > 0){
                code += "'" + number.get(0) + "',";
            }else{
                code += "'" + number.get(0) + "'";
            }
        }
        return code;
    }

}
