LogicOperation.java 6.5 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");

    //逻辑判断后的结果定义
    private static String PERMITTHOUGH = "直接放行";
    private static String GROWSSEXCETION = "禁止通行,重量不在可控范围";
    private static String NORECORD = "车辆未备案";
    private static String INPUTSTATION = "此车辆未做进站申请";
    private static String ENTERSTATION = "此车辆未做出站申请";

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

    /**
     * @Param info 卡口采集数据
     * @Result 计算卡口采集数据并发送报文
     */
    public static boolean operation(GatherInfo info) {
        boolean result = false;
        BigDecimal grosswt = info.getGrosswt();
        //转为double类型
        double growssWt = grosswt.doubleValue();
        String ietype = info.getIetype();
        List<LandBusinessTypeList> lists = null;
        String vaName = info.getVename();
        LandRoadVe ve = logic.veService.selectByFrameNo(vaName);
        if(ve ==null) return result;
        LandBusinessTypeList list = logic.listService.selectForOne(info.getVename(),info.getBarcode(),info.getAreaid
                (),info.getChnlno(),info.getIetype());
        double goodsWt = 0.0;
        if (list.getMasterList().length() > 0) {
            goodsWt = GoodsWt(list.getMasterList(), info.getIetype());
        }
        double selfWt = Double.parseDouble(ve.getSelfWt());
        if (sendBw(info, growssWt, selfWt, goodsWt)) {
            if ("I".equals(info.getIetype())) {
                list.setContrastflag("已进站");
            } else {
                list.setContrastflag("已出站");
            }
            logic.listService.updateById(list);
            result = true;
        }
        return result;
    }

    //将获取的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 static boolean checkResult(double grossWt, double wt, double goodsWt) {
        boolean flag = false;
        double result = 0.0;
        if (goodsWt > 0) {
            result = (grossWt - wt) / goodsWt;
            if (result > 1) {
                result = result - 1;
            } else {
                result = 1 - result;
            }
        } else {
            result = (grossWt - wt);
        }
        BigDecimal bd = new BigDecimal(result);
        if (bd.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue() <= valueDob()) {
            flag = true;
        }
        return flag;
    }

    /**
     * @Param mainifast 主单列表
     * @Param ietype 进出标志
     * 获取货物总重
     */
    public static double GoodsWt(String mainifast, String ietype) {
        Double sum = 0.0;
        if (mainifast.length() > 0) {
            String[] mainifastList = mainifast.split(",");
            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 Url = new URL(url);
            URLConnection yc = Url.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") || map.get("receiptinformation")
                        .toString().startsWith("41106")) {
                    bg = Double.parseDouble((String) map.get("totalweight"));
                    return bg;
                }
            }
            in.close();
        } catch (MalformedURLException e) {
            e.getMessage();
        } catch (IOException e) {
            e.getMessage();
        }
        return bg;
    }


    /**
     * 校验重量并发送报文
     */

    public static boolean sendBw(GatherInfo info, double growsswWt, double selfWt, double goodsWt) {
        boolean flag = false;
        if (checkResult(growsswWt, selfWt, goodsWt)) {
            CommandClient.Client(info, PERMITTHOUGH);
            flag = true;
        } else {
            CommandClient.Client(info, GROWSSEXCETION);
        }
        return flag;
    }
}