package com.younike.system.service.impl;

import java.util.List;
import java.time.LocalDateTime;
import java.util.Objects;

import com.sun.org.apache.xpath.internal.operations.Or;
import com.younike.common.utils.SecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.younike.system.mapper.OrderModifyMapper;
import com.younike.system.domain.OrderModify;
import com.younike.system.service.IOrderModifyService;

/**
 * 订单修改记录Service业务层处理
 * 
 * @author elev
 * @date 2022-02-10
 */
@Service
public class OrderModifyServiceImpl implements IOrderModifyService {
    @Autowired
    private OrderModifyMapper orderModifyMapper;

    /**
     * 查询订单修改记录
     * 
     * @param recordId 订单修改记录主键
     * @return 订单修改记录
     */
    @Override
    public OrderModify selectOrderModifyByRecordId(Long recordId) {
        return orderModifyMapper.selectOrderModifyByRecordId(recordId);
    }

    /**
     * 查询订单修改记录列表
     * 
     * @param orderModify 订单修改记录
     * @return 订单修改记录
     */
    @Override
    public List<OrderModify> selectOrderModifyList(OrderModify orderModify) {
        return orderModifyMapper.selectOrderModifyList(orderModify);
    }

    /**
     * 新增订单修改记录
     * 
     * @param orderModify 订单修改记录
     * @return 结果
     */
    @Override
    public int insertOrderModify(OrderModify orderModify) {
        OrderModify current = getLatestRecord(orderModify.getOrderId());
        if (Objects.nonNull(current)){
            orderModify.setModifyIndex(current.getModifyIndex() + 1);
        } else {
            orderModify.setModifyIndex(1);
        }
        orderModify.setCreateTime(LocalDateTime.now());
        orderModify.setCreateBy(SecurityUtils.getUserId().toString());
        return orderModifyMapper.insertOrderModify(orderModify);
    }

    /**
     * 修改订单修改记录
     * 
     * @param orderModify 订单修改记录
     * @return 结果
     */
    @Override
    public int updateOrderModify(OrderModify orderModify) {
        return orderModifyMapper.updateOrderModify(orderModify);
    }

    /**
     * 批量删除订单修改记录
     * 
     * @param recordIds 需要删除的订单修改记录主键
     * @return 结果
     */
    @Override
    public int deleteOrderModifyByRecordIds(Long[] recordIds) {
        return orderModifyMapper.deleteOrderModifyByRecordIds(recordIds);
    }

    /**
     * 删除订单修改记录信息
     * 
     * @param recordId 订单修改记录主键
     * @return 结果
     */
    @Override
    public int deleteOrderModifyByRecordId(Long recordId)
    {
        return orderModifyMapper.deleteOrderModifyByRecordId(recordId);
    }

    @Override
    public OrderModify getLatestRecord(Long orderId) {
        return orderModifyMapper.getLatestRecord(orderId);
    }
}
