siteInfoManageRepo.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package mongo
  2. import (
  3. "context"
  4. "errors"
  5. "fwh-toolbox-server/database"
  6. "fwh-toolbox-server/domain"
  7. "fwh-toolbox-server/dto"
  8. "time"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/bson/primitive"
  11. "go.mongodb.org/mongo-driver/mongo"
  12. "go.mongodb.org/mongo-driver/mongo/options"
  13. )
  14. const (
  15. CollectionName_SiteInfoManageRecord = "SiteInfoManageRecord"
  16. CollectionName_SiteInfoManageData = "SiteInfoManageData"
  17. )
  18. // 更新站点信息获取结果
  19. func CreateSiteInfoUpdateInfo() (*primitive.ObjectID, error) {
  20. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  21. defer cancel()
  22. collection := database.GetCollection(CollectionName_SiteInfoManageRecord)
  23. result, err := collection.InsertOne(ctx, domain.SiteInfoUpdateRecord{
  24. Progress: domain.ProgressPending,
  25. CreateTime: time.Now(),
  26. UpdateTime: time.Now(),
  27. Total: 0,
  28. })
  29. if err != nil {
  30. return nil, err
  31. }
  32. id := result.InsertedID.(primitive.ObjectID)
  33. return &id, nil
  34. }
  35. // 更新站点信息获取结果
  36. func UpdateSiteInfoUpdateResult(id primitive.ObjectID, progress domain.Progress, total uint64) error {
  37. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  38. defer cancel()
  39. collection := database.GetCollection(CollectionName_SiteInfoManageRecord)
  40. filter := bson.M{"_id": id}
  41. var update map[string]interface{}
  42. if progress == domain.ProgressComplete {
  43. update = bson.M{
  44. "$set": bson.M{
  45. "progress": progress,
  46. "updateTime": time.Now(),
  47. "total": total,
  48. },
  49. }
  50. } else {
  51. update = bson.M{
  52. "$set": bson.M{
  53. "progress": progress,
  54. },
  55. }
  56. }
  57. _, err := collection.UpdateOne(ctx, filter, update)
  58. if err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. // 获取最新站点信息记录数据
  64. func GetSiteInfoUpdateNewRecord() (*domain.SiteInfoUpdateRecord, error) {
  65. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  66. defer cancel()
  67. collection := database.GetCollection(CollectionName_SiteInfoManageRecord)
  68. opts := options.FindOne().SetSort(bson.D{{Key: "_id", Value: -1}})
  69. var result domain.SiteInfoUpdateRecord
  70. err := collection.FindOne(ctx, bson.M{}, opts).Decode(&result)
  71. if err != nil {
  72. if errors.Is(err, mongo.ErrNoDocuments) {
  73. return nil, nil // 没有数据返回空指针
  74. }
  75. return nil, err
  76. }
  77. return &result, nil
  78. }
  79. // 从数据库获取站点信息列表
  80. func GetSiteInfoManageList(page, size uint, filter *domain.SiteInfoFilter, search string) (*dto.ResPage[domain.SiteInfoDomain], error) {
  81. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  82. defer cancel()
  83. collection := database.GetCollection(CollectionName_SiteInfoManageData)
  84. mainFilter := bson.M{}
  85. // 条件
  86. if filter != nil {
  87. subFilterConfig := bson.M{}
  88. mainFilterSet := []bson.M{}
  89. for _, subFilter := range filter.SubFilter {
  90. subFilterSet := []bson.M{}
  91. filterConfigItem := bson.M{}
  92. for _, filterItem := range subFilter.Config {
  93. switch filterItem.Type {
  94. case domain.FilterEqualInt:
  95. // 等于指定值(整数)
  96. if filterItem.Para1.Int != nil {
  97. filterConfigItem[filterItem.Key] = bson.M{"$eq": *filterItem.Para1.Int}
  98. }
  99. case domain.FilterEqualFloat:
  100. // 等于指定值(浮点数)
  101. if filterItem.Para1.Float != nil {
  102. filterConfigItem[filterItem.Key] = bson.M{"$eq": *filterItem.Para1.Float}
  103. }
  104. case domain.FilterEqualString:
  105. // 等于指定字符串
  106. filterConfigItem[filterItem.Key] = bson.M{"$eq": *filterItem.Para1.String}
  107. case domain.FilterIncludeString:
  108. // 包含指定字符串
  109. filterConfigItem[filterItem.Key] = bson.M{"$regex": *filterItem.Para1.String, "$options": "i"} // 选项 $options:大小写不敏感 (i)
  110. case domain.FilterRangeInt:
  111. // 数值范围检索(整数)
  112. if filterItem.Para2 != nil {
  113. if filterItem.Para1.Int != nil && filterItem.Para2 != nil && filterItem.Para2.Int != nil {
  114. filterConfigItem[filterItem.Key] = bson.M{"$gte": *filterItem.Para1.Int, "$lte": *filterItem.Para2.Int}
  115. }
  116. }
  117. case domain.FilterRangeFloat:
  118. // 数值范围检索(浮点数)
  119. if filterItem.Para2 != nil {
  120. if filterItem.Para1.Float != nil && filterItem.Para2 != nil && filterItem.Para2.Float != nil {
  121. filterConfigItem[filterItem.Key] = bson.M{"$gte": *filterItem.Para1.Float, "$lte": *filterItem.Para2.Float}
  122. }
  123. }
  124. case domain.FilterRangeTime:
  125. // 数值范围检索(时间)
  126. if filterItem.Para1.String != nil && filterItem.Para2 != nil && filterItem.Para2.String != nil {
  127. // start, e1 := time.Parse(time.DateTime, *filterItem.Para1.String)
  128. // end, e2 := time.Parse(time.DateTime, *filterItem.Para2.String)
  129. // if e1 == nil && e2 == nil {
  130. // filterConfigItem[filterItem.Key] = bson.M{"$gte": start, "$lte": end}
  131. // }
  132. start := *filterItem.Para1.String
  133. end := *filterItem.Para2.String
  134. filterConfigItem[filterItem.Key] = bson.M{"$gte": start, "$lte": end}
  135. }
  136. case domain.FilterExists:
  137. // 判断字段是否存在或不为空
  138. // filterConfigItem[filterItem.Key] = bson.M{"$exists": true}
  139. value := *filterItem.Para1.String
  140. switch value {
  141. case "yes":
  142. filterConfigItem[filterItem.Key] = bson.M{"$exists": true, "$ne": nil}
  143. case "no":
  144. filterConfigItem["$or"] = bson.M{
  145. filterItem.Key: bson.M{"$exists": false},
  146. filterItem.Key: bson.M{"$eq": nil},
  147. }
  148. }
  149. }
  150. subFilterSet = append(subFilterSet, filterConfigItem)
  151. }
  152. if len(subFilterSet) > 0 {
  153. switch subFilter.Logic {
  154. case "and":
  155. subFilterConfig["$and"] = subFilterSet
  156. case "or":
  157. subFilterConfig["$or"] = subFilterSet
  158. }
  159. }
  160. mainFilterSet = append(mainFilterSet, subFilterConfig)
  161. }
  162. if len(mainFilterSet) > 0 {
  163. switch filter.Logic {
  164. case "and":
  165. mainFilter["$and"] = mainFilterSet
  166. case "or":
  167. mainFilter["$or"] = mainFilterSet
  168. }
  169. }
  170. }
  171. dbFilter := bson.M{}
  172. if len(search) > 0 {
  173. dbFilter["$and"] = []bson.M{
  174. mainFilter,
  175. {
  176. "$or": []bson.M{
  177. {"_id": bson.M{"$regex": search, "$options": "i"}}, // 选项 $options:大小写不敏感 (i)
  178. {"name": bson.M{"$regex": search, "$options": "i"}},
  179. {"distributorName": bson.M{"$regex": search, "$options": "i"}},
  180. {"installerName": bson.M{"$regex": search, "$options": "i"}},
  181. {"maintName": bson.M{"$regex": search, "$options": "i"}},
  182. {"accountName": bson.M{"$regex": search, "$options": "i"}},
  183. {"iccId": bson.M{"$regex": search, "$options": "i"}},
  184. {"imei": bson.M{"$regex": search, "$options": "i"}},
  185. {"fhpSn": bson.M{"$regex": search, "$options": "i"}},
  186. {"version": bson.M{"$regex": search, "$options": "i"}},
  187. {"protocolVer": bson.M{"$regex": search, "$options": "i"}},
  188. {"realSysHdVersion": bson.M{"$regex": search, "$options": "i"}},
  189. {"financierName": bson.M{"$regex": search, "$options": "i"}},
  190. {"remark": bson.M{"$regex": search, "$options": "i"}},
  191. {"country": bson.M{"$regex": search, "$options": "i"}},
  192. {"province": bson.M{"$regex": search, "$options": "i"}},
  193. {"city": bson.M{"$regex": search, "$options": "i"}},
  194. {"site": bson.M{"$regex": search, "$options": "i"}},
  195. {"jobCode": bson.M{"$regex": search, "$options": "i"}},
  196. },
  197. },
  198. }
  199. } else {
  200. dbFilter = mainFilter
  201. }
  202. var findOptions *options.FindOptions
  203. if size > 0 {
  204. findOptions = options.Find().
  205. SetSkip(int64((page - 1) * size)).
  206. SetLimit(int64(size))
  207. }
  208. cursor, err := collection.Find(ctx, dbFilter, findOptions)
  209. if err != nil {
  210. return nil, err
  211. }
  212. defer cursor.Close(ctx)
  213. var infoList []domain.SiteInfoDomain
  214. for cursor.Next(ctx) {
  215. var info domain.SiteInfoDomain
  216. if err := cursor.Decode(&info); err != nil {
  217. return nil, err
  218. }
  219. infoList = append(infoList, info)
  220. }
  221. // 查询总数
  222. total, err := collection.CountDocuments(ctx, dbFilter)
  223. if err != nil {
  224. return nil, err
  225. }
  226. res := dto.ResPage[domain.SiteInfoDomain]{
  227. Data: infoList,
  228. PageSize: size,
  229. PageNum: page,
  230. Total: uint(total),
  231. }
  232. return &res, nil
  233. }
  234. // 插入站点信息数据
  235. func InsertSiteInfoData(data domain.SiteInfoDomain) error {
  236. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  237. defer cancel()
  238. collection := database.GetCollection(CollectionName_SiteInfoManageData)
  239. filter := bson.M{"_id": data.Sn} // 按ID匹配
  240. opts := options.Replace().SetUpsert(true)
  241. _, err := collection.ReplaceOne(ctx, filter, data, opts)
  242. if err != nil {
  243. return err
  244. }
  245. return nil
  246. }