package com.yiqi.order.service.impl;

import java.math.BigDecimal;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.order.mapper.OrderPayInfoMapper;
import com.yiqi.order.domain.OrderPayInfo;
import com.yiqi.order.service.IOrderPayInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 第三方支付记录Service业务层处理
 *
 * @author douya
 * @date 2024-08-21
 */
@Service
public class OrderPayInfoServiceImpl extends ServiceImpl<OrderPayInfoMapper, OrderPayInfo> implements IOrderPayInfoService {
    @Autowired
    private OrderNoUtils orderNoUtils;

    /**
     * 查询第三方支付记录
     *
     * @param id 第三方支付记录主键
     * @return 第三方支付记录
     */
    @Override
    public OrderPayInfo selectOrderPayInfoById(Long id) {
        return baseMapper.selectOrderPayInfoById(id);
    }

    /**
     * 查询第三方支付记录列表
     *
     * @param orderPayInfo 第三方支付记录
     * @return 第三方支付记录
     */
    @Override
    public List<OrderPayInfo> selectOrderPayInfoList(OrderPayInfo orderPayInfo) {
        return baseMapper.selectOrderPayInfoList(orderPayInfo);
    }

    /**
     * 新增第三方支付记录
     *
     * @param orderPayInfo 第三方支付记录
     * @return 结果
     */
    @Override
    public int insertOrderPayInfo(OrderPayInfo orderPayInfo) {
        orderPayInfo.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(orderPayInfo);
    }

    /**
     * 修改第三方支付记录
     *
     * @param orderPayInfo 第三方支付记录
     * @return 结果
     */
    @Override
    public int updateOrderPayInfo(OrderPayInfo orderPayInfo) {
        return baseMapper.updateById(orderPayInfo);
    }

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

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

    @Override
    public OrderPayInfo createOrderPayInfo(Long storeId, String orderNo, BigDecimal payAmount, String payType) {
        OrderPayInfo orderPayInfo = new OrderPayInfo();
        orderPayInfo.setStoreId(storeId);
        orderPayInfo.setPayType(payType);
        orderPayInfo.setOrderNo(orderNo);
        orderPayInfo.setPayAmount(payAmount);
        orderPayInfo.setRequestNo(orderNoUtils.getOrderNo(orderNo.substring(0, 1)));
        orderPayInfo.setCreateTime(DateUtils.getNowDate());
        baseMapper.insert(orderPayInfo);
        return orderPayInfo;
    }

    @Override
    public OrderPayInfo getOrderPayInfoByRequestNo(String payOrderNo) {
        return getOne(new QueryWrapper<OrderPayInfo>().lambda().eq(OrderPayInfo::getRequestNo, payOrderNo));
    }

}
