zouyu
9 小时以前 1b2f1eb44d9f0de6b9238cfe314988a95c87344a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.ruoyi.common.utils.api.mes;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.ruoyi.common.config.mes.MesConfig;
import com.ruoyi.common.config.mes.MesProperties;
import com.ruoyi.common.utils.api.mes.model.WorkingHoursRecordSumVO;
import com.ruoyi.common.utils.api.mes.model.WorkingHoursRecordTotalVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
@Slf4j
@Component
public class MesApiUtils {
 
    @Autowired
    private MesConfig mesConfig;
 
    /**
     * 根据工厂域回去mes配置
     * @param contract
     * @return
     */
    public MesProperties getMesPropertiesByContract(String contract){
        return mesConfig.getProps().stream().filter(f-> org.apache.commons.lang3.StringUtils.equals(f.getContract(),contract)).findFirst().orElse(new MesProperties());
    }
 
    /**
     * 获取token请求链接
     * @return
     */
    private String getAuthTokenUrl(String ip) {
        return ip + "/auth/oauth/token?randomStr=blockPuzzle&grant_type=password";
    }
 
    /**
     * 新增实时库存请求链接
     * @return
     */
    private String getBatchAddStockUrl(String ip){
        return ip + "/mes/stock/batchAddStock";
    }
 
    /**
     * 检测工时统计查询请求链接
     * @return
     */
    private String getCheckWorkingHoursRecordSumUrl(String ip){
        return ip + "/mes/checkWorkingHoursRecord/getCheckWorkingHoursRecordSum";
    }
 
 
 
    /**
     * 获取mes系统token
     * @return 接口响应结果
     */
    public String getToken(MesProperties mesProperties){
        try{
            String bodyStr = "username="+mesProperties.getUser()+"&password="+mesProperties.getPassword();
            String response = HttpRequest.post(getAuthTokenUrl(mesProperties.getIp()))
                    .header("Authorization", "Basic cGlnOnBpZw==")
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .body(bodyStr,"application/x-www-form-urlencoded").execute().body();
            JSONObject responseObj = JSONObject.parseObject(response);
            if(Objects.nonNull(responseObj.getString("access_token"))){
                return "Bearer " + responseObj.getString("access_token");
            }else{
                throw new RuntimeException(responseObj.getString("msg"));
            }
        }catch (Exception e){
            throw new RuntimeException("获取MES系统token接口异常:"+e.getMessage());
        }
    }
 
    /**
     * mes新增实时库存接口
     * @param contract 工厂域
     * @param requestJsonStr 请求参数json字符串
     * @return 接口响应结果
     */
    public boolean batchAddStock(String contract,String requestJsonStr){
        try{
            MesProperties mesProperties = getMesPropertiesByContract(contract);
            String response = HttpRequest.post(getBatchAddStockUrl(mesProperties.getIp()))
                    .header("Authorization", getToken(mesProperties))
                    .header("Content-Type", "application/json")
                    .body(requestJsonStr)
                    .execute()
                    .body();
            JSONObject entries = JSONObject.parseObject(response);
            if(entries.getInteger("code")==0){
                return true;
            }else{
                throw new RuntimeException("【"+contract+"】同步到MES失败:"+entries.getString("msg"));
            }
        }catch (Exception e){
            throw new RuntimeException("【"+contract+"】同步MES实时库存接口异常:"+e.getMessage());
        }
    }
 
    /**
     * 检测工时统计查询
     * @return
     */
    public List<WorkingHoursRecordTotalVO> getCheckWorkingHoursRecordSum(String contract,Map<String,Object> params){
        try {
            MesProperties mesProperties = getMesPropertiesByContract(contract);
            String response = HttpRequest.get(getCheckWorkingHoursRecordSumUrl(mesProperties.getIp()))
                    .header("Authorization", getToken(mesProperties))
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .form(params)
                    .execute()
                    .body();
            JSONObject entries = JSONObject.parseObject(response);
            if(entries.getInteger("code")==0){
                List<WorkingHoursRecordTotalVO> totalVOS = new ArrayList<>();
                List<WorkingHoursRecordSumVO> parseObject = JSONObject.parseObject(entries.getString("data"), new TypeReference<List<WorkingHoursRecordSumVO>>() {}.getType());
                parseObject.forEach(p->{
                    WorkingHoursRecordSumVO.WorkHoursVO monthTotal = p.getWorkingHours().stream().filter(f -> StringUtils.equals(f.getDutyDate(), "月度汇总")).findFirst().orElse(null);
                    WorkingHoursRecordTotalVO totalVO = new WorkingHoursRecordTotalVO();
                    if(ObjectUtils.isNotEmpty(monthTotal)){
                        BeanUtil.copyProperties(monthTotal,totalVO);
                    }
                    totalVO.setYear(p.getYear());
                    totalVO.setMonth(p.getMonth());
                    totalVO.setChecker(p.getChecker());
                    totalVOS.add(totalVO);
                });
                return totalVOS;
            }else{
                throw new RuntimeException("【"+contract+"】拉取MES检测工时失败:"+entries.getString("msg"));
            }
        }catch (Exception e){
            throw new RuntimeException("【"+contract+"】拉取MES检测工时接口异常:"+e.getMessage());
        }
    }
 
 
}