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

import com.hotwj.platform.pay.domain.PayOrder;
import com.hotwj.platform.pay.domain.bo.PayOrderBo;
import com.hotwj.platform.pay.domain.vo.PayOrderVo;
import com.hotwj.platform.pay.mapper.PayOrderMapper;
import com.hotwj.platform.pay.service.IPayOrderService;
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 PayOrderServiceImpl implements IPayOrderService {

    private final PayOrderMapper baseMapper;

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

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

    /**
     * 查询符合条件的支付订单
     * 列表
     *
     * @param bo 查询条件
     * @return 支付订单
     * 列表
     */
    @Override
    public List<PayOrderVo> queryList(PayOrderBo bo) {
        LambdaQueryWrapper<PayOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayOrder> buildQueryWrapper(PayOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayOrder> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(PayOrder::getId);
        lqw.eq(bo.getAppId() != null, PayOrder::getAppId, bo.getAppId());
        lqw.eq(bo.getChannelId() != null, PayOrder::getChannelId, bo.getChannelId());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelCode()), PayOrder::getChannelCode, bo.getChannelCode());
        lqw.eq(StringUtils.isNotBlank(bo.getNotifyUrl()), PayOrder::getNotifyUrl, bo.getNotifyUrl());
        lqw.eq(bo.getPrice() != null, PayOrder::getPrice, bo.getPrice());
        lqw.eq(bo.getStatus() != null, PayOrder::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getUserIp()), PayOrder::getUserIp, bo.getUserIp());
        lqw.eq(bo.getExpireTime() != null, PayOrder::getExpireTime, bo.getExpireTime());
        lqw.eq(bo.getSuccessTime() != null, PayOrder::getSuccessTime, bo.getSuccessTime());
        lqw.eq(bo.getExtensionId() != null, PayOrder::getExtensionId, bo.getExtensionId());
        lqw.eq(StringUtils.isNotBlank(bo.getNo()), PayOrder::getNo, bo.getNo());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelUserId()), PayOrder::getChannelUserId, bo.getChannelUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelOrderNo()), PayOrder::getChannelOrderNo, bo.getChannelOrderNo());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), PayOrder::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), PayOrder::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, PayOrder::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

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

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

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

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