|
|
@@ -0,0 +1,324 @@
|
|
|
+package com.uas.erp.schedular.financeservice.task;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.uas.erp.schedular.agent.v1.AbstractAgent;
|
|
|
+import com.uas.erp.schedular.b2b.domain.Attach;
|
|
|
+import com.uas.erp.schedular.b2b.domain.KeyEntity;
|
|
|
+import com.uas.erp.schedular.b2b.domain.Prod;
|
|
|
+import com.uas.erp.schedular.core.Constant;
|
|
|
+import com.uas.erp.schedular.database.RestJdbcTemplate;
|
|
|
+import com.uas.erp.schedular.entity.Master;
|
|
|
+import com.uas.erp.schedular.service.SettingService;
|
|
|
+import com.uas.erp.schedular.util.CollectionUtil;
|
|
|
+import com.uas.erp.schedular.util.ContextHolder;
|
|
|
+import com.uas.erp.schedular.util.DateUtil;
|
|
|
+import com.uas.erp.schedular.util.HmacUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URI;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by Pro1 on 2017/7/26.
|
|
|
+ */
|
|
|
+public class AbstractTask extends AbstractAgent {
|
|
|
+
|
|
|
+ // 数据传输单次大小限制
|
|
|
+ protected static final int DATA_SIZE_LIMIT = 500;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ protected RestJdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ protected SettingService settingService;
|
|
|
+
|
|
|
+ protected Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按账套环境,使用不同接口地址
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getApiDomain() {
|
|
|
+ String key = "test".equals(ContextHolder.getMaster().getMa_env()) ? "api.finance.service.test.url" : "api.finance.service.url";
|
|
|
+ return settingService.getValue(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * UAS系统外网地址
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getUASUrl() {
|
|
|
+ return settingService.getValue("api.uas.outer.url");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * URI加身份签名
|
|
|
+ * @param url
|
|
|
+ * @param vars
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getURI(String url, Object... vars) {
|
|
|
+ Master master = ContextHolder.getMaster();
|
|
|
+ if (null == master.getMa_uu() || StringUtils.isEmpty(master.getMa_accesssecret())) {
|
|
|
+ throw new RuntimeException("未设置企业UU和私钥");
|
|
|
+ }
|
|
|
+ StringBuffer accessUrl = new StringBuffer(url);
|
|
|
+ accessUrl.append(url.contains("?") ? "&" : "?");
|
|
|
+ // 身份ID
|
|
|
+ accessUrl.append("access_id=").append(master.getMa_uu());
|
|
|
+ // 时间戳
|
|
|
+ accessUrl.append("&_timestamp=").append(System.currentTimeMillis());
|
|
|
+ URI uri = restTemplate.getUriTemplateHandler().expand(getApiDomain() + accessUrl.toString(), vars);
|
|
|
+ url = uri.toString();
|
|
|
+ // 签名
|
|
|
+ return url + "&_signature=" + HmacUtils.encode(url, master.getMa_accesssecret());
|
|
|
+ }
|
|
|
+
|
|
|
+ protected <T> T getForObject(String url, Class<T> objectClass, Object... vars) {
|
|
|
+ return restTemplate.getForObject(getURI(url, vars), objectClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected <T> List<T> getForList(String url, Class<T> objectClass, Object... vars) {
|
|
|
+ String resultStr = restTemplate.getForObject(getURI(url, vars), String.class);
|
|
|
+ return JSON.parseArray(resultStr, objectClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * b2b当前版本的接口只支持以form-data方式提交,requestBody方式暂不支持
|
|
|
+ * @param url
|
|
|
+ * @param vars
|
|
|
+ */
|
|
|
+ protected void post(String url, MultiValueMap<String, String> vars) {
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
+ HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(vars, headers);
|
|
|
+ restTemplate.postForLocation(getURI(url), request);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected ResponseEntity postForEntity(String url, MultiValueMap<String, String> vars) {
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
+ HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(vars, headers);
|
|
|
+ return restTemplate.postForEntity(getURI(url), request, String.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected <T> List<T> postForList(String url, Class<T> objectClass, MultiValueMap<String, String> vars) {
|
|
|
+ ResponseEntity<String> resultEntity = postForEntity(url, vars);
|
|
|
+ return JSON.parseArray(resultEntity.getBody(), objectClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 封装成平台接口数据格式
|
|
|
+ * @param data
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ protected static MultiValueMap<String, String> dataWrap(Object data) {
|
|
|
+ MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
|
|
+ String dataStr = null;
|
|
|
+ Class<?> cls = data.getClass();
|
|
|
+ if (cls.isAssignableFrom(String.class) || cls.isAssignableFrom(Number.class) ||
|
|
|
+ cls.isAssignableFrom(Character.class)) {
|
|
|
+ dataStr = data.toString();
|
|
|
+ } else {
|
|
|
+ dataStr = JSON.toJSONString(data);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ dataStr = URLEncoder.encode(dataStr, "UTF-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ map.set("data", dataStr);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据上传工具,封装了处理前、后置、错误处理方法
|
|
|
+ * @param <T>
|
|
|
+ */
|
|
|
+ protected abstract class AbstractHandler<T> {
|
|
|
+
|
|
|
+ private List<T> data;
|
|
|
+
|
|
|
+ public AbstractHandler(List<T> data) {
|
|
|
+ this.data = data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * onHandler方法前执行
|
|
|
+ */
|
|
|
+ protected void onBefore(){
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ protected abstract ResponseEntity onHandler(List<T> data);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * onHandler方法执行成功后执行
|
|
|
+ */
|
|
|
+ protected void onSuccess(){
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * onHandler方法执行失败后执行
|
|
|
+ */
|
|
|
+ protected void onError(String message){
|
|
|
+ throw new RuntimeException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 所有方法执行完成后执行
|
|
|
+ */
|
|
|
+ protected void onAfter(){
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void run() {
|
|
|
+ String errMsg = null;
|
|
|
+ onBefore();
|
|
|
+ try {
|
|
|
+ ResponseEntity response = onHandler(data);
|
|
|
+ if (!response.getStatusCode().is2xxSuccessful()) {
|
|
|
+ errMsg = String.valueOf(response.getBody());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ errMsg = e.getMessage();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if (null == errMsg) {
|
|
|
+ onSuccess();
|
|
|
+ } else {
|
|
|
+ onError(errMsg);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ onAfter();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * POST方式处理工具
|
|
|
+ * @param <T>
|
|
|
+ */
|
|
|
+ protected class AbstractPostHandler<T> extends AbstractHandler<T> {
|
|
|
+ private String postUri;
|
|
|
+
|
|
|
+ public AbstractPostHandler(String postUri, List<T> data) {
|
|
|
+ super(data);
|
|
|
+ this.postUri = postUri;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected ResponseEntity onHandler(List<T> data) {
|
|
|
+ return postForEntity(postUri, dataWrap(data));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 基于状态控制的处理工具
|
|
|
+ * @param <T>
|
|
|
+ */
|
|
|
+ protected abstract class AbstractStatusBasedHandler<T extends KeyEntity> extends AbstractPostHandler<T> {
|
|
|
+
|
|
|
+ private String idStr;
|
|
|
+ private String tableName;
|
|
|
+ private String idColumn;
|
|
|
+ private String statusColumn;
|
|
|
+
|
|
|
+ public AbstractStatusBasedHandler(String tableName, String idColumn, String statusColumn, String postUri, List<T> data) {
|
|
|
+ super(postUri, data);
|
|
|
+ this.idStr = CollectionUtil.getKeyString(data);
|
|
|
+ this.tableName = tableName;
|
|
|
+ this.idColumn = idColumn;
|
|
|
+ this.statusColumn = statusColumn;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onBefore() {
|
|
|
+ jdbcTemplate.execute(String.format("update %s set %s='上传中' where %s in (%s)", tableName, statusColumn, idColumn, idStr));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onSuccess() {
|
|
|
+ jdbcTemplate.execute(String.format("update %s set %s='已上传' where %s in (%s)", tableName, statusColumn, idColumn, idStr));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onError(String message) {
|
|
|
+ jdbcTemplate.execute(String.format("update %s set %s='待上传' where %s in (%s) and %s='上传中'", tableName, statusColumn, idColumn, idStr, statusColumn));
|
|
|
+ super.onError(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 基于状态控制的处理工具
|
|
|
+ * @param <T>
|
|
|
+ */
|
|
|
+ protected class StatusBasedHandler<T extends KeyEntity> extends AbstractStatusBasedHandler<T> {
|
|
|
+
|
|
|
+ public StatusBasedHandler(String tableName, String idColumn, String statusColumn, String postUri, List<T> data) {
|
|
|
+ super(tableName, idColumn, statusColumn, postUri, data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理物料上传的工具
|
|
|
+ */
|
|
|
+ protected final class ProductHandler extends StatusBasedHandler<Prod>{
|
|
|
+
|
|
|
+ public ProductHandler(List<Prod> data) {
|
|
|
+ super("product", "pr_id", "PR_SENDSTATUS", "/erp/product", data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查找附件
|
|
|
+ * @param attachIds 附件ID
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ protected List<Attach> getAttachs(String[] attachIds) {
|
|
|
+ String erpUrl = getUASUrl();
|
|
|
+ List<Attach> attaches = jdbcTemplate.queryForBeanList("select fp_id, fp_size, fp_name from filepath where fp_id in ("
|
|
|
+ + StringUtils.arrayToDelimitedString(attachIds, ",") + ")", Attach.class);
|
|
|
+ if (!CollectionUtils.isEmpty(attaches)) {
|
|
|
+ for (Attach attach : attaches) {
|
|
|
+ attach.setFp_url(erpUrl + Attach.DOWN_FILE_ACTION + attach.getFp_id());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return attaches;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建消息
|
|
|
+ *
|
|
|
+ * @param caller
|
|
|
+ * @param sourceIds
|
|
|
+ * @param type
|
|
|
+ */
|
|
|
+ protected void createMessage(String caller, String sourceIds, String type) {
|
|
|
+ Integer mid = jdbcTemplate.getInt("select max(mm_id) from MESSAGEMODEL left join MESSAGEROLE on mm_id=mr_mmid where MR_ISUSED=-1 AND MM_ISUSED=-1 and mm_caller=? and MM_OPERATE='b2b' AND MM_ACTION=?",
|
|
|
+ caller, type);
|
|
|
+ if (null != mid) {
|
|
|
+ jdbcTemplate.execute("{call SP_CREATEINFO(?,'ADMIN',?,?)}",
|
|
|
+ mid, sourceIds, DateUtil.parseDateToOracleString(Constant.YMD_HMS, new Date()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|