package cn.reghao.tnb.content.app.mall.service;

import cn.reghao.tnb.content.app.mall.db.mapper.LogisticsMapper;
import cn.reghao.tnb.content.app.mall.db.mapper.LogisticsProgressMapper;
import cn.reghao.tnb.content.app.mall.model.po.Logistics;
import cn.reghao.tnb.content.app.mall.model.po.LogisticsProgress;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author reghao
 * @date 2024-09-19 09:05:39
 */
@Service
public class LogisticsService {
    private final LogisticsMapper logisticsMapper;
    private final LogisticsProgressMapper logisticsProgressMapper;

    public LogisticsService(LogisticsMapper logisticsMapper, LogisticsProgressMapper logisticsProgressMapper) {
        this.logisticsMapper = logisticsMapper;
        this.logisticsProgressMapper = logisticsProgressMapper;
    }

    public void putOrderLogistics(Logistics logistics) {
        String logisticsId = logistics.getLogisticsId();
        Logistics logistics1 = logisticsMapper.findByLogisticsId(logisticsId);
        if (logistics1 != null) {
            return;
        }

        LogisticsProgress logisticsProgress = new LogisticsProgress(logisticsId);
        logisticsMapper.save(logistics);
        logisticsProgressMapper.save(logisticsProgress);
    }

    public void putOrderLogisticsProgress(LogisticsProgress logisticsProgress) {
        long orderId = logisticsProgress.getOrderId();
        Logistics logistics = logisticsMapper.findByOrderId(orderId);
        if (logistics == null) {
            return;
        }

        String logisticsId = logistics.getLogisticsId();
        logisticsProgress.setLogisticsId(logisticsId);
        logisticsProgressMapper.save(logisticsProgress);
    }

    public List<LogisticsProgress> getOrderLogistics(long userId, long orderId) {
        Logistics logistics = logisticsMapper.findByOrderId(orderId);
        if (logistics == null) {
            return Collections.emptyList();
        }

        List<LogisticsProgress> list = logisticsProgressMapper.findByLogisticsId(logistics.getLogisticsId());
        return list;
    }
}
