|
@@ -1,332 +0,0 @@
|
|
-package com.persagy.dmp.rwd.edit.service;
|
|
|
|
-
|
|
|
|
-import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
|
-import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
|
|
|
-import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
-import com.google.gson.Gson;
|
|
|
|
-import com.google.gson.JsonSyntaxException;
|
|
|
|
-import com.google.gson.reflect.TypeToken;
|
|
|
|
-import com.persagy.common.criteria.CriteriaUtils;
|
|
|
|
-import com.persagy.common.criteria.JacksonCriteria;
|
|
|
|
-import com.persagy.common.date.DateUtils;
|
|
|
|
-import com.persagy.common.json.JacksonMapper;
|
|
|
|
-import com.persagy.common.web.MapResponse;
|
|
|
|
-import com.persagy.common.web.PagedResponse;
|
|
|
|
-import com.persagy.dmp.common.http.HttpUtils;
|
|
|
|
-import com.persagy.dmp.rwd.edit.config.web.UserUtils;
|
|
|
|
-import com.persagy.dmp.rwd.edit.entity.ChangeRecord;
|
|
|
|
-import com.persagy.dmp.rwd.edit.entity.QChangeRecord;
|
|
|
|
-import com.persagy.dmp.rwd.edit.enumeration.EnumOperationType;
|
|
|
|
-import com.persagy.dmp.rwd.edit.repository.ChangeRecordRepository;
|
|
|
|
-import com.querydsl.core.types.dsl.BooleanExpression;
|
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
-
|
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
|
-import javax.transaction.Transactional;
|
|
|
|
-import java.io.*;
|
|
|
|
-import java.net.URLEncoder;
|
|
|
|
-import java.util.*;
|
|
|
|
-
|
|
|
|
-@Slf4j
|
|
|
|
-@Service
|
|
|
|
-public class ChangeRecordService {
|
|
|
|
-
|
|
|
|
- @Value("${persagy.dmp.server}")
|
|
|
|
- private String dmpServer;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- ChangeRecordRepository repository;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- CriteriaUtils criteriaUtils;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- HttpUtils httpUtils;
|
|
|
|
-
|
|
|
|
- private List<BooleanExpression> parse(ObjectNode object) {
|
|
|
|
- List<BooleanExpression> list = new LinkedList<>();
|
|
|
|
- QChangeRecord qt = QChangeRecord.changeRecord;
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.id, object.get("id")));
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.code, object.get("code")));
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.infoCode, object.get("infoCode")));
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.operationType, object.get("operationType"), EnumOperationType.class));
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.operationUser, object.get("operationUser")));
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.operationTime, object.get("operationTime")));
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.confirmUser, object.get("confirmUser")));
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.confirmTime, object.get("confirmTime")));
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.publishUser, object.get("publishUser")));
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.publishTime, object.get("publishTime")));
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.finishTime, object.get("finishTime")));
|
|
|
|
- list.addAll(CriteriaUtils.parse(qt.state, object.get("state")));
|
|
|
|
- return list;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public PagedResponse<ChangeRecord> query(JacksonCriteria criteria) {
|
|
|
|
- PagedResponse<ChangeRecord> result = criteriaUtils.query(QChangeRecord.changeRecord, this::parse, criteria);
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- public MapResponse create(ChangeRecord param) {
|
|
|
|
- MapResponse response = new MapResponse();
|
|
|
|
- String code = param.getCode();
|
|
|
|
- String infoCode = param.getInfoCode();
|
|
|
|
- if (code == null) {
|
|
|
|
- response.setFail("code is required");
|
|
|
|
- return response;
|
|
|
|
- }
|
|
|
|
- if (EnumOperationType.create.equals(param.getOperationType())) {
|
|
|
|
- Boolean isExists = isExists(code, infoCode);
|
|
|
|
- if (!isExists) {
|
|
|
|
- response.setFail("code is exists");
|
|
|
|
- return response;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- QChangeRecord qt = QChangeRecord.changeRecord;
|
|
|
|
- BooleanExpression ex = qt.code.eq(code).and(qt.state.eq(1));
|
|
|
|
- if (infoCode != null) {
|
|
|
|
- ex.and(qt.infoCode.eq(infoCode));
|
|
|
|
- }
|
|
|
|
- Iterable<ChangeRecord> all = repository.findAll(ex);
|
|
|
|
- List<ChangeRecord> list = new ArrayList<>();
|
|
|
|
- all.forEach(list::add);
|
|
|
|
- if (list.size() > 0) {
|
|
|
|
- repository.deleteAll(list);
|
|
|
|
- }
|
|
|
|
- param.setOperationUser(UserUtils.currentUserId() + "");
|
|
|
|
- param.setOperationTime(new Date());
|
|
|
|
- param.setValid(true);
|
|
|
|
- param.setState(1);
|
|
|
|
- repository.save(param);
|
|
|
|
- response.add("id", param.getId());
|
|
|
|
- return response;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private Boolean isExists(String code, String infoCode) {
|
|
|
|
- JacksonCriteria criteria = JacksonCriteria.newInstance();
|
|
|
|
- HttpUtils.HttpResult result;
|
|
|
|
- if (infoCode != null) {
|
|
|
|
- criteria.add("type", "common");
|
|
|
|
- criteria.add("classCode", code);
|
|
|
|
- criteria.add("code", infoCode);
|
|
|
|
- StringBuilder url = new StringBuilder(dmpServer);
|
|
|
|
- url.append("/rwd/def/funcid");
|
|
|
|
- url.append("?groupCode=0");
|
|
|
|
- result = httpUtils.post(url.toString(), JacksonMapper.toSimpleJson(criteria), 300000);
|
|
|
|
- } else {
|
|
|
|
- criteria.add("type", "common");
|
|
|
|
- criteria.add("code", code);
|
|
|
|
- StringBuilder url = new StringBuilder(dmpServer);
|
|
|
|
- url.append("/rwd/def/funcid");
|
|
|
|
- url.append("?groupCode=0");
|
|
|
|
- result = httpUtils.post(url.toString(), JacksonMapper.toSimpleJson(criteria), 300000);
|
|
|
|
- }
|
|
|
|
- if (result.success()) {
|
|
|
|
- String content = result.getContent();
|
|
|
|
- ObjectNode object = JacksonMapper.toObject(content, ObjectNode.class);
|
|
|
|
- ArrayNode data = JacksonMapper.getArray(object, "data", JsonNodeFactory.instance.arrayNode());
|
|
|
|
- if (data.size() > 0) {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- public MapResponse update(ChangeRecord param) {
|
|
|
|
- MapResponse response = new MapResponse();
|
|
|
|
- if (param.getId() == null) {
|
|
|
|
- response.setFail("id is required");
|
|
|
|
- return response;
|
|
|
|
- }
|
|
|
|
- List<ChangeRecord> data = repository.findAllById(Arrays.asList(param.getId()));
|
|
|
|
- if (data.size() == 0) {
|
|
|
|
- response.setFail("id not exists");
|
|
|
|
- return response;
|
|
|
|
- }
|
|
|
|
- ChangeRecord changeRecord = data.get(0);
|
|
|
|
- if (param.getValue() != null) {
|
|
|
|
- changeRecord.setValue(param.getValue());
|
|
|
|
- }
|
|
|
|
- if (param.getState() != null) {
|
|
|
|
- changeRecord.setState(param.getState());
|
|
|
|
- }
|
|
|
|
- if (param.getState() == 2) {
|
|
|
|
- changeRecord.setConfirmUser(UserUtils.currentUserId() + "");
|
|
|
|
- changeRecord.setConfirmTime(new Date());
|
|
|
|
- }
|
|
|
|
- if (param.getState() == 3) {
|
|
|
|
- changeRecord.setPublishUser(UserUtils.currentUserId() + "");
|
|
|
|
- changeRecord.setPublishTime(new Date());
|
|
|
|
- }
|
|
|
|
- if (param.getState() == 4) {
|
|
|
|
- changeRecord.setFinishTime(new Date());
|
|
|
|
- }
|
|
|
|
- repository.save(changeRecord);
|
|
|
|
- response.add("id", param.getId());
|
|
|
|
- return response;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- public MapResponse delete(List<Integer> param) {
|
|
|
|
- MapResponse response = new MapResponse();
|
|
|
|
- List<ChangeRecord> list = repository.findAllById(param);
|
|
|
|
- repository.deleteAll(list);
|
|
|
|
- return response;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- public void download(HttpServletResponse resp, List<Integer> idList) {
|
|
|
|
- List<ChangeRecord> data = repository.findAllById(idList);
|
|
|
|
- data.forEach(item -> {
|
|
|
|
- item.setState(3);
|
|
|
|
- item.setPublishUser(UserUtils.currentUserId() + "");
|
|
|
|
- item.setPublishTime(new Date());
|
|
|
|
- });
|
|
|
|
- repository.saveAll(data);
|
|
|
|
-
|
|
|
|
- String fileName = DateUtils.format(new Date()) + ".sql";
|
|
|
|
- List<String> sqlList = getSqlList(idList);
|
|
|
|
-
|
|
|
|
- OutputStream out = null;
|
|
|
|
- try {
|
|
|
|
- InputStream stream = getStream(sqlList);
|
|
|
|
- resp.reset();// 清空输出流
|
|
|
|
- String resultFileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
|
- resp.setCharacterEncoding("UTF-8");
|
|
|
|
- resp.setHeader("Content-disposition", "attachment; filename=" + resultFileName);// 设定输出文件头
|
|
|
|
- resp.setContentType("application/txt");// 定义输出类型
|
|
|
|
- out = resp.getOutputStream();
|
|
|
|
- byte[] buff = new byte[4096];
|
|
|
|
- int size = 0;
|
|
|
|
- while ((size = stream.read(buff)) != -1) {
|
|
|
|
- out.write(buff, 0, size);
|
|
|
|
- }
|
|
|
|
- out.flush();
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- } finally {
|
|
|
|
- try {
|
|
|
|
- if (out != null) {
|
|
|
|
- out.close();
|
|
|
|
- }
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public InputStream getStream(List<String> list) throws IOException {
|
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
- DataOutputStream out = new DataOutputStream(baos);
|
|
|
|
- for (String sql : list) {
|
|
|
|
- out.writeBytes(sql + "\r\n");
|
|
|
|
- }
|
|
|
|
- byte[] bytes = baos.toByteArray();
|
|
|
|
- ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(bytes);
|
|
|
|
- baos.close();
|
|
|
|
- out.close();
|
|
|
|
- return tInputStringStream;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private List<String> getSqlList(List<Integer> idList) {
|
|
|
|
- List<String> sqlList = new ArrayList<>();
|
|
|
|
- List<ChangeRecord> data = repository.findAllById(idList);
|
|
|
|
- for (ChangeRecord changeRecord : data) {
|
|
|
|
- StringBuilder sql = new StringBuilder();
|
|
|
|
- Map<String, Object> map = json2map(JacksonMapper.toSimpleJson(changeRecord.getValue()));
|
|
|
|
- switch (changeRecord.getOperationType()) {
|
|
|
|
- case create:
|
|
|
|
- sql.append("insert into");
|
|
|
|
- if (changeRecord.getInfoCode() == null) {
|
|
|
|
- sql.append(" RWD_DEF_CLASS");
|
|
|
|
- } else {
|
|
|
|
- sql.append(" RWD_DEF_FUNCID");
|
|
|
|
- }
|
|
|
|
- sql.append(" (");//表名
|
|
|
|
- map.keySet().forEach(item -> {
|
|
|
|
- sql.append(humpToUnderline(item) + ",");
|
|
|
|
- });
|
|
|
|
- sql.deleteCharAt(sql.length() - 1);
|
|
|
|
- sql.append(") values (");
|
|
|
|
- map.values().forEach(item -> {
|
|
|
|
- sql.append("'" + item + "',");
|
|
|
|
- });
|
|
|
|
- sql.deleteCharAt(sql.length() - 1);
|
|
|
|
- sql.append(")");
|
|
|
|
- break;
|
|
|
|
- case update:
|
|
|
|
- sql.append("update");
|
|
|
|
- if (changeRecord.getInfoCode() == null) {
|
|
|
|
- sql.append(" RWD_DEF_CLASS");
|
|
|
|
- } else {
|
|
|
|
- sql.append(" RWD_DEF_FUNCID");
|
|
|
|
- }
|
|
|
|
- sql.append(" set");
|
|
|
|
- for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
|
- sql.append(" " + humpToUnderline(entry.getKey()) + " = '" + entry.getValue() + "',");
|
|
|
|
- }
|
|
|
|
- sql.deleteCharAt(sql.length() - 1);
|
|
|
|
- break;
|
|
|
|
- case delete:
|
|
|
|
- sql.append("delete from");
|
|
|
|
- if (changeRecord.getInfoCode() == null) {
|
|
|
|
- sql.append(" RWD_DEF_CLASS");
|
|
|
|
- } else {
|
|
|
|
- sql.append(" RWD_DEF_FUNCID");
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- if (!EnumOperationType.create.equals(changeRecord.getOperationType())) {
|
|
|
|
- sql.append(" where 1=1");
|
|
|
|
- if (changeRecord.getInfoCode() == null) {
|
|
|
|
- sql.append(" and code = '" + changeRecord.getCode() + "'");
|
|
|
|
- } else {
|
|
|
|
- sql.append(" and class_code = '" + changeRecord.getCode() + "'");
|
|
|
|
- sql.append(" and code = '" + changeRecord.getInfoCode() + "'");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- sql.append(";");
|
|
|
|
- sqlList.add(sql.toString());
|
|
|
|
- }
|
|
|
|
- return sqlList;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
- String uuid = UUID.randomUUID().toString().replace("-", "");
|
|
|
|
- System.err.println(uuid);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static Map<String, Object> json2map(String str_json) {
|
|
|
|
- Map<String, Object> res = null;
|
|
|
|
- try {
|
|
|
|
- Gson gson = new Gson();
|
|
|
|
- res = gson.fromJson(str_json, new TypeToken<Map<String, Object>>() {
|
|
|
|
- }.getType());
|
|
|
|
- } catch (JsonSyntaxException e) {
|
|
|
|
- }
|
|
|
|
- return res;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /***
|
|
|
|
- * 驼峰命名转为下划线命名
|
|
|
|
- */
|
|
|
|
- private String humpToUnderline(String para) {
|
|
|
|
- StringBuilder sb = new StringBuilder(para);
|
|
|
|
- int temp = 0;//定位
|
|
|
|
- if (!para.contains("_")) {
|
|
|
|
- for (int i = 0; i < para.length(); i++) {
|
|
|
|
- if (Character.isUpperCase(para.charAt(i))) {
|
|
|
|
- sb.insert(i + temp, "_");
|
|
|
|
- temp += 1;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return sb.toString().toUpperCase();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-}
|
|
|
|
-
|
|
|