package com.ruoyi.info.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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 org.springframework.stereotype.Service;
import com.ruoyi.info.order.domain.bo.OrderRefundBo;
import com.ruoyi.info.order.domain.vo.OrderRefundVo;
import com.ruoyi.info.order.domain.OrderRefund;
import com.ruoyi.info.order.mapper.OrderRefundMapper;
import com.ruoyi.info.order.service.IOrderRefundService;
import com.ruoyi.info.order.exception.OrderRefundExceptionEnum;

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

/**
 * 售后订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-29
 */
@RequiredArgsConstructor
@Service
public class OrderRefundServiceImpl implements IOrderRefundService {

    private final OrderRefundMapper baseMapper;

    /**
     * 查询售后订单分页
     *
     * @param bo 售后订单
     * @return 售后订单
     */
    @Override
    public TableDataInfo<OrderRefundVo> queryPageList(OrderRefundBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderRefund> lqw = buildQueryWrapper(bo);
        Page<OrderRefundVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询售后订单列表
     *
     * @param bo 售后订单
     * @return 售后订单
     */
    @Override
    public List<OrderRefundVo> queryList(OrderRefundBo bo) {
        LambdaQueryWrapper<OrderRefund> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OrderRefund> buildQueryWrapper(OrderRefundBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OrderRefund> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, OrderRefund::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getRefundReason()), OrderRefund::getRefundReason, bo.getRefundReason());
        lqw.eq(bo.getRefundQuantity() != null, OrderRefund::getRefundQuantity, bo.getRefundQuantity());
        lqw.eq(bo.getRefundAmount() != null, OrderRefund::getRefundAmount, bo.getRefundAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getRefundWay()), OrderRefund::getRefundWay, bo.getRefundWay());
        lqw.eq(StringUtils.isNotBlank(bo.getRefundDesc()), OrderRefund::getRefundDesc, bo.getRefundDesc());
        lqw.eq(bo.getRefundStatus() != null, OrderRefund::getRefundStatus, bo.getRefundStatus());
        lqw.eq(bo.getActualRefundAmount() != null, OrderRefund::getActualRefundAmount, bo.getActualRefundAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationReason()), OrderRefund::getOperationReason, bo.getOperationReason());
        lqw.eq(bo.getOperationTime() != null, OrderRefund::getOperationTime, bo.getOperationTime());
        lqw.eq(bo.getOperatorId() != null, OrderRefund::getOperatorId, bo.getOperatorId());
        return lqw;
    }

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

    /**
     * 详情售后订单
     *
     * @param id 售后订单主键
     * @return 售后订单
     */
    @Override
    public OrderRefund loadById(Long id, Boolean tw){
        OrderRefund info = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(OrderRefundExceptionEnum.OrderRefund_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增售后订单
     *
     * @param bo 售后订单
     * @return 结果
     */
    @Override
    public Boolean insertByBo(OrderRefundBo bo) {
        OrderRefund add = BeanUtil.toBean(bo, OrderRefund.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改售后订单
     *
     * @param bo 售后订单
     * @return 结果
     */
    @Override
    public Boolean updateByBo(OrderRefundBo bo) {
        OrderRefund orderRefund = baseMapper.selectById(bo.getId());
        OrderRefund update = BeanCopyUtils.copy(bo, orderRefund);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(OrderRefund entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除售后订单
     *
     * @param ids 需要删除的售后订单主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
