package com.playlet.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.playlet.common.exception.ServiceException;
import com.playlet.common.utils.AesUtils;
import com.playlet.common.utils.DateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.playlet.system.mapper.PlayletUserWithdrawalPasswordMapper;
import com.playlet.system.domain.PlayletUserWithdrawalPassword;
import com.playlet.system.service.IPlayletUserWithdrawalPasswordService;
import com.playlet.common.core.text.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Date: 2024-03-30 12:26
 * @Author: 但星霖
 * @Version: v1.0
 * @Description: 用户提现密码Service业务层处理
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PlayletUserWithdrawalPasswordServiceImpl extends ServiceImpl<PlayletUserWithdrawalPasswordMapper, PlayletUserWithdrawalPassword> implements IPlayletUserWithdrawalPasswordService {


    private final PlayletUserWithdrawalPasswordMapper playletUserWithdrawalPasswordMapper;

    /**
     * 查询用户提现密码
     *
     * @param id 用户提现密码主键
     * @return 用户提现密码
     */
    @Override
    public PlayletUserWithdrawalPassword selectPlayletUserWithdrawalPasswordById(Long id) {
        return playletUserWithdrawalPasswordMapper.selectPlayletUserWithdrawalPasswordById(id);
    }

    /**
     * 查询用户提现密码列表
     *
     * @param playletUserWithdrawalPassword 用户提现密码
     * @return 用户提现密码
     */
    @Override
    public List<PlayletUserWithdrawalPassword> selectPlayletUserWithdrawalPasswordList(PlayletUserWithdrawalPassword playletUserWithdrawalPassword) {
        return playletUserWithdrawalPasswordMapper.selectPlayletUserWithdrawalPasswordList(playletUserWithdrawalPassword);
    }

    /**
     * 新增用户提现密码
     *
     * @param playletUserWithdrawalPassword 用户提现密码
     * @return 结果
     */
    @Override
    public int insertPlayletUserWithdrawalPassword(PlayletUserWithdrawalPassword playletUserWithdrawalPassword) {
        // 根据userId进行查询 一个用户只能存在一个密码
        QueryWrapper<PlayletUserWithdrawalPassword> wrapper = new QueryWrapper<>();
        wrapper.lambda().select().eq(PlayletUserWithdrawalPassword::getUserId, playletUserWithdrawalPassword.getUserId());
        PlayletUserWithdrawalPassword withdrawalPasswordByUserCrw = playletUserWithdrawalPasswordMapper.selectOne(wrapper);
        if (ObjectUtils.isNotNull(withdrawalPasswordByUserCrw)) {
            throw new ServiceException("当前已存在提现密码数据。请勿重复添加。");
        }
        // 密码加密比对。
        if (!Objects.equals(AesUtils.aesEncrypt(playletUserWithdrawalPassword.getConfirmPassword()), AesUtils.aesEncrypt(playletUserWithdrawalPassword.getPassword()))) {
            throw new ServiceException("密码新增失败，两次密码不同。");
        }
        playletUserWithdrawalPassword.setPassword(AesUtils.aesEncrypt(playletUserWithdrawalPassword.getConfirmPassword()));
        playletUserWithdrawalPassword.setCreateTime(DateUtils.getNowDate());
        playletUserWithdrawalPassword.setUpdateTime(DateUtils.getNowDate());
        return playletUserWithdrawalPasswordMapper.insertPlayletUserWithdrawalPassword(playletUserWithdrawalPassword);
    }

    /**
     * 修改用户提现密码
     *
     * @param playletUserWithdrawalPassword 用户提现密码
     * @return 结果
     */
    @Override
    public int updatePlayletUserWithdrawalPassword(PlayletUserWithdrawalPassword playletUserWithdrawalPassword) {
        QueryWrapper<PlayletUserWithdrawalPassword> wrapper = new QueryWrapper<>();
        wrapper.lambda().select().eq(PlayletUserWithdrawalPassword::getUserId, playletUserWithdrawalPassword.getUserId());
        PlayletUserWithdrawalPassword withdrawalPasswordByUserCrw = playletUserWithdrawalPasswordMapper.selectOne(wrapper);
        if (ObjectUtils.isNotNull(withdrawalPasswordByUserCrw)) {
            throw new ServiceException("当前用户未设置提现密码。请先新建密码。");
        }
        // 密码加密比对。
        if (!Objects.equals(withdrawalPasswordByUserCrw.getPassword(), AesUtils.aesEncrypt(playletUserWithdrawalPassword.getOriginalPassword()))) {
            throw new ServiceException("密码修改失败，原始密码错误。");
        }
        // 密码加密比对。
        if (!Objects.equals(AesUtils.aesEncrypt(playletUserWithdrawalPassword.getConfirmPassword()), AesUtils.aesEncrypt(playletUserWithdrawalPassword.getPassword()))) {
            throw new ServiceException("密码修改失败，两次密码不同。");
        }
        playletUserWithdrawalPassword.setPassword(AesUtils.aesEncrypt(playletUserWithdrawalPassword.getConfirmPassword()));
        playletUserWithdrawalPassword.setUpdateTime(DateUtils.getNowDate());
        return playletUserWithdrawalPasswordMapper.updatePlayletUserWithdrawalPassword(playletUserWithdrawalPassword);
    }

    /**
     * 批量删除用户提现密码
     *
     * @param ids 需要删除的用户提现密码主键
     * @return 结果
     */
    @Override
    public int deletePlayletUserWithdrawalPasswordByIds(String ids) {
        return playletUserWithdrawalPasswordMapper.deletePlayletUserWithdrawalPasswordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户提现密码信息
     *
     * @param id 用户提现密码主键
     * @return 结果
     */
    @Override
    public int deletePlayletUserWithdrawalPasswordById(Long id) {
        return playletUserWithdrawalPasswordMapper.deletePlayletUserWithdrawalPasswordById(id);
    }

    @Override
    public Boolean validatePassword(PlayletUserWithdrawalPassword password) {
        QueryWrapper<PlayletUserWithdrawalPassword> wrapper = new QueryWrapper<>();
        wrapper.lambda().select().eq(PlayletUserWithdrawalPassword::getUserId, password.getUserId());
        PlayletUserWithdrawalPassword withdrawalPasswordByUserCrw = playletUserWithdrawalPasswordMapper.selectOne(wrapper);
        if (ObjectUtils.isNotNull(withdrawalPasswordByUserCrw)) {
            throw new ServiceException("验证提现密码失败，用户未设置提现密码。");
        }
        // 密码加密比对。
        if (!Objects.equals(withdrawalPasswordByUserCrw.getPassword(), AesUtils.aesEncrypt(password.getPassword()))) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
