CommonUitl.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. package com.jeesite.modules.mes.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.jeesite.common.config.Global;
  4. import com.jeesite.common.lang.StringUtils;
  5. import com.jeesite.modules.mes.req.MesSearchDateReq;
  6. import okhttp3.*;
  7. import org.apache.tomcat.util.codec.binary.Base64;
  8. import org.beetl.core.util.ArrayUtils;
  9. import java.io.*;
  10. import java.net.HttpURLConnection;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.text.DateFormat;
  14. import java.text.DecimalFormat;
  15. import java.text.ParseException;
  16. import java.text.SimpleDateFormat;
  17. import java.util.*;
  18. import java.util.concurrent.TimeUnit;
  19. public class CommonUitl {
  20. /**
  21. * 时长格式化
  22. * @param interval
  23. * @return
  24. */
  25. public static String formatDuration(Integer interval){
  26. if(interval <= 0){
  27. return "00:00:00";
  28. }
  29. int hours = interval / 3600;
  30. int minutes = (interval % 3600) / 60;
  31. int remainingSeconds = interval % 60;
  32. String str = "";
  33. if(hours >= 10){
  34. str += hours+":";
  35. }else{
  36. str += "0"+hours+":";
  37. }
  38. if(minutes >= 10){
  39. str += minutes+":";
  40. }else{
  41. str += "0"+minutes+":";
  42. }
  43. if(remainingSeconds >= 10){
  44. str += remainingSeconds;
  45. }else{
  46. str += "0"+remainingSeconds;
  47. }
  48. return str;
  49. }
  50. /**
  51. * 格式化工位号
  52. * @param oprno
  53. * @return
  54. */
  55. public static String formatOprno(String oprno){
  56. List<String> lists = new ArrayList<>();
  57. // lists.add("OP180"); // 只有OP180和OP240带ABC
  58. // lists.add("OP240");
  59. if(oprno.length() == 6){
  60. String ysoprno = oprno.substring(0,5).trim();
  61. if(!lists.contains(ysoprno)){
  62. oprno = ysoprno;
  63. }
  64. }
  65. return oprno;
  66. }
  67. /**
  68. * 获取几天前的时间
  69. * @param days
  70. * @return
  71. */
  72. public static Date getPreviousDate(int days) {
  73. Calendar calendar = Calendar.getInstance();
  74. calendar.add(Calendar.DAY_OF_MONTH, -days);
  75. return calendar.getTime();
  76. }
  77. /**
  78. * 获取产品类型 151245P00000106200100023022500001
  79. * @param sn
  80. * @return
  81. */
  82. public static String getProductCateBySn(String sn){
  83. if(sn.length() < 19){
  84. return "";
  85. }
  86. return sn.substring(6,19);
  87. }
  88. /**
  89. * 根据序号获取工艺号
  90. * @param first 首位
  91. * @param number 序号
  92. * @return
  93. */
  94. public static String getCraftByNumber(String first,Integer number){
  95. return first+StringUtils.leftPad(String.valueOf(number),5,"0");
  96. }
  97. public static JSONObject doPost(String httpUrl, String param) {
  98. // System.out.println("httpUrl"+httpUrl);
  99. // System.out.println("param"+param);
  100. HttpURLConnection connection = null;
  101. InputStream is = null;
  102. OutputStream os = null;
  103. BufferedReader br = null;
  104. String result = null;
  105. try {
  106. URL url = new URL(httpUrl);
  107. connection = (HttpURLConnection) url.openConnection();
  108. connection.setRequestMethod("POST");
  109. connection.setConnectTimeout(15000);
  110. connection.setReadTimeout(15000);
  111. connection.setDoOutput(true);
  112. connection.setDoInput(true);
  113. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  114. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  115. os = connection.getOutputStream();
  116. if(!param.isEmpty()){
  117. os.write(param.getBytes());
  118. }
  119. if (connection.getResponseCode() == 200) {
  120. is = connection.getInputStream();
  121. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  122. StringBuffer sbf = new StringBuffer();
  123. String temp = null;
  124. while ((temp = br.readLine()) != null) {
  125. sbf.append(temp);
  126. sbf.append("\r\n");
  127. }
  128. result = sbf.toString();
  129. }
  130. } catch (MalformedURLException e) {
  131. // e.printStackTrace();
  132. } catch (IOException e) {
  133. // e.printStackTrace();
  134. } finally {
  135. if (null != br) {
  136. try {
  137. br.close();
  138. } catch (IOException e) {
  139. e.printStackTrace();
  140. }
  141. }
  142. if (null != os) {
  143. try {
  144. os.close();
  145. } catch (IOException e) {
  146. e.printStackTrace();
  147. }
  148. }
  149. if (null != is) {
  150. try {
  151. is.close();
  152. } catch (IOException e) {
  153. e.printStackTrace();
  154. }
  155. }
  156. connection.disconnect();
  157. }
  158. System.out.println("result="+result);
  159. if(result.equalsIgnoreCase("false") || result.isEmpty()) {
  160. return null;
  161. }else {
  162. return JSONObject.parseObject(result);
  163. }
  164. }
  165. public static String Date2TimeStamp(Date date) {
  166. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  167. String format = sdf.format(date);
  168. try {
  169. String valueOf = String.valueOf(sdf.parse(format).getTime() / 1000);
  170. return valueOf;
  171. } catch (ParseException e) {
  172. e.printStackTrace();
  173. }
  174. return "";
  175. }
  176. public static String getToday() {
  177. try {
  178. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  179. Date date = new Date();
  180. String today = sdf.format(date);
  181. return today;
  182. } catch (Exception e) {
  183. e.printStackTrace();
  184. }
  185. return "";
  186. }
  187. public static String TimeStamp2Date(String timestampString) {
  188. // String timestampString = "1556169710";
  189. String formats = "yyyy-MM-dd HH:mm:ss";
  190. Long timestamp = Long.parseLong(timestampString) * 1000;
  191. //日期格式字符串
  192. String dateStr = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
  193. return dateStr;
  194. }
  195. public static Date String2Date(String str){
  196. try{
  197. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期格式
  198. Date date = sdf.parse(str); // 进行转换
  199. return date;
  200. }catch (Exception e){
  201. return null;
  202. }
  203. }
  204. public static Date String2DateTime(String str){
  205. try{
  206. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义日期格式
  207. Date date = sdf.parse(str); // 进行转换
  208. return date;
  209. }catch (Exception e){
  210. return null;
  211. }
  212. }
  213. /**
  214. * 检验工件码的正确性
  215. * @param sn 工件码
  216. * @param cateSn 产品类型编码
  217. * @return
  218. */
  219. public static Boolean checkProductSn(String sn,String cateSn){
  220. switch (cateSn){
  221. case "ZHXT":
  222. // 21位流水码验证
  223. if(sn.length() == 21){
  224. // 21位流水码格式:+KA99IS3031 + 生产日期(4位) + 生产流水号(4位) + 原材料追溯码(2位,01)
  225. // 例如:+KA99IS3031RA1800A201
  226. // 1. 验证固定前缀:+KA99IS3031 或 +KA64IS3031
  227. List<String> prefixList = Arrays.asList("+KA99IS3031", "+KA64IS3031","+KB23IS3031","+KB24IS3031","+KA93IS3031");
  228. String snPrefix = sn.substring(0, 11);
  229. if(!prefixList.contains(snPrefix)){
  230. return false;
  231. }
  232. // // 2. 验证生产日期部分(第12-15位,共4位)
  233. // String dateCode = sn.substring(11, 15);
  234. // // 年份字母(1位) + 月份(1位) + 日期(2位)
  235. // if(dateCode.length() != 4){
  236. // return false;
  237. // }
  238. //
  239. // // 验证年份字母(第1位)
  240. // char yearChar = dateCode.charAt(0);
  241. // if(!Character.isLetter(yearChar) || !Character.isUpperCase(yearChar)){
  242. // return false;
  243. // }
  244. //
  245. // // 验证月份(第2位):1-9或A、B、C
  246. // char monthChar = dateCode.charAt(1);
  247. // if(!(Character.isDigit(monthChar) && monthChar >= '1' && monthChar <= '9')
  248. // && !(monthChar == 'A' || monthChar == 'B' || monthChar == 'C')){
  249. // return false;
  250. // }
  251. //
  252. // // 验证日期(第3-4位)
  253. // String dayStr = dateCode.substring(2, 4);
  254. // if(!dayStr.matches("\\d{2}")){
  255. // return false;
  256. // }
  257. // int day = Integer.parseInt(dayStr);
  258. // if(day < 1 || day > 31){
  259. // return false;
  260. // }
  261. //
  262. // // 3. 验证生产流水号(第16-19位,共4位,应为数字)
  263. // String serialNumber = sn.substring(15, 19);
  264. // if(!serialNumber.matches("\\d{4}")){
  265. // return false;
  266. // }
  267. //
  268. // // 4. 验证原材料追溯码(第20-21位,固定为01)
  269. // String traceCode = sn.substring(19, 21);
  270. // if(!traceCode.equals("01")){
  271. // return false;
  272. // }
  273. return true;
  274. }
  275. // 原有的32位和24位工件码验证
  276. else if(sn.length() != 32 && sn.length() != 24){
  277. return false;
  278. }
  279. // 000020015305-0100101425011400038 0000+10位料号+7位供应商代码+6位生产日期+5位序列号
  280. // 2520200FF31006368EE00050 印尼版 24位 10位零件号+6位供应商代码+7位批次码+1位返工码
  281. List<String> xwds = new ArrayList<>();
  282. String xwd = ""; // 物料号
  283. String gys = ""; // 供应商代码
  284. if(sn.length() == 24){
  285. xwds.add("2520200FF3"); //印尼版长续航
  286. xwds.add("2520200FF1"); //印尼版超长续航
  287. // 物料号
  288. xwd = sn.substring(0,10);
  289. // 供应商代码
  290. gys = "100636";
  291. if(!sn.substring(10,16).equals(gys)){
  292. return false;
  293. }
  294. }else{
  295. xwds.add("000020015308-0"); // 国内长续航
  296. xwds.add("000020015305-0"); // 国内超长续航
  297. xwds.add("000020018302-0"); // 海外长续航
  298. xwds.add("000020018301-0"); // 海外超长续航
  299. xwds.add("000020020155-0"); // 国内长续航
  300. xwds.add("000020020156-0"); // 国内超长续航
  301. xwds.add("000020020890-0"); // 海外长续航
  302. xwds.add("000020020889-0"); // 海外超长续航
  303. xwds.add("000020017939-0"); // 中亚长续航
  304. xwds.add("000020017933-0"); // 中亚超长续航
  305. // 物料号
  306. xwd = sn.substring(0,14);
  307. // 供应商代码
  308. gys = "1001014";
  309. if(!sn.substring(14,21).equals(gys)){
  310. return false;
  311. }
  312. }
  313. if(!xwds.contains(xwd)){
  314. return false;
  315. }
  316. return true;
  317. }
  318. return false;
  319. }
  320. // 检查底护板批次码是否正确
  321. public static boolean checkBatchSn(String batchSn,String sn) {
  322. if(batchSn.length() < 20 || sn.length() < 20){
  323. return false;
  324. }
  325. if(sn.length() != 24 && sn.length() != 32){
  326. return false;
  327. }
  328. String xwd = "";
  329. if(sn.length() == 24){
  330. xwd = sn.substring(0,10);
  331. }else{
  332. xwd = sn.substring(0,14);
  333. }
  334. String bts = batchSn.substring(0,14);
  335. switch (xwd){
  336. case "000020015308-0":// 国内长续航
  337. case "000020015305-0":// 国内超长续航
  338. case "000020020155-0":// 国内长续航
  339. case "000020020156-0":// 国内超长续航
  340. case "000020017939-0":// 中亚长续航
  341. case "000020017933-0":// 中亚超长续航
  342. if(bts.equals("000010056457-0")){ //000010056457-0100101424122630087
  343. return true;
  344. }else{
  345. return false;
  346. }
  347. case "000020018302-0":// 海外长续航
  348. case "000020018301-0":// 海外超长续航
  349. case "000020020890-0":// 海外长续航
  350. case "000020020889-0":// 海外超长续航
  351. case "2520200FF3": // 印尼版
  352. case "2520200FF1": // 印尼版
  353. if(bts.equals("000010061509-0")){
  354. return true;
  355. }else{
  356. return false;
  357. }
  358. }
  359. return false;
  360. }
  361. public static String getBase64Decode(String str) {
  362. if (str == null || "".equals(str)) {
  363. return "";
  364. }
  365. try {
  366. byte[] bt2 = Base64.decodeBase64(str.getBytes("UTF-8"));
  367. str = new String(bt2,"UTF-8");
  368. } catch (UnsupportedEncodingException e) {
  369. e.printStackTrace();
  370. str = "";
  371. }
  372. return str;
  373. }
  374. public static double secondsToHours(double seconds){
  375. double hours = seconds / 3600;
  376. return hours;
  377. }
  378. public static String[] getTimeInterval(String strStartTime, String strStopTime) {
  379. String arrStr[] = new String[2];
  380. try {
  381. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  382. Date startTime = df.parse(strStartTime);
  383. Date stopTime = df.parse(strStopTime);
  384. DecimalFormat decimalFormat = new DecimalFormat("00");
  385. long diff = stopTime.getTime() - startTime.getTime();//得到的差值
  386. //logger.debug("------ " + diff);
  387. long hours = diff / (1000 * 60 * 60); //获取时
  388. long minutes = (diff - hours * (1000 * 60 * 60)) / (1000 * 60); //获取分钟
  389. long s = (diff / 1000 - hours * 60 * 60 - minutes * 60);//获取秒
  390. String countTime = "" + decimalFormat.format(hours) + ":" + decimalFormat.format(minutes) + ":" + decimalFormat.format(s);
  391. long second = (diff / 1000);//获取 共有多少秒
  392. String strTimeLenS = second + "";
  393. arrStr[0] = countTime;//00:12:50 时间格式
  394. arrStr[1] = strTimeLenS;//140 总秒数
  395. return arrStr;
  396. } catch (Exception ex) {
  397. // logger.error("getTimeInterval() 获取时间间隔 " + strStartTime + " "+strStopTime + ex.toString());
  398. arrStr[0] = "-1";//00:12:50 时间格式
  399. arrStr[1] = "-1";//140 总秒数
  400. return arrStr;
  401. }
  402. }
  403. public static long convertDateStringToTimestamp(String dateString, String format){
  404. try{
  405. SimpleDateFormat sdf = new SimpleDateFormat(format);
  406. Date date = sdf.parse(dateString);
  407. return date.getTime();
  408. }catch (Exception e){
  409. return 0;
  410. }
  411. }
  412. /**
  413. * 判断是否是+KA64IS3031开头的21位流水码
  414. * @param sn 工件码
  415. * @return
  416. */
  417. public static Boolean isKA64Sn(String sn){
  418. if(sn == null || sn.length() != 21){
  419. return false;
  420. }
  421. return sn.startsWith("+KA64IS3031");
  422. }
  423. // 判断是否是单部件
  424. public static Boolean checkDbjOprno(String oprno,String lineSn){
  425. List<String> dbjoprnos = new ArrayList<>();
  426. Boolean ret = false;
  427. if(lineSn.equals("XT")){
  428. dbjoprnos.add("OP030");
  429. if(dbjoprnos.contains(oprno)){
  430. return true;
  431. }
  432. }
  433. return ret;
  434. }
  435. /**
  436. * 表单形式post请求
  437. *
  438. * @paramurl 请求地址
  439. * @parammap post请求参数
  440. * @return请求结果
  441. */
  442. public static JSONObject doPostWithForm(Map<String, String> map, String url) {
  443. try{
  444. OkHttpClient client = new OkHttpClient().newBuilder()
  445. .connectTimeout(30, TimeUnit.SECONDS)
  446. .readTimeout(30, TimeUnit.SECONDS)
  447. .writeTimeout(30, TimeUnit.SECONDS)
  448. .build();
  449. //构建一个formBody builder
  450. FormBody.Builder builder = new FormBody.Builder();
  451. //循环form表单,将表单内容添加到form builder中
  452. for (Map.Entry<String, String> entry : map.entrySet()) {
  453. String key = entry.getKey();
  454. String value = entry.getValue();
  455. builder.add(key, value);
  456. }
  457. //构建formBody,将其传入Request请求中
  458. FormBody body = builder.build();
  459. Request request = new Request.Builder().url(url).post(body).build();
  460. Call call = client.newCall(request);
  461. //返回请求结果
  462. try (Response response = call.execute()) {
  463. String result = response.body().string();
  464. if(result.equalsIgnoreCase("false") || result.isEmpty()) {
  465. return null;
  466. }else {
  467. return JSONObject.parseObject(result);
  468. }
  469. } catch (IOException e) {
  470. e.printStackTrace();
  471. return null;
  472. }
  473. }catch (Exception e){
  474. e.printStackTrace();
  475. return null;
  476. }
  477. }
  478. /**
  479. * 判段工件码是否符合要求
  480. * @param sn 工件码
  481. * @param spec 规格
  482. * @param specStart 规格开始位置
  483. * @param length 码长度
  484. * @return
  485. */
  486. public static Boolean checkSnByModel(String sn, String spec,Integer specStart, Integer length) {
  487. if(sn.length()!=length){
  488. return false;
  489. }
  490. Integer start=specStart - 1;
  491. Integer end=start + spec.length();
  492. if(length > end){
  493. String newSpec = sn.substring(start,end);
  494. if(newSpec.equals(spec)){
  495. return true;
  496. }
  497. }
  498. return false;
  499. }
  500. //驭视科技CCD接口返回xml模板
  501. public static String getXml(String content){
  502. String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  503. xml += "<string xmlns=\"http://mesateapi.com/\">"+content+"</string>";
  504. return xml;
  505. }
  506. public static MesSearchDateReq getSearchDate(){
  507. String days = Global.getConfig("mes.search.days");
  508. if(StringUtils.isEmpty(days)){
  509. days = "7";
  510. }
  511. int day = Integer.parseInt(days);
  512. if(day < 1){ // 小于1使用默认值
  513. day = 7;
  514. }
  515. Date curDate = new Date();
  516. // 获取当前日期的Calendar实例
  517. Calendar calendar = Calendar.getInstance();
  518. // 将当前日期的Calendar回退day天
  519. calendar.add(Calendar.DAY_OF_MONTH, -day);
  520. // 获取3天前的Date
  521. Date sevenDaysAgo = calendar.getTime();
  522. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  523. String sevenDaysAgoStr = sdf.format(sevenDaysAgo)+" 00:00";
  524. String curDateStr = sdf.format(curDate)+" 23:59";
  525. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  526. try {
  527. sevenDaysAgo = dateFormat.parse(sevenDaysAgoStr);
  528. curDate = dateFormat.parse(curDateStr);
  529. } catch (ParseException e) {
  530. e.printStackTrace();
  531. }
  532. MesSearchDateReq map = new MesSearchDateReq();
  533. map.setStartDate(sevenDaysAgo);
  534. map.setEndtDate(curDate);
  535. return map;
  536. }
  537. }