package com.younike.system.service.impl;

import com.younike.system.domain.OrderRecord;
import com.younike.system.domain.vo.OrderRecordVo;
import com.younike.system.enums.OrderStateEnum;
import com.younike.system.mapper.OrderRecordMapper;
import com.younike.system.service.IOrderRecordService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 订单记录Service业务层处理
 *
 * @author elev
 * @date 2022-01-08
 */
@Service
public class OrderRecordServiceImpl implements IOrderRecordService {
    @Autowired
    private OrderRecordMapper orderRecordMapper;

    /**
     * 查询订单记录
     *
     * @param id 订单记录主键
     * @return 订单记录
     */
    @Override
    public OrderRecord selectOrderRecordById(Long id) {
        return orderRecordMapper.selectOrderRecordById(id);
    }

    /**
     * 查询订单记录列表
     *
     * @param orderRecord 订单记录
     * @return 订单记录
     */
    @Override
    public List<OrderRecord> selectOrderRecordList(OrderRecord orderRecord) {
        return orderRecordMapper.selectOrderRecordList(orderRecord);
    }

    /**
     * 新增订单记录
     *
     * @param orderRecord 订单记录
     * @return 结果
     */
    @Override
    public int insertOrderRecord(OrderRecord orderRecord) {
        orderRecord.setCreateTime(LocalDateTime.now());
        return orderRecordMapper.insertOrderRecord(orderRecord);
    }

    /**
     * 修改订单记录
     *
     * @param orderRecord 订单记录
     * @return 结果
     */
    @Override
    public int updateOrderRecord(OrderRecord orderRecord) {
        return orderRecordMapper.updateOrderRecord(orderRecord);
    }

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

    /**
     * 删除订单记录信息
     *
     * @param id 订单记录主键
     * @return 结果
     */
    @Override
    public int deleteOrderRecordById(Long id)
    {
        return orderRecordMapper.deleteOrderRecordById(id);
    }

    /**
     * 根据订单编号获取最近一条订单记录
     * @param orderId
     * @return
     */
    @Override
    public OrderRecord selectRefunfd(Long orderId) {
        return orderRecordMapper.selectRefunfd(orderId);
    }

    /**
     * 查询订单追踪记录列表
     *
     * @param orderId
     * @return
     */
    @Override
    public List<OrderRecordVo> getMemberOrderRecord(Long orderId) {
        List<OrderRecordVo> orderRecordVoList = orderRecordMapper.getMemberOrderRecord(orderId);
        if (CollectionUtils.isNotEmpty(orderRecordVoList)) {
            // 获取订单状态名称
            for (OrderRecordVo orderRecordVo : orderRecordVoList) {
                orderRecordVo.setPreStateName(OrderStateEnum.getDescription(orderRecordVo.getPreState()));
                orderRecordVo.setNowStateName(OrderStateEnum.getDescription(orderRecordVo.getNowState()));
            }
        }
        return orderRecordVoList;
    }
}
