package com.palatform.yijia;
import com.platform.yijia.YijiaApplication;
import com.platform.yijia.pojo.*;
import com.platform.yijia.service.CustomerPointsRecordService;
import com.platform.yijia.service.CustomerPointsService;
import com.platform.yijia.service.IntegralRuleService;
import com.platform.yijia.service.StationService;
import com.platform.yijia.utils.weixinapp.Token;
import com.platform.yijia.utils.weixinapp.WeiXinUserUtil;
import com.platform.yijia.utils.weixinapp.WxPushUtil;
import javafx.application.Application;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import net.sf.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = YijiaApplication.class)
public  class YijiaApplicationTests {

    @Resource
    private IntegralRuleService integralRuleService;
    @Resource
    private CustomerPointsService customerPointsService;
    @Resource
    private CustomerPointsRecordService customerPointsRecordService;
    @Resource
    private StationService stationService;
    @Resource
    private WxPushUtil wxPushUtil;
    @Resource
    private WeiXinUserUtil weiXinUserUtil;
//    @Resource
//    private CustomerManage customerManage;

    @Test
    public void testIntegral() {
        String str = "2021-01-01";
        //String str = "2021-01-01, 2021-01-02";

//        String [] datePicker={};     //积分比例时间
//        if(str != null && str !=""){
//            if(str.contains(",")){
//                datePicker = str.split(",");
//            }else {
//                datePicker= new String[1];
//                datePicker[0] =str;
//            }
//
//        }
//        System.out.println(datePicker);



        BigDecimal b1 = new BigDecimal(0.01);

        BigDecimal integral = b1.divide(new BigDecimal(0.01), 0).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_DOWN);
        System.out.println(Integer.valueOf(integral.toString()));
        System.out.println(integral);


//        Map<String, Object> userSessionKeyAndOpenID = weiXinUserUtil.getUserSessionKeyAndOpenID("wxe1135cd390b38a54", "f5a7cf7318cf8638b85bd0fa38f800d0", "003nzx000zeDkL1ssD0001GMQT3nzx0W");
//        System.out.println(userSessionKeyAndOpenID);

        //小程序 ："wxe1135cd390b38a54", "f5a7cf7318cf8638b85bd0fa38f800d0"
        //公众号：wxc8189d3b3a7283e0 3f18fc7e4b02de2f8a14f55b55ab6245
//        String token = weiXinUserUtil.getTokenByRedisCache("wxe1135cd390b38a54", "f5a7cf7318cf8638b85bd0fa38f800d0");
//        System.out.println("token信息："+token);
//
//        //公众号：oJR5R6r4EJhaORFcPap70r_mtFZo
//        //小程序：oYdrL5Sn0YaqiksSthiZMl5cq9FA
//        JSONObject unionIdInfo = weiXinUserUtil.getUnionIdInfo(token, "oYdrL5Sn0YaqiksSthiZMl5cq9FA");
//        System.out.println("unionId信息："+ unionIdInfo);




    /*//================================================================================================================================
        CustomerManage customerManage = new CustomerManage();
        //customerManage.setGrade("铜牌");
        String discountPlanType ="1";
        Map infoMap = new HashMap();
        infoMap.put("stationId", 1);
        infoMap.put("oilName", "92#");
        infoMap.put("consumer", "M");
        infoMap.put("mobilePhone", "18654585051");
        infoMap.put("userType", "2");
        infoMap.put("amt", "200");
        infoMap.put("receivableAmt", "300");
        infoMap.put("orderLiters", "55");



        //客户积分
        IntegralRule integralRule  =new IntegralRule();
        integralRule.setStationId(Integer.valueOf(infoMap.get("stationId").toString()));
        integralRule.setOilName(infoMap.get("oilName").toString());
        //获取该油站的积分规则
        List<IntegralRule> rule = integralRuleService.getIntegralRule(integralRule);
        if(rule !=null && rule.size() > 0 && rule.get(0).getEmptyDate().compareTo(new Date()) ==1){
            CustomerPoints customerPoints = new CustomerPoints();       //客户积分实体表
            CustomerPointsRecord customerPointsRecord = new CustomerPointsRecord();     //客户积分记录实体表
            if(infoMap.containsKey("unionId")){
                customerPoints.setUnionId(infoMap.get("unionId").toString());
                customerPointsRecord.setUnionId(infoMap.get("unionId").toString());
            }
            customerPoints.setCustomerName(infoMap.get("consumer").toString());
            customerPoints.setMobilePhone(infoMap.get("mobilePhone").toString());   //存入手机号

            customerPointsRecord.setCustomerName(infoMap.get("consumer").toString());       //客户名称
            customerPointsRecord.setRecordType("+");            // "+"表示增加积分
            customerPointsRecord.setStationId(Integer.valueOf(infoMap.get("stationId").toString()));
            customerPointsRecord.setCreateTime(new Date());

            if(infoMap.containsKey("userType") && infoMap.get("userType") !=null){
                switch (infoMap.get("userType").toString()){
                    case "1":
                        customerPoints.setBlogOpenId(infoMap.get("blogOpenid").toString());
                        break;
                    case "2":
                        customerPoints.setMinaOpenId(infoMap.get("minaOpenid").toString());
                        break;
                }
            }

            //获取该用户积分信息
            CustomerPoints customerPointsInfo = customerPointsService.getCustomerPointsInfo(customerPoints);
            if(customerPointsInfo !=null){
                //String ruleType = rule.get(0).getRuleType();   //获取积分规则类型
                String[] datePicker={};     //积分比例时间
                if(rule.get(0).getDatePicker() != null && rule.get(0).getDatePicker() !=""){
                    datePicker = rule.get(0).getDatePicker().split(",");
                }
                String currentTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); //当前时间
                List<IntegralRuleDetail> integralRuleDetailList = rule.get(0).getIntegralRuleDetailList();  //积分规则详细计算信息
                if(integralRuleDetailList !=null && integralRuleDetailList.size() > 0){
                    BigDecimal realAmt = new BigDecimal(infoMap.get("amt").toString());  //本次消费实收金额
                    BigDecimal receivableAmt = new BigDecimal(infoMap.get("receivableAmt").toString());  //本次消费实收金额
                    BigDecimal orderLiters = new BigDecimal(infoMap.get("orderLiters").toString());    //本次加油升数
                    //循环规则明细
                    for(IntegralRuleDetail integralRuleDetail : integralRuleDetailList){
                        //规则类型
                        String ruleType = integralRuleDetail.getRuleType();
                        if(ruleType !=null && ruleType.equals("1") && realAmt.compareTo(integralRuleDetail.getRuleTerms()) ==1){
                            //以实收金额为准时
                            if(discountPlanType == "1" && integralRuleDetail.getGread() !=null && customerManage.getGrade().equals(integralRuleDetail.getGread())){
                                //计算积分并取整
                                BigDecimal integral = realAmt.divide(integralRuleDetail.getSaleAmt(), 0).multiply(integralRuleDetail.getIntegral()).setScale(0, BigDecimal.ROUND_DOWN);
                                //设置积分多倍时间时
                                if(datePicker !=null){
                                    for(String str: datePicker){
                                        //如果当前时间在改日期比例时间内
                                        if (str.contains(currentTime) && rule.get(0).getIntegralProportion() != null){
                                            integral=integral.multiply(new BigDecimal(rule.get(0).getIntegralProportion()));
                                            break;
                                        }
                                    }
                                }
                                customerPoints.setPoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPoints.setAccumulatePoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPointsRecord.setIntegral(Integer.valueOf(integral.toString()));
                                break;
                            }else {
                                //计算积分并取整
                                BigDecimal integral = realAmt.divide(integralRuleDetail.getSaleAmt(), 0).multiply(integralRuleDetail.getIntegral()).setScale(0, BigDecimal.ROUND_DOWN);
                                //设置积分多倍时间时
                                if(datePicker !=null){
                                    for(String str: datePicker){
                                        if (str.contains(currentTime) && rule.get(0).getIntegralProportion() != null){
                                            integral=integral.multiply(new BigDecimal(rule.get(0).getIntegralProportion()));
                                            break;
                                        }
                                    }
                                }
                                customerPoints.setPoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPoints.setAccumulatePoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPointsRecord.setIntegral(Integer.valueOf(integral.toString()));
                                break;
                            }
                        }else if(ruleType !=null && ruleType.equals("2") && receivableAmt.compareTo(integralRuleDetail.getRuleTerms()) ==1){
                            //应收金额为准时
                            if(discountPlanType == "1" && integralRuleDetail.getGread() !=null && customerManage.getGrade().equals(integralRuleDetail.getGread())){
                                //计算积分并取整
                                BigDecimal integral = receivableAmt.divide(integralRuleDetail.getSaleAmt(), 0).multiply(integralRuleDetail.getIntegral()).setScale(0, BigDecimal.ROUND_DOWN);
                                //设置积分多倍时间时
                                if(datePicker !=null){
                                    for(String str: datePicker){
                                        if (str.contains(currentTime) && rule.get(0).getIntegralProportion() != null){
                                            integral=integral.multiply(new BigDecimal(rule.get(0).getIntegralProportion()));
                                            break;
                                        }
                                    }
                                }
                                customerPoints.setPoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPoints.setAccumulatePoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPointsRecord.setIntegral(Integer.valueOf(integral.toString()));
                                break;
                            }else {
                                //计算积分并取整
                                BigDecimal integral = receivableAmt.divide(integralRuleDetail.getSaleAmt(), 0).multiply(integralRuleDetail.getIntegral()).setScale(0, BigDecimal.ROUND_DOWN);
                                //设置积分多倍时间时
                                if(datePicker !=null){
                                    for(String str: datePicker){
                                        if (str.contains(currentTime) && rule.get(0).getIntegralProportion() != null){
                                            integral=integral.multiply(new BigDecimal(rule.get(0).getIntegralProportion()));
                                            break;
                                        }
                                    }
                                }
                                customerPoints.setPoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPoints.setAccumulatePoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPointsRecord.setIntegral(Integer.valueOf(integral.toString()));
                                break;
                            }

                        }else if(ruleType !=null && ruleType.equals("3") && orderLiters.compareTo(integralRuleDetail.getRuleTerms()) ==1){
                            //加油升数为准时
                            if(discountPlanType == "1" && integralRuleDetail.getGread() !=null && customerManage.getGrade().equals(integralRuleDetail.getGread())){
                                //计算积分并取整
                                BigDecimal integral = orderLiters.divide(integralRuleDetail.getSaleAmt(), 0).multiply(integralRuleDetail.getIntegral()).setScale(0, BigDecimal.ROUND_DOWN);
                                //设置积分多倍时间时
                                if(datePicker !=null){
                                    for(String str: datePicker){
                                        if (str.contains(currentTime) && rule.get(0).getIntegralProportion() !=null ){
                                            integral=integral.multiply(new BigDecimal(rule.get(0).getIntegralProportion()));
                                            break;
                                        }
                                    }
                                }
                                customerPoints.setPoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPoints.setAccumulatePoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPointsRecord.setIntegral(Integer.valueOf(integral.toString()));
                                break;
                            }else {
                                //计算积分并取整
                                BigDecimal integral = orderLiters.divide(integralRuleDetail.getSaleAmt(), 0).multiply(integralRuleDetail.getIntegral()).setScale(0, BigDecimal.ROUND_DOWN);
                                //设置积分多倍时间时
                                if(datePicker !=null){
                                    for(String str: datePicker){
                                        if (str.contains(currentTime) && rule.get(0).getIntegralProportion() !=null ){
                                            integral=integral.multiply(new BigDecimal(rule.get(0).getIntegralProportion()));
                                            break;
                                        }
                                    }
                                }
                                customerPoints.setPoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPoints.setAccumulatePoints(customerPointsInfo.getPoints() + Integer.valueOf(integral.toString()));
                                customerPointsRecord.setIntegral(Integer.valueOf(integral.toString()));
                                break;
                            }
                        }
                    }
                }

                //更新客户余额积分
                customerPointsService.updateCustomerPointsInfo(customerPoints);
                //插入客户积分记录表
                customerPointsRecordService.insertCustomerPointsInfo(customerPointsRecord);

                //客户公众号积分增加通知
                String gzhAppId ="";
                String gzhAppSecret ="";
                String stationName = "";        //油站名称
                Map<String, String> m = stationService.getStationAppIdAndAppSecret(Integer.valueOf(infoMap.get("stationId").toString()));
                if(m !=null && m.containsKey("stationName") && m.containsKey("gzhAppId") && m.containsKey("gzhAppSecret")){
                    stationName = m.get("stationName");
                    gzhAppId = m.get("gzhAppId");
                    gzhAppSecret = m.get("gzhAppSecret");
                }
                List<WxMpTemplateData> wxMpTemplate = new ArrayList<>();
                wxMpTemplate.add(new WxMpTemplateData("first","尊敬的"+infoMap.get("consumer").toString()+"，您好：\n" + "您在"+stationName+"的积分最新交易信息如下"));
                wxMpTemplate.add(new WxMpTemplateData("time", new SimpleDateFormat("yyyy年MM月dd日HH时mm分").format(new Date())));
                wxMpTemplate.add(new WxMpTemplateData("type", "增加"));
                wxMpTemplate.add(new WxMpTemplateData("Point", customerPointsRecord.getIntegral().toString()));
                wxMpTemplate.add(new WxMpTemplateData("From", stationName));
                wxMpTemplate.add(new WxMpTemplateData("remark",
                        "截止至" + new SimpleDateFormat(" yyyy年MM月dd日HH时mm分").format(new Date())+"，您在"+stationName+"的可用积分为 "+customerPoints.getPoints().toString()+" 分"));
                String templateId = "G9tN--a3tGM5BN3otqZr73b4ErQCYWISSgyshbTqLYc";  //积分兑换成功通知模板
                wxPushUtil.push(gzhAppId, gzhAppSecret, templateId, "oJR5R6r4EJhaORFcPap70r_mtFZo", wxMpTemplate);
            }
        }

*///========================================================================================================================================================//







//        WxPushUtil wxPushUtil = new WxPushUtil();
//        String appId ="wxc8189d3b3a7283e0";
//        String appSecret ="3f18fc7e4b02de2f8a14f55b55ab6245";
//        String templateId ="CPZ8l9FyIA30jq0rkm5uCozySSVwFO6gOvnFSWvCSrI";
//        String openId ="oJR5R6r4EJhaORFcPap70r_mtFZo";
//        List<WxMpTemplateData> wxMpTemplate = new ArrayList<>();
//        wxMpTemplate.add(new WxMpTemplateData("first","李先生，您好！您的本次消费明细如下：", "#363636"));
//        wxMpTemplate.add(new WxMpTemplateData("keyword1", "宁欣门店1", "#1C1C1C"));
//        wxMpTemplate.add(new WxMpTemplateData("keyword2", "2个", "#0000CD"));
//        wxMpTemplate.add(new WxMpTemplateData("keyword3", "168.8元", "#0000CD"));
//        wxMpTemplate.add(new WxMpTemplateData("keyword4", new Date().toString(), "#0000CD"));
//        wxMpTemplate.add(new WxMpTemplateData("keyword5", "2014120900189", "#0000CD"));
//        wxMpTemplate.add(new WxMpTemplateData("remark", "欢迎您再次光临!", "#0000CD"));
//        wxPushUtil.push(appId, appSecret, templateId, openId, wxMpTemplate);
    }

}
