审查视图

src/main/java/com/tianbo/analysis/service/imp/TransArriveExportImpl.java 6.7 KB
xudada authored
1 2 3 4
package com.tianbo.analysis.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
5
import com.tianbo.analysis.config.CustomsProperties;
xudada authored
6
import com.tianbo.analysis.dao.TRANSTOARRIVEEXPORTMapper;
7
import com.tianbo.analysis.model.MANIFEST_AIR_CHANGE;
朱兆平 authored
8
import com.tianbo.analysis.model.ResultJson;
xudada authored
9 10
import com.tianbo.analysis.model.TRANSTOARRIVEEXPORT;
import com.tianbo.analysis.service.TransArriveExportService;
朱兆平 authored
11 12 13 14
import com.tianbo.analysis.tools.XmlTools;
import com.tianbo.util.Date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
xudada authored
15
import org.springframework.beans.factory.annotation.Autowired;
朱兆平 authored
16
import org.springframework.beans.factory.annotation.Value;
xudada authored
17
import org.springframework.stereotype.Service;
朱兆平 authored
18 19 20 21 22
import org.springframework.util.ResourceUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.exceptions.TemplateInputException;
import org.xml.sax.SAXParseException;
xudada authored
23
朱兆平 authored
24 25 26 27 28 29
import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
xudada authored
30
import java.util.List;
朱兆平 authored
31 32
import java.util.Locale;
import java.util.UUID;
xudada authored
33 34

@Service
朱兆平 authored
35
@Slf4j
xudada authored
36
public class TransArriveExportImpl implements TransArriveExportService {
朱兆平 authored
37 38

    @Resource
xudada authored
39
    TRANSTOARRIVEEXPORTMapper mapper;
朱兆平 authored
40 41 42 43

    @Autowired
    private TemplateEngine templateEngine;
44 45 46
    @Autowired
    private CustomsProperties customsProperties;
朱兆平 authored
47 48 49
    @Value("${customs.transarrive.xml-save}")
    private String saveXmlForSendDir;
xudada authored
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    @Override
    public int addTransArriveExport(TRANSTOARRIVEEXPORT transtoarriveexport) {
        return mapper.insertSelective(transtoarriveexport);
    }

    @Override
    public int ediTransArriveExport(TRANSTOARRIVEEXPORT transtoarriveexport) {
        return mapper.updateByPrimaryKeySelective(transtoarriveexport);
    }

    @Override
    public int delTransArriveExport(String autoid) {
        return mapper.deleteByPrimaryKey(autoid);
    }

    @Override
    public PageInfo<TRANSTOARRIVEEXPORT> selectTrans(TRANSTOARRIVEEXPORT transtoarriveexport, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TRANSTOARRIVEEXPORT> list=mapper.selectTrans(transtoarriveexport);
朱兆平 authored
69
        PageInfo<TRANSTOARRIVEEXPORT> pageInfo=new PageInfo<TRANSTOARRIVEEXPORT>(list);
xudada authored
70 71
        return pageInfo;
    }
朱兆平 authored
72 73 74

    @Override
    public ResultJson send(TRANSTOARRIVEEXPORT record) {
75 76 77 78
        //设置海关相关信息配置
        record.setApplycode(customsProperties.getApplyCode());
        record.setApplyname(customsProperties.getApplyName());
        record.setCopcode(customsProperties.getCopCode());
79 80 81
        record.setClientseqno(customsProperties.getTransarrive().getClientSeqno());
        record.setHostid(customsProperties.getTransarrive().getHostId());
        record.setCertno(customsProperties.getTransarrive().getCertNo());
82 83 84 85
        //操作员IC卡号
        record.setInputopid(customsProperties.getInputOpId());
        // 操作员姓名
        record.setInputopname(customsProperties.getInputOpName());
朱兆平 authored
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        ResultJson resultJson= new ResultJson();
        try{

            String nowStr = DateUtil.getCurrentTime17();
            //从配置文件读取
            String messageID = "CN_"
                    + "TRANSARRIVE_EXPORT_DECLARE"
                    + "_1P0_"
                    + record.getCustomscode()+
                    "_" + nowStr;

            Context context = new Context();
            context.setVariable("declare",record);
            context.setLocale(Locale.SIMPLIFIED_CHINESE);

            //生成的文件名
            String fileName = messageID + ".xml";
            //生成的报文路径,带斜杠
104
            String filePath = customsProperties.getTransarrive().getXmlSave()+fileName;
朱兆平 authored
105 106 107 108 109
            File file = ResourceUtils.getFile(filePath);
            /**
             * 中文的地方在模板中要用utext,否则中文会被 escape 转义
             */
            String xmlStr = templateEngine.process("transToArrive/EXPORT_DECLARE_TPL.xml",context);
110
            log.info("[XML-INFO]\n{}",xmlStr);
朱兆平 authored
111
112 113 114
            boolean valied = XmlTools.validateXMLSchemaThrowError("xsd/ETAImportMessage.xsd", xmlStr);
            if (valied) {
                FileUtils.writeStringToFile(file, xmlStr, StandardCharsets.UTF_8);
朱兆平 authored
115 116 117 118
                //todo:更新发送状态,插入发送日志

                resultJson.setCode("200");
                resultJson.setMsg("报文发送成功");
119
                log.info("[XML-BUILD]- 报文已生成到目录{}",filePath);
朱兆平 authored
120 121 122 123
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
            resultJson.setCode("401");
124 125 126
            resultJson.setMsg("报文发送失败,文件未找到");
            resultJson.setError(e.toString());
            log.error("报文发送失败,[{}]文件未找到",e.toString());
朱兆平 authored
127 128 129
        }catch (IOException e) {
            e.printStackTrace();
            resultJson.setCode("402");
130 131 132
            resultJson.setMsg("报文发送失败,文件读取失败");
            resultJson.setError(e.toString());
            log.error("报文发送失败,[{}]文件IO读取失败",e.toString());
朱兆平 authored
133 134
        }catch (TemplateInputException e){
            resultJson.setCode("404");
135 136 137
            resultJson.setMsg("报文发送失败,报文模板未找到");
            resultJson.setError(e.toString());
            log.error("报文发送失败,[{}]报文模板未找到",e.toString());
朱兆平 authored
138
        }catch (SAXParseException e){
139 140 141 142
            log.error("[XML-ERR]-xml报文校验失败:[{}]", e.getMessage());
            resultJson.setCode("400");
            resultJson.setMsg("报文发送失败,报文校验不通过");
            resultJson.setError(e.getMessage());
朱兆平 authored
143 144
        } catch (Exception ignored){
            resultJson.setCode("405");
145 146 147
            resultJson.setMsg("报文发送失败,其他错误");
            resultJson.setError(ignored.toString());
            log.error("[XML-ERR]-报文发送失败,其他错误:[{}]", ignored.getMessage());
朱兆平 authored
148 149 150 151 152
        }
        return resultJson;
    }

    @Override
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    public ResultJson batchSend(List<String> autoIDlist) {
        ResultJson resultJson = new ResultJson();
        List<TRANSTOARRIVEEXPORT> list = selectListByAutoIDS(autoIDlist);
        for (TRANSTOARRIVEEXPORT manifestAirChange : list) {
            ResultJson result = send(manifestAirChange);
            /*if (!result){
                message = message + manifestAirChange.getWaybillnomaster() +"发送失败\n";
            }*/
        }
        return  resultJson;
    }

    @Override
    public List<TRANSTOARRIVEEXPORT> selectListByAutoIDS(List<String> autoIDlist) {
        List<TRANSTOARRIVEEXPORT> list =  mapper.selectListByAutoIDS(autoIDlist);
        return list;
朱兆平 authored
169
    }
xudada authored
170
}