siteInfoManageRepo.go 9.2 KB

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