update.go 13 KB

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