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

import com.hotwj.platform.pay.domain.PayOrderExtension;
import com.hotwj.platform.pay.domain.bo.PayOrderExtensionBo;
import com.hotwj.platform.pay.domain.vo.PayOrderExtensionVo;
import com.hotwj.platform.pay.mapper.PayOrderExtensionMapper;
import com.hotwj.platform.pay.service.IPayOrderExtensionService;
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 PayOrderExtensionServiceImpl implements IPayOrderExtensionService {

    private final PayOrderExtensionMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<PayOrderExtension> buildQueryWrapper(PayOrderExtensionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayOrderExtension> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(PayOrderExtension::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getNo()), PayOrderExtension::getNo, bo.getNo());
        lqw.eq(bo.getOrderId() != null, PayOrderExtension::getOrderId, bo.getOrderId());
        lqw.eq(bo.getChannelId() != null, PayOrderExtension::getChannelId, bo.getChannelId());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelCode()), PayOrderExtension::getChannelCode, bo.getChannelCode());
        lqw.eq(StringUtils.isNotBlank(bo.getUserIp()), PayOrderExtension::getUserIp, bo.getUserIp());
        lqw.eq(bo.getStatus() != null, PayOrderExtension::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelExtras()), PayOrderExtension::getChannelExtras, bo.getChannelExtras());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelErrorCode()), PayOrderExtension::getChannelErrorCode, bo.getChannelErrorCode());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelErrorMsg()), PayOrderExtension::getChannelErrorMsg, bo.getChannelErrorMsg());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelNotifyData()), PayOrderExtension::getChannelNotifyData, bo.getChannelNotifyData());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), PayOrderExtension::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), PayOrderExtension::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, PayOrderExtension::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

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

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

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

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