CommonUitl.java 19 KB

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