package com.hotwj.platform.operationManagement.fundRechargeOrder.service.impl;

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 com.hotwj.platform.operationManagement.fundRechargeOrder.domain.bo.HotFundRechargeOrderBo;
import com.hotwj.platform.operationManagement.fundRechargeOrder.domain.vo.HotFundRechargeOrderVo;
import com.hotwj.platform.operationManagement.fundRechargeOrder.domain.HotFundRechargeOrder;
import com.hotwj.platform.operationManagement.fundRechargeOrder.mapper.HotFundRechargeOrderMapper;
import com.hotwj.platform.operationManagement.fundRechargeOrder.service.IHotFundRechargeOrderService;

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

/**
 * 充值订单Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotFundRechargeOrderServiceImpl implements IHotFundRechargeOrderService {

    private final HotFundRechargeOrderMapper baseMapper;

    /**
     * 查询充值订单
     *
     * @param id 主键
     * @return 充值订单
     */
    @Override
    public HotFundRechargeOrderVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询充值订单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 充值订单分页列表
     */
    @Override
    public TableDataInfo<HotFundRechargeOrderVo> queryPageList(HotFundRechargeOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotFundRechargeOrder> lqw = buildQueryWrapper(bo);
        Page<HotFundRechargeOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的充值订单列表
     *
     * @param bo 查询条件
     * @return 充值订单列表
     */
    @Override
    public List<HotFundRechargeOrderVo> queryList(HotFundRechargeOrderBo bo) {
        LambdaQueryWrapper<HotFundRechargeOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotFundRechargeOrder> buildQueryWrapper(HotFundRechargeOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotFundRechargeOrder> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotFundRechargeOrder::getId);
        lqw.eq(bo.getCompanyId() != null, HotFundRechargeOrder::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getAccountId() != null, HotFundRechargeOrder::getAccountId, bo.getAccountId());
        lqw.eq(bo.getUserId() != null, HotFundRechargeOrder::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getRechargeOrderNo()), HotFundRechargeOrder::getRechargeOrderNo, bo.getRechargeOrderNo());
        lqw.eq(bo.getRechargeAmount() != null, HotFundRechargeOrder::getRechargeAmount, bo.getRechargeAmount());
        lqw.eq(bo.getPayMethod() != null, HotFundRechargeOrder::getPayMethod, bo.getPayMethod());
        lqw.eq(bo.getPayScene() != null, HotFundRechargeOrder::getPayScene, bo.getPayScene());
        lqw.eq(bo.getPayStatus() != null, HotFundRechargeOrder::getPayStatus, bo.getPayStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getThirdTradeNo()), HotFundRechargeOrder::getThirdTradeNo, bo.getThirdTradeNo());
        lqw.eq(bo.getPaidTime() != null, HotFundRechargeOrder::getPaidTime, bo.getPaidTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFailReason()), HotFundRechargeOrder::getFailReason, bo.getFailReason());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotFundRechargeOrder::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotFundRechargeOrder::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotFundRechargeOrder::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增充值订单
     *
     * @param bo 充值订单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotFundRechargeOrderBo bo) {
        HotFundRechargeOrder add = MapstructUtils.convert(bo, HotFundRechargeOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改充值订单
     *
     * @param bo 充值订单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotFundRechargeOrderBo bo) {
        HotFundRechargeOrder update = MapstructUtils.convert(bo, HotFundRechargeOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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