LogicOperation.java 5.4 KB
package com.sy.logic;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sy.model.GatherInfo;
import com.sy.model.LandBusinessTypeList;
import com.sy.model.LandRoadVe;
import com.sy.service.LandBusListService;
import com.sy.service.LandRoadVeService;
import com.sy.socket.CommandClient;
import com.sy.utils.FileTool;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@Component
public class LogicOperation {

    private static final Logger logger = Logger.getLogger(LogicOperation.class);

    @Autowired
    private LandBusListService listService;

    @Autowired
    private LandRoadVeService veService;

    private static LogicOperation logic;

    //读取配置文件里的载重与称重的可控范围
    private static String checkWt = FileTool.readProperties("grossWt");

    @PostConstruct
    public void init(){
        logic = this;
        logic.listService = this.listService;
        logic.veService = this.veService;
    }

    public boolean operation(GatherInfo info){
        //开关
        boolean sign = false;
        BigDecimal grosswt = info.getGrosswt();
        //转为double类型
        double growsswWt = grosswt.doubleValue();
        String ietype = info.getIetype();
        List<LandBusinessTypeList>lists = null;
        LandRoadVe ve = logic.veService.selectByFrameNo(info.getIcvename());
        if("E".equals(ietype)){
            ietype = "出场站";
            lists = logic.listService.selectByFrameNoAndType(info.getIcvename(),ietype);
            for (LandBusinessTypeList list:lists) {
                //查询是否做了出场申请
                if(list.getContrastflag()=="" | list.getContrastflag()==null){
                    //判断车重
                    //判断载货重量是否符合所设定的范围
                    double goodsWt = GoodsWt(list.getMasterList(),"E");
                    if(checkResult(growsswWt,Double.parseDouble(ve.getSelfWt()),goodsWt)){
                        sign = true;
                    }
                    //发送放行指令
                    String flag = "";
                    CommandClient.Client(info,flag);
                }
            }
        }else {
            ietype = "进场站";
            lists = listService.selectByFrameNoAndType(info.getIcvename(),ietype);
        }
        return sign;
    }

    //将获取的checkWt进行小数转化
    public static double valueDob(){
        NumberFormat nf=NumberFormat.getPercentInstance();
        Number m = null;
        try {
            m=nf.parse(checkWt);//将百分数转换成Number类型
        }catch (ParseException e){
            e.printStackTrace();
            logger.info(e.getMessage());
        }
        return m.doubleValue();
    }

    /**
     * @Param grossWt 地磅称重
     * @Param wt 车辆自重
     * @Param goodsWt 货物总重
     * @Result 获取运单重量
     * */
    //校验载重和称重是否在合理的范围
    public boolean checkResult(double grossWt,double wt,double goodsWt){
        boolean flag = false;
        double result = (grossWt-wt)/goodsWt;
        BigDecimal bd = new BigDecimal(result);
        if(bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue()<=valueDob()){
            flag = true;
        }
        return flag;
    }

    /**
     * @Param mainifast 主单列表
     * @Param ietype 进出标志
     * 获取货物总重
     * */
    public static double GoodsWt(String mainifast,String ietype){
        String [] mainifastList = mainifast.split(",");
        Double sum = 0.0;
        for (String mainBill:mainifastList) {
            sum = sum+=getGrossWt(mainBill,ietype);
        }
        return sum;
    }


    /**
     * @Param waybill 主单号
     * @Param imp 进出港标识
     * @Result 获取运单重量
     * */
    public static double getGrossWt(String waybill, String imp) {
        String url = "http://tjfx.15miaoo.com:8003/tj/orig/orig?waybill=" + waybill + "&imp=" + imp;
        StringBuilder json = new StringBuilder();
        Map map = null;
        double bg = 0;
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            JSONArray array = JSONArray.parseArray(json.toString());
            for (int i = 0; i < array.size(); i++) {
                map = JSON.parseObject(array.getString(i));
                if (map.get("receiptinformation").toString().startsWith("41301")) {
                    bg = Double.parseDouble((String) map.get("totalweight"));
                    return bg;
                }
            }
            in.close();
        } catch (MalformedURLException e) {
            e.getMessage();
        } catch (IOException e) {
            e.getMessage();
        }
        return bg;
    }
}