weightFusion.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. qu "qfw/util"
  6. "time"
  7. "go.mongodb.org/mongo-driver/bson/primitive"
  8. )
  9. //处理融合数据-返回,融合新数据数据-新增
  10. func (weight *weightDataMap) dealWithAddFusionStruct ()(map[string]interface{},map[string]interface{}){
  11. //指定模板数据dict-单条数据
  12. dict :=weight.data[weight.templateid].data
  13. //采用新增id
  14. delete(dict,"_id")
  15. //最早发布时间 (小)
  16. dict["early_publishtime"] = qu.IntAll(dict["publishtime"])
  17. //最近发布时间 (大)
  18. dict["lately_publishtime"] = qu.IntAll(dict["publishtime"])
  19. //最早入库时间 (小)
  20. dict["early_comeintime"] = qu.IntAll(dict["comeintime"])
  21. //最近入库时间 (大)
  22. dict["lately_comeintime"] = qu.IntAll(dict["comeintime"])
  23. //当前更新时间
  24. dict["current_updatetime"] = qu.IntAll(time.Now().Unix())
  25. //融合生成时间
  26. dict["current_updatetime"] = qu.IntAll(time.Now().Unix())
  27. //所有相关联ids
  28. dict["fusion_allids"] = weight.allids
  29. //融合保存相关联ids
  30. dict["fusion_saveids"] = weight.saveids
  31. return dict,dict
  32. }
  33. //处理多条融合数据-返回融合新数据,融合细节数据
  34. func (weight *weightDataMap) dealWithMultipleFusionStruct ()(map[string]interface{},map[string]interface{}){
  35. //指定模板数据dict
  36. dict :=weight.data[weight.templateid].data
  37. //最早|近发布时间
  38. dict["early_publishtime"],dict["lately_publishtime"] = weight.dealWithTimeData("publishtime")
  39. //最早|近入库时间
  40. dict["early_comeintime"],dict["lately_comeintime"] = weight.dealWithTimeData("comeintime")
  41. //当前更新时间
  42. dict["current_updatetime"] = qu.IntAll(time.Now().Unix())
  43. //融合生成时间-取融合表融合数据
  44. dict["current_updatetime"] = ""
  45. //所有相关联ids
  46. dict["fusion_allids"] = weight.allids
  47. //融合保存相关联ids
  48. dict["fusion_saveids"] = weight.saveids
  49. //日志记录-还有快照页面 等等
  50. recordDict := make(map[string]interface{},0)
  51. //结构体字段逻辑处理
  52. structData := weight.dealWithStructData(&recordDict)
  53. for k,v:=range structData {
  54. log.Println("key:",k,"value",v)
  55. dict["k"] = v
  56. }
  57. //非空新增字段
  58. otherFieldData := weight.dealWithOtherFieldData(&recordDict)
  59. for k,v:=range otherFieldData {
  60. //log.Println("key:",k,"value",v)
  61. dict[k] = v
  62. }
  63. dict["repeat"] = 0
  64. //log.Println("待更新数据:",dict)
  65. //log.Println("待更新日志:",recordDict)
  66. //返回,更新数据,日志记录数据
  67. return dict,recordDict
  68. }
  69. //处理其他字段数据
  70. func (weight *weightDataMap)dealWithOtherFieldData(recordDict *map[string]interface{}) map[string]interface{} {
  71. //模板id 数据
  72. templateid := weight.templateid
  73. templateTmp := weight.data[templateid].data
  74. modifyData := make(map[string]interface{}, 0) //返回修改的数据
  75. //找到非空数据
  76. arr := make([]string,0)
  77. for key,value:=range templateTmp {
  78. //判断是否为有效值-
  79. if !judgeIsEffectiveData(value,key) { //无效
  80. arr = append(arr,key)
  81. }
  82. }
  83. //第一步,替换模板,存在且空值
  84. if arr!=nil && len(arr)>0 {
  85. for _,key:=range arr {
  86. isRank := 2
  87. L: for {
  88. for _,v:=range weight.saveids {
  89. if v == templateid {
  90. continue
  91. }
  92. dataInfo:=weight.data[v]
  93. if dataInfo.ranking==isRank { //找到指定排名-字段数据
  94. value:=dataInfo.data[key]
  95. if value !=nil && judgeIsEffectiveData(value,key) {
  96. modifyData[key] = value
  97. templateTmp[key] = value
  98. (*recordDict)[key] = map[string]interface{}{
  99. "id":v,
  100. "value":value,
  101. }
  102. break L
  103. }
  104. break
  105. }
  106. }
  107. isRank++
  108. if isRank > len(weight.saveids) {
  109. break L
  110. }
  111. }
  112. }
  113. }
  114. log.Println("待替换key:",arr,"修改后:",modifyData)
  115. //第二步-集合最大化
  116. isRank := 2
  117. for { //不断遍历,找到其他排名数据
  118. for _,v:=range weight.saveids {
  119. if v == templateid {
  120. continue
  121. }
  122. dataInfo:=weight.data[v]
  123. if dataInfo.ranking==isRank { //找到指定排名数据
  124. for key,newValue:=range dataInfo.data{
  125. if key=="_id" || templateTmp[key]!=nil || NoNeedFusionKey[key]!=nil{
  126. continue
  127. }
  128. if judgeIsEffectiveData(newValue,key) {
  129. log.Println("最大化有效-",key)
  130. templateTmp[key] = newValue
  131. modifyData[key] = newValue
  132. (*recordDict)[key] = map[string]interface{}{
  133. "id":v,
  134. "value":newValue,
  135. }
  136. }
  137. }
  138. break
  139. }
  140. }
  141. isRank++
  142. if isRank > len(weight.saveids) {
  143. break
  144. }
  145. }
  146. log.Println("isRank:",isRank,len(modifyData))
  147. return modifyData
  148. }
  149. //处理结构数据
  150. func (weight *weightDataMap)dealWithStructData(recordDict *map[string]interface{}) map[string]interface{} {
  151. //模板id 数据
  152. templateid:=weight.templateid
  153. templateTmp:=weight.data[templateid].data
  154. modifyData :=make(map[string]interface{},0)
  155. //附件attach_text
  156. /*
  157. "attach_text" : {
  158. "1" : {
  159. "0" : {
  160. "file_name" : "政采贷融资.doc",
  161. "attach_url" : "d5ca0944-6af1-11eb-a8bb-0242ac120002"
  162. }
  163. },
  164. "0" : {
  165. "0" : {
  166. "file_name" : "01永嘉县人民医院发光免疫试剂采购及设备租赁项目公开招标文件(电子招标).doc",
  167. "attach_url" : "7827b2d4-6adb-11eb-bd40-0242ac120002"
  168. }
  169. }
  170. },
  171. */
  172. attach_text,isAttach:=make(map[string]interface{},0),false
  173. if tmp_arr,b := templateTmp["attach_text"].(map[string]interface{});b {
  174. //有值符合-
  175. attach_text = tmp_arr
  176. log.Println("默认初始:",attach_text)
  177. }
  178. //附件判重-并合并新增
  179. keyIndex := -1
  180. //找到当前最大keyIndex
  181. for k,_:=range attach_text {
  182. key:=qu.IntAll(k)
  183. if key>keyIndex {
  184. keyIndex = key
  185. }
  186. }
  187. log.Println("当前keyIndex",keyIndex)
  188. for _,value_id :=range weight.saveids {
  189. if templateid == value_id {
  190. continue
  191. }
  192. rankData := weight.data[value_id].data //具体其他排名数据
  193. if attachData,b := rankData["attach_text"].(map[string]interface{});b {
  194. if len(attachData)>0 { //有值
  195. for _,v:=range attachData { //子元素
  196. if attach,isOK := v.(map[string]interface{});isOK {
  197. log.Println(attach)
  198. if !dealWithRepeatAttachData(attach_text,attach) {
  199. //符合条件-不重复直接添加
  200. keyIndex++
  201. saveKey := fmt.Sprintf("%v",keyIndex)
  202. attach_text[saveKey] = attach //key累加
  203. log.Println(attach_text)
  204. isAttach = true
  205. //多条情况-融合
  206. if (*recordDict)["attach_text"]==nil {
  207. (*recordDict)["attach_text"] = []map[string]interface{}{
  208. map[string]interface{}{
  209. "id":value_id,
  210. "value":attach,
  211. },
  212. }
  213. }else {
  214. arr := (*recordDict)["attach_text"].([]map[string]interface{})
  215. arr = append(arr,map[string]interface{}{
  216. "id":value_id,
  217. "value":attach,
  218. })
  219. (*recordDict)["attach_text"] = arr
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. //联系人 winnerorder
  228. winnerCount:=qu.IntAll(0)
  229. winnerArr,b,isWinner,winnerid:=make(primitive.A,0),false,false,templateid
  230. if winnerArr,b = templateTmp["winnerorder"].([]interface{});b {
  231. winnerCount = qu.IntAll(len(winnerArr))
  232. }
  233. //分包 package
  234. packageCount:=qu.IntAll(0)
  235. packageArr,b,isPackage,packageid:=make(map[string]interface{},0),false,false,templateid
  236. if packageArr,b = templateTmp["package"].(map[string]interface{});b {
  237. packageCount = qu.IntAll(len(packageArr))
  238. }
  239. //遍历其他数据-
  240. for _,value:=range weight.saveids {
  241. if templateid == value {
  242. continue
  243. }
  244. //winnerorder
  245. tmp:=weight.data[value].data
  246. if arr_1,winner_b := tmp["winnerorder"].(primitive.A);winner_b {
  247. count:=qu.IntAll(len(arr_1))
  248. if count > winnerCount {
  249. winnerCount = count
  250. winnerArr = arr_1
  251. isWinner = true
  252. winnerid = value
  253. }
  254. }
  255. //package
  256. if arr_2,package_b := (tmp["package"]).(map[string]interface{});package_b {
  257. count:=qu.IntAll(len(arr_2))
  258. if count > packageCount {
  259. packageCount = count
  260. packageArr = arr_2
  261. isPackage = true
  262. packageid = value
  263. }
  264. }
  265. }
  266. //改变的值
  267. if len(winnerArr)>0 && winnerArr!=nil && isWinner {
  268. modifyData["winnerorder"] = winnerArr
  269. (*recordDict)["winnerorder"] = map[string]interface{}{
  270. "id":winnerid,
  271. "value":winnerArr,
  272. }
  273. }
  274. if len(packageArr)>0 && packageArr!=nil && isPackage {
  275. modifyData["package"] = packageArr
  276. (*recordDict)["package"] = map[string]interface{}{
  277. "id":packageid,
  278. "value":packageArr,
  279. }
  280. }
  281. if len(attach_text)>0 && attach_text!=nil && isAttach {
  282. modifyData["attach_text"] = attach_text
  283. }
  284. return modifyData
  285. }