package com.jeesharp.business.modules.pay.payfundwithdraw.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.pay.paychannelconfig.support.PayChannelTenantSupport;
import com.jeesharp.business.modules.pay.payfundwithdraw.entity.PayFundWithdraw;
import com.jeesharp.business.modules.pay.payfundwithdraw.enums.PayFundWithdrawStatusEnum;
import com.jeesharp.business.modules.pay.payfundwithdraw.mapper.PayFundWithdrawMapper;
import com.jeesharp.business.modules.pay.payfundwithdraw.service.PayFundWithdrawService;
import com.jeesharp.business.modules.pay.payfundwithdraw.vo.PayFundWithdrawQueryVO;
import com.jeesharp.framework.pojo.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 预约提现领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-12
 */
@Service
@Transactional(readOnly = true)
public class PayFundWithdrawServiceImpl
        extends com.jeesharp.framework.mybatis.core.service.CrudService<PayFundWithdrawMapper, PayFundWithdraw, PayFundWithdrawQueryVO>
        implements PayFundWithdrawService {

    @Override
    @Transactional(readOnly = false)
    public PayFundWithdraw createFundWithdraw(PayFundWithdraw entity) {
        if (StrUtil.isNotBlank(entity.getId())) {
            throw new IllegalStateException("新增提现记录不应预置主键");
        }
        if (StrUtil.isBlank(entity.getWithdrawStatus())) {
            entity.setWithdrawStatus(PayFundWithdrawStatusEnum.CREATE_SUCCESS.getValue());
        }
        entity.preInsert();
        getBaseMapper().insert(entity);
        return entity;
    }

    @Override
    @Transactional(readOnly = false)
    public void updateFundWithdraw(PayFundWithdraw entity) {
        if (StrUtil.isBlank(entity.getId())) {
            throw new IllegalArgumentException("更新提现记录主键不能为空");
        }
        if (get(entity.getId()) == null) {
            throw new IllegalArgumentException("提现记录不存在");
        }
        entity.preUpdate();
        getBaseMapper().updateById(entity);
    }

    @Override
    public PayFundWithdraw findByOutRequestNo(String outRequestNo) {
        if (StrUtil.isBlank(outRequestNo)) {
            return null;
        }
        LambdaQueryWrapper<PayFundWithdraw> w = new LambdaQueryWrapper<>();
        w.eq(PayFundWithdraw::getOutRequestNo, outRequestNo.trim());
        w.last("LIMIT 1");
        return getBaseMapper().selectOne(w);
    }

    @Override
    public Page<PayFundWithdraw> findScopePage(Page<PayFundWithdraw> page, PayFundWithdrawQueryVO query) {
        LambdaQueryWrapper<PayFundWithdraw> w = new LambdaQueryWrapper<>();
        if (query != null) {
            if (StrUtil.isNotBlank(query.getScopeType())) {
                w.eq(PayFundWithdraw::getScopeType, query.getScopeType().trim());
            }
            if (StrUtil.isNotBlank(query.getApplymentTenantId())) {
                w.eq(PayFundWithdraw::getApplymentTenantId,
                        PayChannelTenantSupport.toStorageKey(query.getApplymentTenantId()));
            }
            if (StrUtil.isNotBlank(query.getWithdrawStatus())) {
                w.eq(PayFundWithdraw::getWithdrawStatus, query.getWithdrawStatus().trim());
            }
        }
        w.orderByDesc(PayFundWithdraw::getSubmitDate);
        w.orderByDesc(PayFundWithdraw::getCreateDate);
        return getBaseMapper().selectPage(page, w);
    }
}
