/rt-node-master/src/main/java/xyz/redtorch/node/master/dao/impl/OperatorDaoImpl.java

https://github.com/sun0x00/RedTorch · Java · 118 lines · 97 code · 21 blank · 0 comment · 9 complexity · 44c41beb7bdb1c47401bc9dc9364193c MD5 · raw file

  1. package xyz.redtorch.node.master.dao.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.bson.Document;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.InitializingBean;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import com.alibaba.fastjson.JSON;
  12. import xyz.redtorch.common.mongo.MongoDBClient;
  13. import xyz.redtorch.node.db.MongoDBClientService;
  14. import xyz.redtorch.node.master.dao.OperatorDao;
  15. import xyz.redtorch.node.master.po.OperatorPo;
  16. @Service
  17. public class OperatorDaoImpl implements OperatorDao, InitializingBean {
  18. private static final Logger logger = LoggerFactory.getLogger(NodeDaoImpl.class);
  19. private static final String GATEWAY_COLLECTION_NAME = "operator_collection";
  20. @Autowired
  21. private MongoDBClientService mongoDBClientService;
  22. private MongoDBClient managementDBClient;
  23. private String managementDBName;
  24. @Override
  25. public void afterPropertiesSet() throws Exception {
  26. this.managementDBClient = mongoDBClientService.getManagementDBClient();
  27. this.managementDBName = mongoDBClientService.getManagementDBName();
  28. }
  29. @Override
  30. public OperatorPo queryOperatorByOperatorId(String operatorId) {
  31. if (StringUtils.isBlank(operatorId)) {
  32. logger.error("根据操作员ID查询操作员错误,参数operatorId缺失");
  33. throw new IllegalArgumentException("根据操作员ID查询操作员错误,参数operatorId缺失");
  34. }
  35. Document filter = new Document();
  36. filter.append("operatorId", operatorId);
  37. List<Document> documentList = managementDBClient.find(managementDBName, GATEWAY_COLLECTION_NAME, filter);
  38. if (documentList == null || documentList.isEmpty()) {
  39. return null;
  40. }
  41. Document document = documentList.get(0);
  42. try {
  43. OperatorPo operator = JSON.parseObject(JSON.toJSONString(document), OperatorPo.class);
  44. return operator;
  45. } catch (Exception e) {
  46. logger.error("根据操作员ID查询操作员,数据转换发生错误Document-{}", document.toJson(), e);
  47. return null;
  48. }
  49. }
  50. @Override
  51. public List<OperatorPo> queryOperatorList() {
  52. List<Document> documentList = managementDBClient.find(managementDBName, GATEWAY_COLLECTION_NAME);
  53. List<OperatorPo> operatorList = new ArrayList<>();
  54. for (Document document : documentList) {
  55. try {
  56. OperatorPo operator = JSON.parseObject(JSON.toJSONString(document), OperatorPo.class);
  57. operatorList.add(operator);
  58. } catch (Exception e) {
  59. logger.error("查询操作员列表,数据转换发生错误,Document-{}", document.toJson(), e);
  60. }
  61. }
  62. return operatorList;
  63. }
  64. @Override
  65. public void upsertOperatorByOperatorId(OperatorPo operator) {
  66. if (operator == null) {
  67. logger.error("根据操作员ID更新或保存操作员错误,参数operator缺失");
  68. throw new IllegalArgumentException("根据操作员ID更新或保存操作员错误,参数operator缺失");
  69. }
  70. if (StringUtils.isBlank(operator.getOperatorId())) {
  71. logger.error("根据操作员ID更新或保存操作员错误,参数operatorId缺失");
  72. throw new IllegalArgumentException("根据操作员ID更新或保存操作员错误,参数operatorId缺失");
  73. }
  74. try {
  75. Document document = Document.parse(JSON.toJSONString(operator));
  76. Document filter = new Document();
  77. filter.append("operatorId", operator.getOperatorId());
  78. managementDBClient.upsert(managementDBName, GATEWAY_COLLECTION_NAME, document, filter);
  79. } catch (IllegalArgumentException e) {
  80. logger.error("根据操作员ID更新或保存操作员错误", e);
  81. }
  82. }
  83. @Override
  84. public void deleteOperatorByOperatorId(String operatorId) {
  85. if (StringUtils.isBlank(operatorId)) {
  86. logger.error("根据操作员ID删除操作员错误,参数operatorId缺失");
  87. throw new IllegalArgumentException("根据操作员ID删除操作员错误,参数operatorId缺失");
  88. }
  89. try {
  90. Document filter = new Document();
  91. filter.append("operatorId", operatorId);
  92. managementDBClient.delete(managementDBName, GATEWAY_COLLECTION_NAME, filter);
  93. } catch (IllegalArgumentException e) {
  94. logger.error("根据操作员ID删除操作员发生错误,操作员ID:{}", operatorId, e);
  95. }
  96. }
  97. }