package cn.iocoder.yudao.module.order.listener.export;

import cn.iocoder.yudao.framework.apollo.core.event.export.OrderCargoControlExcelExportEvent;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import cn.iocoder.yudao.module.order.service.orderCargoControl.OrderCargoControlService;
import cn.iocoder.yudao.module.order.vo.order.OrderQueryVO;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;


/**
 * 订单excel导出监听
 *
 * @author zhengYi
 */
@Component("OrderCargoControlExcelExportListener")
@AllArgsConstructor
@Slf4j
public class OrderCargoControlExcelExportListener {

    private final OrderCargoControlService orderCargoControlService;
    /**
     * 订单导出监听
     *
     * @param event 编号记录
     */
    @EventListener(OrderCargoControlExcelExportEvent.class)
    public void orderCargoControlExcelExportEvent(OrderCargoControlExcelExportEvent event) {
        if (StringUtils.isNotBlank(event.getRequestParams())){
            try {
                OrderQueryVO query = JSONObject.parseObject(event.getRequestParams(), OrderQueryVO.class);
                query.setLang(event.getLang());
                FileDO fileDO = orderCargoControlService.makeCargoControlOrderExcelFile(event.getUserId(), event.getUserType(), query);
                event.setPath(fileDO.getPath());
                event.setFileName(fileDO.getPath());
                event.setUrl(fileDO.getUrl());
                event.setFileId(fileDO.getId());
            }catch (Exception e){
                log.error(e.getMessage());
                event.setResult(e.getMessage());
            }
        }else {
            event.setResult("param fail");
        }

    }
}
