package com.playlet.system.service.impl;

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

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.DateUtils;
import com.playlet.common.utils.uuid.UUID;
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.PlayletUserWithdrawDepositRecordMapper;
import com.playlet.system.domain.PlayletUserWithdrawDepositRecord;
import com.playlet.system.service.IPlayletUserWithdrawDepositRecordService;
import com.playlet.common.core.text.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Date: 2024-03-29 23:58
 * @Author: 但星霖
 * @Version: v1.0
 * @Description: 用户交易记录Service业务层处理
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PlayletUserWithdrawDepositRecordServiceImpl extends ServiceImpl<PlayletUserWithdrawDepositRecordMapper, PlayletUserWithdrawDepositRecord> implements IPlayletUserWithdrawDepositRecordService {

    private final PlayletUserWithdrawDepositRecordMapper playletUserWithdrawDepositRecordMapper;

    /**
     * 查询用户交易记录
     *
     * @param id 用户交易记录主键
     * @return 用户交易记录
     */
    @Override
    public PlayletUserWithdrawDepositRecord selectPlayletUserWithdrawDepositRecordById(Long id) {
        return playletUserWithdrawDepositRecordMapper.selectPlayletUserWithdrawDepositRecordById(id);
    }

    /**
     * 查询用户交易记录列表
     *
     * @param playletUserWithdrawDepositRecord 用户交易记录
     * @return 用户交易记录
     */
    @Override
    public List<PlayletUserWithdrawDepositRecord> selectPlayletUserWithdrawDepositRecordList(PlayletUserWithdrawDepositRecord playletUserWithdrawDepositRecord) {
        return playletUserWithdrawDepositRecordMapper.selectPlayletUserWithdrawDepositRecordList(playletUserWithdrawDepositRecord);
    }

    /**
     * 新增用户交易记录
     *
     * @param playletUserWithdrawDepositRecord 用户交易记录
     * @return 结果
     */
    @Override
    public int insertPlayletUserWithdrawDepositRecord(PlayletUserWithdrawDepositRecord playletUserWithdrawDepositRecord) {
        playletUserWithdrawDepositRecord.setCreateTime(DateUtils.getNowDate());
        playletUserWithdrawDepositRecord.setUpdateTime(DateUtils.getNowDate());
        // 订单号生成
        String uuIdMsg = null;
        for (; ; ) {
            uuIdMsg = UUID.randomUUID().toString();
            QueryWrapper<PlayletUserWithdrawDepositRecord> wrapper = new QueryWrapper<>();
            wrapper.lambda().select().eq(PlayletUserWithdrawDepositRecord::getWithdrawOrder, uuIdMsg);
            PlayletUserWithdrawDepositRecord recordByDb = playletUserWithdrawDepositRecordMapper.selectOne(wrapper);
            if (ObjectUtils.isNull(recordByDb)) {
                break;
            }
        }
        // todo:根据银行手续费提示，进行修改金额。待补充。
        playletUserWithdrawDepositRecord.setWithdrawOrder(UUID.randomUUID().toString());
        return playletUserWithdrawDepositRecordMapper.insertPlayletUserWithdrawDepositRecord(playletUserWithdrawDepositRecord);
    }

    /**
     * 修改用户交易记录
     *
     * @param playletUserWithdrawDepositRecord 用户交易记录
     * @return 结果
     */
    @Override
    public int updatePlayletUserWithdrawDepositRecord(PlayletUserWithdrawDepositRecord playletUserWithdrawDepositRecord) {
        // 通过订单号去查询 如果没有当前订单号禁止修改。
        QueryWrapper<PlayletUserWithdrawDepositRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda().select().eq(PlayletUserWithdrawDepositRecord::getWithdrawOrder, playletUserWithdrawDepositRecord.getWithdrawOrder());
        PlayletUserWithdrawDepositRecord recordByDb = playletUserWithdrawDepositRecordMapper.selectOne(wrapper);
        if (ObjectUtils.isNull(recordByDb)) {
            throw new ServiceException("当前订单号不存在，禁止修改");
        }
        // todo:根据银行手续费提示，进行修改金额。待补充。
        playletUserWithdrawDepositRecord.setUpdateTime(DateUtils.getNowDate());
        return playletUserWithdrawDepositRecordMapper.updatePlayletUserWithdrawDepositRecord(playletUserWithdrawDepositRecord);
    }

    /**
     * 批量删除用户交易记录
     *
     * @param ids 需要删除的用户交易记录主键
     * @return 结果
     */
    @Override
    public int deletePlayletUserWithdrawDepositRecordByIds(String ids) {
        return playletUserWithdrawDepositRecordMapper.deletePlayletUserWithdrawDepositRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户交易记录信息
     *
     * @param id 用户交易记录主键
     * @return 结果
     */
    @Override
    public int deletePlayletUserWithdrawDepositRecordById(Long id) {
        return playletUserWithdrawDepositRecordMapper.deletePlayletUserWithdrawDepositRecordById(id);
    }

    @Override
    public Boolean recordCallbackUpdate(String orderId, Long status) {
        // 通过订单号去查询 如果没有当前订单号禁止修改。
        QueryWrapper<PlayletUserWithdrawDepositRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda().select().eq(PlayletUserWithdrawDepositRecord::getWithdrawOrder, orderId);
        PlayletUserWithdrawDepositRecord recordByDb = playletUserWithdrawDepositRecordMapper.selectOne(wrapper);
        if (ObjectUtils.isNull(recordByDb)) {
            throw new ServiceException("当前订单号不存在，回调更新失败");
        }

        // 更新状态 1 申请提现 2 审批通过 3 交易完成 4 审批不通过
        if (status == 1L || status == 2L || status == 3L || status == 4L) {
        } else {
            throw new ServiceException("状态信息异常。回调更新失败。");
        }
        recordByDb.setStatus(status);
        recordByDb.setUpdateTime(DateUtils.getNowDate());
        return playletUserWithdrawDepositRecordMapper.updatePlayletUserWithdrawDepositRecord(recordByDb) > 0;
    }
}
