update.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package main
  2. import (
  3. "github.com/go-xweb/log"
  4. "go.mongodb.org/mongo-driver/bson/primitive"
  5. "math"
  6. qu "qfw/util"
  7. "reflect"
  8. "sort"
  9. "strings"
  10. )
  11. func (p *ProjectTask) modifyUpdate(pInfoId string, index int, info *Info, tmp map[string]interface{}, tmpPro map[string]interface{}, modifyMap map[string]interface{}) {
  12. backupPro(tmpPro)
  13. infoList := []interface{}(tmpPro["list"].(primitive.A))
  14. infoMap := infoList[index].(map[string]interface{})
  15. infoList[index] = updateValue(infoMap, modifyMap)
  16. set := map[string]interface{}{
  17. "$set": tmpPro,
  18. }
  19. err := MongoTool.UpdateById(ProjectColl, pInfoId, set)
  20. log.Println(err)
  21. }
  22. func (p *ProjectTask) mergeAndModify(pInfoId string, index int, info *Info, tmp map[string]interface{}, tmpPro map[string]interface{}, modifyMap map[string]interface{}) {
  23. p.AllIdsMapLock.Lock()
  24. currentPro := p.AllIdsMap[pInfoId]
  25. p.AllIdsMapLock.Unlock()
  26. merge, pId, i, comStr, mergePro := p.updateMerge(index, info, pInfoId, tmp, tmpPro)
  27. //招标信息在list中第1个
  28. if index == 0 {
  29. log.Println("第1条合并", "------1------")
  30. //快照
  31. backupPro(tmpPro)
  32. p.AllIdsMapLock.Lock()
  33. delete(p.AllIdsMap, pInfoId)
  34. p.AllIdsMapLock.Unlock()
  35. count := MongoTool.Delete(ProjectColl, pInfoId)
  36. log.Println(count)
  37. for _, infoId := range currentPro.P.Ids{
  38. if infoId == info.Id {
  39. continue
  40. }
  41. tmp1 := MongoTool.FindById(ExtractColl, infoId)
  42. p.CommonMerge(tmp1)
  43. }
  44. }else {
  45. log.Println("第n条合并", "------n------")
  46. //快照 招标信息所在的项目
  47. backupPro(tmpPro)
  48. if merge {
  49. //跟原项目list第0条信息作对比,可以合并
  50. if pId == pInfoId {
  51. //合并到当前项目
  52. mergeProject(p, currentPro.P, info, tmpPro, index)
  53. p.AllIdsMapLock.Lock()
  54. p.AllIdsMap[currentPro.P.Id.Hex()].P = currentPro.P
  55. p.AllIdsMapLock.Unlock()
  56. infoList := []interface{}(tmpPro["list"].(primitive.A))
  57. infoMap := infoList[index].(map[string]interface{})
  58. infoList[index] = updateValue(infoMap, modifyMap)
  59. set := map[string]interface{}{
  60. "$set": tmpPro,
  61. }
  62. MongoTool.UpdateById(ProjectColl, pInfoId, set)
  63. }else {
  64. //合并到的其它项目里边
  65. //更新其它的项目
  66. pro := MongoTool.FindById(ProjectColl, mergePro.Id.Hex())
  67. backupPro(pro, )
  68. choose, ex := CompareStatus(mergePro, info)
  69. if !choose {
  70. p.UpdateProject(tmp, info, mergePro, i, comStr, ex)
  71. }else {
  72. id, p1 := p.NewProject(tmp, info)
  73. p.AllIdsMapLock.Lock()
  74. p.AllIdsMap[id] = &ID{Id: id, P: p1}
  75. p.AllIdsMapLock.Unlock()
  76. }
  77. //内部合并
  78. p.innerMerge(currentPro.P, info, tmp, tmpPro)
  79. }
  80. }else {
  81. //合并到新建的项目里边,原有项目内部合并
  82. p.innerMerge(currentPro.P, info, tmp, tmpPro)
  83. }
  84. }
  85. }
  86. func (p *ProjectTask) updateMerge(index int, info *Info, pInfoId string, tmp map[string]interface{}, tmpPro map[string]interface{}) (bool, string, int, string, *ProjectInfo) {
  87. bpn, bpc, bptc, bpb, pids, _, IDArr := p.getCompareIds(info.ProjectName, info.ProjectCode, info.PTC, info.Buyer)
  88. defer p.wg.Done()
  89. for _, m := range pids {
  90. defer m.Lock.Unlock()
  91. }
  92. for _, id := range IDArr {
  93. defer id.Lock.Unlock()
  94. }
  95. bFindProject := false
  96. findPid := ""
  97. //获取完id,进行计算
  98. //定义两组
  99. comRes1 := []*ProjectInfo{} //优先级最高的对比结果数组
  100. comRes2 := []*ProjectInfo{} //优化级其次
  101. comRes3 := []*ProjectInfo{}
  102. for _, v := range IDArr {
  103. if index == 0 && pInfoId == v.P.Id.Hex() {
  104. //招标条目信息在原项目中是第0条时,忽略
  105. continue
  106. }
  107. comStr := ""
  108. compareProject := v.P
  109. compareProject.score = 0
  110. diffTime := int64(math.Abs(float64(info.Publishtime - compareProject.LastTime)))
  111. if diffTime <= p.validTime {
  112. //代理机构完全不相同,直接新建项目
  113. if CheckContain(compareProject.Agency, info.Agency) == 3 {
  114. break
  115. }
  116. //地区(省、市、区)不同,直接新建项目
  117. if ComparePlace(compareProject, info) {
  118. break
  119. }
  120. info.PNBH = 0
  121. info.PCBH = 0
  122. info.PTCBH = 0
  123. compareStr, score := comparePNC(info, compareProject)
  124. resVal, pjVal := Select(compareStr, info, compareProject)
  125. if resVal > 0 {
  126. compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount, score2 := p.compareBCTABB(info, compareProject, diffTime, score)
  127. //项目名称、项目编号、标题项目编号、采购单位、省、市、发布时间、代理机构
  128. comStr = compareStr + compareBuyer + compareCity + compareTime + compareAgency + compareBudget + compareBidmount
  129. compareProject.comStr = comStr
  130. compareProject.pjVal = pjVal
  131. compareProject.resVal = resVal
  132. eqV := compareResult(resVal, pjVal, score2, comStr, compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount)
  133. if eqV == 1 {
  134. comRes1 = append(comRes1, compareProject)
  135. } else if eqV == 2 {
  136. comRes2 = append(comRes2, compareProject)
  137. } else if eqV == 3 {
  138. comRes3 = append(comRes3, compareProject)
  139. }
  140. }
  141. }
  142. }
  143. //--------------------------------对比完成-----------------------
  144. k := -1
  145. comStr := ""
  146. mergePro := &ProjectInfo{}
  147. for kv, resN := range [][]*ProjectInfo{comRes1, comRes2, comRes3} {
  148. if len(resN) > 0 {
  149. if len(resN) > 1 {
  150. sort.Slice(resN, func(i, j int) bool {
  151. return resN[i].score > resN[j].score
  152. })
  153. }
  154. bFindProject = true
  155. findPid = resN[0].Id.Hex()
  156. for k2, bv := range []int{bpn, bpc, bptc, bpb} {
  157. if bv > -1 {
  158. pids[bv].Arr = append(pids[bv].Arr, findPid)
  159. if k2 == 0 {
  160. if resN[0].ProjectName == "" {
  161. resN[0].ProjectName = info.ProjectName
  162. } else {
  163. if resN[0].MPN == nil {
  164. resN[0].MPN = []string{info.ProjectName}
  165. } else {
  166. resN[0].MPN = append(resN[0].MPN, info.ProjectName)
  167. }
  168. }
  169. } else if k2 < 3 {
  170. if resN[0].ProjectCode == "" {
  171. resN[0].ProjectCode = qu.If(k2 == 1, info.ProjectCode, info.PTC).(string)
  172. } else {
  173. if resN[0].MPC == nil {
  174. resN[0].MPC = []string{qu.If(k2 == 1, info.ProjectCode, info.PTC).(string)}
  175. } else {
  176. resN[0].MPC = append(resN[0].MPC, qu.If(k2 == 1, info.ProjectCode, info.PTC).(string))
  177. }
  178. }
  179. } else {
  180. if resN[0].Buyer == "" {
  181. resN[0].Buyer = info.Buyer
  182. }
  183. }
  184. }
  185. }
  186. if index != 0 {
  187. //招标条目信息在原项目中是第非0条时,返回对比结果
  188. k = kv+1
  189. comStr = resN[0].comStr
  190. mergePro = resN[0]
  191. }else {
  192. mergePro = resN[0]
  193. choose, ex := CompareStatus(resN[0], info)
  194. if !choose {
  195. //如果有合并,合并之前备份(快照)
  196. backupPro(tmpPro)
  197. p.UpdateProject(tmp, info, resN[0], kv+1, resN[0].comStr, ex)
  198. }else {
  199. bFindProject = false
  200. }
  201. }
  202. break
  203. }
  204. }
  205. if !bFindProject {
  206. id, p1 := p.NewProject(tmp, info)
  207. p.AllIdsMapLock.Lock()
  208. p.AllIdsMap[id] = &ID{Id: id, P: p1}
  209. p.AllIdsMapLock.Unlock()
  210. for _, m := range pids {
  211. m.Arr = append(m.Arr, id)
  212. }
  213. }
  214. return bFindProject, findPid, k, comStr, mergePro
  215. }
  216. //内部合并
  217. func (p *ProjectTask) innerMerge(pInfo *ProjectInfo, info *Info, tmp map[string]interface{}, tmpPro map[string]interface{}) {
  218. infoList := []interface{}(tmpPro["list"].(primitive.A))
  219. for index, info1 := range infoList{
  220. info2 := info1.(map[string]interface{})
  221. if info2["infoid"] == info.Id {
  222. deleteSlice1(infoList, info1)
  223. continue
  224. }
  225. mergeProject(p, pInfo, info, tmpPro, index)
  226. }
  227. p.AllIdsMapLock.Lock()
  228. p.AllIdsMap[pInfo.Id.Hex()].P = pInfo
  229. p.AllIdsMapLock.Unlock()
  230. set := map[string]interface{}{
  231. "$set": tmpPro,
  232. }
  233. MongoTool.UpdateById(ProjectColl, pInfo.Id.Hex(), set)
  234. }
  235. //合并字段到project
  236. func mergeProject(p *ProjectTask, pInfo *ProjectInfo, thisinfo *Info, set map[string]interface{}, index int) map[string]interface{} {
  237. //1--firsttime
  238. if thisinfo.Publishtime < pInfo.FirstTime && thisinfo.Publishtime > 0 {
  239. pInfo.FirstTime = thisinfo.Publishtime
  240. set["firsttime"] = thisinfo.Publishtime
  241. if thisinfo.TopType == "招标" {
  242. set["zbtime"] = thisinfo.Publishtime
  243. }
  244. }
  245. //2--lasttime
  246. if thisinfo.Publishtime > pInfo.LastTime {
  247. pInfo.LastTime = thisinfo.Publishtime
  248. set["lasttime"] = thisinfo.Publishtime
  249. bt := bidtype[thisinfo.SubType]
  250. if bt != "" {
  251. set["bidtype"] = bt
  252. }
  253. if thisinfo.SubType != "" {
  254. set["bidstatus"] = thisinfo.SubType
  255. if thisinfo.SubType != "预告" {
  256. set["jgtime"] = thisinfo.Publishtime
  257. }
  258. }else if thisinfo.Infoformat == 2 {
  259. set["bidstatus"] = "拟建"
  260. }else if thisinfo.SubType == "招标" {
  261. set["bidstatus"] = thisinfo.TopType
  262. }else {
  263. set["bidstatus"] = thisinfo.SubType
  264. }
  265. }
  266. //3\4\5--省、市、县
  267. if thisinfo.Area != "全国" {
  268. //xt := true
  269. if pInfo.Area == "全国" {
  270. pInfo.Area = thisinfo.Area
  271. set["area"] = thisinfo.Area
  272. } else if pInfo.Area != thisinfo.Area {
  273. //xt = false
  274. }
  275. if pInfo.City == "" && thisinfo.City != "" {
  276. pInfo.City = thisinfo.City
  277. set["city"] = thisinfo.City
  278. } else if pInfo.City != thisinfo.City {
  279. //xt = false
  280. }
  281. if thisinfo.District != "" && pInfo.District == "" {
  282. pInfo.District = thisinfo.District
  283. set["district"] = thisinfo.District
  284. }
  285. }
  286. //6--项目名称
  287. if (thisinfo.ProjectName != "" && pInfo.ProjectName == "") || (len([]rune(pInfo.ProjectName)) < 6 && thisinfo.LenPN > 6) {
  288. pInfo.ProjectName = thisinfo.ProjectName
  289. set["projectname"] = thisinfo.ProjectName
  290. }
  291. //7--项目编号
  292. if (pInfo.ProjectCode == "" && thisinfo.ProjectCode != "") || (len([]rune(pInfo.ProjectCode)) < 6 && len([]rune(thisinfo.ProjectCode)) > 6) {
  293. pInfo.ProjectCode = thisinfo.ProjectCode
  294. set["projectcode"] = thisinfo.ProjectCode
  295. }
  296. //7--采购单位
  297. if (pInfo.Buyer == "" && thisinfo.Buyer != "") || (len([]rune(pInfo.Buyer)) < 5 && len([]rune(thisinfo.Buyer)) > 5) {
  298. pInfo.Buyer = thisinfo.Buyer
  299. set["buyer"] = thisinfo.Buyer
  300. pInfo.Buyerclass = thisinfo.Buyerclass
  301. set["buyerclass"] = thisinfo.Buyerclass
  302. }
  303. if pInfo.Buyer == "" {
  304. set["buyerclass"] = ""
  305. }
  306. //8--代理机构
  307. if (pInfo.Agency == "" && thisinfo.Agency != "") || (len([]rune(pInfo.Agency)) < 5 && len([]rune(thisinfo.Agency)) > 5) {
  308. pInfo.Agency = thisinfo.Agency
  309. set["agency"] = thisinfo.Agency
  310. }
  311. //9--采购单位联系人
  312. if thisinfo.Buyerperson != "" && strings.Index(pInfo.Buyerperson, thisinfo.Buyerperson) < 0 {
  313. pInfo.Buyerperson = thisinfo.Buyerperson
  314. set["buyerperson"] = pInfo.Buyerperson
  315. }
  316. //10--采购单位電話
  317. if thisinfo.Buyertel != "" && strings.Index(pInfo.Buyertel, thisinfo.Buyertel) < 0 {
  318. pInfo.Buyertel = thisinfo.Buyertel
  319. set["buyertel"] = pInfo.Buyertel
  320. }
  321. if thisinfo.Bidopentime > pInfo.Bidopentime {
  322. pInfo.Bidopentime = thisinfo.Bidopentime
  323. set["bidopentime"] = pInfo.Bidopentime
  324. }
  325. if thisinfo.Bidamount > 0 && pInfo.Bidamount < 1 {
  326. pInfo.Bidamount = thisinfo.Bidamount
  327. set["bidamount"] = pInfo.Bidamount
  328. }
  329. if thisinfo.Budget > 0 && pInfo.Budget < 1 { //多包的会有问题,没有进行合计。
  330. pInfo.Budget = thisinfo.Budget
  331. set["budget"] = pInfo.Budget
  332. }
  333. if len(thisinfo.Topscopeclass) > 0 {
  334. sort.Strings(pInfo.Topscopeclass)
  335. for _, k := range thisinfo.Topscopeclass {
  336. if BinarySearch(pInfo.Topscopeclass, k) == -1 {
  337. pInfo.Topscopeclass = append(pInfo.Topscopeclass, k)
  338. sort.Strings(pInfo.Topscopeclass)
  339. }
  340. }
  341. set["topscopeclass"] = pInfo.Topscopeclass
  342. }
  343. if len(thisinfo.Subscopeclass) > 0 {
  344. sort.Strings(pInfo.Subscopeclass)
  345. for _, k := range thisinfo.Subscopeclass {
  346. if BinarySearch(pInfo.Subscopeclass, k) == -1 {
  347. pInfo.Subscopeclass = append(pInfo.Subscopeclass, k)
  348. sort.Strings(pInfo.Subscopeclass)
  349. }
  350. }
  351. set["subscopeclass"] = pInfo.Subscopeclass
  352. set["s_subscopeclass"] = strings.Join(pInfo.Subscopeclass, ",")
  353. }
  354. //winner
  355. if len(thisinfo.Winners) > 0 {
  356. sort.Strings(pInfo.Winners)
  357. for _, k := range thisinfo.Winners {
  358. if BinarySearch(pInfo.Winners, k) == -1 {
  359. pInfo.Winners = append(pInfo.Winners, k)
  360. sort.Strings(pInfo.Winners)
  361. }
  362. }
  363. //set["winners"] = pInfo.Winners
  364. set["s_winner"] = strings.Join(pInfo.Winners, ",")
  365. }
  366. if thisinfo.HasPackage {
  367. pkg, _, _ := PackageFormat(thisinfo, pInfo)
  368. pInfo.Package = pkg
  369. }
  370. set["mpn"] = pInfo.MPN
  371. set["mpc"] = pInfo.MPC
  372. set["pici"] = p.pici
  373. if thisinfo.HasPackage {
  374. set["multipackage"] = 1
  375. } else {
  376. set["multipackage"] = 0
  377. }
  378. return set
  379. }
  380. func deleteSlice1(arr []interface{}, v interface{}) []interface{} {
  381. for k, v1 := range arr {
  382. if reflect.DeepEqual(v1, v) {
  383. return append(arr[:k], arr[k+1:]...)
  384. }
  385. }
  386. return arr
  387. }
  388. func updateValue(info map[string]interface{}, tmp map[string]interface{}) map[string]interface{} {
  389. for k := range info{
  390. for k1 := range tmp {
  391. if k == k1 && info[k] != tmp[k1] {
  392. info[k] = tmp[k]
  393. break
  394. }
  395. }
  396. }
  397. return info
  398. }
  399. //备份(快照)
  400. func backupPro(tmp map[string]interface{}) {
  401. tmp1 := make(map[string]interface{})
  402. for k, v := range tmp{
  403. tmp1[k] = v
  404. }
  405. if Sysconfig["backupFlag"].(bool) {
  406. tmp1["sourceprojectid"] = tmp1["_id"]
  407. delete(tmp1, "_id")
  408. MongoTool.Save(BackupColl, tmp1)
  409. }
  410. }