package com.sf.order.service.impl;

import com.sf.common.exception.ServiceException;
import com.sf.common.utils.DateUtils;
import com.sf.common.utils.SnowflakeIdWorker;
import com.sf.order.domain.OrderInfo;
import com.sf.order.domain.dto.OrderCreateDto;
import com.sf.order.domain.req.OrderListReqVo;
import com.sf.order.domain.res.OrderListResVo;
import com.sf.order.mapper.OrderInfoMapper;
import com.sf.order.service.IOrderInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单基础信息Service业务层处理
 *
 * @author ztzh
 * @date 2024-04-09
 */
@Service
public class OrderInfoServiceImpl implements IOrderInfoService {
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 查询订单基础信息
     *
     * @param id 订单基础信息主键
     * @return 订单基础信息
     */
    @Override
    public OrderInfo selectOrderInfoById(Long id) {
        return orderInfoMapper.selectOrderInfoById(id);
    }

    /**
     * 查询订单基础信息列表
     *
     * @param orderInfo 订单基础信息
     * @return 订单基础信息
     */
    @Override
    public List<OrderInfo> selectOrderInfoList(OrderInfo orderInfo) {
        return orderInfoMapper.selectOrderInfoList(orderInfo);
    }

    public List<OrderListResVo> queryList(OrderListReqVo vo) {
        return orderInfoMapper.queryList(vo);
    }

    /**
     * 新增订单基础信息
     *
     * @param orderCreateDto 订单基础信息
     * @return 结果
     */
    @Override
    public Long createOrder(OrderCreateDto orderCreateDto) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderNo(snowflakeIdWorker.nextId());
        orderInfo.setPayType(1L);
        orderInfo.setReceiveType(0L);
        orderInfo.setOrderStatus(0L);
        orderInfo.setCreateUserId(orderCreateDto.getUserId());
        orderInfo.setUpdateUserId(orderCreateDto.getUserId());
        orderInfo.setGoodsId(orderCreateDto.getGoodsId());
        orderInfo.setOrderAmt(orderCreateDto.getAmount());
        orderInfo.setPayAmt(orderCreateDto.getAmount());
        orderInfo.setCreateTime(DateUtils.getNowDate());
        orderInfo.setUpdateTime(DateUtils.getNowDate());
        orderInfo.setOutOrderNo(orderCreateDto.getOutOrderNo());
        orderInfoMapper.insertOrderInfo(orderInfo);
        return snowflakeIdWorker.nextId();
    }

    /**
     * 修改订单基础信息
     *
     * @param orderInfo 订单基础信息
     * @return 结果
     */
    @Override
    public int updateOrderInfo(OrderInfo orderInfo) {
        orderInfo.setUpdateTime(DateUtils.getNowDate());
        return orderInfoMapper.updateOrderInfo(orderInfo);
    }

    @Override
    public void orderPay(Long orderId) {
        OrderInfo updateOrder = this.selectOrderInfoById(orderId);
        if (updateOrder == null) {
            throw new ServiceException("订单缺失！");
        }
//        if (!OrderInfoConstant.ORDER_WAITING_FOR_PAYMENT.equals(orderInfo.getOrderStatus())) {
//            throw new ServiceException("订单状态异常！");
//        }
        updateOrder.setOrderStatus(1L);
        updateOrder.setPayTime(DateUtils.getNowDate());
        // 修改订单状态
        if (1 > this.updateOrderInfo(updateOrder)) {
            throw new ServiceException("支付异常，请联系管理员!");
        }
    }

    @Override
    public OrderInfo selectOrderInfoByOrderNo(String orderNo) {

        return orderInfoMapper.selectOrderInfoByOrderNo(orderNo);
    }

    @Override
    public void insertOrder(OrderInfo orderInfo) {
        orderInfoMapper.insertOrderInfo(orderInfo);
    }

    /**
     * 批量删除订单基础信息
     *
     * @param ids 需要删除的订单基础信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderInfoByIds(Long[] ids) {
        return orderInfoMapper.deleteOrderInfoByIds(ids);
    }

    /**
     * 删除订单基础信息信息
     *
     * @param id 订单基础信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderInfoById(Long id) {
        return orderInfoMapper.deleteOrderInfoById(id);
    }

}
