package com.hfkj.qianzhu;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.CommonUtil;
import com.hfkj.common.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
  * @ClassName StarbucksService
  * @Author Sum1Dream
  * @Description 查询星巴克
  * @Date 2024/8/1 下午2:25
 **/
public class StarbucksService {

    private static Logger log = LoggerFactory.getLogger(QianZhuService.class);

    /**
     * @MethodName starbucksStores
     * @Description: 查询星巴克门店
     * @param map
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/8/1 下午2:19
     */
    public static JSONObject starbucksStores(Map<String , Object> map) throws Exception {

        JSONObject object = QianZhuService.requestH5("starbucksStores.listByLonAndLat" , map);

        if (object.getBoolean("success")) {
            return object;
        } else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "查询失败！");
        }

    }

    /**
     * @MethodName starbucksProducts
     * @Description: 查询星巴克产品
     * @param map
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/8/1 下午2:19
     */
    public static JSONObject starbucksProducts(Map<String , Object> map) throws Exception {

        JSONObject object = QianZhuService.requestH5("starbucksProducts.pagedQuery" , map);

        if (object.getBoolean("success")) {
            return object;
        } else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "查询失败！");
        }

    }

    public static JSONObject getTokenV2(Map<String , Object> map) throws Exception {

        JSONObject object = QianZhuService.requestToken("users.getTokenV2" , map);

        if (object.getBoolean("success")) {
            return object;
        } else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取失败！");
        }

    }

    /**
     * @MethodName starbucksOrders
     * @Description: 星巴克预下单
     * @param map 
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/8/1 下午2:20
     */
    public static JSONObject starbucksOrders(Map<String , Object> map) throws Exception {

        JSONObject object = QianZhuService.requestH5("starbucksOrders.createOrderV2" , map);

        if (object.getBoolean("success")) {
            return object;
        } else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "下单失败！");
        }

    }

    /**
     * @MethodName starbucksOrdersPay
     * @Description: 星巴克支付
     * @param map 
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/8/1 下午2:21
     */
    public static JSONObject starbucksOrdersPay(Map<String , Object> map) {
        try {
            JSONObject object = QianZhuService.requestH5("starbucksOrders.pay" , map);

            if (object.getBoolean("success")) {
                return object;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * @MethodName starbucksOrder
     * @Description: 查询星巴克订单
     * @param map
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/8/1 下午2:24
     */
    public static JSONObject starbucksOrder(Map<String , Object> map) {
        try {
            JSONObject object = QianZhuService.requestH5("starbucksOrders.getByOrderNo" , map);

            if (object.getBoolean("success")) {
                return object;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }

    }


    /**
     * @MethodName starbucksStores
     * @Description: 查询组装星巴克门店数据
     * @param map
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/8/1 下午2:19
     */
    public static List<JSONObject> starbucksStoresList(Map<String , Object> map) throws Exception {

        JSONObject object = starbucksStores(map);

        JSONArray array = object.getJSONArray("data");

        List<JSONObject> list = new ArrayList<>();

        if (!array.isEmpty()) {
            for (Object jsonArray : array) {
                JSONObject jsonObject = (JSONObject) jsonArray;
                jsonObject.put("distance" , CommonUtil.unitLengthTransform(jsonObject.getBigDecimal("distance")));
                String startDate = DateUtil.date2String(new Date() , DateUtil.Y_M_D) + " " +jsonObject.getString("startTime");
                String endDate = DateUtil.date2String(new Date() , DateUtil.Y_M_D)  + " " + jsonObject.getString("endTime");
                Date startTime = DateUtil.format(startDate , DateUtil.Y_M_D_HMS);
                Date endTime = DateUtil.format(endDate , DateUtil.Y_M_D_HMS);
                if (jsonObject.getBoolean("enabled") && DateUtil.isEffectiveDate(new Date() , startTime, endTime)) {
                    jsonObject.put("statusDescribe" , "营业中");
                    jsonObject.put("status" , true);
                } else {
                    jsonObject.put("statusDescribe" , "未营业");
                    jsonObject.put("status" , false);
                }

                list.add(jsonObject);
            }
        }

        return list;

    }


}
