| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 |
- package com.jeesite.modules.mes.util;
- import com.alibaba.fastjson.JSONObject;
- import com.jeesite.common.config.Global;
- import com.jeesite.common.lang.StringUtils;
- import com.jeesite.modules.mes.req.MesSearchDateReq;
- import com.jeesite.modules.mes.service.MesProductModelService;
- import okhttp3.*;
- import org.apache.tomcat.util.codec.binary.Base64;
- import org.beetl.core.util.ArrayUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import java.io.*;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.nio.file.StandardCopyOption;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.text.DateFormat;
- import java.text.DecimalFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.concurrent.TimeUnit;
- public class CommonUitl {
- /**
- * 时长格式化
- * @param interval
- * @return
- */
- public static String formatDuration(Integer interval){
- if(interval <= 0){
- return "00:00:00";
- }
- int hours = interval / 3600;
- int minutes = (interval % 3600) / 60;
- int remainingSeconds = interval % 60;
- String str = "";
- if(hours >= 10){
- str += hours+":";
- }else{
- str += "0"+hours+":";
- }
- if(minutes >= 10){
- str += minutes+":";
- }else{
- str += "0"+minutes+":";
- }
- if(remainingSeconds >= 10){
- str += remainingSeconds;
- }else{
- str += "0"+remainingSeconds;
- }
- return str;
- }
- /**
- * 格式化工位号
- * @param oprno
- * @return
- */
- public static String formatOprno(String oprno){
- List<String> lists = new ArrayList<>();
- // lists.add("OP180"); // 只有OP180和OP240带ABC
- // lists.add("OP240");
- if(oprno.length() == 6){
- String ysoprno = oprno.substring(0,5).trim();
- if(!lists.contains(ysoprno)){
- oprno = ysoprno;
- }
- }
- return oprno;
- }
- /**
- * 获取几天前的时间
- * @param days
- * @return
- */
- public static Date getPreviousDate(int days) {
- Calendar calendar = Calendar.getInstance();
- calendar.add(Calendar.DAY_OF_MONTH, -days);
- return calendar.getTime();
- }
- /**
- * 获取产品类型 151245P00000106200100023022500001
- * @param sn
- * @return
- */
- public static String getProductCateBySn(String sn){
- if(sn.length() < 19){
- return "";
- }
- return sn.substring(6,19);
- }
- /**
- * 根据序号获取工艺号
- * @param first 首位
- * @param number 序号
- * @return
- */
- public static String getCraftByNumber(String first,Integer number){
- return first+StringUtils.leftPad(String.valueOf(number),5,"0");
- }
- public static JSONObject doPost(String httpUrl, String param) {
- // System.out.println("httpUrl"+httpUrl);
- // System.out.println("param"+param);
- HttpURLConnection connection = null;
- InputStream is = null;
- OutputStream os = null;
- BufferedReader br = null;
- String result = null;
- try {
- URL url = new URL(httpUrl);
- connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setConnectTimeout(15000);
- connection.setReadTimeout(15000);
- connection.setDoOutput(true);
- connection.setDoInput(true);
- connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
- os = connection.getOutputStream();
- if(!param.isEmpty()){
- os.write(param.getBytes());
- }
- if (connection.getResponseCode() == 200) {
- is = connection.getInputStream();
- br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
- StringBuffer sbf = new StringBuffer();
- String temp = null;
- while ((temp = br.readLine()) != null) {
- sbf.append(temp);
- sbf.append("\r\n");
- }
- result = sbf.toString();
- }
- } catch (MalformedURLException e) {
- // e.printStackTrace();
- } catch (IOException e) {
- // e.printStackTrace();
- } finally {
- if (null != br) {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (null != os) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (null != is) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- connection.disconnect();
- }
- System.out.println("result="+result);
- if(result.equalsIgnoreCase("false") || result.isEmpty()) {
- return null;
- }else {
- return JSONObject.parseObject(result);
- }
- }
- public static String Date2TimeStamp(Date date) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String format = sdf.format(date);
- try {
- String valueOf = String.valueOf(sdf.parse(format).getTime() / 1000);
- return valueOf;
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return "";
- }
- public static String getToday() {
- try {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Date date = new Date();
- String today = sdf.format(date);
- return today;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
- public static String TimeStamp2Date(String timestampString) {
- // String timestampString = "1556169710";
- String formats = "yyyy-MM-dd HH:mm:ss";
- Long timestamp = Long.parseLong(timestampString) * 1000;
- //日期格式字符串
- String dateStr = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
- return dateStr;
- }
- public static Date String2Date(String str){
- try{
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期格式
- Date date = sdf.parse(str); // 进行转换
- return date;
- }catch (Exception e){
- return null;
- }
- }
- public static Date String2DateTime(String str){
- try{
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义日期格式
- Date date = sdf.parse(str); // 进行转换
- return date;
- }catch (Exception e){
- return null;
- }
- }
- /**
- * 检验工件码的正确性
- * @param sn 工件码
- * @param cateSn 产品类型编码
- * @return
- */
- public static Boolean checkProductSn(String sn,String cateSn){
- switch (cateSn){
- case "ZHXT":
- if(sn.length() != 28 ){
- return false;
- }
- // 000020015305-0100101425011400038 0000+10位料号+7位供应商代码+6位生产日期+5位序列号
- // 2520200FF31006368EE00050 印尼版 24位 10位零件号+6位供应商代码+7位批次码+1位返工码
- // List<String> xwds = new ArrayList<>();
- // String xwd = ""; // 物料号
- // String gys = ""; // 供应商代码
- // if(sn.length() == 24){
- // xwds.add("2520200FF3"); //印尼版
- // // 物料号
- // xwd = sn.substring(0,10);
- // // 供应商代码
- // gys = "100636";
- // if(!sn.substring(10,16).equals(gys)){
- // return false;
- // }
- // }else{
- // xwds.add("000020015308-0"); // 国内长续航
- // xwds.add("000020015305-0"); // 国内超长续航
- // xwds.add("000020018302-0"); // 海外长续航
- // xwds.add("000020018301-0"); // 海外超长续航
- // xwds.add("000020017939-0"); // 中亚长续航
- // xwds.add("000020017933-0"); // 中亚超长续航
- //
- // // 物料号
- // xwd = sn.substring(0,14);
- // // 供应商代码
- // gys = "1001014";
- // if(!sn.substring(14,21).equals(gys)){
- // return false;
- // }
- // }
- //
- // if(!xwds.contains(xwd)){
- // return false;
- // }
- // return true;
- String gdStr1 = "509900661376119990";
- if (!sn.substring(0,18).equals(gdStr1)){
- return false;
- }
- return true;
- }
- return false;
- }
- // 检查底护板批次码是否正确
- public static boolean checkBatchSn(String batchSn,String sn) {
- if(batchSn.length() < 20 || sn.length() < 20){
- return false;
- }
- if(sn.length() != 24 && sn.length() != 32){
- return false;
- }
- String xwd = "";
- if(sn.length() == 24){
- xwd = sn.substring(0,10);
- }else{
- xwd = sn.substring(0,14);
- }
- String bts = batchSn.substring(0,14);
- switch (xwd){
- case "000020015308-0":// 国内长续航
- case "000020015305-0":// 国内超长续航
- case "000020017939-0":// 中亚长续航
- case "000020017933-0":// 中亚超长续航
- if(bts.equals("000010056457-0")){ //000010056457-0100101424122630087
- return true;
- }else{
- return false;
- }
- case "000020018302-0":// 海外长续航
- case "000020018301-0":// 海外超长续航
- case "2520200FF3":
- if(bts.equals("000010061509-0")){
- return true;
- }else{
- return false;
- }
- }
- return false;
- }
- public static String getBase64Decode(String str) {
- if (str == null || "".equals(str)) {
- return "";
- }
- try {
- byte[] bt2 = Base64.decodeBase64(str.getBytes("UTF-8"));
- str = new String(bt2,"UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- str = "";
- }
- return str;
- }
- public static double secondsToHours(double seconds){
- double hours = seconds / 3600;
- return hours;
- }
- public static String[] getTimeInterval(String strStartTime, String strStopTime) {
- String arrStr[] = new String[2];
- try {
- DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date startTime = df.parse(strStartTime);
- Date stopTime = df.parse(strStopTime);
- DecimalFormat decimalFormat = new DecimalFormat("00");
- long diff = stopTime.getTime() - startTime.getTime();//得到的差值
- //logger.debug("------ " + diff);
- long hours = diff / (1000 * 60 * 60); //获取时
- long minutes = (diff - hours * (1000 * 60 * 60)) / (1000 * 60); //获取分钟
- long s = (diff / 1000 - hours * 60 * 60 - minutes * 60);//获取秒
- String countTime = "" + decimalFormat.format(hours) + ":" + decimalFormat.format(minutes) + ":" + decimalFormat.format(s);
- long second = (diff / 1000);//获取 共有多少秒
- String strTimeLenS = second + "";
- arrStr[0] = countTime;//00:12:50 时间格式
- arrStr[1] = strTimeLenS;//140 总秒数
- return arrStr;
- } catch (Exception ex) {
- // logger.error("getTimeInterval() 获取时间间隔 " + strStartTime + " "+strStopTime + ex.toString());
- arrStr[0] = "-1";//00:12:50 时间格式
- arrStr[1] = "-1";//140 总秒数
- return arrStr;
- }
- }
- public static long convertDateStringToTimestamp(String dateString, String format){
- try{
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- Date date = sdf.parse(dateString);
- return date.getTime();
- }catch (Exception e){
- return 0;
- }
- }
- // 判断是否是单部件
- public static Boolean checkDbjOprno(String oprno,String lineSn){
- List<String> dbjoprnos = new ArrayList<>();
- Boolean ret = false;
- if(lineSn.equals("XT")){
- // dbjoprnos.add("OP080A");
- if(dbjoprnos.contains(oprno)){
- return true;
- }
- }
- return ret;
- }
- /**
- * 表单形式post请求
- *
- * @paramurl 请求地址
- * @parammap post请求参数
- * @return请求结果
- */
- public static JSONObject doPostWithForm(Map<String, String> map, String url) {
- try{
- OkHttpClient client = new OkHttpClient().newBuilder()
- .connectTimeout(30, TimeUnit.SECONDS)
- .readTimeout(30, TimeUnit.SECONDS)
- .writeTimeout(30, TimeUnit.SECONDS)
- .build();
- //构建一个formBody builder
- FormBody.Builder builder = new FormBody.Builder();
- //循环form表单,将表单内容添加到form builder中
- for (Map.Entry<String, String> entry : map.entrySet()) {
- String key = entry.getKey();
- String value = entry.getValue();
- builder.add(key, value);
- }
- //构建formBody,将其传入Request请求中
- FormBody body = builder.build();
- Request request = new Request.Builder().url(url).post(body).build();
- Call call = client.newCall(request);
- //返回请求结果
- try (Response response = call.execute()) {
- String result = response.body().string();
- if(result.equalsIgnoreCase("false") || result.isEmpty()) {
- return null;
- }else {
- return JSONObject.parseObject(result);
- }
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }catch (Exception e){
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 判段工件码是否符合要求
- * @param sn 工件码
- * @param spec 规格
- * @param specStart 规格开始位置
- * @param length 码长度
- * @return
- */
- public static Boolean checkSnByModel(String sn, String spec,Integer specStart, Integer length) {
- if(sn.length()!=length){
- return false;
- }
- Integer start=specStart - 1;
- Integer end=start + spec.length();
- if(length > end){
- String newSpec = sn.substring(start,end);
- if(newSpec.equals(spec)){
- return true;
- }
- }
- return false;
- }
- //驭视科技CCD接口返回xml模板
- public static String getXml(String content){
- String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
- xml += "<string xmlns=\"http://mesateapi.com/\">"+content+"</string>";
- return xml;
- }
- public static MesSearchDateReq getSearchDate(){
- String days = Global.getConfig("mes.search.days");
- if(StringUtils.isEmpty(days)){
- days = "7";
- }
- int day = Integer.parseInt(days);
- if(day < 1){ // 小于1使用默认值
- day = 7;
- }
- Date curDate = new Date();
- // 获取当前日期的Calendar实例
- Calendar calendar = Calendar.getInstance();
- // 将当前日期的Calendar回退day天
- calendar.add(Calendar.DAY_OF_MONTH, -day);
- // 获取3天前的Date
- Date sevenDaysAgo = calendar.getTime();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- String sevenDaysAgoStr = sdf.format(sevenDaysAgo)+" 00:00";
- String curDateStr = sdf.format(curDate)+" 23:59";
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
- try {
- sevenDaysAgo = dateFormat.parse(sevenDaysAgoStr);
- curDate = dateFormat.parse(curDateStr);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- MesSearchDateReq map = new MesSearchDateReq();
- map.setStartDate(sevenDaysAgo);
- map.setEndtDate(curDate);
- return map;
- }
- public static String getMD5Hash(String input) {
- try {
- // 创建MessageDigest对象,指定MD5算法
- MessageDigest md = MessageDigest.getInstance("MD5");
- // 计算输入字符串的MD5散列值
- byte[] messageDigest = md.digest(input.getBytes());
- // 将字节数组转换为十六进制字符串
- StringBuilder hexString = new StringBuilder();
- for (byte b : messageDigest) {
- String hex = Integer.toHexString(0xff & b);
- if (hex.length() == 1) hexString.append('0');
- hexString.append(hex);
- }
- return hexString.toString();
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- // throw new RuntimeException(e);
- return "";
- }
- }
- public static Boolean createDir(String newDir) {
- Path path = Paths.get(newDir);
- // 检查目录是否存在
- if (!Files.exists(path)) {
- try {
- // 创建目录,包括所有必需但不存在的父目录
- Files.createDirectories(path);
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
- return true;
- }
- public static Boolean moveFile(String filepath,String movedir){
- Path sourcePath = Paths.get(filepath); // 例如:Paths.get("C:/old/file.txt")
- Path destinationPath = Paths.get(movedir); // 例如:Paths.get("C:/new/")
- Path newFilePath = destinationPath.resolve(sourcePath.getFileName()); // 确保文件名在新目录中唯一
- try {
- // 复制文件到新目录
- Files.copy(sourcePath, newFilePath, StandardCopyOption.REPLACE_EXISTING);
- // 如果需要,删除原始文件
- // Files.delete(sourcePath);
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- }
|