package com.jsowell.pile.service.impl;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.enums.MemberWalletEnum;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.SecurityUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.id.IdUtils;
import com.jsowell.pile.domain.MemberBasicInfo;
import com.jsowell.pile.domain.MemberPlateNumberRelation;
import com.jsowell.pile.domain.MemberWalletInfo;
import com.jsowell.pile.domain.MemberWalletLog;
import com.jsowell.pile.dto.CreateMerchantVipDTO;
import com.jsowell.pile.dto.PlatformTesterDTO;
import com.jsowell.pile.dto.QueryMemberInfoDTO;
import com.jsowell.pile.dto.UniAppQueryMemberBalanceDTO;
import com.jsowell.pile.mapper.MemberBasicInfoMapper;
import com.jsowell.pile.mapper.MemberPlateNumberRelationMapper;
import com.jsowell.pile.mapper.MemberWalletLogMapper;
import com.jsowell.pile.service.*;
import com.jsowell.pile.util.MerchantUtils;
import com.jsowell.pile.util.UserUtils;
import com.jsowell.pile.vo.base.MerchantInfoVO;
import com.jsowell.pile.vo.uniapp.customer.*;
import com.jsowell.pile.vo.web.PlatformTesterVO;
import com.jsowell.pile.vo.web.UpdateMemberBalanceDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员基础信息Service业务层处理
 *
 * @author jsowell
 * @date 2022-10-12
 */
@Slf4j
@Service
public class MemberBasicInfoServiceImpl implements MemberBasicInfoService {
	@Autowired
	private MemberBasicInfoMapper memberBasicInfoMapper;

	// @Autowired
	// private MemberWalletInfoMapper memberWalletInfoMapper;

	@Autowired
	private MemberWalletInfoService memberWalletInfoService;

	@Autowired
	private MemberWalletLogMapper memberWalletLogMapper;

	@Autowired
	private PileBasicInfoService pileBasicInfoService;

	@Autowired
	private MemberPlateNumberRelationMapper memberPlateNumberRelationMapper;

	@Autowired
	private RedisCache redisCache;

	@Autowired
	private PileMerchantInfoService pileMerchantInfoService;

	@Autowired
	private MemberTransactionRecordService memberTransactionRecordService;

	@Autowired
	private PileStationInfoService pileStationInfoService;

	/**
	 * 查询会员基础信息
	 *
	 * @param id 会员基础信息主键
	 * @return 会员基础信息
	 */
	@Override
	public MemberBasicInfo selectMemberBasicInfoById(Integer id) {
		return memberBasicInfoMapper.selectMemberBasicInfoById(id);
	}

	/**
	 * 查询会员基础信息列表
	 *
	 * @param memberBasicInfo 会员基础信息
	 * @return 会员基础信息
	 */
	/*@Override
	public List<MemberVO> selectMemberBasicInfoList(MemberBasicInfo memberBasicInfo) {
		List<MemberVO> voList = memberBasicInfoMapper.selectMemberList(memberBasicInfo.getMobileNumber(), memberBasicInfo.getNickName());
		for (MemberVO memberVO : voList) {
			memberVO.setPrincipalBalance(memberVO.getPrincipalBalance() == null ? BigDecimal.ZERO : memberVO.getPrincipalBalance());
			memberVO.setGiftBalance(memberVO.getGiftBalance() == null ? BigDecimal.ZERO : memberVO.getGiftBalance());
		}
		return voList;
	}*/

	/**
	 * 新增会员基础信息
	 *
	 * @param memberBasicInfo 会员基础信息
	 * @return 结果
	 */
	@Override
	public int insertMemberBasicInfo(MemberBasicInfo memberBasicInfo) {
		memberBasicInfo.setCreateTime(DateUtils.getNowDate());
		return memberBasicInfoMapper.insertMemberBasicInfo(memberBasicInfo);
	}

	/**
	 * 修改会员基础信息
	 *
	 * @param memberBasicInfo 会员基础信息
	 * @return 结果
	 */
	@Override
	public int updateMemberBasicInfo(MemberBasicInfo memberBasicInfo) {
		memberBasicInfo.setUpdateTime(DateUtils.getNowDate());
		return memberBasicInfoMapper.updateMemberBasicInfo(memberBasicInfo);
	}

	/**
	 * 批量删除会员基础信息
	 *
	 * @param ids 需要删除的会员基础信息主键
	 * @return 结果
	 */
	@Override
	public int deleteMemberBasicInfoByIds(List<Integer> ids) {
		return memberBasicInfoMapper.deleteMemberBasicInfoByIds(ids);
	}

	/**
	 * 通过物理卡号查询会员基本信息
	 *
	 * @param physicsCard 物理卡号
	 * @return 会员基本信息
	 */
	@Override
	public MemberVO selectInfoByPhysicsCard(String physicsCard) {
		return memberBasicInfoMapper.selectInfoByPhysicsCard(physicsCard);
	}

	/**
	 * 根据手机号和运营商id查询会员信息
	 * @param mobileNumber 手机号
	 * @param firstLevelMerchantId 所属一级运营商 运营商id
	 * @return 会员信息
	 */
	@Override
	public MemberBasicInfo selectInfoByMobileNumber(String mobileNumber, String firstLevelMerchantId) {
		return memberBasicInfoMapper.selectInfoByMobileNumberAndMerchantId(mobileNumber, firstLevelMerchantId);
	}

	// @Override
	// public MemberBasicInfo selectInfoByMobileNumber(String mobileNumber) {
	// 	return selectInfoByMobileNumber(mobileNumber, null);
	// }

	@Override
	public MemberBasicInfo selectInfoByMemberId(String memberId) {
		return memberBasicInfoMapper.selectInfoByMemberId(memberId);
	}

	/**
	 * 生成钱包卡号WalletCode
	 * @return
	 */
	@Override
	public String generateWalletCode() {
		while (true) {
			String walletCode = IdUtils.get16UUID("89");
			// 通过walletCode查询是否已经存在
			MemberWalletInfo memberWalletInfo = memberWalletInfoService.selectByWalletCode(walletCode);
			if (memberWalletInfo == null) {
				return walletCode;
			}
		}
	}

	public static void main(String[] args) {
		for (int i = 0; i < 191; i++) {
			String walletCode = IdUtils.get16UUID("89");
			System.out.println(walletCode);
		}
	}

	/**
	 * 修改用户余额 唯一方法
	 * 接收的金额都是正数，通过操作类型判断 充值还是扣减
	 */
	@Override
	public int updateMemberBalance(UpdateMemberBalanceDTO dto) {
		String memberId = dto.getMemberId(); // 会员id
		BigDecimal updateGiftBalance = dto.getUpdateGiftBalance(); // 更新赠送金额
		BigDecimal updatePrincipalBalance = dto.getUpdatePrincipalBalance(); // 更新本金金额

		// 没有传过来目标运营商，有可能是希晓的小程序在线充值
		if (StringUtils.isBlank(dto.getTargetMerchantId())) {
			// 根据memberId查询会员信息
			MemberBasicInfo memberInfo = selectInfoByMemberId(memberId);
			if (memberInfo != null) {
				// 如果这个会员属于希晓，那么目标运营商就设置为希晓的id
				if (MerchantUtils.isXiXiaoMerchant(memberInfo.getMerchantId() + "")) {
					dto.setTargetMerchantId(MerchantUtils.XIXIAO_MERCHANT_ID);
				}
			}
		}

		String targetMerchantId = dto.getTargetMerchantId(); // 目标运营商id
		log.info("修改用户余额-param:{}", JSON.toJSONString(dto));

		/*
		 更新类型
		 1-进账；2-出账
		 */
		String type = dto.getType();

		/*
		子类型
	  	进账：10-充值, 11-赠送, 12-订单结算退款
	  	出账：20-后管扣款, 21-订单付款, 22-用户退款
		 */
		String subType;

		// 本次操作人员
		String createBy = dto.getMemberId();

		// 来自后管的充值，校验权限
		if (StringUtils.equals(dto.getFromWeb(), Constants.ONE)) {
			// LoginUserDetailVO loginUserDetail = UserUtils.getLoginUserDetail();
			List<MerchantInfoVO> merchantInfoVOList = UserUtils.getMerchantInfoVOList();
			List<String> collect = merchantInfoVOList.stream().map(MerchantInfoVO::getMerchantId).collect(Collectors.toList());
			if (!collect.contains(targetMerchantId)) {
				throw new BusinessException(ReturnCodeEnum.CODE_PERMISSION_DENIED);
			}
			// 后管操作的，记录为后管登录账号
			createBy = SecurityUtils.getLoginUser().getUserId() + "";
		}

		// 查询用户余额
		MemberWalletInfo walletInfo = memberWalletInfoService.selectByMemberId(memberId, targetMerchantId);
		log.info("修改用户余额-根据会员id:{}, 目标运营商id:{}, 查询结果:{}", memberId, targetMerchantId, JSON.toJSONString(walletInfo));
		if (walletInfo == null) {
			log.info("修改用户余额-根据会员id:{}, 目标运营商id:{}, 查询会员信息为空, 新建会员钱包", memberId, targetMerchantId);
			// 如果查询到钱包为空，就新建一个该运营商的钱包
			walletInfo = MemberWalletInfo.builder()
					.walletCode(generateWalletCode())
					.memberId(memberId)
					.merchantId(targetMerchantId)
					.giftBalance(BigDecimal.ZERO)
					.principalBalance(BigDecimal.ZERO)
					.version(0)
					.build();
			memberWalletInfoService.insertSelective(walletInfo);
		}

		// 钱包编号
		String walletCode = walletInfo.getWalletCode();

		// 记录流水
		List<MemberWalletLog> logList = Lists.newArrayList();
		// 计算新的余额
		BigDecimal newPrincipalBalance = null;
		BigDecimal newGiftBalance = null;

		/*
		 更新本金金额
		 */
		if (updatePrincipalBalance != null && updatePrincipalBalance.compareTo(BigDecimal.ZERO) > 0) {
			// 余额类型（1-本金，2-赠送）
			String category = Constants.ONE;
			// 获取子类型
			subType = getTheSubType(dto, category);
			// 出账时，金额改为负数
			if (StringUtils.equals(type, MemberWalletEnum.TYPE_OUT.getValue())) {
				// 扣款 转为负数
				updatePrincipalBalance = updatePrincipalBalance.negate();
			}
			// 原金额
			BigDecimal oldPrincipalBalance = walletInfo.getPrincipalBalance() == null
					? BigDecimal.ZERO
					: walletInfo.getPrincipalBalance();
			// 新金额 = 原金额 + 变动金额
			newPrincipalBalance = oldPrincipalBalance.add(updatePrincipalBalance);
			if (newPrincipalBalance.compareTo(BigDecimal.ZERO) < 0) {
				log.info("修改用户余额-变更后本金余额不能为负数");
				throw new BusinessException("", "变更后本金余额不能为负数");
			}
			// 备注，拼接存入数据库
			String remark = "日期:" + DateUtils.getDateTime() +
					", 操作人员Id:" + createBy +
					", 变更用户本金金额:" + updatePrincipalBalance +
					", 原金额:" + oldPrincipalBalance +
					", 变更后金额:" + newPrincipalBalance;
			// 记流水
			logList.add(MemberWalletLog.builder()
					.memberId(memberId)
					.walletCode(walletCode)
					.type(type)
					.subType(subType)
					.amount(updatePrincipalBalance)
					.category(category)
					.relatedOrderCode(dto.getRelatedOrderCode())
					.beforeAmount(oldPrincipalBalance)
					.afterAmount(newPrincipalBalance)
					.remark(remark)
					.createBy(createBy)
					.createTime(DateUtils.getNowDate())
					.build());
		}

		/*
		 更新赠送金额
		 */
		if (updateGiftBalance != null && updateGiftBalance.compareTo(BigDecimal.ZERO) > 0) {
			// 余额类型（1-本金，2-赠送）
			String category = Constants.TWO;
			// 获取子类型
			subType = getTheSubType(dto, category);
			// 出账时，金额改为负数
			if (StringUtils.equals(type, MemberWalletEnum.TYPE_OUT.getValue())) {
				// 扣款 转为负数
				updateGiftBalance = updateGiftBalance.negate();
			}
			// 原金额
			BigDecimal oldGiftBalance = walletInfo.getGiftBalance() == null
					? BigDecimal.ZERO
					: walletInfo.getGiftBalance();
			// 新金额 = 原金额 + 变动金额
			newGiftBalance = oldGiftBalance.add(updateGiftBalance);
			// 余额不能为负数
			if (newGiftBalance.compareTo(BigDecimal.ZERO) < 0) {
				log.info("修改用户余额-变更后赠送余额不能为负数");
				throw new BusinessException("", "变更后赠送余额不能为负数");
			}
			// 备注，拼接存入数据库
			String remark = "日期:" + DateUtils.getDateTime() +
					", 操作人员Id:" + createBy +
					", 变更用户赠送金额:" + updateGiftBalance +
					", 原金额:" + oldGiftBalance +
					", 变更后金额:" + newGiftBalance;
			// 记流水
			logList.add(MemberWalletLog.builder()
					.memberId(memberId)
					.walletCode(walletCode)
					.type(type)
					.subType(subType)
					.amount(updateGiftBalance)
					.category(category)
					.relatedOrderCode(dto.getRelatedOrderCode())
					.beforeAmount(oldGiftBalance)
					.afterAmount(newGiftBalance)
					.remark(remark)
					.createBy(createBy)
					.createTime(DateUtils.getNowDate())
					.build());
		}

		// 修改数据库
		int i = 0;
		if (newPrincipalBalance != null || newGiftBalance != null) {
			// log.info("修改用户余额-memberId:{}, walletCode:{}, newPrincipalBalance:{}, newGiftBalance:{}, version:{}"
			// 	, memberId, walletCode, newPrincipalBalance, newGiftBalance, walletInfo.getVersion());
			i = memberBasicInfoMapper.updateMemberBalance(memberId, walletCode, newPrincipalBalance, newGiftBalance, walletInfo.getVersion());
			if (i == 0) {
				log.info("修改用户余额-修改余额失败, memberId:{}", memberId);
			}
		}
		// 插入 member_wallet_log 表
		if (CollectionUtils.isNotEmpty(logList)) {
			memberWalletLogMapper.batchInsert(logList);
		}
		log.info("修改用户余额-end memberId:{}, walletCode:{}, newPrincipalBalance:{}, newGiftBalance:{}, version:{}"
				, memberId, walletCode, newPrincipalBalance, newGiftBalance, walletInfo.getVersion());
		return i;
	}

	// 获取子类型
	private String getTheSubType(UpdateMemberBalanceDTO dto, String category) {
		String subType;
		if (!StringUtils.equals(dto.getFromWeb(), Constants.ONE)) {
			// 如果不是后管操作，subType等于dto中传过来的值
			subType = dto.getSubType();
		} else {
			// 来自后管的操作
			if (StringUtils.equals(dto.getType(), MemberWalletEnum.TYPE_OUT.getValue())) {
				// 后管操作 && 出账 = 就是后管扣款
				subType = MemberWalletEnum.SUBTYPE_WEB_DEDUCT_MONEY.getValue();
			} else {
				// 进账
				if (StringUtils.equals(category, Constants.ONE)) {
					// 充值本金
					subType = MemberWalletEnum.SUBTYPE_TOP_UP.getValue();
				} else {
					// 充值赠送金
					subType = MemberWalletEnum.SUBTYPE_GIVING.getValue();
				}
			}
		}
		return subType;
	}

	@Override
	public MemberVO queryMemberInfoByMemberId(String memberId) {
		return queryMemberInfoByMemberId(memberId, null);
	}

	@Override
	public MemberVO queryMemberInfoByMemberId(String memberId, String merchantId) {
		if (StringUtils.isBlank(memberId)) {
			return null;
		}

		MemberVO vo = memberBasicInfoMapper.queryMemberInfoByMemberId(memberId);
		if (vo == null) {
			return null;
		}

		if (MerchantUtils.isXiXiaoMerchant(vo.getMerchantId())) {
			// 如果会员属于希晓，那么merchantId默认为希晓的运营商id
			merchantId = MerchantUtils.XIXIAO_MERCHANT_ID;
		}

		try {
			// BigDecimal totalAccountAmount = BigDecimal.ZERO;
			BigDecimal principalBalance = BigDecimal.ZERO;
			BigDecimal giftBalance = BigDecimal.ZERO;
			MemberWalletInfo memberWalletInfo = memberWalletInfoService.selectByMemberId(memberId, merchantId);
			if (memberWalletInfo != null) {
				if (memberWalletInfo.getPrincipalBalance() != null) {
					principalBalance = memberWalletInfo.getPrincipalBalance();
				}
				if (memberWalletInfo.getGiftBalance() != null) {
					giftBalance = memberWalletInfo.getGiftBalance();
				}
				// totalAccountAmount = memberWalletInfo.getPrincipalBalance();
				vo.setMerchantId(memberWalletInfo.getMerchantId());
			}
			// 计算用户注册天数
			String days = DateUtils.getPoorDays(new Date(), vo.getCreateTime());
			vo.setRegistrationDays(days);
			vo.setPrincipalBalance(principalBalance);
			vo.setGiftBalance(giftBalance);
			vo.setTotalBalance(vo.getPrincipalBalance().add(vo.getGiftBalance()));
		} catch (Exception e) {
			log.error("查询用户余额异常 memberId:{}, merchantId:{}", memberId, merchantId, e);
		}

		// 查询用户车牌号
		MemberPlateNumberRelation memberPlateNumberRelation = new MemberPlateNumberRelation();
		memberPlateNumberRelation.setMemberId(memberId);
		List<MemberPlateNumberRelation> list = memberPlateNumberRelationMapper.selectMemberPlateNumberRelationList(memberPlateNumberRelation);
		List<String> plateNumberList = new ArrayList<>();
		List<String> vinCodeList = new ArrayList<>();
		for (MemberPlateNumberRelation relation : list) {
			if (StringUtils.isNotBlank(relation.getLicensePlateNumber())) {
				plateNumberList.add(relation.getLicensePlateNumber());
			}
			if (StringUtils.isNotBlank(relation.getVinCode())) {
				vinCodeList.add(relation.getVinCode());
			}
		}
		vo.setPlateNumberList(plateNumberList);
		vo.setVinCodeList(vinCodeList);
		return vo;
	}

	/**
	 * 查询用户基本信息列表
	 * @param mobileNumber
	 * @param nickName
	 * @return
	 */
	@Override
	public List<MemberVO> selectMemberList(String mobileNumber, String nickName) {
		QueryMemberInfoDTO dto = new QueryMemberInfoDTO();
		dto.setMobileNumber(mobileNumber);
		dto.setNickName(nickName);

		return selectMemberList(dto);
	}

	/**
	 * 查询用户基本信息列表
	 * @param dto
	 * @return
	 */
	@Override
	public List<MemberVO>  selectMemberList(QueryMemberInfoDTO dto) {

		// 获取登录账号信息
		// AuthorizedDeptVO authorizedMap = SecurityUtils.getAuthorizedMap();
		// AuthorizedDeptVO authorizedMap = UserUtils.getAuthorizedMap();
		// if (authorizedMap == null) {
		// 	return new ArrayList<>();
		// }
		// List<String> merchantDeptIds = authorizedMap.getMerchantDeptIds();
		// if (CollectionUtils.isNotEmpty(merchantDeptIds)) {
		// 	dto.setMerchantDeptIds(merchantDeptIds);
		// }

		// PileMerchantInfo pileMerchantInfo = pileMerchantInfoService.queryInfoByDeptId(authorizedMap.getDeptId());
		// if (pileMerchantInfo != null) {
		// 	String id = pileMerchantInfoService.getFirstLevelMerchantIdByMerchantId(pileMerchantInfo.getId() + "");
		// 	dto.setMerchantId(id);
		// }

		// LoginUserDetailVO loginUserDetail = UserUtils.getLoginUserDetail();
		List<String> firstMerchantIdList = UserUtils.getFirstMerchantIdList();
		if (CollectionUtils.isNotEmpty(firstMerchantIdList)) {
			dto.setFirstMerchantIds(firstMerchantIdList);
		}

		PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
		List<MemberVO> memberVOS = memberBasicInfoMapper.selectMemberList(dto);

		return memberVOS;
	}

	/**
	 * 查询用户账户余额变动信息
	 * @param memberId 会员id
	 * @param type 1-进账；2-出账 不传查全部
	 */
    @Override
    public List<MemberWalletLogVO> getMemberBalanceChanges(String memberId, String type) {
    	return memberWalletLogMapper.getMemberBalanceChanges(memberId, type);
    }

	/**
	 * 通过memberId查询会员的个人桩信息
	 * @param memberId
	 * @return
	 */
	@Override
	public List<PersonalPileInfoVO> getMemberPersonPileInfo(String memberId) {
		return pileBasicInfoService.getPileInfoByMemberId(memberId);
	}

	/**
	 * 修改平台测试员状态
	 * @param dto
	 */
	@Override
    public void updatePlatformTester(PlatformTesterDTO dto) {
        if (StringUtils.isBlank(dto.getMemberId()) || StringUtils.isBlank(dto.getStatus())) {
			return;
		}

		String redisKey = CacheConstants.PLATFORM_TESTER + dto.getMemberId();
		if (StringUtils.equals(dto.getStatus(), Constants.ONE)) {
			// 放缓存
			redisCache.setCacheObject(redisKey, Constants.ONE);
		} else {
			// 清缓存
			redisCache.deleteObject(redisKey);
		}
    }

	@Override
	public PlatformTesterVO selectPlatformTesterStatus(String memberId) {
		PlatformTesterVO vo = new PlatformTesterVO();
		String redisKey = CacheConstants.PLATFORM_TESTER + memberId;
		String status = redisCache.getCacheObject(redisKey);
		if (StringUtils.isNotBlank(status)) {
			vo.setStatus(status);
		} else {
			vo.setStatus(Constants.ZERO);
		}
		vo.setMemberId(memberId);
		return vo;
	}

	/**
	 * 小程序查询会员钱包明细
	 * @param dto
	 * @return
	 */
	@Override
	public List<MemberWalletLogVO> getMemberWalletDetail(UniAppQueryMemberBalanceDTO dto) {
		return memberWalletLogMapper.getMemberWalletDetail(dto);
	}

	/**
	 * 批量查询用户可退款金额
	 * @param memberIds
	 * @return
	 */
	@Override
	public List<MemberBalanceVO> getMemberRefundAmount(List<String> memberIds) {
		return memberWalletLogMapper.getMemberRefundAmount(memberIds);
	}

	@Override
	public List<MerchantVipVO> queryMerchantVipList(QueryMemberInfoDTO dto) {
		// 获取后管登录用户的权限
		// LoginUserDetailVO loginUserDetail = UserUtils.getLoginUserDetail();
		List<MerchantInfoVO> merchantInfoVOList = UserUtils.getMerchantInfoVOList();
		List<String> merchantIdList = merchantInfoVOList.stream()
				.map(MerchantInfoVO::getMerchantId).collect(Collectors.toList());

		// 分页
		int pageNum = dto.getPageNum() == null ? 1 : dto.getPageNum();
		int pageSize = dto.getPageSize() == null ? 10 : dto.getPageSize();
		PageHelper.startPage(pageNum, pageSize);
		List<MerchantVipVO> resultList = memberBasicInfoMapper.queryMerchantVipList(merchantIdList, dto);
		if (CollectionUtils.isNotEmpty(resultList)) {
			for (MerchantVipVO merchantVipVO : resultList) {
				BigDecimal principalBalance = merchantVipVO.getPrincipalBalance() == null ? BigDecimal.ZERO : merchantVipVO.getPrincipalBalance();
				BigDecimal giftBalance = merchantVipVO.getGiftBalance() == null ? BigDecimal.ZERO : merchantVipVO.getGiftBalance();
				merchantVipVO.setTotalAccountAmount(principalBalance.add(giftBalance));
			}
		}
		return resultList;
	}

    @Override
    public void createMerchantVip(CreateMerchantVipDTO dto) {
		// 查询一级运营商
		String firstLevelMerchantId = pileMerchantInfoService.getFirstLevelMerchantIdByMerchantId(dto.getTargetMerchantId());
		if (StringUtils.isBlank(firstLevelMerchantId)) {
			throw new BusinessException("", "没有查询到以及运营商信息");
		}
		// 根据手机号查询会员信息
		MemberBasicInfo memberBasicInfo = memberBasicInfoMapper.selectInfoByMobileNumberAndMerchantId(dto.getMobileNumber(), firstLevelMerchantId);
		if (memberBasicInfo == null) {
			throw new BusinessException("", "根据手机号没有查询到会员信息");
		}
		UpdateMemberBalanceDTO updateMemberBalanceDTO = new UpdateMemberBalanceDTO();
		updateMemberBalanceDTO.setMemberId(memberBasicInfo.getMemberId());
		updateMemberBalanceDTO.setType(dto.getType());
		updateMemberBalanceDTO.setUpdatePrincipalBalance(dto.getUpdatePrincipalBalance());
		updateMemberBalanceDTO.setUpdateGiftBalance(dto.getUpdateGiftBalance());
		updateMemberBalanceDTO.setTargetMerchantId(dto.getTargetMerchantId());
		if (StringUtils.isNotBlank(dto.getFromWeb())) {
			updateMemberBalanceDTO.setFromWeb(dto.getFromWeb());
		}
		updateMemberBalance(updateMemberBalanceDTO);
	}

	/**
	 * 通过车牌号查询用户基本信息
	 * @param plateNumber
	 * @return
	 */
	@Override
	public List<MemberVO> getMemberInfoByPlateNumber(String plateNumber) {
		return memberBasicInfoMapper.getMemberInfoByPlateNumber(plateNumber);
	}

}
