package com.yiqi.order.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.OrderNoUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.order.domain.OrderPayInfo;
import com.yiqi.order.domain.dto.PayRefundDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.order.mapper.OrderRefundInfoMapper;
import com.yiqi.order.domain.OrderRefundInfo;
import com.yiqi.order.service.IOrderRefundInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 第三方退款记录Service业务层处理
 *
 * @author douya
 * @date 2024-09-16
 */
@Service
public class OrderRefundInfoServiceImpl extends ServiceImpl<OrderRefundInfoMapper, OrderRefundInfo> implements IOrderRefundInfoService {

    @Autowired
    private OrderNoUtils orderNoUtils;

    /**
     * 查询第三方退款记录
     *
     * @param id 第三方退款记录主键
     * @return 第三方退款记录
     */
    @Override
    public OrderRefundInfo selectOrderRefundInfoById(Long id) {
        return baseMapper.selectOrderRefundInfoById(id);
    }

    /**
     * 查询第三方退款记录列表
     *
     * @param orderRefundInfo 第三方退款记录
     * @return 第三方退款记录
     */
    @Override
    public List<OrderRefundInfo> selectOrderRefundInfoList(OrderRefundInfo orderRefundInfo) {
        return baseMapper.selectOrderRefundInfoList(orderRefundInfo);
    }

    /**
     * 新增第三方退款记录
     *
     * @param orderRefundInfo 第三方退款记录
     * @return 结果
     */
    @Override
    public int insertOrderRefundInfo(OrderRefundInfo orderRefundInfo) {
        orderRefundInfo.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(orderRefundInfo);
    }

    /**
     * 修改第三方退款记录
     *
     * @param orderRefundInfo 第三方退款记录
     * @return 结果
     */
    @Override
    public int updateOrderRefundInfo(OrderRefundInfo orderRefundInfo) {
        return baseMapper.updateById(orderRefundInfo);
    }

    /**
     * 批量删除第三方退款记录
     *
     * @param ids 需要删除的第三方退款记录主键
     * @return 结果
     */
    @Override
    public int deleteOrderRefundInfoByIds(Long[] ids) {
        return baseMapper.deleteOrderRefundInfoByIds(ids);
    }

    /**
     * 删除第三方退款记录信息
     *
     * @param id 第三方退款记录主键
     * @return 结果
     */
    @Override
    public int deleteOrderRefundInfoById(Long id) {
        return baseMapper.deleteOrderRefundInfoById(id);
    }

    @Override
    public OrderRefundInfo createOrderRefundInfo(PayRefundDTO payRefundDTO) {
        OrderRefundInfo orderRefundInfo = new OrderRefundInfo();
        orderRefundInfo.setStoreId(payRefundDTO.getStoreId());
        orderRefundInfo.setOriginOrderNo(payRefundDTO.getOrderNo());
        orderRefundInfo.setRefundOrderNo(payRefundDTO.getOutRequestNo());
        orderRefundInfo.setRequestNo(orderNoUtils.getOrderNo(payRefundDTO.getOrderNo().substring(0, 1)));
        orderRefundInfo.setRefundAmount(payRefundDTO.getRefundAmount());
        orderRefundInfo.setPayAmount(payRefundDTO.getTotalAmount());
        orderRefundInfo.setPayType(payRefundDTO.getPayType());
        orderRefundInfo.setRefundStatus("0");
        orderRefundInfo.setCreateTime(DateUtils.getNowDate());
        insertOrderRefundInfo(orderRefundInfo);
        return orderRefundInfo;
    }

    @Override
    public OrderRefundInfo getOrderRefundInfoByRequestNo(String requestNo) {
        return getOne(new QueryWrapper<OrderRefundInfo>().lambda().eq(OrderRefundInfo::getRequestNo, requestNo));
    }

}
