package com.miaxis.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.exam.domain.ExamInfo;
import com.miaxis.exam.service.IExamInfoService;
import com.miaxis.examvip.domain.VipUserExam;
import com.miaxis.examvip.service.IVipUserExamService;
import com.miaxis.newgzpt.domain.GzptSchActivation;
import com.miaxis.newgzpt.domain.GzptSchPayLog;
import com.miaxis.newgzpt.domain.GzptUserInfo;
import com.miaxis.newgzpt.domain.GzptUserInfoDel;
import com.miaxis.newgzpt.service.*;
import com.miaxis.order.domain.OrderInfo;
import com.miaxis.order.dto.QuerySchoolOrderListDTO;
import com.miaxis.order.mapper.OrderInfoMapper;
import com.miaxis.order.service.IOrderInfoService;
import com.miaxis.order.vo.OrderIncomeVo;
import com.miaxis.order.vo.QuerySchoolOrderDetailedVo;
import com.miaxis.order.vo.QuerySchoolOrderListVo;
import com.miaxis.userInfo.domain.UserVip;
import com.miaxis.userInfo.service.IUserVipService;
import com.miaxis.wx.domain.WxJsOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单信息Service业务层处理
 *
 * @author miaxis
 * @date 2022-06-01
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private IUserVipService userVipService;

    @Autowired
    private IGzptUserInfoService userInfoService;

    @Autowired
    private IGzptUserInfoDelService userInfoDelService;


    @Autowired
    private IGzptSchActivationService schActivationService;

    @Autowired
    private IGzptSchPayLogService schPayLogService;

    @Autowired
    private IGzptSchPayConfigService schPayConfigService;

    @Autowired
    private IExamInfoService examInfoService;

    @Autowired
    private IVipUserExamService vipUserExamService;

    /**
     * 查询订单信息列表
     *
     * @param orderInfo 订单信息
     * @return 订单信息
     */
    @Override
    public List<OrderInfo> selectOrderInfoList(OrderInfo orderInfo) {
        return orderInfoMapper.selectOrderInfoList(orderInfo);
    }



    @Override
    public OrderInfo getByOutTradeNo(String outTradeNo) {
        return orderInfoMapper.getByOutTradeNo(outTradeNo);
    }



    public void writeVipDataToDb(WxJsOrder order, int payType) {
        long oneYearLong = 1000 * 60 * 60 * 24 * 365l;
        Date now = new Date();

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setGoodsName(order.getGoodsName());
        orderInfo.setGoodsType(order.getGoodsType());
        orderInfo.setUserId(order.getUserId());
        orderInfo.setUserName(order.getUserName());
        orderInfo.setLogincode(order.getLogincode());
        orderInfo.setOutTradeNo(order.getOutTradeNo());
        orderInfo.setTotal(order.getTotal());
        orderInfo.setTradeType(1);
        orderInfo.setSuccessTime(order.getSuccessTime());
        orderInfo.setPayType(payType);
        orderInfo.setDqbh(order.getDqbh());
        orderInfo.setDqmc(order.getDqmc());
        orderInfo.setSchool(order.getSchool());
        orderInfo.setSchoolName(order.getSchoolName());
        orderInfo.setSchoolCommission(order.getSchoolCommission());
        orderInfo.setPhoneType(order.getPhoneType());
        orderInfo.setIsShare(order.getIsShare());
        orderInfo.setCreateId(order.getCreateId());

        GzptUserInfo userInfo = userInfoService.getInfoById(order.getUserId());
        int isActiva = schActivationService.isSchActivation(order.getUserId());
        if ("理论实操计时".equals(order.getGoodsName())) {
            //激活操作
            if(isActiva==0) {  //未激活
                saveActiva(order, userInfo);
                this.save(orderInfo);
            } else if (isActiva==2) {  //激活过，退款
                schActivationService.upDelSchActivationByUserId(order.getUserId(),0);
                this.save(orderInfo);
            }
        } else if ("科一专项VIP(含计时)".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km1Date = userVip.getSubject1();
                if (km1Date != null && now.compareTo(km1Date)<0) {  //存在会员时间
                    long x = km1Date.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date km1oneYear = new Date(x);
                uv.setSubject1(km1oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
            //激活操作
            if(isActiva!=1) {
                saveActiva(order, userInfo);
            }
        } else if ("科一专项VIP".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km1Date = userVip.getSubject1();
                if (km1Date != null && now.compareTo(km1Date)<0) {  //存在会员时间
                    long x = km1Date.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date km1oneYear = new Date(x);
                uv.setSubject1(km1oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }

        } else if ("科二专项VIP(含计时)".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km2Date = userVip.getSubject2();
                if (km2Date != null && now.compareTo(km2Date)<0) {  //存在会员时间
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date km2oneYear = new Date(x);
                uv.setSubject2(km2oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
            //激活操作
            if(isActiva!=1) {
                saveActiva(order, userInfo);
            }
        } else if ("科二专项VIP".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km2Date = userVip.getSubject2();
                if (km2Date != null && now.compareTo(km2Date)<0) {  //存在会员时间
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date km2oneYear = new Date(x);
                uv.setSubject2(km2oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("科三专项VIP(含计时)".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km3Date = userVip.getSubject3();
                if (km3Date != null && now.compareTo(km3Date)<0) {  //存在会员时间
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date km3oneYear = new Date(x);
                uv.setSubject3(km3oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
            //激活操作
            if(isActiva!=1) {
                saveActiva(order, userInfo);
            }
        } else if ("科三专项VIP".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km3Date = userVip.getSubject3();
                if (km3Date != null && now.compareTo(km3Date)<0) {  //存在会员时间
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date km3oneYear = new Date(x);
                uv.setSubject3(km3oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }

        } else if ("科四专项VIP(含计时)".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km4Date = userVip.getSubject4();
                if (km4Date != null && now.compareTo(km4Date)<0) {  //存在会员时间
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date km4oneYear = new Date(x);
                uv.setSubject4(km4oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
            //激活操作
            if(isActiva!=1) {
                saveActiva(order, userInfo);
            }
        } else if ("科四专项VIP".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km4Date = userVip.getSubject4();
                if (km4Date != null && now.compareTo(km4Date)<0) {  //存在会员时间 未过期
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date km4oneYear = new Date(x);
                uv.setSubject4(km4oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }

        } else if ("科目一二三四所有付费权益".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km1Date = userVip.getSubject1();
                Date km2Date = userVip.getSubject2();
                Date km3Date = userVip.getSubject3();
                Date km4Date = userVip.getSubject4();
                if (km1Date != null && now.compareTo(km1Date)<0) { //科目一处理 会员还没有过期，用这个判断
                    long x = km1Date.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                }
                if (km2Date != null && now.compareTo(km2Date)<0) { //科目二处理
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                }
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject1(oneYear);
                uv.setSubject2(oneYear);
                uv.setSubject3(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("科一+科四VIP(含计时)".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km1Date = userVip.getSubject1();
                Date km4Date = userVip.getSubject4();
                if (km1Date != null && now.compareTo(km1Date)<0) { //科目一处理
                    long x = km1Date.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject1(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
            //激活操作
            if(isActiva!=1) {
                saveActiva(order, userInfo);
            }
        } else if ("科一+科四VIP".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km1Date = userVip.getSubject1();
                Date km4Date = userVip.getSubject4();
                if (km1Date != null && now.compareTo(km1Date)<0) { //科目一处理
                    long x = km1Date.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject1(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("科二+科三VIP(含计时)".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km2Date = userVip.getSubject2();
                Date km3Date = userVip.getSubject3();
                if (km2Date != null && now.compareTo(km2Date)<0) { //科目二处理
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                }
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject2(oneYear);
                uv.setSubject3(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
            //激活操作
            if(isActiva!=1) {
                saveActiva(order, userInfo);
            }
        } else if ("科二+科三VIP".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km2Date = userVip.getSubject2();
                Date km3Date = userVip.getSubject3();
                if (km2Date != null && now.compareTo(km2Date)<0) { //科目二处理
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                }
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject2(oneYear);
                uv.setSubject3(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("科二+科三+科四(含计时)".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km2Date = userVip.getSubject2();
                Date km3Date = userVip.getSubject3();
                Date km4Date = userVip.getSubject4();
                if (km2Date != null && now.compareTo(km2Date)<0) { //科目二处理
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                }
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject2(oneYear);
                uv.setSubject3(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
            //激活操作
            if(isActiva!=1) {
                saveActiva(order, userInfo);
            }
        } else if ("科二+科三+科四".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km2Date = userVip.getSubject2();
                Date km3Date = userVip.getSubject3();
                Date km4Date = userVip.getSubject4();
                if (km2Date != null && now.compareTo(km2Date)<0) { //科目二处理
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                }
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject2(oneYear);
                uv.setSubject3(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("科三+科四VIP(含计时)".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km3Date = userVip.getSubject3();
                Date km4Date = userVip.getSubject4();
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject3(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
            //激活操作
            if(isActiva!=1) {
                saveActiva(order, userInfo);
            }
        } else if ("科三+科四VIP".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km3Date = userVip.getSubject3();
                Date km4Date = userVip.getSubject4();
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject3(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("全科目超级VIP(含计时)".equals(order.getGoodsName())){
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km1Date = userVip.getSubject1();
                Date km2Date = userVip.getSubject2();
                Date km3Date = userVip.getSubject3();
                Date km4Date = userVip.getSubject4();

                if (km1Date != null && now.compareTo(km1Date)<0) { //科目一处理
                    long x = km1Date.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                }
                if (km2Date != null && now.compareTo(km2Date)<0) { //科目二处理
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                }
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject1(oneYear);
                uv.setSubject2(oneYear);
                uv.setSubject3(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
            //激活操作
            if(isActiva!=1) {
                saveActiva(order, userInfo);
            }
        } else if ("全科目超级VIP".equals(order.getGoodsName())) {
            UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
            if (userVip != null) {
                //修改会员信息
                Date km1Date = userVip.getSubject1();
                Date km2Date = userVip.getSubject2();
                Date km3Date = userVip.getSubject3();
                Date km4Date = userVip.getSubject4();
                if (km1Date != null && now.compareTo(km1Date)<0) { //科目一处理
                    long x = km1Date.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                }
                if (km2Date != null && now.compareTo(km2Date)<0) { //科目二处理
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                }
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(order.getUserId());
                uv.setUserName(order.getUserName());
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject1(oneYear);
                uv.setSubject2(oneYear);
                uv.setSubject3(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        }
        //如果是合作驾校插入消费记录表
        int isShare = schPayConfigService.isSchPayConfigByUser(userInfo);
        if (isShare == 1) {
            savePayLog(order, userInfo,1);
        }
    }

    @Override
    public void writeExamDataToDb(WxJsOrder order, int payType) {
        long oneYearLong = 1000 * 60 * 60 * 24 * 365l;

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setGoodsId(order.getGoodsId());
        orderInfo.setGoodsName(order.getGoodsName());
        orderInfo.setGoodsType(order.getGoodsType());
        orderInfo.setUserId(order.getUserId());
        orderInfo.setUserName(order.getUserName());
        orderInfo.setLogincode(order.getLogincode());
        orderInfo.setIsShare(order.getIsShare());
        orderInfo.setOutTradeNo(order.getOutTradeNo());
        orderInfo.setTotal(order.getTotal());
        orderInfo.setTradeType(1);
        orderInfo.setSuccessTime(order.getSuccessTime());
        orderInfo.setPayType(payType);
        orderInfo.setPhoneType(order.getPhoneType());
        orderInfo.setPayType(payType);
        orderInfo.setDqbh(order.getDqbh());
        orderInfo.setDqmc(order.getDqmc());
        orderInfo.setSchool(order.getSchool());
        orderInfo.setSchoolName(order.getSchoolName());
        orderInfo.setCreateId(order.getCreateId());

        if(order.getGoodsType()==2) { //考场
            //添加会员信息
            ExamInfo examInfo = examInfoService.getById(order.getGoodsId());
            VipUserExam vipUserExam = new VipUserExam();
            vipUserExam.setUserId(order.getUserId());
            vipUserExam.setName(order.getUserName());
            vipUserExam.setLogincode(order.getLogincode());
            vipUserExam.setExamId(examInfo.getId());
            vipUserExam.setExamName(examInfo.getName());
            Date now = new Date();
            long x = now.getTime() + oneYearLong;
            Date oneYear = new Date(x);
            vipUserExam.setExpirationTime(oneYear);
            vipUserExamService.save(vipUserExam);
            this.save(orderInfo);
        } else if(order.getGoodsType()==3) { //商品
            //添加会员信息
        }
    }


    @Override
    public void writeVipDataToDb(String goodsName, Long userId, String userName, String outTrandeNo, Integer total, Integer payType) {
        long oneYearLong = 1000 * 60 * 60 * 24 * 365l;
        Date now = new Date();

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setGoodsName(goodsName);
        orderInfo.setUserId(userId);
        orderInfo.setOutTradeNo(outTrandeNo);
        orderInfo.setTotal(total);
        orderInfo.setTradeType(1);
        orderInfo.setSuccessTime(now);
        orderInfo.setPayType(payType);


        if ("科一专项VIP".equals(goodsName)) {
            UserVip userVip = userVipService.getUserVipByUserId(userId);
            if (userVip != null) {
                //修改会员信息
                Date km1Date = userVip.getSubject1();
                if (km1Date != null && now.compareTo(km1Date)<0) {  //存在会员时间
                    long x = km1Date.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(userId);
                uv.setUserName(userName);
                long x = now.getTime() + oneYearLong;
                Date km1oneYear = new Date(x);
                uv.setSubject1(km1oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }

        } else if ("科二专项VIP".equals(goodsName)) {
            UserVip userVip = userVipService.getUserVipByUserId(userId);
            if (userVip != null) {
                //修改会员信息
                Date km2Date = userVip.getSubject2();
                if (km2Date != null && now.compareTo(km2Date)<0) {  //存在会员时间
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(userId);
                uv.setUserName(userName);
                long x = now.getTime() + oneYearLong;
                Date km2oneYear = new Date(x);
                uv.setSubject2(km2oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("科三专项VIP".equals(goodsName)) {
            UserVip userVip = userVipService.getUserVipByUserId(userId);
            if (userVip != null) {
                //修改会员信息
                Date km3Date = userVip.getSubject3();
                if (km3Date != null && now.compareTo(km3Date)<0) {  //存在会员时间
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(userId);
                uv.setUserName(userName);
                long x = now.getTime() + oneYearLong;
                Date km3oneYear = new Date(x);
                uv.setSubject3(km3oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }

        } else if ("科四专项VIP".equals(goodsName)) {
            UserVip userVip = userVipService.getUserVipByUserId(userId);
            if (userVip != null) {
                //修改会员信息
                Date km4Date = userVip.getSubject4();
                if (km4Date != null && now.compareTo(km4Date)<0) {  //存在会员时间
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                    userVipService.updateById(userVip);
                    this.save(orderInfo);
                }
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(userId);
                uv.setUserName(userName);
                long x = now.getTime() + oneYearLong;
                Date km4oneYear = new Date(x);
                uv.setSubject4(km4oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }

        } else if ("科目一二三四所有付费权益".equals(goodsName)) {
            UserVip userVip = userVipService.getUserVipByUserId(userId);
            if (userVip != null) {
                //修改会员信息
                Date km1Date = userVip.getSubject1();
                Date km2Date = userVip.getSubject2();
                Date km3Date = userVip.getSubject3();
                Date km4Date = userVip.getSubject4();
                if (km1Date != null && now.compareTo(km1Date)<0) { //科目一处理
                    long x = km1Date.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                }
                if (km2Date != null && now.compareTo(km2Date)<0) { //科目二处理
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                }
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                if (km4Date != null && now.compareTo(km3Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(userId);
                uv.setUserName(userName);
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject1(oneYear);
                uv.setSubject2(oneYear);
                uv.setSubject3(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("科一+科四VIP".equals(goodsName)) {
            UserVip userVip = userVipService.getUserVipByUserId(userId);
            if (userVip != null) {
                //修改会员信息
                Date km1Date = userVip.getSubject1();
                Date km4Date = userVip.getSubject4();
                if (km1Date != null && now.compareTo(km1Date)<0) { //科目一处理
                    long x = km1Date.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(userId);
                uv.setUserName(userName);
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject1(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("科二+科三VIP".equals(goodsName)) {
            UserVip userVip = userVipService.getUserVipByUserId(userId);
            if (userVip != null) {
                //修改会员信息
                Date km2Date = userVip.getSubject2();
                Date km3Date = userVip.getSubject3();
                if (km2Date != null && now.compareTo(km2Date)<0) { //科目二处理
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                }
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(userId);
                uv.setUserName(userName);
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject2(oneYear);
                uv.setSubject3(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("科二+科三+科四".equals(goodsName)) {
            UserVip userVip = userVipService.getUserVipByUserId(userId);
            if (userVip != null) {
                //修改会员信息
                Date km2Date = userVip.getSubject2();
                Date km3Date = userVip.getSubject3();
                Date km4Date = userVip.getSubject4();
                if (km2Date != null && now.compareTo(km2Date)<0) { //科目二处理
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                }
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(userId);
                uv.setUserName(userName);
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject2(oneYear);
                uv.setSubject3(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("科三+科四VIP".equals(goodsName)) {
            UserVip userVip = userVipService.getUserVipByUserId(userId);
            if (userVip != null) {
                //修改会员信息
                Date km3Date = userVip.getSubject3();
                Date km4Date = userVip.getSubject4();
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(userId);
                uv.setUserName(userName);
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject3(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        } else if ("全科目超级VIP".equals(goodsName)) {
            UserVip userVip = userVipService.getUserVipByUserId(userId);
            if (userVip != null) {
                //修改会员信息
                Date km1Date = userVip.getSubject1();
                Date km2Date = userVip.getSubject2();
                Date km3Date = userVip.getSubject3();
                Date km4Date = userVip.getSubject4();
                if (km1Date != null  && now.compareTo(km1Date)<0) { //科目一处理
                    long x = km1Date.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km1oneYear = new Date(x);
                    userVip.setSubject1(km1oneYear);
                }
                if (km2Date != null && now.compareTo(km2Date)<0) { //科目二处理
                    long x = km2Date.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km2oneYear = new Date(x);
                    userVip.setSubject2(km2oneYear);
                }
                if (km3Date != null && now.compareTo(km3Date)<0) { //科目三处理
                    long x = km3Date.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km3oneYear = new Date(x);
                    userVip.setSubject3(km3oneYear);
                }
                if (km4Date != null && now.compareTo(km4Date)<0) { //科目四处理
                    long x = km4Date.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                } else {
                    long x = now.getTime() + oneYearLong;
                    Date km4oneYear = new Date(x);
                    userVip.setSubject4(km4oneYear);
                }
                userVipService.updateById(userVip);
                this.save(orderInfo);
            } else {
                //新增会员信息
                UserVip uv = new UserVip();
                uv.setUserId(userId);
                uv.setUserName(userName);
                long x = now.getTime() + oneYearLong;
                Date oneYear = new Date(x);
                uv.setSubject1(oneYear);
                uv.setSubject2(oneYear);
                uv.setSubject3(oneYear);
                uv.setSubject4(oneYear);
                userVipService.save(uv);
                this.save(orderInfo);
            }
        }

    }

    @Override
    public List<QuerySchoolOrderListVo> selectSchoolOrderInfoList(QuerySchoolOrderListDTO schoolOrderListDTO) {
        return orderInfoMapper.selectSchoolOrderInfoList(schoolOrderListDTO);
    }

    @Override
    public List<OrderInfo> selectSchoolOrderDetailed(QuerySchoolOrderListDTO schoolOrderListDTO) {
        return orderInfoMapper.selectSchoolOrderDetailed(schoolOrderListDTO);
    }

    @Override
    public List<QuerySchoolOrderDetailedVo> selectSchoolOrderDetailedExcel(QuerySchoolOrderListDTO schoolOrderListDTO) {
        return orderInfoMapper.selectSchoolOrderDetailedExcel(schoolOrderListDTO);
    }

    @Override
    public Integer getTotalMoney(QuerySchoolOrderListDTO orderInfo) {
        return orderInfoMapper.getTotalMoney(orderInfo);
    }


    @Override
    public Integer getCommissionMoney(QuerySchoolOrderListDTO orderInfo) {
        return orderInfoMapper.getCommissionMoney(orderInfo);
    }

    @Override
    public List<QuerySchoolOrderListVo> selectSchoolOrderInfoExcel(QuerySchoolOrderListDTO schoolOrderListDTO) {
        return orderInfoMapper.selectSchoolOrderInfoExcel(schoolOrderListDTO);
    }

    @Override
    public List<OrderIncomeVo> getIncomeList(QuerySchoolOrderListDTO schoolOrderListDTO) {
        return orderInfoMapper.getIncomeList(schoolOrderListDTO);
    }

    /**
     * 退款方法
     * @param order
     */
    public void refundOrder(WxJsOrder order) {

        switch (order.getGoodsType()) {
            case 1:
                long oneYearLong = 1000 * 60 * 60 * 24 * 365l;
                GzptUserInfo userInfo = userInfoService.getInfoById(order.getUserId());
                if(userInfo==null){
                    GzptUserInfoDel userInfoDel = userInfoDelService.getInfoDelById(order.getUserId());
                    BeanUtils.copyProperties(userInfoDel,userInfo);
                }

                int isActiva = schActivationService.isSchActivation(order.getUserId());
                if ("理论实操计时".equals(order.getGoodsName())) {
                    //删除激活
                    if(isActiva==1) {
                        schActivationService.upDelSchActivationByUserId(order.getUserId(),1);
                    }
                } else if ("科一专项VIP(含计时)".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km1Date = userVip.getSubject1();
                        if (km1Date != null) {  //存在会员时间
                            long x = km1Date.getTime() - oneYearLong;
                            Date km1oneYear = new Date(x);
                            userVip.setSubject1(km1oneYear);
                            userVipService.updateById(userVip);
                        }
                    }
                    //删除激活
                    if(isActiva==1) {
                        schActivationService.upDelSchActivationByUserId(order.getUserId(),1);
                    }
                } else if ("科一专项VIP".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km1Date = userVip.getSubject1();
                        if (km1Date != null) {  //存在会员时间
                            long x = km1Date.getTime() - oneYearLong;
                            Date km1oneYear = new Date(x);
                            userVip.setSubject1(km1oneYear);
                            userVipService.updateById(userVip);
                        }
                    }
                } else if ("科二专项VIP(含计时)".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km2Date = userVip.getSubject2();
                        if (km2Date != null) {  //存在会员时间
                            long x = km2Date.getTime() - oneYearLong;
                            Date km2oneYear = new Date(x);
                            userVip.setSubject2(km2oneYear);
                            userVipService.updateById(userVip);
                        }
                    }
                    //删除激活
                    if(isActiva==1) {
                        schActivationService.upDelSchActivationByUserId(userInfo.getOutId(),1);
                    }
                } else if ("科二专项VIP".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km2Date = userVip.getSubject2();
                        if (km2Date != null) {  //存在会员时间
                            long x = km2Date.getTime() - oneYearLong;
                            Date km2oneYear = new Date(x);
                            userVip.setSubject2(km2oneYear);
                            userVipService.updateById(userVip);
                        }
                    }
                } else if ("科三专项VIP(含计时)".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km3Date = userVip.getSubject3();
                        if (km3Date != null) {  //存在会员时间
                            long x = km3Date.getTime() - oneYearLong;
                            Date km3oneYear = new Date(x);
                            userVip.setSubject3(km3oneYear);
                            userVipService.updateById(userVip);
                        }
                    }
                    //删除激活
                    if(isActiva==1) {
                        schActivationService.upDelSchActivationByUserId(order.getUserId(),1);
                    }
                } else if ("科三专项VIP".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km3Date = userVip.getSubject3();
                        if (km3Date != null) {  //存在会员时间
                            long x = km3Date.getTime() - oneYearLong;
                            Date km3oneYear = new Date(x);
                            userVip.setSubject3(km3oneYear);
                            userVipService.updateById(userVip);
                        }
                    }
                } else if ("科四专项VIP(含计时)".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km4Date = userVip.getSubject4();
                        if (km4Date != null) {  //存在会员时间
                            long x = km4Date.getTime() - oneYearLong;
                            Date km4oneYear = new Date(x);
                            userVip.setSubject4(km4oneYear);
                            userVipService.updateById(userVip);
                        }
                    }
                    //删除激活
                    if(isActiva==1) {
                        schActivationService.upDelSchActivationByUserId(order.getUserId(),1);
                    }
                } else if ("科四专项VIP".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km4Date = userVip.getSubject4();
                        if (km4Date != null) {  //存在会员时间
                            long x = km4Date.getTime() - oneYearLong;
                            Date km4oneYear = new Date(x);
                            userVip.setSubject4(km4oneYear);
                            userVipService.updateById(userVip);
                        }
                    }
                } else if ("科目一二三四所有付费权益".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km1Date = userVip.getSubject1();
                        if (km1Date != null) {  //存在会员时间
                            long x = km1Date.getTime() - oneYearLong;
                            Date km1oneYear = new Date(x);
                            userVip.setSubject1(km1oneYear);
                        }
                        Date km2Date = userVip.getSubject2();
                        if (km2Date != null) {  //存在会员时间
                            long x = km2Date.getTime() - oneYearLong;
                            Date km2oneYear = new Date(x);
                            userVip.setSubject2(km2oneYear);
                        }
                        Date km3Date = userVip.getSubject3();
                        if (km3Date != null) {  //存在会员时间
                            long x = km3Date.getTime() - oneYearLong;
                            Date km3oneYear = new Date(x);
                            userVip.setSubject3(km3oneYear);
                        }

                        Date km4Date = userVip.getSubject4();
                        if (km4Date != null) {  //存在会员时间
                            long x = km4Date.getTime() - oneYearLong;
                            Date km4oneYear = new Date(x);
                            userVip.setSubject4(km4oneYear);
                        }
                        userVipService.updateById(userVip);
                    }
                } else if ("科一+科四VIP(含计时)".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km1Date = userVip.getSubject1();
                        if (km1Date != null) {  //存在会员时间
                            long x = km1Date.getTime() - oneYearLong;
                            Date km1oneYear = new Date(x);
                            userVip.setSubject1(km1oneYear);
                        }
                        Date km4Date = userVip.getSubject4();
                        if (km4Date != null) {  //存在会员时间
                            long x = km4Date.getTime() - oneYearLong;
                            Date km4oneYear = new Date(x);
                            userVip.setSubject4(km4oneYear);
                        }
                        userVipService.updateById(userVip);
                    }
                    //删除激活
                    if(isActiva==1) {
                        schActivationService.upDelSchActivationByUserId(order.getUserId(),1);
                    }
                } else if ("科一+科四VIP".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km1Date = userVip.getSubject1();
                        if (km1Date != null) {  //存在会员时间
                            long x = km1Date.getTime() - oneYearLong;
                            Date km1oneYear = new Date(x);
                            userVip.setSubject1(km1oneYear);
                        }
                        Date km4Date = userVip.getSubject4();
                        if (km4Date != null) {  //存在会员时间
                            long x = km4Date.getTime() - oneYearLong;
                            Date km4oneYear = new Date(x);
                            userVip.setSubject4(km4oneYear);
                        }
                        userVipService.updateById(userVip);
                    }
                } else if ("科二+科三VIP(含计时)".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km2Date = userVip.getSubject2();
                        if (km2Date != null) {  //存在会员时间
                            long x = km2Date.getTime() - oneYearLong;
                            Date km2oneYear = new Date(x);
                            userVip.setSubject2(km2oneYear);
                        }
                        Date km3Date = userVip.getSubject3();
                        if (km3Date != null) {  //存在会员时间
                            long x = km3Date.getTime() - oneYearLong;
                            Date km3oneYear = new Date(x);
                            userVip.setSubject3(km3oneYear);
                        }
                        userVipService.updateById(userVip);
                    }
                    //删除激活
                    if(isActiva==1) {
                        schActivationService.upDelSchActivationByUserId(order.getUserId(),1);
                    }
                } else if ("科二+科三VIP".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km2Date = userVip.getSubject2();
                        if (km2Date != null) {  //存在会员时间
                            long x = km2Date.getTime() - oneYearLong;
                            Date km2oneYear = new Date(x);
                            userVip.setSubject2(km2oneYear);
                        }
                        Date km3Date = userVip.getSubject3();
                        if (km3Date != null) {  //存在会员时间
                            long x = km3Date.getTime() - oneYearLong;
                            Date km3oneYear = new Date(x);
                            userVip.setSubject3(km3oneYear);
                        }
                        userVipService.updateById(userVip);
                    }
                } else if ("科二+科三+科四(含计时)".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km2Date = userVip.getSubject2();
                        if (km2Date != null) {  //存在会员时间
                            long x = km2Date.getTime() - oneYearLong;
                            Date km2oneYear = new Date(x);
                            userVip.setSubject2(km2oneYear);
                        }
                        Date km3Date = userVip.getSubject3();
                        if (km3Date != null) {  //存在会员时间
                            long x = km3Date.getTime() - oneYearLong;
                            Date km3oneYear = new Date(x);
                            userVip.setSubject3(km3oneYear);
                        }
                        Date km4Date = userVip.getSubject4();
                        if (km4Date != null) {  //存在会员时间
                            long x = km4Date.getTime() - oneYearLong;
                            Date km4oneYear = new Date(x);
                            userVip.setSubject4(km4oneYear);
                        }
                        userVipService.updateById(userVip);
                    }
                    //删除激活
                    if(isActiva==1) {
                        schActivationService.upDelSchActivationByUserId(order.getUserId(),1);
                    }
                } else if ("科二+科三+科四".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km2Date = userVip.getSubject2();
                        if (km2Date != null) {  //存在会员时间
                            long x = km2Date.getTime() - oneYearLong;
                            Date km2oneYear = new Date(x);
                            userVip.setSubject2(km2oneYear);
                        }
                        Date km3Date = userVip.getSubject3();
                        if (km3Date != null) {  //存在会员时间
                            long x = km3Date.getTime() - oneYearLong;
                            Date km3oneYear = new Date(x);
                            userVip.setSubject3(km3oneYear);
                        }
                        Date km4Date = userVip.getSubject4();
                        if (km4Date != null) {  //存在会员时间
                            long x = km4Date.getTime() - oneYearLong;
                            Date km4oneYear = new Date(x);
                            userVip.setSubject4(km4oneYear);
                        }
                        userVipService.updateById(userVip);
                    }
                } else if ("科三+科四VIP(含计时)".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km3Date = userVip.getSubject3();
                        if (km3Date != null) {  //存在会员时间
                            long x = km3Date.getTime() - oneYearLong;
                            Date km3oneYear = new Date(x);
                            userVip.setSubject3(km3oneYear);
                        }
                        Date km4Date = userVip.getSubject4();
                        if (km4Date != null) {  //存在会员时间
                            long x = km4Date.getTime() - oneYearLong;
                            Date km4oneYear = new Date(x);
                            userVip.setSubject4(km4oneYear);
                        }
                        userVipService.updateById(userVip);
                    }
                    //删除激活
                    if(isActiva==1) {
                        schActivationService.upDelSchActivationByUserId(order.getUserId(),1);
                    }
                } else if ("科三+科四VIP".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km3Date = userVip.getSubject3();
                        if (km3Date != null) {  //存在会员时间
                            long x = km3Date.getTime() - oneYearLong;
                            Date km3oneYear = new Date(x);
                            userVip.setSubject3(km3oneYear);
                        }
                        Date km4Date = userVip.getSubject4();
                        if (km4Date != null) {  //存在会员时间
                            long x = km4Date.getTime() - oneYearLong;
                            Date km4oneYear = new Date(x);
                            userVip.setSubject4(km4oneYear);
                        }
                        userVipService.updateById(userVip);
                    }
                } else if ("全科目超级VIP(含计时)".equals(order.getGoodsName())){
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km1Date = userVip.getSubject1();
                        if (km1Date != null) {  //存在会员时间
                            long x = km1Date.getTime() - oneYearLong;
                            Date km1oneYear = new Date(x);
                            userVip.setSubject1(km1oneYear);
                        }
                        Date km2Date = userVip.getSubject2();
                        if (km2Date != null) {  //存在会员时间
                            long x = km2Date.getTime() - oneYearLong;
                            Date km2oneYear = new Date(x);
                            userVip.setSubject2(km2oneYear);
                        }
                        Date km3Date = userVip.getSubject3();
                        if (km3Date != null) {  //存在会员时间
                            long x = km3Date.getTime() - oneYearLong;
                            Date km3oneYear = new Date(x);
                            userVip.setSubject3(km3oneYear);
                        }

                        Date km4Date = userVip.getSubject4();
                        if (km4Date != null) {  //存在会员时间
                            long x = km4Date.getTime() - oneYearLong;
                            Date km4oneYear = new Date(x);
                            userVip.setSubject4(km4oneYear);
                        }
                        userVipService.updateById(userVip);
                    }
                    //删除激活
                    if(isActiva==1) {
                        schActivationService.upDelSchActivationByUserId(order.getUserId(),1);
                    }
                } else if ("全科目超级VIP".equals(order.getGoodsName())) {
                    UserVip userVip = userVipService.getUserVipByUserId(order.getUserId());
                    if (userVip != null) {
                        //修改会员信息
                        Date km1Date = userVip.getSubject1();
                        if (km1Date != null) {  //存在会员时间
                            long x = km1Date.getTime() - oneYearLong;
                            Date km1oneYear = new Date(x);
                            userVip.setSubject1(km1oneYear);
                        }
                        Date km2Date = userVip.getSubject2();
                        if (km2Date != null) {  //存在会员时间
                            long x = km2Date.getTime() - oneYearLong;
                            Date km2oneYear = new Date(x);
                            userVip.setSubject2(km2oneYear);
                        }
                        Date km3Date = userVip.getSubject3();
                        if (km3Date != null) {  //存在会员时间
                            long x = km3Date.getTime() - oneYearLong;
                            Date km3oneYear = new Date(x);
                            userVip.setSubject3(km3oneYear);
                        }

                        Date km4Date = userVip.getSubject4();
                        if (km4Date != null) {  //存在会员时间
                            long x = km4Date.getTime() - oneYearLong;
                            Date km4oneYear = new Date(x);
                            userVip.setSubject4(km4oneYear);
                        }
                        userVipService.updateById(userVip);
                    }
                    //删除激活
                    if(isActiva==1) {
                        schActivationService.upDelSchActivationByUserId(order.getUserId(),1);
                    }
                }
                int isShare = schPayConfigService.isSchPayConfigByUser(userInfo);
                if (isShare == 1) {
                    //如果是合作驾校插入消费记录表
                    savePayLog(order, userInfo,2);
                }
                break;
            case 2:
                vipUserExamService.deleteVipUserExamByUserIdAndGoodsId(order.getUserId(),order.getGoodsId());
                System.out.println("退未取消考场");
                break;
            default:
                break;

        }

    }



    /**
     * 插入支付记录
     * @param order
     * @param gzptUserInfo
     */
    private void savePayLog(WxJsOrder order, GzptUserInfo gzptUserInfo,int state) {
        GzptSchPayLog schPayLog = new GzptSchPayLog();
        schPayLog.setStuId(gzptUserInfo.getOutId());
        schPayLog.setDqbh(order.getDqbh());
        schPayLog.setPrice(order.getTotal());
        schPayLog.setJxbh(order.getSchool());
        schPayLog.setCrtime(new Date());
        schPayLog.setState(state); // 1充值  2退款
        schPayLog.setGoodsName(order.getGoodsName());
        schPayLog.setOutTradeNo(order.getOutTradeNo());
        schPayLog.setCreateId(order.getCreateId());
        schPayLog.setSchoolCommission(order.getSchoolCommission());
        schPayLogService.saveSchPayLog(schPayLog);
    }

    /**
     * 插入激活记录
     * @param order
     * @param gzptUserInfo
     */
    private void saveActiva(WxJsOrder order, GzptUserInfo gzptUserInfo) {
        GzptSchActivation schActivation = new GzptSchActivation();
        schActivation.setDqbh(order.getDqbh());
        schActivation.setDqmc(order.getDqmc());
        schActivation.setJxbh(order.getSchool());
        schActivation.setJxmc(order.getSchoolName());
        schActivation.setUserId(order.getUserId());
        schActivation.setStuId(gzptUserInfo.getOutId());
        schActivation.setCrtime(new Date());
        schActivationService.saveSchActivation(schActivation);
    }

    private void delActiva(Long userId) {
        schActivationService.delSchActivationByUserId(userId);
    }

}
