package com.starry.admin.modules.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.starry.admin.modules.order.mapper.PlayOrderRefundInfoMapper;
import com.starry.admin.modules.order.module.entity.PlayOrderRefundInfoEntity;
import com.starry.admin.modules.order.module.vo.PlayOrderRefundQueryVo;
import com.starry.admin.modules.order.module.vo.PlayOrderRefundReturnVo;
import com.starry.admin.modules.order.service.IPlayOrderRefundInfoService;
import com.starry.common.utils.IdUtils;
import java.math.BigDecimal;
import java.util.Arrays;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 订单退款信息Service业务层处理
 *
 * @author admin
 * @since 2024-05-09
 */
@Service
public class PlayOrderRefundInfoServiceImpl extends ServiceImpl<PlayOrderRefundInfoMapper, PlayOrderRefundInfoEntity>
        implements
            IPlayOrderRefundInfoService {
    @Resource
    private PlayOrderRefundInfoMapper playOrderRefundInfoMapper;

    @Override
    public PlayOrderRefundInfoEntity selectPlayOrderRefundInfoByOrderId(String orderId) {
        LambdaQueryWrapper<PlayOrderRefundInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayOrderRefundInfoEntity::getOrderId, orderId);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 查询订单退款信息
     *
     * @param id
     *            订单退款信息主键
     * @return 订单退款信息
     */
    @Override
    public PlayOrderRefundInfoEntity selectPlayOrderRefundInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public IPage<PlayOrderRefundReturnVo> selectByPage(PlayOrderRefundQueryVo vo) {
        MPJLambdaWrapper<PlayOrderRefundInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        return this.baseMapper.selectJoinPage(new Page<>(vo.getPageNum(), vo.getPageSize()),
                PlayOrderRefundReturnVo.class, lambdaQueryWrapper);
    }

    /**
     * 查询订单退款信息列表
     *
     * @param playOrderRefundInfo
     *            订单退款信息
     * @return 订单退款信息
     */
    @Override
    public IPage<PlayOrderRefundInfoEntity> selectPlayOrderRefundInfoByPage(
            PlayOrderRefundInfoEntity playOrderRefundInfo) {
        Page<PlayOrderRefundInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<PlayOrderRefundInfoEntity>());
    }

    @Override
    public void add(String orderId, String customId, String clerkId, String payMethod, String refundType,
            BigDecimal refundAmount, String refundReason, String refundByType, String refundById, String refundState,
            String reviewedRequired) {
        PlayOrderRefundInfoEntity orderRefundInfo = new PlayOrderRefundInfoEntity();
        orderRefundInfo.setOrderId(orderId);
        orderRefundInfo.setCustomId(customId);
        orderRefundInfo.setClerkId(clerkId);
        orderRefundInfo.setPayMethod(payMethod);
        orderRefundInfo.setRefundType(refundType);
        orderRefundInfo.setRefundAmount(refundAmount);
        orderRefundInfo.setRefundReason(refundReason);
        orderRefundInfo.setRefundByType(refundByType);
        orderRefundInfo.setRefundById(refundById);
        orderRefundInfo.setRefundState(refundState);
        orderRefundInfo.setReviewedRequired(reviewedRequired);
        this.save(orderRefundInfo);
    }

    /**
     * 新增订单退款信息
     *
     * @param playOrderRefundInfo
     *            订单退款信息
     * @return 结果
     */
    @Override
    public boolean create(PlayOrderRefundInfoEntity playOrderRefundInfo) {
        if (StrUtil.isBlankIfStr(playOrderRefundInfo.getId())) {
            playOrderRefundInfo.setId(IdUtils.getUuid());
        }
        return save(playOrderRefundInfo);
    }

    /**
     * 修改订单退款信息
     *
     * @param playOrderRefundInfo
     *            订单退款信息
     * @return 结果
     */
    @Override
    public boolean update(PlayOrderRefundInfoEntity playOrderRefundInfo) {
        return updateById(playOrderRefundInfo);
    }

    /**
     * 批量删除订单退款信息
     *
     * @param ids
     *            需要删除的订单退款信息主键
     * @return 结果
     */
    @Override
    public int deletePlayOrderRefundInfoByIds(String[] ids) {
        return playOrderRefundInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除订单退款信息信息
     *
     * @param id
     *            订单退款信息主键
     * @return 结果
     */
    @Override
    public int deletePlayOrderRefundInfoById(String id) {
        return playOrderRefundInfoMapper.deleteById(id);
    }
}
