FFMInfoHandle.java 11.3 KB
package com.tianbo.analysis.task;

import com.tianbo.analysis.model.FFMInfo;
import com.tianbo.analysis.service.FFMInfoService;
import com.tianbo.analysis.service.FWBInfoService;
import com.tianbo.analysis.service.ManifestMasterService;
import com.tianbo.analysis.service.ManifestSecondService;
import com.tianbo.util.Date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * ffm临时业务表处理入业务库
 * 舱单 =  航班号+航班日期+运单号
 * 运单 =  运单号
 */
@Slf4j
@Component
public class FFMInfoHandle {

    @Autowired
    FFMInfoService ffmInfoService;


    @Autowired
    ManifestMasterService manifestMasterService;


    @Autowired
    FWBInfoService fwbInfoService;


    @Autowired
    ManifestSecondService secondService;

    private static  FFMInfoHandle ffmInfoHandle;

    //通过@PostConstruct实现初始化bean之前进行的操作,解决service调用空指针问题
    @PostConstruct
    public void init() {
        ffmInfoHandle = this;
        ffmInfoHandle.ffmInfoService = this.ffmInfoService;
        ffmInfoHandle.manifestMasterService = this.manifestMasterService;
        ffmInfoHandle.fwbInfoService = this.fwbInfoService;
        ffmInfoHandle.secondService = this.secondService;
        // 初使化时将已静态化的testService实例化
    }
    /**
     * 处理板箱非空的舱单数据入业务库
     * @return
     */
    public boolean dealFFMInfoWithPallet(){
        boolean result =false;
        String flightDate = "";
        String flghtNo = "";
        String waybill = "";
        try {
            //1.取航班非空的板箱的运单列表数据,并且报文接收标识last存在。
            List<FFMInfo> ffmInfoList = ffmInfoHandle.ffmInfoService.getDistinctFlightWithPallet(false);
            if (ffmInfoList!=null && !ffmInfoList.isEmpty()){
                for (FFMInfo item_f:ffmInfoList) {

                    flightDate = DateUtil.dateToString(item_f.getFlightdate());
                    flghtNo = item_f.getFlightno();
                    StringBuilder sb = new StringBuilder();
                    log.warn(sb.append("开始处理航班带板箱数据:").append(flghtNo).append("/").append(flightDate).append("\n").toString());

                    //针对补发报文处理,补发的报文: FFM_INFOB表数据,存储的报文批次01对应标识是LAST
                    List<FFMInfo> ffmlist_reportorder_1 = ffmInfoHandle.ffmInfoService.getFFMLastReportOrderByPallet(item_f,false);



                    /**
                     * 判断列表是否为空
                     * 不为空则说明 FFM只有一份,第一份报文包含LAST标识
                     */
                    if (ffmlist_reportorder_1 !=null && !ffmlist_reportorder_1.isEmpty()){
                        for (FFMInfo item:ffmlist_reportorder_1) {

                            waybill = item.getWaybillnomaster();
                            log.info(new StringBuilder("--------------开始入库").append(flghtNo).append("/").append(flightDate).append(":").append(waybill).append("------------").toString());


                            result = updateFFM2Manifest(item);

                            log.info("-----------入库结束--------------结果:"+result);
                        }
                    }
                    /**
                     *FFM文件多份处理
                     */
                    else
                    {
                        //首先确定带有LAST的ffm的最批次大序号
                        //根据航班号+航班日期 判断LAST报对应的报文批次
                        List<FFMInfo> _last = ffmInfoHandle.ffmInfoService.getLastReportOrder(item_f,false);
                        List<FFMInfo> _last_count = ffmInfoHandle.ffmInfoService.getLastCountByPallet(item_f,false);
                        if (_last != null && _last.size() > 0 && _last_count != null && _last_count.size() > 0){
                            int mm = Integer.parseInt(_last.get(0).getReportorder());
                            int nn = Integer.parseInt(_last_count.get(0).getReportorder());
                            if (mm > 0 && nn > 0 && mm == nn){
                                // 业务数据 开始入库

                                List<FFMInfo> ffmlist = ffmInfoHandle.ffmInfoService.getFFMinfoByPallet(item_f,false);
                                if (ffmlist != null && ffmlist.size() > 0){

                                    for (FFMInfo item:ffmlist) {

                                        log.info(new StringBuilder("开始入库").append(flghtNo).append("/").append(flightDate).append(":").append(item.getWaybillnomaster()).toString());


                                        result = updateFFM2Manifest(item);

                                        log.info("------------入库结束-------------结果:"+result);

                                    }

                                }
                            }
                        }
                    }
                }
            }

            //空板箱舱单入库:
//            dealFFMInfoWithNullPallet();
        }catch (Exception e){
            e.printStackTrace();
            String errstr = new StringBuilder("航班:").append(flghtNo).append("/").append(flightDate).append("/").append(waybill).append("->解析入库失败").toString();
            log.error(errstr,e);
        }
        return true;
    }

    /**
     * 查看舱单运单业务表是否已存在运单数据,不存在则插入业务表,存在则更新临时表的数据为已处理
     * 将舱单临时表的数据 写入舱单业务表,并挂载主分单关系
     * @param item 临时表业务数据
     * @return 成功或者失败
     */
    private boolean updateFFM2Manifest(FFMInfo item) {
        int Flag = 0;
        /**
         * 舱单业务表插入数据服务,
         * 插入前判定是否有此航班的运单数据,插入语成功返回 1
         * 业务库存在此航班的运单数据则不会插入,并更新ffminfo临时表的此舱单的数据状态为已处理 返回 0
         * @return 0 or 1
         */
        String fwb = item.getWaybillnomaster();
        Flag = ffmInfoHandle.manifestMasterService.insertManifestWithMaster(item);
        log.info(new StringBuilder("运单号:").append(fwb).append("->入库:").append(Flag).toString());

        if (Flag > 0) {

            //业务数据入库成功后,更新舱单待处理表的数据状态为 1,根据航班号+航班日期+主单号
            int ffm_i = ffmInfoHandle.ffmInfoService.setDealstatusSuccess(item);
            log.info(new StringBuilder("更新运单:").append("舱单临时表状态为已处理:").append(ffm_i).toString());

            //舱单报文入库后, 更新FWB信息,将FWB临时表的信息更新到 舱单业务表
            int fwb_i=ffmInfoHandle.fwbInfoService.updateFwbToManifest(item.getWaybillnomaster());
            log.info(new StringBuilder("更新运单:").append("运单FWB数据到业务库:").append(fwb_i).toString());

            //分单挂主单
            int fhl_i = ffmInfoHandle.secondService.setManifestMaster(item);
            log.info("挂载分单成功->挂载数量:"+fhl_i);
            return true;
        }
        return false;
    }

    /**
     * 处理板箱为空的舱单数据入业务库
     * @return
     */
    public boolean dealFFMInfoWithNullPallet(){
        boolean result = false;
        try{
            //1.取航班非空的板箱的运单列表数据,并且报文接收标识last存在。
            List<FFMInfo> listflightno0 = ffmInfoHandle.ffmInfoService.getDistinctFlightWithPallet(true);
            if (listflightno0!=null && listflightno0.size()>0){

                for (FFMInfo item_f:listflightno0) {
                    //     CustomsCargoSystem.Helper.Log.ffminfo_log("舱单数据入业务库调试信息", "舱单数据入业务库调试信息(板号空).txt",
                    //string.Format(@"{0}入业务库开始航班号:{1},航班日期:{2}" + Environment.NewLine, DateTime.Now, item_f.flightno, item_f.flightdate));
                    //针对补发报文处理
                    String flightDate = DateUtil.dateToString(item_f.getFlightdate());
                    String flghtNo = item_f.getFlightno();
                    StringBuilder sb = new StringBuilder();
                    log.warn(sb.append("开始处理航班空板箱:").append(flghtNo).append("/").append(flightDate).append("\n").toString());

                    List<FFMInfo> ffmlist_reportorder_1 = ffmInfoHandle.ffmInfoService.getFFMLastReportOrderByPallet(item_f,true);

                    /**
                     * 判断列表是否为空
                     * 不为空则说明 FFM只有一份,第一份报文包含LAST标识
                     */
                    if (ffmlist_reportorder_1 !=null && !ffmlist_reportorder_1.isEmpty()){
                        for (FFMInfo item:ffmlist_reportorder_1) {
                            log.info(new StringBuilder("开始入库").append(flghtNo).append("/").append(flightDate).append(":").append(item.getWaybillnomaster()).toString());
                            result = updateFFM2Manifest(item);
                            log.info("------------入库结束-------------结果:"+result);
                        }
                    }
                    /**
                     * FFM文件多份处理
                     */
                    else
                    {
                        //写判断逻辑 根据航班号+航班日期 判断LAST报对应的报文批次
                        //首先确定带有LAST的ffm的最批次大序号
                        //根据航班号+航班日期 判断LAST报对应的报文批次
                        List<FFMInfo> _last = ffmInfoHandle.ffmInfoService.getLastReportOrder(item_f,true);
                        List<FFMInfo> _last_count = ffmInfoHandle.ffmInfoService.getLastCountByPallet(item_f,true);
                        if (_last != null && _last.size() > 0 && _last_count != null && _last_count.size() > 0){
                            int mm = Integer.parseInt(_last.get(0).getReportorder());
                            int nn = Integer.parseInt(_last_count.get(0).getReportorder());
                            if (mm > 0 && nn > 0 && mm == nn){
                                // 业务数据 开始入库

                                List<FFMInfo> ffmlist = ffmInfoHandle.ffmInfoService.getFFMinfoByPallet(item_f,true);
                                if (ffmlist != null && ffmlist.size() > 0){

                                    for (FFMInfo item:ffmlist) {

                                        log.info(new StringBuilder("开始入库").append(flghtNo).append("/").append(flightDate).append(":").append(item.getWaybillnomaster()).toString());
                                        result = updateFFM2Manifest(item);
                                        log.info("------------入库结束-------------结果:"+result);
                                    }

                                }
                            }
                        }
                    }

                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }

}