PREPARMASTERServiceImpl.java 8.6 KB
package com.tianbo.analysis.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tianbo.analysis.dao.PREPAREMASTERMapper;
import com.tianbo.analysis.dao.PREPARESECONDARYMapper;
import com.tianbo.analysis.dao.SENDLOGMapper;
import com.tianbo.analysis.model.*;
import com.tianbo.analysis.service.PREPARMASTERService;
import com.tianbo.analysis.tools.XSDValidateWithXML;
import com.tianbo.util.Date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;

@Service
@Slf4j
public class PREPARMASTERServiceImpl implements PREPARMASTERService {

    @Resource
    PREPAREMASTERMapper preparemasterMapper;

    @Resource
    PREPARESECONDARYMapper preparesecondaryMapper;

    @Resource
    SENDLOGMapper sendlogMapper;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${customs.xml-path}")
    private String saveXmlDir;


    @Value("${customs.transport-number}")
    private String transportNo;

    @Override
    public PageInfo<PREPAREMASTER> search(PREPAREMASTER preparemaster,int pageNum, int pageSize) {
        Page<PREPAREMASTER> page =  PageHelper.startPage(pageNum,pageSize);
        List<PREPAREMASTER> list =  preparemasterMapper.selectBy(preparemaster);
        PageInfo<PREPAREMASTER> result = new PageInfo<PREPAREMASTER>(list);
        return result;
    }

    @Override
    public PageInfo search_exp(String awba, String flightDate, String flightNo, int pageNum, int pageSize) {
        Page<PREPAREMASTER> page =  PageHelper.startPage(pageNum,pageSize);
        String carrier = StringUtils.isEmpty(flightNo)  ? "" : flightNo.substring(0,2);
        String flight = StringUtils.isEmpty(flightNo)  ? "" : flightNo.substring(2);
        List<PREPAREMASTER> list =  preparemasterMapper.searchTree(awba,flight,flightDate,carrier);
        PageInfo<PREPAREMASTER> result = new PageInfo<PREPAREMASTER>(list);
        return result;
    }

    @Override
    public boolean batchDel(BatchSend batchSend, String username) {
        CustomsHead customsHead = new CustomsHead();
        String nowStr = DateUtil.getCurrentTime17();
        String messageID = "CN_"
                + "MT2201"
                + "_1P0_"
                + transportNo+
                "_" + nowStr;

        customsHead.setMessageId(messageID);
        customsHead.setSendTime(nowStr);

        //航班信息存储
        HashMap<String, String> flightMap = new HashMap<>();
        ArrayList<Map> bills = new ArrayList<>();


        /**
         * noList: master;fc68f797-58f9-4276-850d-f772ab54e041;Y87489;2018/7/16 0:00:00;871-52149624;NO;4620
         * noList: sub;e430bacf-d34d-49c4-a646-c6bab53245d9;Y87489;2018/7/16 0:00:00;871-52149624;5758599033;4620
         * noList: sub;4ff7853f-0315-4632-ad0f-6a028054513d;Y87489;2018/7/16 0:00:00;871-52149624;5758599022;4620
         * noList: sub;52dc1407-d686-42ac-87cd-ddacdb84ca18;Y87489;2018/7/16 0:00:00;871-52149624;5758599044;4620
         * noList: sub;26bf643c-cf5b-4af2-8394-51585c02d88d;Y87489;2018/7/16 0:00:00;871-52149624;5758599077;4620
         */
        if (batchSend.billListDes!=null && !batchSend.billListDes.isEmpty() && batchSend.businessAlterLog!=null){
            for (String item : batchSend.billListDes) {
                HashMap<String, String> billsMap = new HashMap<>();
                String[] split = item.split(";");
                //单证类别:master为主单,sub为分单
                String billType = split[0];
                String billAutoId = split[1];
                //航班号
                String flightNo = split[2];
                //航班日期
                String flightDateString = split[3];
                // 主单号
                String billMasterNo = split[4].replace("-","");
                // 分单号
                String billSecNo = split[5];
                // 申报关区
                String customCode = split[6];

                flightMap.put("flightNo",flightNo);
                flightMap.put("flightDate",flightDateString);
                //设置关区
                customsHead.setReceiverID(customCode);

                billsMap.put("billAutoId",billAutoId);
                billsMap.put("billMasterNo",billMasterNo);
                billsMap.put("billSecNo",billMasterNo +"_" +billSecNo);
                billsMap.put("billType",billType);
                bills.add(billsMap);
            }
        }

        Context context = new Context();
        context.setVariable("bills",bills);
        context.setVariable("head",customsHead);
        context.setVariable("flight",flightMap);
        context.setVariable("reason",batchSend.businessAlterLog);
        context.setLocale(Locale.SIMPLIFIED_CHINESE);

        //生成的文件名
        String fileName = messageID + ".xml";
        //生成的报文路径,带斜杠
        String filePath = saveXmlDir+fileName;
        try {
            File file = ResourceUtils.getFile(filePath);
            /**
             * 中文的地方在模板中要用utext,否则中文会被 escape 转义
             */
            String xmlStr = templateEngine.process("mt2201/MT2201_D.xml",context);
            System.out.println("xmlStr = " + xmlStr);
            boolean valied =  XSDValidateWithXML.validateXMLSchema("xsd/Manifest_Declare_Export_Air_Delete_2201_3.xsd",xmlStr);

            if (valied){
                // 生成报文
                FileUtils.writeStringToFile(file,xmlStr, StandardCharsets.UTF_8);
                log.info("[PRE-BATCH-SEND]-SUCCESS,批量删除发送成功");

                // 更新运单状态,含主单表和分单表
                Boolean b = updateBatchStatus(batchSend, bills, username);
                return b;
            }else {
                return false;
            }
        } catch (Exception e) {
            log.error("[PRE-BATCH-DEL]-批量删除发生异常,{}",e.getMessage());
            return false;
        }
    }

    private Boolean updateBatchStatus(BatchSend batchSend,ArrayList<Map> bills,String username) {
        //存储更新主单状态的列表
        ArrayList<PREPAREMASTER> preMasters = new ArrayList<>();
        ArrayList<PREPARESECONDARY> preSubs = new ArrayList<>();
        List<SENDLOG> sendlogList = new ArrayList<>();

        for (Map<String,String> item : bills) {
            // 批量插入发送日志
            SENDLOG sendlog = new SENDLOG();
            sendlog.setAutoid(UUID.randomUUID().toString());
            sendlog.setMessageautoid(item.get("billAutoId"));
            sendlog.setMessagetype(batchSend.businessAlterLog.getBusinesstype());
            sendlog.setCreatedate(new Date());
            sendlog.setReceiption("新舱单系统发送预配舱单批量删除报");
            sendlog.setOpauthor(username);
            sendlog.setSendpeice(new Long(0));
            sendlog.setSendweight(new BigDecimal(0));
            sendlogList.add(sendlog);

            if ("master".equals(item.get("billType"))){
                PREPAREMASTER preparemaster = new PREPAREMASTER();
                preparemaster.setAutoid(item.get("billAutoId"));
                preMasters.add(preparemaster);
            }else {
                PREPARESECONDARY preparesecondary = new PREPARESECONDARY();
                preparesecondary.setAutoid(item.get("billAutoId"));
                preSubs.add(preparesecondary);
            }
        }
        int i = sendlogMapper.batchInsert(sendlogList);
        if (i>0){
            log.info("[PRE-BATCH-DEL-LOG-INSERT]-SUCCESS");
        }else {
            log.error("[PRE-BATCH-DEL-LOG-INSERT]-FAILD");
            return false;
        }

        /**
         * 以下两个批量更新语句无法返回具体影响行数,返回为-1
         * 要么得配置在mybatis-config.xml,如下
         * <configuration>
         *     <settings>
         *         <setting name="defaultExecutorType" value="SIMPLE"/>
         *         <setting name="defaultExecutorType" value="BATCH"/>
         *     </settings>
         * </configuration>
         */

        int i1 = preparemasterMapper.updateDelStatusByPrimaryKey(preMasters);
        int i2 = preparesecondaryMapper.updateDelStatusByPrimaryKey(preSubs);

        return true;
    }
}