| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723 |
- 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.text.DateFormat;
- import java.text.DecimalFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.LocalDate;
- import java.time.Year;
- 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 oprno
- * @return
- */
- public static String getOprnoNumber(String oprno){
- List<String> lists = new ArrayList<>();
- String ysoprno = oprno.substring(2,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;
- }
- }
- public static Date String2DateTime2(String str){
- try{
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); // 定义日期格式
- 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() != 32 && sn.length() != 24){
- 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;
- }
- return false;
- }
- /**
- * 检验后面板工件码的正确性
- * @param sn 工件码
- * @return
- */
- public static Boolean checkProductSnHmb(String sn){
- if(sn == null){
- return false;
- }
- // 新规则 24 位(EVE 编码):F956210325195AI24A010001
- if(sn.length() == 24){
- return checkProductSnHmbEve(sn);
- }
- // 老规则 28 位已隐藏,仅保留 24 位新码校验
- /*
- // 老规则 28 位:0050010400435E6348245C120001
- if(sn.length() != 28){
- return false;
- }
- List<String> xwds = new ArrayList<>();
- xwds.add("0050010400435");
- String xwd = sn.substring(0,13);
- if(!xwds.contains(xwd)){
- return false;
- }
- // 供应商代码
- String gys = "34824";
- if(!sn.substring(15,20).equals(gys)){
- return false;
- }
- return true;
- */
- return false;
- }
- // 后梁码 EVE 编码规则(24位):
- // 供应商5位(F9562) + 物料代码8位(数字) + 日期4位(年A-Z、月A-L、日01-31) + 批次3位(产线字母+2位数字) + 流水4位(数字)
- private static final java.util.regex.Pattern HMB_EVE_PATTERN =
- java.util.regex.Pattern.compile("^F9562\\d{8}[A-Z][A-L](0[1-9]|[12]\\d|3[01])[A-Z]\\d{2}\\d{4}$");
- public static Boolean checkProductSnHmbEve(String sn){
- return sn != null && HMB_EVE_PATTERN.matcher(sn).matches();
- }
- /**
- * 检验前工序工件码的正确性
- * @param sn 工件码
- * @return
- */
- public static Boolean checkProductSnQgx(String sn){
- // 500104004355C230001
- if(sn.length() != 19){
- return false;
- }
- List<String> xwds = new ArrayList<>();
- xwds.add("50010400435");
- String xwd = sn.substring(0,11);
- if(!xwds.contains(xwd)){
- return false;
- }
- return true;
- }
- public static int getYearLastChar(){
- // 获取当前年份
- int currentYear = Year.now().getValue();
- // 转换为字符串,然后获取最后一个字符
- String lastDigit = String.valueOf(currentYear).substring(String.valueOf(currentYear).length() - 1);
- // 将字符串的最后一个数字转换回整数
- int lastDigitAsInt = Integer.parseInt(lastDigit);
- return lastDigitAsInt;
- }
- public static String getMonthChar(){
- // 获取当前日期
- LocalDate today = LocalDate.now();
- // 获取当前月份,LocalDate的getMonthValue方法直接返回月份数字,无需加1
- int month = today.getMonthValue();
- if(month == 10){
- return "A";
- }else if(month == 11){
- return "B";
- }else if(month == 12){
- return "C";
- }else{
- return ""+month;
- }
- }
- public static String getDayChar(){
- // 获取当前日期
- LocalDate today = LocalDate.now();
- // 获取今天是该月的第几天
- int dayOfMonth = today.getDayOfMonth();
- if(dayOfMonth >= 10){
- return dayOfMonth + "";
- }else{
- return "0"+dayOfMonth;
- }
- }
- // 检查底护板批次码是否正确
- 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;
- }
- }
- // 按产线读取允许跳过的工位配置(mes.pass.oprno.{产线小写}),未配置返回空串即不跳站
- public static String getPassOprnos(String lineSn){
- if(StringUtils.isEmpty(lineSn)){
- return "";
- }
- String v = Global.getConfig("mes.pass.oprno." + lineSn.trim().toLowerCase());
- return v == null ? "" : v;
- }
- // 逗号分隔精确匹配,避免 contains 子串误匹配(如 OP12 命中 OP120)
- public static boolean matchOprnos(String oprnos, String oprno){
- if(StringUtils.isEmpty(oprnos) || StringUtils.isEmpty(oprno)){
- return false;
- }
- for(String s : oprnos.split(",")){
- if(s.trim().equals(oprno)){
- return true;
- }
- }
- return false;
- }
- // 判断是否是单部件(配置 mes.dbj.oprno.{产线小写},XT 缺省回退 OP120)
- public static Boolean checkDbjOprno(String oprno,String lineSn){
- if(StringUtils.isEmpty(lineSn)){
- return false;
- }
- String cfg = Global.getConfig("mes.dbj.oprno." + lineSn.trim().toLowerCase());
- if(StringUtils.isEmpty(cfg) && "XT".equals(lineSn)){
- cfg = "OP120";
- }
- return matchOprnos(cfg, oprno);
- }
- // 按产线读取下线工位配置(mes.offline.oprno.{产线小写}),未配置返回空串;XT 未配置时回退 OP280
- public static String getOfflineOprno(String lineSn){
- if(StringUtils.isEmpty(lineSn)){
- return "";
- }
- String v = Global.getConfig("mes.offline.oprno." + lineSn.trim().toLowerCase());
- if(StringUtils.isEmpty(v) && "XT".equals(lineSn.trim())){
- return "OP280";
- }
- return v == null ? "" : v.trim();
- }
- // 判断某工位是否为该产线的下线工位(工位码归一后精确匹配)
- public static boolean isOfflineOprno(String oprno, String lineSn){
- String off = getOfflineOprno(lineSn);
- if(StringUtils.isEmpty(off) || StringUtils.isEmpty(oprno)){
- return false;
- }
- return formatOprno(oprno).equals(off);
- }
- /**
- * 表单形式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 int timeDifference(String dateStr1, String dateStr2){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- try {
- Date date1 = sdf.parse(dateStr1);
- Date date2 = sdf.parse(dateStr2);
- long diff = date2.getTime() - date1.getTime(); // 计算差异(毫秒)
- long diffMinutes = diff / (60 * 1000); // 转换为分钟
- // long diffHours = diff / (60 * 60 * 1000); // 转换为小时
- return Integer.parseInt(String.valueOf(diffMinutes));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public static List<String> getBeforeDays(Integer maxday) {
- // 获取当前日期
- Date currentDate = new Date();
- // 创建Calendar实例
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(currentDate);
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- //当天月日
- List<String> list = new ArrayList<>();
- list.add(sdf.format(new Date()));
- // 遍历并输出前6天的日期
- for (int i = 0; i < (maxday - 1); i++) {
- calendar.add(Calendar.DAY_OF_MONTH, -1);
- Date previousDate = calendar.getTime();
- list.add(sdf.format(previousDate));
- }
- return list;
- }
- }
|