UpfileJsonController.java 11.0 KB
package com.tianbo.analysis.controller;

import com.tianbo.analysis.model.ORIGINMANIFESTMASTER;
import com.tianbo.analysis.model.Originmanifestsecondary;
import com.tianbo.analysis.model.ResultJson;
import com.tianbo.analysis.service.UpfileOrigService;
import com.tianbo.analysis.service.UpfileSecondaryService;

import com.tianbo.analysis.tools.ExcelUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/upfile")
public class UpfileJsonController {

    @Autowired
    UpfileOrigService upfileService;
    @Autowired
    UpfileSecondaryService upfileSecondaryService;
    @PostMapping("upfile.json")
    @Transactional
    @ResponseBody
    public ResultJson uploadFileExcel(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request){
        int msg=0;
        Pattern pattern= Pattern.compile("[0-9]*");
        SimpleDateFormat format=new SimpleDateFormat( "yyyy-MM-dd");
        List<ArrayList<String>> readResult = null;//总行记录
        try {
            //判断文件是否为空
            if (file.isEmpty()) {
                return new ResultJson("400","上传文件为空");
            }
            //判断文件大小
            long size = file.getSize();
            String name = file.getOriginalFilename();
            if (StringUtils.isBlank(name) || size == 0) {
                return new ResultJson("400","上传文件为空");
            }
            String postfix = ExcelUtils.getPostfix(name);
            //读取文件内容
            if (StringUtils.equals("xlsx", postfix)) {
                readResult = ExcelUtils.readXlsx(file);
            } else if (StringUtils.equals("xls", postfix)) {
                readResult = ExcelUtils.readXls(file);
            } else {
                return new ResultJson("400","上传文件类型错误");
            }
            if (readResult == null || readResult.size() == 0) {
                return new ResultJson("400","上传文件解析失败");            }
            for (ArrayList<String> arr : readResult) {
                ORIGINMANIFESTMASTER or=new ORIGINMANIFESTMASTER();
                or.setAutoid(UUID.randomUUID().toString());
                if(pattern.matcher(arr.get(2)).matches()){
                    or.setWaybillnomaster(arr.get(2).substring(0,3)+"-"+arr.get(2).substring(3));
                }else{
                    or.setWaybillnomaster(arr.get(2));
                }
                or.setSegment(arr.get(103)+"-"+arr.get(104));//航段
                or.setOriginatingstation(arr.get(103));//起始站
                or.setDestinationstation(arr.get(104));//目的站
                or.setTotalweight(arr.get(8));//总重量
                or.setTotalpiece(arr.get(6));//总件数
                or.setManifesttotalpiece(arr.get(6));//舱单件数
                or.setManifesttotalweight(arr.get(8));//舱单重量
                or.setFlightno(arr.get(64)+arr.get(65));//航班号
                if(pattern.matcher(arr.get(10)).matches()){
                    or.setProductname("CONSOLE");
                }else{
                    if(arr.get(10).length()>150){
                        or.setProductname(arr.get(10).substring(0,100));
                    }else{
                        or.setProductname(arr.get(10));//货物描述
                    }
                }
                or.setCarrier1(arr.get(64));//承运人1
                or.setOriginatingstationBill(arr.get(103));
                or.setDestinationstationBill(arr.get(104));
                or.setArrivalstation1(arr.get(104));//到达城市1
                or.setPaymode("PP");//付费方式
                or.setCustomscode("4604");//海关关区代码
                if(arr.get(89)!=null && arr.get(89)!=""){
                    or.setShippername(arr.get(89));//发货人名称
                }else{
                    or.setShippername("SHIPPER");
                }
                or.setShipperaddress(arr.get(105));//发货人地址
                if(arr.get(50)!=null && arr.get(50)!=""){
                    or.setConsigneename(arr.get(50));//收货人名称
                }else{
                    or.setConsigneename("CONSIGNEER");//收货人名称
                }
                or.setConsigneeaddress(arr.get(47));//收货人地址
                or.setCreatedate(new Date());//创建日期yyyy-MM-dd HH:mm:ss
                or.setFlightDate(format.parse(arr.get(66)));//航班日期yyyy-MM-dd
                or.setStatus("17");//操作状态
                or.setIsbatch("T");//分批标识
                or.setShipperCountrycode(arr.get(86));//发货人国家代码
                or.setShipperPhone(arr.get(106));//发货人联系电话
                or.setConsigneeCountrycode(arr.get(49));//收货人国家代码
                or.setSpecificConsigneename(arr.get(50));//具体收货人名称
                or.setSpecificConsigneePhone(arr.get(51));//具体收货人电话
                or.setConsigneePhone(arr.get(51));//收货人电话
                int result=upfileService.insert(or);
                if(result>0){
                    msg=1;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg>0?new ResultJson("200","导入成功"):new ResultJson("500","导入失效");
    }

    @PostMapping("upfileSecondary.json")
    @Transactional
    @ResponseBody
    public ResultJson uploadSecondary(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request){
        Pattern pattern= Pattern.compile("[0-9]*");
        String waybill=null;
        int msg=0;
        SimpleDateFormat format=new SimpleDateFormat( "yyyy-MM-dd");
        List<ArrayList<String>> readResult = null;//总行记录
        try{
            //判断文件是否为空
            if (file.isEmpty()) {
                return new ResultJson("400","上传文件为空");
            }
            //判断文件大小
            long size = file.getSize();
            String name = file.getOriginalFilename();
            if (StringUtils.isBlank(name) || size == 0) {
                return new ResultJson("400","上传文件为空");
            }
            String postfix = ExcelUtils.getPostfix(name);
            //读取文件内容
            if (StringUtils.equals("xlsx", postfix)) {
                readResult = ExcelUtils.readXlsx(file);
            } else if (StringUtils.equals("xls", postfix)) {
                readResult = ExcelUtils.readXls(file);
            } else {
                return new ResultJson("400","上传文件类型错误");
            }
            if (readResult == null || readResult.size() == 0) {
                return new ResultJson("400","上传文件解析失败");            }
            for (ArrayList<String> arr : readResult) {
                 Originmanifestsecondary or =new Originmanifestsecondary();
                String[] strArr = arr.get(2).split("_");
                if(pattern.matcher(strArr[0]).matches()){
                    waybill=strArr[0].substring(0,3)+"-"+strArr[0].substring(3);
                }else{
                    waybill=strArr[0];
                }
                ORIGINMANIFESTMASTER originmanifestmaster=upfileService.selectwaybill(waybill);
                if(originmanifestmaster!=null){
                    or.setAutoid(UUID.randomUUID().toString());
                    or.setWaybillnomaster(waybill);
                    or.setWaybillnosecondary(strArr[1]);
                    or.setWeight(arr.get(8));
                    or.setPiece(arr.get(6));
                    or.setManifestweight(arr.get(8));
                    or.setManifestpiece(arr.get(6));
                    if(pattern.matcher(arr.get(10)).matches()){
                        or.setProductname("CONSOLE");
                    }else{
                        if(arr.get(10).length()>150){
                            or.setProductname(arr.get(10).substring(0,100));
                        }else{
                            or.setProductname(arr.get(10));//货物描述
                        }
                    }
                    or.setPaymode("PP");//付费方式
                    or.setCustomscode("4604");//海关关区代码
                    if(arr.get(89)!=null && arr.get(89)!=""){
                        or.setShippername(arr.get(89));//发货人名称
                    }else{
                        or.setShippername("SHIPPER");
                    }
                    or.setShipperaddress(arr.get(105));//发货人地址
                    if(arr.get(50)!=null && arr.get(50)!=""){
                        or.setConsigneename(arr.get(50));//收货人名称
                    }else{
                        or.setConsigneename("CONSIGNEER");//收货人名称
                    }
                    or.setConsigneeaddress(arr.get(47));//收货人地址
                    or.setCreatedate(new Date());//创建日期yyyy-MM-dd HH:mm:ss
                    or.setOriginmanifestmasterautoid(originmanifestmaster.getAutoid());
                    or.setStatus("17");//操作状态
                    or.setShipperCountrycode(arr.get(86));//发货人国家代码
                    or.setShipperPhone(arr.get(106));//发货人联系电话
                    or.setConsigneeCountrycode(arr.get(49));//收货人国家代码
                    or.setSpecificConsigneename(arr.get(50));//具体收货人名称
                    or.setSpecificConsigneePhone(arr.get(51));//具体收货人电话
                    or.setConsigneePhone(arr.get(51));//收货人电话
                    or.setOriginatingstationBill(arr.get(103));
                    or.setDestinationstationBill(arr.get(104));
                    int result=upfileSecondaryService.insert(or);
                    if(result>0){
                        msg=1;
                    }
                }else{
                    return new ResultJson("200","没有对应主单");
                }

            }

        }catch (Exception e) {
            e.printStackTrace();
        }
        return msg>0?new ResultJson("200","导入成功"):new ResultJson("500","导入失效");
    }
    @PostMapping("ediOrig")
    public ResultJson ediOrig(@RequestBody ORIGINMANIFESTMASTER originmanifestmaster) throws Exception{
        int res=upfileService.ediOrig(originmanifestmaster);
        return res>0?new ResultJson("200","更新成功"):new ResultJson("201","更新失败");
    }
    @PostMapping("ediOrigDary")
    public ResultJson ediOrigDary(@RequestBody Originmanifestsecondary originmanifestsecondary) throws Exception{
        int res=upfileSecondaryService.ediSecondary(originmanifestsecondary);
        return res>0?new ResultJson("200","更新成功"):new ResultJson("201","更新失败");
    }
}