package com.hotwj.platform.pay.service.impl;

import com.hotwj.platform.pay.domain.PayWalletTransaction;
import com.hotwj.platform.pay.domain.PayWallet;
import com.hotwj.platform.pay.domain.bo.PayWalletTransactionBo;
import com.hotwj.platform.pay.domain.vo.PayWalletTransactionVo;
import com.hotwj.platform.pay.mapper.PayWalletMapper;
import com.hotwj.platform.pay.mapper.PayWalletTransactionMapper;
import com.hotwj.platform.pay.service.IPayWalletTransactionService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会员钱包流水Service业务层处理
 *
 * @author shihongwei
 * @date 2026-05-08
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PayWalletTransactionServiceImpl implements IPayWalletTransactionService {

    private static final Long COMPANY_WALLET_TYPE = 2L;

    private final PayWalletTransactionMapper baseMapper;
    private final PayWalletMapper payWalletMapper;

    /**
     * 查询会员钱包流水
     *
     * @param id 主键
     * @return 会员钱包流水
     */
    @Override
    public PayWalletTransactionVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询会员钱包流水列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 会员钱包流水分页列表
     */
    @Override
    public TableDataInfo<PayWalletTransactionVo> queryPageList(PayWalletTransactionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayWalletTransaction> lqw = buildQueryWrapper(bo);
        Page<PayWalletTransactionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<PayWalletTransactionVo> queryCompanyWalletPage(Long companyId, PayWalletTransactionBo bo, PageQuery pageQuery) {
        Long walletId = resolveCompanyWalletId(companyId);
        PayWalletTransactionBo queryBo = bo == null ? new PayWalletTransactionBo() : bo;
        // 企业钱包流水查询必须锁定到目标企业钱包，避免越权传参。
        queryBo.setWalletId(walletId == null ? -1L : walletId);
        LambdaQueryWrapper<PayWalletTransaction> lqw = buildQueryWrapper(queryBo);
        Page<PayWalletTransactionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的会员钱包流水列表
     *
     * @param bo 查询条件
     * @return 会员钱包流水列表
     */
    @Override
    public List<PayWalletTransactionVo> queryList(PayWalletTransactionBo bo) {
        LambdaQueryWrapper<PayWalletTransaction> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayWalletTransaction> buildQueryWrapper(PayWalletTransactionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayWalletTransaction> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(PayWalletTransaction::getId);
        lqw.eq(bo.getWalletId() != null, PayWalletTransaction::getWalletId, bo.getWalletId());
        lqw.eq(bo.getBizType() != null, PayWalletTransaction::getBizType, bo.getBizType());
        lqw.eq(StringUtils.isNotBlank(bo.getBizId()), PayWalletTransaction::getBizId, bo.getBizId());
        lqw.eq(StringUtils.isNotBlank(bo.getNo()), PayWalletTransaction::getNo, bo.getNo());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), PayWalletTransaction::getTitle, bo.getTitle());
        lqw.eq(bo.getPrice() != null, PayWalletTransaction::getPrice, bo.getPrice());
        lqw.eq(bo.getBalance() != null, PayWalletTransaction::getBalance, bo.getBalance());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), PayWalletTransaction::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), PayWalletTransaction::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, PayWalletTransaction::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    private Long resolveCompanyWalletId(Long companyId) {
        if (companyId == null) {
            return null;
        }
        PayWallet wallet = payWalletMapper.selectOne(Wrappers.<PayWallet>lambdaQuery()
                .eq(PayWallet::getUserId, companyId)
                .eq(PayWallet::getWalletType, COMPANY_WALLET_TYPE)
                .last("limit 1"));
        return wallet == null ? null : wallet.getId();
    }

    /**
     * 新增会员钱包流水
     *
     * @param bo 会员钱包流水
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PayWalletTransactionBo bo) {
        PayWalletTransaction add = MapstructUtils.convert(bo, PayWalletTransaction.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改会员钱包流水
     *
     * @param bo 会员钱包流水
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PayWalletTransactionBo bo) {
        PayWalletTransaction update = MapstructUtils.convert(bo, PayWalletTransaction.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PayWalletTransaction entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除会员钱包流水信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
