package com.seer.teach.pay.admin.withdrawal.service;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayWithdrawalApplyPageRequest;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayWithdrawalApplyReq;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayWithdrawalAuditReq;
import com.seer.teach.pay.admin.withdrawal.controller.response.PayWithdrawalApplyResponse;
import com.seer.teach.pay.admin.withdrawal.convert.PayWithdrawalApplyConvert;
import com.seer.teach.pay.entity.PayMerchantBankAccountEntity;
import com.seer.teach.pay.entity.PayMerchantWithdrawalApplyEntity;
import com.seer.teach.pay.service.IPayMerchantBankAccountService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@RequiredArgsConstructor
@Service
public class AdminPayWithdrawalApplyQueryService {

    private final com.seer.teach.pay.service.IPayMerchantWithdrawalApplyService payWithdrawalApplyService;

    private final IPayMerchantBankAccountService payMerchantBankAccountService;

    private final IAdminPayMerchantWithdrawalApplyService payMerchantWithdrawalApplyCoreService;

    public PageListBean<PayWithdrawalApplyResponse> pageList(PayWithdrawalApplyPageRequest pageRequest) {
        IPage<PayMerchantWithdrawalApplyEntity> page = new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize());

        IPage<PayMerchantWithdrawalApplyEntity> result = payWithdrawalApplyService.page(page, new LambdaQueryWrapper<PayMerchantWithdrawalApplyEntity>()
                .eq(Objects.nonNull(pageRequest.getUserId()), PayMerchantWithdrawalApplyEntity::getUserId, pageRequest.getUserId())
                .eq(Objects.nonNull(pageRequest.getAccountId()), PayMerchantWithdrawalApplyEntity::getAccountId, pageRequest.getAccountId())
                .eq(Objects.nonNull(pageRequest.getApplyStatus()), PayMerchantWithdrawalApplyEntity::getApplyStatus, pageRequest.getApplyStatus())
                .eq(StrUtil.isNotBlank(pageRequest.getWithdrawalNo()), PayMerchantWithdrawalApplyEntity::getWithdrawalNo, pageRequest.getWithdrawalNo())
                .orderByDesc(PayMerchantWithdrawalApplyEntity::getApplyTime));

        return PageConverterUtils.convertPageListBean(result, PayWithdrawalApplyConvert.INSTANCE::entityListToResponseList);
    }

    public List<PayWithdrawalApplyResponse> list(PayWithdrawalApplyPageRequest queryParam) {
        List<PayMerchantWithdrawalApplyEntity> result = payWithdrawalApplyService.list(new LambdaQueryWrapper<PayMerchantWithdrawalApplyEntity>()
                .eq(Objects.nonNull(queryParam.getUserId()), PayMerchantWithdrawalApplyEntity::getUserId, queryParam.getUserId())
                .eq(Objects.nonNull(queryParam.getAccountId()), PayMerchantWithdrawalApplyEntity::getAccountId, queryParam.getAccountId())
                .eq(Objects.nonNull(queryParam.getApplyStatus()), PayMerchantWithdrawalApplyEntity::getApplyStatus, queryParam.getApplyStatus()));
        return PayWithdrawalApplyConvert.INSTANCE.entityListToResponseList(result);
    }

    public PayWithdrawalApplyResponse getDetail(Long id) {
        Integer userId = StpUtil.getLoginIdAsInt();

        PayMerchantWithdrawalApplyEntity result = payWithdrawalApplyService.getById(id);
        log.info("getById:{}", result);
        PayMerchantBankAccountEntity bankAccount = payMerchantBankAccountService.getOne(new LambdaQueryWrapper<PayMerchantBankAccountEntity>()
                .eq(PayMerchantBankAccountEntity::getUserId, userId).eq(PayMerchantBankAccountEntity::getId, result.getAccountId()));
        // 初始化默认值
        String accountNo = "";
        Integer accountType = null;
        // 根据提现申请中的 accountId 匹配对应的银行账户信息
        if (result != null && result.getAccountId() != null) {
            if (bankAccount.getId().equals(result.getAccountId())) {
                accountNo = bankAccount.getAccountNo();
                accountType = bankAccount.getAccountType();
            }
        }
        PayWithdrawalApplyResponse response = PayWithdrawalApplyConvert.INSTANCE.entityToResponse(result);
        response.setAccountNo(accountNo);
        response.setAccountType(accountType);
        return response;
    }

    /**
     * 申请提现
     *
     * @param payWithdrawalApply 提现申请的请求参数
     * @return String 提现申请单号
     */
    public String applyWithdrawal(PayWithdrawalApplyReq payWithdrawalApply) {
        Long userId = StpUtil.getLoginIdAsLong();
        return payMerchantWithdrawalApplyCoreService.applyWithdrawal(userId, payWithdrawalApply);
    }

    /**
     * 审核提现申请
     *
     * @param id           提现申请ID
     * @param auditRequest 审核请求参数
     * @return Boolean      审核结果
     */
    public boolean auditWithdrawal(Integer id, PayWithdrawalAuditReq auditRequest) {
        Long userId = StpUtil.getLoginIdAsLong();
        return payMerchantWithdrawalApplyCoreService.auditWithdrawal(id, auditRequest, userId);
    }

    public Boolean updateById(PayWithdrawalApplyReq payWithdrawalApply) {
        PayMerchantWithdrawalApplyEntity entity = PayWithdrawalApplyConvert.INSTANCE.reqToEntity(payWithdrawalApply);
        boolean updated = payWithdrawalApplyService.updateById(entity);
        log.info("updateById:{}", updated);
        return updated;
    }

    public Boolean deleteById(Integer id) {
        boolean updated = payWithdrawalApplyService.removeById(id);
        log.info("deleteById:{}", updated);
        return updated;
    }
}
