/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.delivery.multishop.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.event.GetTransportNamesEvent;
import com.yami.shop.bean.param.TransportParam;
import com.yami.shop.delivery.comment.model.Transport;
import com.yami.shop.delivery.comment.service.TransportService;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 获取所有的运费模板名称
 * @author LHD
 */
@Component("getTransportNamesListener")
@AllArgsConstructor
public class GetTransportNamesListener {

    private TransportService transportService;
    private MapperFacade mapperFacade;

    /**
     * 获取所有的运费模板名称
     */
    @EventListener(GetTransportNamesEvent.class)
    public void getNamesEvnet(GetTransportNamesEvent event) {
        List<Transport> transports = transportService.list(new LambdaQueryWrapper<Transport>().eq(Transport::getShopId, event.getShopId()));
        if(CollectionUtils.isNotEmpty(transports)) {
            List<TransportParam> transportParams = event.getTransportParams();
            transportParams.addAll(mapperFacade.mapAsList(transports, TransportParam.class));
        }
    }

}
