package com.ruoyi.utils;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.code.DictConstant;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.exception.enums.CoreExceptionEnum;
import com.ruoyi.controller.req.*;
import com.ruoyi.enums.IntegralDetailEnums;
import com.ruoyi.enums.WalletDetailEnums;
import com.ruoyi.enums.sms.SmsEnums;
import com.ruoyi.enums.user.UserEnums;
import com.ruoyi.frequency.customer.entity.Customer;
import com.ruoyi.frequency.customer.service.CustomerService;
import com.ruoyi.frequency.enterprise.service.EnterpriseService;
import com.ruoyi.frequency.givewallerecord.service.GiveWalletRecordService;
import com.ruoyi.frequency.integralrecord.service.IntegralRecordService;
import com.ruoyi.frequency.refereeuser.entity.RefereeUser;
import com.ruoyi.frequency.refereeuser.service.RefereeUserService;
import com.ruoyi.frequency.sale.entity.Sale;
import com.ruoyi.frequency.sale.service.SaleService;
import com.ruoyi.frequency.store.entity.Store;
import com.ruoyi.frequency.store.service.StoreService;
import com.ruoyi.frequency.wallerecord.service.WalletRecordService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.utils.pwd.MD5EncryptionPwdUtil;
import com.ruoyi.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;

/**
 * @Author liwenlong
 * @Date 2020/9/16 14:24
 */
@Slf4j
@Component
public class LoginUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Resource
    private CustomerService customerService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private SaleService saleService;


    @Autowired
    private HttpServletRequest request;

    @Autowired
    private BusinessUtil businessUtil;

    @Autowired
    private RefereeUserService refereeUserService;

    @Autowired
    private WalletRecordService walletRecordService;

    /**
     * 注册
     *
     * @param req
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseData register(RegisterUserReq req) {

        if (ObjectUtil.isEmpty(req.getMobile())) {
            req.setMobile(req.getAccount());
        }

        // 验证码校验
        businessUtil.checkCode(req.getCode(), SmsEnums.REGISTER, req.getAreaCode(), req.getMobile(), req.getUserType());

        //MD5加密
        String md5Pwd = MD5EncryptionPwdUtil.encryptionMd5(req.getPwd());

        Long userId = null;

        //不同用户类型
        switch (UserEnums.getUserEnums(req.getUserType())) {
            case customer:
                //账号验证
                Customer customer = customerService.getBaseMapper().selectOne(Wrappers.<Customer>lambdaQuery().
                        eq(Customer::getMobile, req.getMobile()).ne(Customer::getDelFlag, PublicCommon.删除));
                if (customer != null) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_HAS_EXIST);
                }

                //组装用户
                customer = customerService.assembleCustomer(req, md5Pwd);
                customer.setClientId(req.getClientId());

                //清空其他账号同clientId
                if (ObjectUtil.isNotEmpty(req.getClientId())) {
                    customerService.lambdaUpdate().set(Customer::getClientId, null).eq(Customer::getClientId, req.getClientId()).update();
                }

                //生成邀请码
                String invitationCode = refereeUserCode(6, 1);
                customer.setInvitationCode(invitationCode);

                customerService.getBaseMapper().insert(customer);


                //用户id
                userId = customer.getId();
                break;

            case store:
                //账号验证
                Store store = storeService.getBaseMapper().selectOne(Wrappers.<Store>lambdaQuery().
                        eq(Store::getMobile, req.getMobile()).ne(Store::getDelFlag, PublicCommon.删除));
                if (store != null) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_HAS_EXIST);
                }
                //组装用户
                store = storeService.assembleStore(req, md5Pwd);
                //清空其他账号同clientId
                if (ObjectUtil.isNotEmpty(req.getClientId())) {
                }
                //生成邀请码
                String storeInvitationCode = refereeUserCode(6, 2);
                store.setInvitationCode(storeInvitationCode);

                System.out.println("雪花id --- >" + store.getId());
                storeService.getBaseMapper().insert(store);

                //用户id
                userId = store.getId();
                break;

            case sale:
                //账号验证
                Sale sale = saleService.getBaseMapper().selectOne(Wrappers.<Sale>lambdaQuery().
                        eq(Sale::getMobile, req.getMobile()).ne(Sale::getDelFlag, PublicCommon.删除));
                if (sale != null) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_HAS_EXIST);
                }
                //组装用户
                sale = saleService.assembleSale(req, md5Pwd);
                //清空其他账号同clientId
                if (ObjectUtil.isNotEmpty(req.getClientId())) {
                }
                //生成邀请码
                String saleInvitationCode = refereeUserCode(6, 3);
                sale.setInviteCode(saleInvitationCode);

                saleService.getBaseMapper().insert(sale);

                //用户id
                userId = sale.getId();
                break;

            default:
                throw new CustomException(CoreExceptionEnum.ERROR_PARAM);
        }

        //判断是否是他人邀请的用户
        if (ObjectUtil.isNotEmpty(req.getInvitationCode())) {
            RefereeUser refereeUser = assembleRefereeUser(new UserVo(req.getUserType(), userId), req.getInvitationCode(), req.getInviteSource());
            refereeUserService.save(refereeUser);

            //销售修改邀请数量
            if (UserEnums.sale.getCode().equals(refereeUser.getParentUserType())) {
                saleService.lambdaUpdate().setSql("invite_num=invite_num+1").eq(Sale::getId, refereeUser.getParentUserId()).update();
            }

            //设计师邀请设计师 赠送优惠金额
            if (UserEnums.customer.getCode().equals(refereeUser.getUserType())) {
                //查询字典配置的赠送优惠金额
                SysDictData dict = businessUtil.getDict(DictConstant.注册赠送抵用金);

                if (dict != null) {
                    BigDecimal giveAmount = new BigDecimal(dict.getDictValue());

                    //钱包记录
                    giveWalletRecordService.updateGiveBalance(new UserVo(refereeUser.getUserType(), refereeUser.getUserId()), WalletDetailEnums.GIVE_BALANCE, refereeUser.getId(), giveAmount, PublicCommon.Wallet.可提现);

                }

                //查询字典配置的赠送抵积分
                dict = businessUtil.getDict(DictConstant.注册赠送积分);

                if (dict != null) {
                    BigDecimal integral = new BigDecimal(dict.getDictValue());
                    //积分记录
                    integralRecordService.updateIntegral(new UserVo(refereeUser.getParentUserType(), refereeUser.getParentUserId()), null, integral, IntegralDetailEnums.REFEREE_SEND,null);
                }
            }
        }

        return businessUtil.getToken(userId, req.getUserType());
    }

    @Autowired
    private IntegralRecordService integralRecordService;


    @Autowired
    private GiveWalletRecordService giveWalletRecordService;


    /**
     * 组装邀请记录
     *
     * @param userVo
     * @return
     */
    private RefereeUser assembleRefereeUser(UserVo userVo, String code, Integer inviteSource) {

        UserVo parentUserVo = new UserVo(inviteSource, null);

        switch (userVo.getUserType()) {
            case 1:
                //根据邀请码查询邀请人id
                if (inviteSource == 1) {
                    Customer customer = customerService.getBaseMapper().selectOne(new QueryWrapper<Customer>().lambda()
                            .eq(Customer::getInvitationCode, code));
                    if (customer == null) {
                        throw new CustomException(CoreExceptionEnum.REFEREE_CODE_ERROR);
                    }
                    parentUserVo.setUserType(UserEnums.customer.getCode());
                    parentUserVo.setUserId(customer.getId());
                } else if (inviteSource == 3) {
                    Sale sale = saleService.getBaseMapper().selectOne(new QueryWrapper<Sale>().lambda()
                            .eq(Sale::getInviteCode, code));
                    if (sale == null) {
                        throw new CustomException(CoreExceptionEnum.REFEREE_CODE_ERROR);
                    }
                    parentUserVo.setUserType(UserEnums.sale.getCode());
                    parentUserVo.setUserId(sale.getId());
                }
                break;
            case 2:
                //根据邀请码查询邀请人id
                Store store = storeService.getBaseMapper().selectOne(new QueryWrapper<Store>().lambda()
                        .eq(Store::getInvitationCode, code));
                if (store == null) {
                    throw new CustomException(CoreExceptionEnum.REFEREE_CODE_ERROR);
                }
                parentUserVo.setUserType(UserEnums.store.getCode());
                parentUserVo.setUserId(store.getId());
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + userVo.getUserType());
        }

        return new RefereeUser().setParentUserId(parentUserVo.getUserId())
                .setParentUserType(parentUserVo.getUserType())
                .setUserId(userVo.getUserId())
                .setUserType(userVo.getUserType());
    }


    /**
     * 生成普通用户验证码
     *
     * @param codeLength 编码长度
     * @return 验证码字符窜
     */
    private String refereeUserCode(int codeLength, Integer userType) {
        int i;
        int count = 0;
//        char[] str = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
//                '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
//                'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
//                'Z'};
        char[] str = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9'};
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        boolean isHave = true;
        while (isHave) {
            while (count < codeLength) {
                i = Math.abs(r.nextInt(str.length));
                if (i >= 0 && i < str.length) {
                    pwd.append(str[i]);
                    count++;
                }
            }

            if (ObjectUtil.equal(UserEnums.customer.getCode(), userType)) {
                Customer customer = customerService.getBaseMapper().selectOne(new QueryWrapper<Customer>().lambda()
                        .eq(Customer::getInvitationCode, pwd.toString()));
                if (customer == null) {
                    isHave = false;
                }
            } else if (ObjectUtil.equal(UserEnums.store.getCode(), userType)) {
                Store store = storeService.getBaseMapper().selectOne(new QueryWrapper<Store>().lambda()
                        .eq(Store::getInvitationCode, pwd.toString()));
                if (store == null) {
                    isHave = false;
                }
            } else if (ObjectUtil.equal(UserEnums.sale.getCode(), userType)) {
                Sale sale = saleService.getBaseMapper().selectOne(new QueryWrapper<Sale>().lambda()
                        .eq(Sale::getInviteCode, pwd.toString()));
                if (sale == null) {
                    isHave = false;
                }
            } else {
                isHave = false;
            }

            count = 0;
        }
        return pwd.toString();
    }

    /**
     * 修改密码
     *
     * @param req
     * @return
     */
    public ResponseData editPwd(UserEditPwdLoginReq req, UserVo userVo) {

        if (!businessUtil.loginPwdTrue(userVo, req.getOldPwd())) {
            return ResponseData.error(CoreExceptionEnum.PWD_ERROR);
        }
        //修改密码
        boolean update = updateLoginPwd(userVo, req.getPwd());
        if (!update) {
            return ResponseData.error(CoreExceptionEnum.UPDATE_PWD_ERROR);
        }

        return ResponseData.success();
    }

    /**
     * 根据不同用户，修改登入密码
     *
     * @param userVo
     * @param pwd    修改后的密码
     * @return
     */
    private boolean updateLoginPwd(UserVo userVo, String pwd) {

        //是否修改成功
        boolean update = true;
        //根据不同的用户类型 修改用户的密码
        switch (UserEnums.getUserEnums(userVo.getUserType())) {
            case customer:
                update = customerService.lambdaUpdate().set(Customer::getLoginPwd, MD5EncryptionPwdUtil.encryptionMd5(pwd))
                        .eq(Customer::getId, userVo.getUserId()).update();
                break;
            case store:
                update = storeService.lambdaUpdate().set(Store::getLoginPwd, MD5EncryptionPwdUtil.encryptionMd5(pwd))
                        .eq(Store::getId, userVo.getUserId()).update();
                break;
            case sale:
                update = saleService.lambdaUpdate().set(Sale::getLoginPwd, MD5EncryptionPwdUtil.encryptionMd5(pwd))
                        .eq(Sale::getId, userVo.getUserId()).update();
                break;
            default:
                throw new CustomException(CoreExceptionEnum.ERROR_PARAM);
        }
        return update;
    }


    /**
     * 根据不同用户，修改支付密码
     *
     * @param userVo
     * @param pwd    修改后的密码
     * @return
     */
    private boolean updatePayPwd(UserVo userVo, String pwd) {

        //是否修改成功
        boolean update = true;
        //根据不同的用户类型 修改用户的支付密码
        switch (UserEnums.getUserEnums(userVo.getUserType())) {
            case customer:
                update = customerService.lambdaUpdate().set(Customer::getPayPwd, MD5EncryptionPwdUtil.encryptionMd5(pwd))
                        .eq(Customer::getId, userVo.getUserId()).update();
                break;
            case store:
                update = storeService.lambdaUpdate().set(Store::getPayPwd, MD5EncryptionPwdUtil.encryptionMd5(pwd))
                        .eq(Store::getId, userVo.getUserId()).update();
                break;
            case sale:
                update = saleService.lambdaUpdate().set(Sale::getPayPwd, MD5EncryptionPwdUtil.encryptionMd5(pwd))
                        .eq(Sale::getId, userVo.getUserId()).update();
                break;
            case enterprise:
                Store one = storeService.getOne(new QueryWrapper<Store>().lambda().eq(Store::getBindEnterpriseId, userVo.getUserId()));
                if (one != null) {
                    update = storeService.lambdaUpdate().set(Store::getPayPwd, MD5EncryptionPwdUtil.encryptionMd5(pwd))
                            .eq(Store::getId, one.getId()).update();
                }
                break;
            default:
                throw new CustomException(CoreExceptionEnum.ERROR_PARAM);
        }


        return update;
    }

    /**
     * 忘记密码
     *
     * @param req
     * @return
     */
    public ResponseData forgetPwd(UserForgetPwdLoginReq req) {

        businessUtil.checkCode(req.getCode(), SmsEnums.FORGET_PWD, req.getAreaCode(), req.getMobile(), req.getUserType());

        Long userId = getUserId(req.getUserType(), req.getMobile());

        //修改密码
        boolean update = updateLoginPwd(new UserVo(req.getUserType(), userId), req.getPwd());
        if (!update) {
            return ResponseData.error(CoreExceptionEnum.UPDATE_PWD_ERROR);
        }

        return ResponseData.success();
    }

    /**
     * 获取用户id
     *
     * @param userType
     * @param account  用户账号
     * @return
     */
    private Long getUserId(Integer userType, String account) {

        Long userId = null;

        //根据不同的用户类型 查询用户id
        switch (userType) {
            case 1:
                Customer customer = customerService.getBaseMapper().selectOne(new QueryWrapper<Customer>().lambda()
                        .eq(Customer::getMobile, account)
                        .ne(Customer::getDelFlag, PublicCommon.删除));

                if (customer == null) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_NOT_EXIST);
                } else if (customer.getDelFlag().equals(PublicCommon.禁用)) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_DISABLE);
                }

                userId = customer.getId();
                break;
            case 2:
                Store store = storeService.getBaseMapper().selectOne(new QueryWrapper<Store>().lambda()
                        .eq(Store::getMobile, account)
                        .ne(Store::getDelFlag, PublicCommon.删除));

                if (store == null) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_NOT_EXIST);
                } else if (store.getDelFlag().equals(PublicCommon.禁用)) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_DISABLE);
                }

                userId = store.getId();
                break;
            case 3:
                Sale sale = saleService.getBaseMapper().selectOne(new QueryWrapper<Sale>().lambda()
                        .eq(Sale::getMobile, account)
                        .ne(Sale::getDelFlag, PublicCommon.删除));

                if (sale == null) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_NOT_EXIST);
                } else if (sale.getDelFlag().equals(PublicCommon.禁用)) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_DISABLE);
                }

                userId = sale.getId();
                break;
            case 4:
                store = storeService.getBaseMapper().selectOne(new QueryWrapper<Store>().lambda()
                        .eq(Store::getMobile, account)
                        .ne(Store::getDelFlag, PublicCommon.删除));

                if (store == null) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_NOT_EXIST);
                } else if (store.getDelFlag().equals(PublicCommon.禁用)) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_DISABLE);
                }

                userId = store.getId();
                break;
            default:
                throw new CustomException(CoreExceptionEnum.ERROR_PARAM);
        }

        return userId;

    }

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 忘记支付密码
     *
     * @param req
     * @param userVo
     * @return
     */
    public ResponseData forgetPayPwd(UserForgetPwdLoginReq req, UserVo userVo) {

        businessUtil.checkCode(req.getCode(), SmsEnums.PAY_PWD, req.getAreaCode(), req.getMobile(), userVo.getUserType());

        //根据账号获取 用户id
        Long userId = getUserId(userVo.getUserType(), req.getMobile());

        //修改支付密码
        boolean update = updatePayPwd(new UserVo(userVo.getUserType(), userId), req.getPwd());
        if (!update) {
            return ResponseData.error(CoreExceptionEnum.UPDATE_PWD_ERROR);
        }

        return ResponseData.success();
    }


    /**
     * 修改支付密码
     *
     * @param pwd
     * @param userVo
     * @return
     */
    public ResponseData editPayPwd(String pwd, UserVo userVo) {

        //修改支付密码
        boolean update = updatePayPwd(userVo, pwd);
        if (!update) {
            return ResponseData.error(CoreExceptionEnum.UPDATE_PWD_ERROR);
        }

        return ResponseData.success();
    }


    /**
     * 退出
     *
     * @param userVo
     * @return
     */
    public ResponseData logout(UserVo userVo) {

        //根据不同的用户类型 清空client_id
        switch (UserEnums.getUserEnums(userVo.getUserType())) {
            case customer:
                customerService.lambdaUpdate().set(Customer::getClientId, "").eq(Customer::getId, userVo.getUserId()).update();
                break;
            case store:
//                storeService.lambdaUpdate().set(Store::getClientId, "").eq(Store::getId, userVo.getUserId()).update();
                break;
            case sale:
//                saleService.lambdaUpdate().set(Sale::getClientId, "").eq(Sale::getId, userVo.getUserId()).update();
                break;
            case enterprise:
                break;
            default:
                throw new CustomException(CoreExceptionEnum.ERROR_PARAM);
        }
        String token = request.getHeader("token");
        redisTemplate.delete(token);
        return ResponseData.success();
    }


    /**
     * 注销
     *
     * @param userVo
     * @return
     */
    public ResponseData cancellation(UserVo userVo) {

        //根据不同的用户类型 注销用户
        switch (userVo.getUserType()) {
            case 1:
                customerService.lambdaUpdate().set(Customer::getClientId, "")
                        .set(Customer::getCancellationTime, new Date())
                        .set(Customer::getDelFlag, PublicCommon.删除)
                        .eq(Customer::getId, userVo.getUserId()).update();
                break;
            case 2:
                storeService.lambdaUpdate()
                        .set(Store::getCancellationTime, new Date())
                        .set(Store::getDelFlag, PublicCommon.删除)
                        .eq(Store::getId, userVo.getUserId()).update();
                break;
            case 3:
                break;
            case 4:

                break;
            default:
                throw new CustomException(CoreExceptionEnum.ERROR_PARAM);
        }
        return ResponseData.success();

    }


    /**
     * 校验旧手机号
     *
     * @param req
     * @param userVo
     * @return
     */
    public ResponseData checkOldMobile(CheckOldMobileReq req, UserVo userVo) {

        //验证码验证
        businessUtil.checkCode(req.getOldCode(), SmsEnums.OLD_MOBILE_BIND, req.getAreaCode(), req.getOldMobile(), userVo.getUserType());
        return ResponseData.success();

    }

    /**
     * 换绑手机号
     *
     * @param req
     * @param userVo
     * @return
     */
    public ResponseData changeBindingMobile(ChangeBindingMobileReq req, UserVo userVo) {

        //验证码验证
        businessUtil.checkCode(req.getNewCode(), SmsEnums.NEW_MOBILE_BIND, req.getAreaCode(), req.getNewMobile(), userVo.getUserType());


        Long userId = getUserId(userVo.getUserType(), req.getOldMobile());

        if (!userId.equals(userVo.getUserId())) {
            return ResponseData.error("请使用绑定的手机号码");
        }

        //根据不同的用户类型 判断修改后的手机号是否存在 ,修改手机号
        switch (userVo.getUserType()) {
            case 1:
                Customer customerDb = customerService.getBaseMapper().selectOne(new QueryWrapper<Customer>().lambda()
                        .ne(Customer::getId, userVo.getUserId())
                        .eq(Customer::getMobile, req.getNewMobile()).ne(Customer::getDelFlag, PublicCommon.删除));
                if (customerDb != null) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_HAS_EXIST);
                }

                //修改手机号
                boolean update = customerService.lambdaUpdate().set(Customer::getAccount, req.getNewMobile()).set(Customer::getMobile, req.getNewMobile()).eq(Customer::getId, userId).update();
                if (!update) {
                    throw new CustomException(CoreExceptionEnum.UPDATE_MOBILE_ERROR);
                }
                break;
            case 2:
                Store storeDb = storeService.getBaseMapper().selectOne(new QueryWrapper<Store>().lambda()
                        .ne(Store::getId, userVo.getUserId())
                        .eq(Store::getMobile, req.getNewMobile()).ne(Store::getDelFlag, PublicCommon.删除));
                if (storeDb != null) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_HAS_EXIST);
                }

                //修改手机号
                boolean update2 = storeService.lambdaUpdate().set(Store::getAccount, req.getNewMobile()).set(Store::getMobile, req.getNewMobile()).eq(Store::getId, userId).update();
                if (!update2) {
                    throw new CustomException(CoreExceptionEnum.UPDATE_MOBILE_ERROR);
                }
                break;
            case 3:
                Sale saleDb = saleService.getBaseMapper().selectOne(new QueryWrapper<Sale>().lambda()
                        .ne(Sale::getId, userVo.getUserId())
                        .eq(Sale::getMobile, req.getNewMobile()).ne(Sale::getDelFlag, PublicCommon.删除));
                if (saleDb != null) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_HAS_EXIST);
                }

                //修改手机号
                boolean update3 = saleService.lambdaUpdate().set(Sale::getMobile, req.getNewMobile()).eq(Sale::getId, userId).update();
                if (!update3) {
                    throw new CustomException(CoreExceptionEnum.UPDATE_MOBILE_ERROR);
                }
                break;
            case 4:
                storeDb = storeService.getBaseMapper().selectOne(new QueryWrapper<Store>().lambda()
                        .ne(Store::getId, userVo.getUserId())
                        .eq(Store::getMobile, req.getNewMobile()).ne(Store::getDelFlag, PublicCommon.删除));
                if (storeDb != null) {
                    throw new CustomException(CoreExceptionEnum.ACCOUNT_HAS_EXIST);
                }

                //修改手机号
                update2 = storeService.lambdaUpdate().set(Store::getAccount, req.getNewMobile()).set(Store::getMobile, req.getNewMobile()).eq(Store::getId, userId).update();
                if (!update2) {
                    throw new CustomException(CoreExceptionEnum.UPDATE_MOBILE_ERROR);
                }
                break;
            default:
                throw new CustomException(CoreExceptionEnum.ERROR_PARAM);
        }

        return ResponseData.success();

    }


    /**
     * 校验支付密码
     *
     * @param pwd
     * @param userVo
     * @return
     */
    public ResponseData checkPayPwd(String pwd, UserVo userVo) {

        Boolean isTrue = businessUtil.payPwdTrue(userVo, pwd);
        if (!isTrue) {
            return ResponseData.error(CoreExceptionEnum.PWD_ERROR);
        }
        return ResponseData.success();
    }
}
