package com.platform.yijia.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.platform.yijia.pojo.CustomerManage;
import com.platform.yijia.service.CustomerGradeServices;
import com.platform.yijia.service.PayOrderService;
import com.platform.yijia.service.StationService;
import com.platform.yijia.utils.CodeMsg;
import com.platform.yijia.utils.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户优惠和营销
 */
@Controller
@RequestMapping("/demo")
public class CustomerGradeController {
    private static Logger logger =(Logger) LoggerFactory.getLogger(CustomerGradeController.class);
    @Resource
    private CustomerGradeServices customerGradeServices;

    @Resource
    private PayOrderService payOrderService;

    @Resource
    private StationService stationService;

    /*
     * 查询直降客户等级条件和优惠
     * @param stationId
     * @return
     */
    @RequestMapping(value = "/getCustomerGradeList", method = RequestMethod.GET)
    @ResponseBody
    public String getCustomerGradeList(@RequestParam("stationId") String stationId){
        Gson gson =new Gson();
        ResultData resultData =null;
        if(StringUtils.isNotBlank(stationId)){
            Map params = new HashMap();
            params.put("stationId", stationId);
            String discountPlanType = stationService.getStationDiscountWay(stationId);
            if(discountPlanType.equals("1")){
                //直降表查询
                List<Map> customerGradeList = customerGradeServices.getCustomerGradeList(params);
                resultData = ResultData.success(customerGradeList);
            }else {
                //立减满减表查询
                List<Map> customerMarkertPlanList = customerGradeServices.getCustomerMarkertPlanList(params);
                resultData = ResultData.success(customerMarkertPlanList);
            }
        }else {
            resultData=ResultData.error(CodeMsg.SEARCH_FAIL);
        }
        return gson.toJson(resultData);
    }

    /*
     * 查询客户等级信息
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/getCustomerGradeInfo", method = RequestMethod.POST)
    @ResponseBody
    public String getCustomerGradeInfo(@RequestBody JSONObject jsonObject){
        Gson gson =new Gson();
        ResultData resultData =null;
        Map<String, Object> params = new HashMap<>();
        //oilName
        params.put("oilName", jsonObject.get("oilName"));
        params.put("mobilePhone", jsonObject.get("mobilePhone"));
        params.put("stationId", jsonObject.get("stationId"));
        params.put("userType", jsonObject.get("userType"));
        //用户类型
        switch (jsonObject.get("userType").toString()){
            case "1":
                params.put("blogOpenid", jsonObject.get("openId").toString());
                break;
            case "2":
                params.put("minaOpenid", jsonObject.get("openId").toString());
                break;
        }
        logger.info("查询客户等级所传参数："+params.toString());
        //优惠方案类型
        String discountPlanType = stationService.getStationDiscountWay(params.get("stationId").toString());
        if(discountPlanType != null && discountPlanType !=""){
            params.put("discountPlanType", discountPlanType);
            CustomerManage customerManage = new CustomerManage();
            customerManage.setPhoneNumber(params.get("mobilePhone").toString());
            customerManage.setOilName(params.get("oilName").toString());
            customerManage.setStationId(Integer.valueOf(params.get("stationId").toString()));
            CustomerManage existCustomer = payOrderService.isExistCustomer(customerManage);
            //如果为空则是此油品第一次下单
            if(existCustomer ==null){
                if(discountPlanType.equals("1")){
                    //如果该油站使用是直降
                    List<Map<String, Object>> customerGradeInfoList = customerGradeServices.getCustomerGradeInfo(params);
                    //只存在一种等级的时候
                    if(customerGradeInfoList !=null && customerGradeInfoList.size() ==1){
                        resultData = ResultData.success(customerGradeInfoList);
                    }else if(customerGradeInfoList !=null && customerGradeInfoList.size() >1){
                        //存在多个等级时 查询出结果按优惠的油量升序排列取第一个即可
                        resultData = ResultData.success(customerGradeInfoList.get(0));
                    }else{
                        //直降表查询 升序取第一个
                        List<Map> customerGradeList = customerGradeServices.getCustomerGradeList(params);
                        resultData = ResultData.success(customerGradeList);
                    }
                }else {
                    //此处重新赋值123的原因：station_pay表取值 1 2 3 4 分别代表 等级直降 满减 立减 直降； 而 在markert_plan 1 2 3代表 满减 立减 直降
                    switch (discountPlanType){
                        case "2":
                            params.put("discountPlanType", "1");
                            break;
                        case "3":
                            params.put("discountPlanType", "2");
                            break;
                        case "4":
                            params.put("discountPlanType", "3");
                            break;
                    }
                    //立减满减表查询站的优惠
                    List<Map> customerMarkertPlanList = customerGradeServices.getCustomerMarkertPlanList(params);
                    resultData = ResultData.success(customerMarkertPlanList);
                }
            }else{
                //否则该客户此油品已下过订单
                String memberGrade = existCustomer.getMemberGrade();
                if(memberGrade != null && memberGrade !=""){
                    params.put("grade", memberGrade);
                }
                if(discountPlanType.equals("1")){
                    //此处判断用于当该站由其他优惠改为等级直降时 （先使用其他优惠不存在等级时再改为直降方式）
                    if(memberGrade == null || memberGrade.equals("")){
                        Map map = new HashMap();
                        map.put("stationId", existCustomer.getStationId());
                        map.put("oilName", existCustomer.getOilName());
                        List<Map> customerGradeInfo = customerGradeServices.getCustomerGradeList(map);  //直降
                        for (Map m: customerGradeInfo){
                            if(Double.valueOf(customerManage.getAmt().toString()) >= Double.valueOf(m.get("memberConditStart").toString()) && Double.valueOf(customerManage.getAmt().toString()) < Double.valueOf(m.get("memberConditEnd").toString())){
                                params.put("grade", m.get("id").toString());
                                break;
                            }
                        }
                    }
                    //如果该油站使用是直降
                    List<Map<String, Object>> customerGradeInfoList = customerGradeServices.getCustomerGradeInfo(params);
                    resultData = ResultData.success(customerGradeInfoList);
                }else{
                    switch (discountPlanType){
                        case "2":
                            params.put("discountPlanType", "1");
                            break;
                        case "3":
                            params.put("discountPlanType", "2");
                            break;
                        case "4":
                            params.put("discountPlanType", "3");
                            break;
                    }
                    //立减满减表查询站的优惠
                    List<Map> customerMarkertPlanList = customerGradeServices.getCustomerMarkertPlanList(params);
                    resultData = ResultData.success(customerMarkertPlanList);
                }
            }
        }else {
            resultData = ResultData.success("该油站暂无设置优惠方案");
        }
        return gson.toJson(resultData);
    }

}
