project.go 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. package main
  2. import (
  3. "log"
  4. // "log"
  5. "math"
  6. qu "qfw/util"
  7. "sort"
  8. "strings"
  9. "time"
  10. //"gopkg.in/mgo.v2/bson"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "go.mongodb.org/mongo-driver/bson/primitive"
  13. )
  14. /**
  15. 项目合并,对比,计算,合并,生成项目
  16. **/
  17. //从对应map中获取对比的项目id
  18. func (p *ProjectTask) getCompareIds(pn, pc, ptc, pb string) (bpn, bpc, bptc, bpb int, res []*Key, idArr []string, IDArr []*ID) {
  19. p.findLock.Lock()
  20. defer p.findLock.Unlock()
  21. // p.ConCurrentLock(n1, n2, n3, n4)
  22. // defer p.ConCurrentUnLock(n1, n2, n3, n4)
  23. p.wg.Add(1)
  24. //查找到id数组
  25. res = []*Key{}
  26. //是否查找到,并标识位置。-1代表值为空或已经存在。
  27. bpn, bpc, bptc, bpb = -1, -1, -1, -1
  28. if pn != "" {
  29. ids := p.mapPn[pn]
  30. if ids == nil {
  31. ids = &Key{Arr: []string{}}
  32. p.mapPn[pn] = ids
  33. bpn = 0
  34. }
  35. ids.Lock.Lock()
  36. res = append(res, ids)
  37. }
  38. if pc != "" {
  39. ids := p.mapPc[pc]
  40. if ids == nil {
  41. ids = &Key{Arr: []string{}}
  42. p.mapPc[pc] = ids
  43. bpc = len(res)
  44. }
  45. ids.Lock.Lock()
  46. res = append(res, ids)
  47. }
  48. if ptc != "" {
  49. ids := p.mapPc[ptc]
  50. if ids == nil {
  51. ids = &Key{Arr: []string{}}
  52. p.mapPc[ptc] = ids
  53. bptc = len(res)
  54. }
  55. ids.Lock.Lock()
  56. res = append(res, ids)
  57. }
  58. if pb != "" {
  59. ids := p.mapPb[pb]
  60. if ids == nil {
  61. ids = &Key{Arr: []string{}}
  62. p.mapPb[pb] = ids
  63. bpb = len(res)
  64. }
  65. ids.Lock.Lock()
  66. res = append(res, ids)
  67. }
  68. repeatId := map[string]bool{}
  69. idArr = []string{} //项目id
  70. IDArr = []*ID{} //项目信息
  71. for _, m := range res {
  72. for _, id := range m.Arr {
  73. if !repeatId[id] {
  74. repeatId[id] = true
  75. //_, _ = strconv.ParseInt(id[0:8], 16, 64)
  76. p.AllIdsMapLock.Lock()
  77. Id := p.AllIdsMap[id]
  78. p.AllIdsMapLock.Unlock()
  79. if Id != nil {
  80. Id.Lock.Lock()
  81. idArr = append(idArr, id)
  82. IDArr = append(IDArr, Id)
  83. }
  84. }
  85. }
  86. }
  87. return
  88. }
  89. func (p *ProjectTask) startProjectMerge(info *Info, tmp map[string]interface{}) {
  90. //只有或没有采购单位的无法合并
  91. //bpn, bpc, bptc, bpb 是否查找到,并标识位置。-1代表未查找到。
  92. //pids 是项目id数组集合
  93. //IDArr,是单个项目ID对象集合
  94. bpn, bpc, bptc, bpb, pids, _, IDArr := p.getCompareIds(info.ProjectName, info.ProjectCode, info.PTC, info.Buyer) //, info.LenPN, info.LenPC, info.LenPTC, len([]rune(info.Buyer)))
  95. defer p.wg.Done()
  96. //map--k为pn,ptn,pc,ptc,buyer值 v为Id数组和lock
  97. for _, m := range pids {
  98. defer m.Lock.Unlock()
  99. }
  100. for _, id := range IDArr {
  101. defer id.Lock.Unlock()
  102. }
  103. bFindProject := false
  104. findPid := ""
  105. //获取完id,进行计算
  106. //定义两组
  107. comRes1 := []*ProjectInfo{} //优先级最高的对比结果数组
  108. comRes2 := []*ProjectInfo{} //优化级其次
  109. comRes3 := []*ProjectInfo{}
  110. for _, v := range IDArr {
  111. comStr := ""
  112. compareProject := v.P
  113. compareProject.score = 0
  114. //问题出地LastTime!!!!!
  115. diffTime := int64(math.Abs(float64(info.Publishtime - compareProject.LastTime)))
  116. if diffTime <= p.validTime {
  117. //代理机构完全不相同,直接新建项目
  118. if CheckContain(compareProject.Agency, info.Agency) == 3 {
  119. continue
  120. }
  121. //地区(省、市、区)不同,直接新建项目
  122. if ComparePlace(compareProject, info) {
  123. continue
  124. }
  125. info.PNBH = 0
  126. info.PCBH = 0
  127. info.PTCBH = 0
  128. compareStr, score := comparePNC(info, compareProject)
  129. resVal, pjVal := Select(compareStr, info, compareProject)
  130. //---------------------------------------
  131. //log.Println(resVal, pjVal, compareProject)
  132. if resVal > 0 {
  133. compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount, score2 := p.compareBCTABB(info, compareProject, diffTime, score)
  134. //项目名称、项目编号、标题项目编号、采购单位、省、市、发布时间、代理机构
  135. comStr = compareStr + compareBuyer + compareCity + compareTime + compareAgency + compareBudget + compareBidmount
  136. compareProject.comStr = comStr
  137. compareProject.pjVal = pjVal
  138. compareProject.resVal = resVal
  139. //log.Println(compareProject.comStr)
  140. eqV := compareResult(resVal, pjVal, score2, comStr, compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount)
  141. if eqV == 1 {
  142. comRes1 = append(comRes1, compareProject)
  143. } else if eqV == 2 {
  144. comRes2 = append(comRes2, compareProject)
  145. } else if eqV == 3 {
  146. comRes3 = append(comRes3, compareProject)
  147. }
  148. }
  149. }
  150. }
  151. //--------------------------------对比完成-----------------------
  152. //更新数组、更新项目
  153. for kv, resN := range [][]*ProjectInfo{comRes1, comRes2, comRes3} {
  154. if len(resN) > 0 {
  155. if len(resN) > 1 {
  156. sort.Slice(resN, func(i, j int) bool {
  157. return resN[i].score > resN[j].score
  158. })
  159. }
  160. ex := 0
  161. resArr := []*ProjectInfo{}
  162. for _, res := range resN {
  163. choose, e := p.CompareStatus(resN[0], info)
  164. if !choose {
  165. ex = e
  166. resArr = append(resArr, res)
  167. }
  168. }
  169. if len(resArr) > 0 {
  170. bFindProject = true
  171. findPid = resArr[0].Id.Hex()
  172. for k2, bv := range []int{bpn, bpc, bptc, bpb} {
  173. if bv > -1 {
  174. pids[bv].Arr = append(pids[bv].Arr, findPid)
  175. if k2 == 0 {
  176. if resArr[0].ProjectName == "" {
  177. resArr[0].ProjectName = info.ProjectName
  178. } else {
  179. if resArr[0].MPN == nil {
  180. resArr[0].MPN = []string{info.ProjectName}
  181. } else {
  182. resArr[0].MPN = append(resArr[0].MPN, info.ProjectName)
  183. }
  184. }
  185. } else if k2 < 3 {
  186. if resArr[0].ProjectCode == "" {
  187. resArr[0].ProjectCode = qu.If(k2 == 1, info.ProjectCode, info.PTC).(string)
  188. } else {
  189. if resArr[0].MPC == nil {
  190. resArr[0].MPC = []string{qu.If(k2 == 1, info.ProjectCode, info.PTC).(string)}
  191. } else {
  192. resArr[0].MPC = append(resArr[0].MPC, qu.If(k2 == 1, info.ProjectCode, info.PTC).(string))
  193. }
  194. }
  195. } else {
  196. if resArr[0].Buyer == "" {
  197. resArr[0].Buyer = info.Buyer
  198. }
  199. }
  200. }
  201. }
  202. p.UpdateProject(tmp, info, resArr[0], kv+1, resArr[0].comStr, ex)
  203. } else {
  204. bFindProject = false
  205. findPid = ""
  206. }
  207. break
  208. }
  209. }
  210. if !bFindProject {
  211. id, p1 := p.NewProject(tmp, info)
  212. p.AllIdsMapLock.Lock()
  213. p.AllIdsMap[id] = &ID{Id: id, P: p1}
  214. p.AllIdsMapLock.Unlock()
  215. for _, m := range pids {
  216. m.Arr = append(m.Arr, id)
  217. }
  218. }
  219. }
  220. func (p *ProjectTask) compareBCTABB(info *Info, cp *ProjectInfo, diffTime int64, score int) (compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount string, score2 int) {
  221. compareBuyer = "D"
  222. if len([]rune(info.Buyer)) > 3 && len([]rune(cp.Buyer)) > 3 {
  223. v := CheckContain(info.Buyer, cp.Buyer)
  224. if v == 1 {
  225. compareBuyer = "A"
  226. score += 3
  227. } else {
  228. //v1 := CosineSimilar(info.Buyer, cp.Buyer)
  229. if v == 2 {
  230. compareBuyer = "B"
  231. score += 1
  232. } else {
  233. compareBuyer = "C"
  234. }
  235. }
  236. }
  237. //---------------------------------------
  238. compareCity = ""
  239. if info.Area != "全国" && info.Area != "" && info.Area == cp.Area {
  240. compareCity += "A"
  241. score += 2
  242. } else if info.Area == "全国" || cp.Area == "全国" {
  243. compareCity += "B"
  244. score += 1
  245. } else {
  246. compareCity += "C"
  247. }
  248. if compareCity != "C" {
  249. if info.City != "" && info.City == cp.City {
  250. compareCity += "A"
  251. score += 2
  252. } else {
  253. if info.Area == "全国" || cp.Area == "全国" {
  254. compareCity += "B"
  255. } else if info.City == compareCity {
  256. compareCity += "B"
  257. } else {
  258. compareCity += "C"
  259. }
  260. }
  261. } else {
  262. compareCity += "C"
  263. }
  264. score2 = 0
  265. if compareCity == "AA" {
  266. if info.District != "" && info.District == cp.District {
  267. score2 = 1
  268. }
  269. }
  270. compareTime = "D"
  271. if diffTime < 45*86400 {
  272. compareTime = "A"
  273. score += 2
  274. } else if diffTime < 90*86400 {
  275. compareTime = "B"
  276. score += 1
  277. }
  278. compareAgency = "D"
  279. if info.Agency != "" {
  280. if info.Agency == cp.Agency {
  281. compareAgency = "A"
  282. score += 2
  283. score2 += 1
  284. } else if cp.Agency != "" {
  285. if strings.Contains(info.Agency, cp.Agency) || strings.Contains(cp.Agency, info.Agency) {
  286. compareAgency = "B"
  287. score += 1
  288. score2 += 1
  289. } else {
  290. compareAgency = "C"
  291. }
  292. }
  293. }
  294. compareBudget = "C"
  295. if info.Budget > 0 && (info.Budget == cp.Budget || (cp.Bidamount > 0 && info.Budget > cp.Bidamount && (info.Budget-cp.Bidamount) < (0.15*info.Budget))) {
  296. compareBudget = "A"
  297. score += 1
  298. score2 += 1
  299. }
  300. // else if info.Budget == 0 && cp.Budget == 0 {
  301. // compareBudget = "B"
  302. // }
  303. compareBidmount = "C"
  304. if info.Bidamount > 0 && (info.Bidamount == cp.Bidamount || (cp.Budget > 0 && cp.Budget > info.Bidamount && (cp.Budget-info.Bidamount) < 0.15*cp.Budget)) {
  305. compareBidmount = "A"
  306. score += 1
  307. score2 += 1
  308. }
  309. // else if info.Bidamount == 0 && cp.Bidamount == 0 {
  310. // compareBidmount = "B"
  311. // }
  312. cp.score = score
  313. return
  314. }
  315. func compareResult(resVal, pjVal, score2 int, comStr, compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount string) int {
  316. eqV := 0
  317. switch resVal {
  318. case 3:
  319. if pjVal == 3 && comStr[3:] != "CCCDCCC" {
  320. eqV = 1
  321. } else if compareBuyer < "C" {
  322. if pjVal > 1 {
  323. eqV = 1
  324. } else { //if (compareCity[1:1] != "C" || compareTime != "D") && score2 > 0
  325. eqV = 2
  326. }
  327. } else if compareBuyer == "D" {
  328. if pjVal > 1 && (compareCity[1:1] != "C" || score2 > 0) {
  329. eqV = 2
  330. } else if compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  331. eqV = 3
  332. }
  333. } else {
  334. if pjVal == 3 && (score2 > 0 || compareCity[1:1] != "C") {
  335. eqV = 2
  336. } else if pjVal == 2 && compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  337. eqV = 3
  338. } else if compareCity == "AA" && compareTime == "A" && score2 > 0 {
  339. eqV = 3
  340. }
  341. }
  342. case 2:
  343. if compareBuyer < "C" {
  344. if pjVal > 1 {
  345. eqV = 2
  346. } else if compareCity[1:1] != "C" && compareTime == "A" || score2 > 0 {
  347. eqV = 3
  348. }
  349. } else if compareBuyer == "D" {
  350. if pjVal > 1 && (score2 > 0 || compareCity[1:1] != "C") {
  351. eqV = 2
  352. } else if compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  353. eqV = 3
  354. }
  355. } else {
  356. if pjVal > 1 && compareTime == "A" && (score2 > 0 || compareCity[1:1] != "C") {
  357. eqV = 2
  358. } else if compareCity[1:1] != "C" && compareTime == "A" && (compareAgency == "A" || score2 > 0) && (compareBudget == "A" || compareBidmount == "A") {
  359. eqV = 3
  360. }
  361. }
  362. case 1:
  363. if compareBuyer < "C" {
  364. if pjVal > 1 && (score2 > 0 || compareCity[1:1] != "C") {
  365. eqV = 2
  366. } else if compareCity[1:1] != "C" && compareTime == "A" && (compareAgency == "A" || score2 > 0) && (compareBudget == "A" || compareBidmount == "A") {
  367. eqV = 3
  368. }
  369. } else if compareBuyer == "D" {
  370. if pjVal > 1 && compareTime == "A" && (score2 > 0 || compareCity[1:1] != "C") {
  371. eqV = 2
  372. } else if compareCity[1:1] != "C" && compareTime == "A" && (compareAgency == "A" || score2 > 0) && (compareBudget == "A" || compareBidmount == "A") {
  373. eqV = 3
  374. }
  375. } else {
  376. if pjVal > 1 && compareTime == "A" && score2 > 0 && (compareBudget == "A" || compareBidmount == "A") && compareCity[1:1] != "C" {
  377. eqV = 3
  378. }
  379. }
  380. }
  381. return eqV
  382. }
  383. var FIELDS = []string{
  384. "area",
  385. "city",
  386. "district",
  387. "projectname",
  388. "projectcode",
  389. "buyer",
  390. "buyerclass",
  391. "buyerperson",
  392. "buyertel",
  393. "winner",
  394. //"budget",
  395. //"bidamount",
  396. //"bidstatus",
  397. "agency",
  398. "projectscope",
  399. "topscopeclass",
  400. "subscopeclass",
  401. "winnerorder",
  402. "package",
  403. }
  404. var bidstatus = map[string]string{
  405. "预告": "预告",
  406. "中标": "中标",
  407. "成交": "成交",
  408. "废标": "废标",
  409. "流标": "流标",
  410. "合同": "合同",
  411. }
  412. //招标时间zbtime、中标时间jgtime、项目状态bidstatus、招标类型bidtype、最后发布时间lasttime、首次发布时间firsttime
  413. func (p *ProjectTask) NewProject(tmp map[string]interface{}, thisinfo *Info) (string, *ProjectInfo) {
  414. pId := primitive.NewObjectID() //NewObjectId()
  415. set := map[string]interface{}{}
  416. set["_id"] = pId
  417. for _, f := range FIELDS {
  418. if tmp[f] != nil {
  419. set[f] = tmp[f]
  420. }
  421. }
  422. if tmp["budget"] != nil {
  423. set["budget"] = thisinfo.Budget
  424. }
  425. if tmp["bidamount"] != nil {
  426. set["bidamount"] = thisinfo.Bidamount
  427. }
  428. bidopentime := qu.Int64All(tmp["bidopentime"])
  429. if bidopentime > 0 {
  430. set["bidopentime"] = bidopentime
  431. }
  432. if thisinfo.ProjectName != "" {
  433. set["s_projectname"] = tmp["projectname"]
  434. }
  435. now := time.Now().Unix()
  436. set["createtime"] = now
  437. set["sourceinfoid"] = thisinfo.Id
  438. set["sourceinfourl"] = tmp["href"]
  439. set["firsttime"] = tmp["publishtime"]
  440. set["lasttime"] = tmp["publishtime"]
  441. set["pici"] = p.pici
  442. set["ids"] = []string{thisinfo.Id}
  443. if thisinfo.TopType == "招标" {
  444. set["zbtime"] = tmp["publishtime"]
  445. } else if thisinfo.TopType == "结果" {
  446. set["jgtime"] = tmp["publishtime"]
  447. }
  448. //异常标记
  449. if thisinfo.TopType != "招标" && thisinfo.TopType != "拟建" && thisinfo.TopType != "预告" {
  450. set["exception"] = 1
  451. }
  452. //projecthref保存
  453. if jsonData, ok := tmp["jsondata"].(map[string]interface{}); ok {
  454. if jsonData != nil && qu.ObjToString(jsonData["projecthref"]) != "" {
  455. set["projecthref"] = jsonData["projecthref"]
  456. }
  457. }
  458. //招标类型
  459. bt := qu.ObjToString(tmp["toptype"])
  460. set["bidtype"] = bt
  461. bs, _ := tmp["subtype"].(string)
  462. p.mapBidLock.Lock()
  463. if bidstatus[bs] != "" {
  464. set["bidstatus"] = thisinfo.SubType
  465. } else if tmp["infoformat"] == 2 {
  466. set["bidstatus"] = "拟建"
  467. } else if tmp["subytpe"] == "招标" {
  468. set["bidstatus"] = thisinfo.TopType
  469. } else {
  470. set["bidstatus"] = "其它"
  471. }
  472. p.mapBidLock.Unlock()
  473. p1, pkg := p.NewCachePinfo(pId, thisinfo, bt)
  474. if len(thisinfo.Subscopeclass) > 0 {
  475. s_subscopeclass := strings.Join(thisinfo.Subscopeclass, ",")
  476. set["s_subscopeclass"] = s_subscopeclass
  477. }
  478. if len(thisinfo.Winners) > 0 {
  479. set["s_winner"] = strings.Join(thisinfo.Winners, ",")
  480. p1.Winners = thisinfo.Winners
  481. }
  482. if thisinfo.HasPackage {
  483. set["multipackage"] = 1
  484. set["package"] = pkg
  485. } else {
  486. set["multipackage"] = 0
  487. }
  488. push := p.PushListInfo(tmp, thisinfo.Id)
  489. set["list"] = []bson.M{
  490. push,
  491. }
  492. //p.savePool <- set
  493. p.updatePool <- []map[string]interface{}{
  494. {
  495. "_id": pId,
  496. },
  497. {
  498. "$set": set,
  499. },
  500. }
  501. return pId.Hex(), &p1
  502. }
  503. var INFOFIELDS = []string{
  504. "projectname",
  505. "projectcode",
  506. "title",
  507. "href",
  508. "publishtime",
  509. "comeintime",
  510. "bidopentime",
  511. "toptype",
  512. "subtype",
  513. "buyer",
  514. "buyerclass",
  515. "agency",
  516. "winner",
  517. "budget",
  518. "bidamount",
  519. "topscopeclass",
  520. "subscopclass",
  521. "infoformat",
  522. "buyerperson",
  523. "buyertel",
  524. "area",
  525. "city",
  526. "list",
  527. }
  528. //项目中list的信息
  529. func (p *ProjectTask) PushListInfo(tmp map[string]interface{}, infoid string) bson.M {
  530. res := bson.M{
  531. "infoid": infoid,
  532. }
  533. for _, k := range INFOFIELDS {
  534. if tmp[k] != nil {
  535. res[k] = tmp[k]
  536. }
  537. }
  538. return res
  539. }
  540. //生成存放在内存中的对象
  541. func (p *ProjectTask) NewCachePinfo(id primitive.ObjectID, thisinfo *Info, bidtype string) (ProjectInfo, map[string]interface{}) {
  542. pkg := map[string]interface{}{}
  543. if thisinfo.HasPackage {
  544. pkg = PackageFormat(thisinfo, nil)
  545. }
  546. p1 := ProjectInfo{
  547. Id: id,
  548. Ids: []string{thisinfo.Id},
  549. ProjectName: thisinfo.ProjectName,
  550. ProjectCode: thisinfo.ProjectCode,
  551. Buyer: thisinfo.Buyer,
  552. Buyerclass: thisinfo.Buyerclass,
  553. Buyerperson: thisinfo.Buyerperson,
  554. Buyertel: thisinfo.Buyertel,
  555. Topscopeclass: thisinfo.Topscopeclass,
  556. Subscopeclass: thisinfo.Subscopeclass,
  557. Agency: thisinfo.Agency,
  558. Area: thisinfo.Area,
  559. City: thisinfo.City,
  560. District: thisinfo.District,
  561. MPN: []string{},
  562. MPC: []string{},
  563. FirstTime: thisinfo.Publishtime,
  564. LastTime: thisinfo.Publishtime,
  565. Budget: thisinfo.Budget,
  566. Package: pkg,
  567. Bidamount: thisinfo.Bidamount,
  568. Bidstatus: thisinfo.SubType,
  569. Bidtype: bidtype,
  570. }
  571. if thisinfo.LenPTC > 5 {
  572. p1.MPC = append(p1.MPC, thisinfo.PTC)
  573. }
  574. return p1, pkg
  575. }
  576. //更新项目
  577. func (p *ProjectTask) UpdateProject(tmp map[string]interface{}, thisinfo *Info, pInfo *ProjectInfo, weight int, comStr string, ex int) {
  578. if p.currentType != "ql" && p.currentType != "updateInfo" {
  579. if BinarySearch(pInfo.Ids, thisinfo.Id) > -1 {
  580. log.Println("repeat", thisinfo.Id)
  581. return
  582. }
  583. }
  584. set := map[string]interface{}{}
  585. pInfo.Ids = append(pInfo.Ids, thisinfo.Id)
  586. //1--firsttime
  587. if thisinfo.Publishtime < pInfo.FirstTime && thisinfo.Publishtime > 0 {
  588. pInfo.FirstTime = thisinfo.Publishtime
  589. set["firsttime"] = thisinfo.Publishtime
  590. if thisinfo.TopType == "招标" {
  591. set["zbtime"] = tmp["publishtime"]
  592. }
  593. }
  594. //2--lasttime
  595. pInfo.LastTime = thisinfo.Publishtime
  596. set["lasttime"] = thisinfo.Publishtime
  597. set["bidtype"] = tmp["toptype"]
  598. bs, _ := tmp["subtype"].(string)
  599. p.mapBidLock.Lock()
  600. if bidstatus[bs] != "" {
  601. set["bidstatus"] = thisinfo.SubType
  602. if bidstatus[bs] != "预告" && bidstatus[bs] != "合同" {
  603. set["jgtime"] = tmp["publishtime"]
  604. }
  605. } else if tmp["infoformat"] == 2 {
  606. set["bidstatus"] = "拟建"
  607. } else if tmp["subytpe"] == "招标" {
  608. set["bidstatus"] = thisinfo.TopType
  609. } else {
  610. set["bidstatus"] = "其它"
  611. }
  612. p.mapBidLock.Unlock()
  613. //废标、流标 处理时间
  614. if thisinfo.SubType == "流标" || thisinfo.SubType == "废标" {
  615. pInfo.FirstTime = thisinfo.Publishtime
  616. pInfo.Bidopentime = int64(0)
  617. pInfo.LastTime = thisinfo.Publishtime
  618. set["firsttime"] = thisinfo.Publishtime
  619. set["zbtime"] = int64(0)
  620. set["publishtime"] = thisinfo.Publishtime
  621. set["bidopentime"] = int64(0)
  622. }
  623. //异常标记
  624. if ex > 0 {
  625. set["exception"] = ex
  626. }
  627. //3\4\5--省、市、县
  628. if thisinfo.Area != "全国" {
  629. if pInfo.Area == "全国" {
  630. pInfo.Area = thisinfo.Area
  631. set["area"] = thisinfo.Area
  632. } else if pInfo.Area != thisinfo.Area {
  633. //xt = false
  634. }
  635. if pInfo.City == "" && thisinfo.City != "" {
  636. pInfo.City = thisinfo.City
  637. set["city"] = thisinfo.City
  638. } else if pInfo.City != thisinfo.City {
  639. //xt = false
  640. }
  641. if thisinfo.District != "" && pInfo.District == "" {
  642. pInfo.District = thisinfo.District
  643. set["district"] = thisinfo.District
  644. }
  645. }
  646. //6--项目名称
  647. if (thisinfo.ProjectName != "" && pInfo.ProjectName == "") || (len([]rune(pInfo.ProjectName)) < 6 && thisinfo.LenPN > 6) {
  648. pInfo.ProjectName = thisinfo.ProjectName
  649. set["projectname"] = thisinfo.ProjectName
  650. }
  651. //7--项目编号
  652. if (pInfo.ProjectCode == "" && thisinfo.ProjectCode != "") || (len([]rune(pInfo.ProjectCode)) < 6 && len([]rune(thisinfo.ProjectCode)) > 6) {
  653. pInfo.ProjectCode = thisinfo.ProjectCode
  654. set["projectcode"] = thisinfo.ProjectCode
  655. }
  656. //7--采购单位
  657. if (pInfo.Buyer == "" && thisinfo.Buyer != "") || (len([]rune(pInfo.Buyer)) < 5 && len([]rune(thisinfo.Buyer)) > 5) {
  658. pInfo.Buyer = thisinfo.Buyer
  659. set["buyer"] = thisinfo.Buyer
  660. pInfo.Buyerclass = thisinfo.Buyerclass
  661. set["buyerclass"] = thisinfo.Buyerclass
  662. }
  663. if pInfo.Buyer == "" {
  664. set["buyerclass"] = ""
  665. }
  666. //8--代理机构
  667. if (pInfo.Agency == "" && thisinfo.Agency != "") || (len([]rune(pInfo.Agency)) < 5 && len([]rune(thisinfo.Agency)) > 5) {
  668. pInfo.Agency = thisinfo.Agency
  669. set["agency"] = thisinfo.Agency
  670. }
  671. //9--采购单位联系人
  672. if thisinfo.Buyerperson != "" && strings.Index(pInfo.Buyerperson, thisinfo.Buyerperson) < 0 {
  673. pInfo.Buyerperson = thisinfo.Buyerperson
  674. set["buyerperson"] = pInfo.Buyerperson
  675. }
  676. //10--采购单位電話
  677. if thisinfo.Buyertel != "" && strings.Index(pInfo.Buyertel, thisinfo.Buyertel) < 0 {
  678. pInfo.Buyertel = thisinfo.Buyertel
  679. set["buyertel"] = pInfo.Buyertel
  680. }
  681. if thisinfo.Bidopentime > pInfo.Bidopentime {
  682. pInfo.Bidopentime = thisinfo.Bidopentime
  683. set["bidopentime"] = pInfo.Bidopentime
  684. }
  685. if len(thisinfo.Topscopeclass) > 0 {
  686. sort.Strings(pInfo.Topscopeclass)
  687. for _, k := range thisinfo.Topscopeclass {
  688. if BinarySearch(pInfo.Topscopeclass, k) == -1 {
  689. pInfo.Topscopeclass = append(pInfo.Topscopeclass, k)
  690. sort.Strings(pInfo.Topscopeclass)
  691. }
  692. }
  693. set["topscopeclass"] = pInfo.Topscopeclass
  694. }
  695. if len(thisinfo.Subscopeclass) > 0 {
  696. sort.Strings(pInfo.Subscopeclass)
  697. for _, k := range thisinfo.Subscopeclass {
  698. if BinarySearch(pInfo.Subscopeclass, k) == -1 {
  699. pInfo.Subscopeclass = append(pInfo.Subscopeclass, k)
  700. sort.Strings(pInfo.Subscopeclass)
  701. }
  702. }
  703. set["subscopeclass"] = pInfo.Subscopeclass
  704. set["s_subscopeclass"] = strings.Join(pInfo.Subscopeclass, ",")
  705. }
  706. //winner
  707. if len(thisinfo.Winners) > 0 {
  708. sort.Strings(pInfo.Winners)
  709. for _, k := range thisinfo.Winners {
  710. if BinarySearch(pInfo.Winners, k) == -1 {
  711. pInfo.Winners = append(pInfo.Winners, k)
  712. sort.Strings(pInfo.Winners)
  713. }
  714. }
  715. //set["winners"] = pInfo.Winners
  716. set["s_winner"] = strings.Join(pInfo.Winners, ",")
  717. }
  718. if thisinfo.HasPackage { //多包处理
  719. set["multipackage"] = 1
  720. pkg := PackageFormat(thisinfo, pInfo)
  721. pInfo.Package = pkg
  722. set["package"] = pInfo.Package
  723. } else {
  724. set["multipackage"] = 0
  725. }
  726. //处理多包后,计算预算金额、中标金额
  727. CountAmount(pInfo, thisinfo)
  728. set["budget"] = pInfo.Budget
  729. set["bidamount"] = pInfo.Bidamount
  730. //
  731. set["mpn"] = pInfo.MPN
  732. set["mpc"] = pInfo.MPC
  733. set["pici"] = p.pici
  734. update := map[string]interface{}{}
  735. if len(set) > 0 {
  736. update["$set"] = set
  737. }
  738. //保留原数据吧
  739. push := p.PushListInfo(tmp, thisinfo.Id)
  740. push["compareStr"] = comStr
  741. push["resVal"] = pInfo.resVal
  742. push["pjVal"] = pInfo.pjVal
  743. update["$push"] = map[string]interface{}{
  744. "list": push,
  745. "ids": thisinfo.Id,
  746. }
  747. if len(update) > 0 {
  748. updateInfo := []map[string]interface{}{
  749. {
  750. "_id": pInfo.Id,
  751. },
  752. update,
  753. }
  754. p.updatePool <- updateInfo
  755. }
  756. }
  757. /**
  758. * 更新项目时,项目状态的处理
  759. * 返回是否新增项目,异常标记
  760. * 1、项目时,新项目时,招标信息的状态(toptype)不是招标、拟建、预告 异常:1
  761. * 异常1是在项目新建的时候才会产生
  762. * 3、项目合并时,项目状态是”流标“/”废标“,招标信息状态不是”招标“ 异常:2
  763. * 4、项目合并时,项目状态是”合同“/”其它“,招标信息类型是”结果“ 异常:3
  764. */
  765. func (p *ProjectTask) CompareStatus(project *ProjectInfo, info *Info) (bool, int) {
  766. if info.TopType == "拟建" || info.TopType == "预告" || info.TopType == "招标" {
  767. if project.Bidstatus == "拟建" || project.Bidstatus == "预告" || project.Bidstatus == "招标" {
  768. return false, 0
  769. } else {
  770. return true, 0
  771. }
  772. } else if info.TopType == "结果" {
  773. if project.Bidstatus == "拟建" || project.Bidstatus == "预告" || project.Bidstatus == "招标" {
  774. return false, 0
  775. } else if project.Bidstatus == info.SubType {
  776. //状态一样,根据发布时间判断是否合并
  777. if (info.Publishtime - project.FirstTime) > p.statusTime {
  778. return true, 0
  779. } else {
  780. return false, 0
  781. }
  782. } else if project.Bidstatus == "成交" && info.SubType == "中标" {
  783. return true, 0
  784. } else if project.Bidstatus == "流标" || project.Bidstatus == "废标" {
  785. return false, 2
  786. } else if project.Bidstatus == "合同" || project.Bidstatus == "其它" {
  787. return false, 3
  788. } else {
  789. return false, 0
  790. }
  791. } else {
  792. return false, 0
  793. }
  794. }
  795. /*
  796. * 对比地区(省、市、区),存在且不同,不能合并
  797. * 返回是否新建项目
  798. */
  799. func ComparePlace(project *ProjectInfo, info *Info) bool {
  800. if info.Area == "全国" || info.Area == "" {
  801. return false
  802. }
  803. if project.Area == "全国" || project.Area == "" {
  804. return false
  805. }
  806. if info.Area == project.Area {
  807. if info.City == "" {
  808. return false
  809. } else if info.City == project.City {
  810. if info.District == "" || info.District == project.District {
  811. return false
  812. } else {
  813. return true
  814. }
  815. } else {
  816. return true
  817. }
  818. } else {
  819. return true
  820. }
  821. }
  822. var PackageEle = []string{
  823. "origin",
  824. "name",
  825. "text",
  826. "budget",
  827. "winner",
  828. "bidamount",
  829. "bidamounttype",
  830. "currency",
  831. "bidstatus",
  832. }
  833. func packageEle(map1 map[string]interface{}, id string) map[string]interface{} {
  834. p2 := map[string]interface{}{}
  835. for _, k := range PackageEle {
  836. if map1[k] != nil {
  837. p2[k] = map1[k]
  838. }
  839. infoid := p2["infoid"]
  840. if infoid == nil {
  841. p2["infoid"] = id
  842. }
  843. }
  844. return p2
  845. }
  846. func PackageFormat(info *Info, project *ProjectInfo) map[string]interface{} {
  847. p1 := map[string]interface{}{}
  848. if project != nil && project.Package != nil && len(project.Package) > 0 {
  849. p1 = project.Package
  850. for k, v := range info.Package {
  851. if v1, ok := v.(map[string]interface{}); ok {
  852. v2 := map[string]interface{}{}
  853. v2 = packageEle(v1, info.Id)
  854. if v2["bidstatus"] == nil {
  855. v2["bidstatus"] = info.SubType
  856. }
  857. addFlag := false
  858. for k1, v3 := range p1 {
  859. if v4, ok := v3.([]map[string]interface{}); ok {
  860. if qu.ObjToString(v4[0]["origin"]) == qu.ObjToString(v2["origin"]) && qu.ObjToString(v4[0]["name"]) == qu.ObjToString(v2["name"]) {
  861. v4 = append(v4, v2)
  862. p1[k1] = v4
  863. addFlag = true
  864. break
  865. }
  866. }
  867. }
  868. if !addFlag {
  869. p1[k] = []map[string]interface{}{v2}
  870. }
  871. }
  872. }
  873. } else {
  874. for k, v := range info.Package {
  875. v1, _ := v.(map[string]interface{})
  876. p2 := map[string]interface{}{}
  877. p2 = packageEle(v1, info.Id)
  878. if p2["bidstatus"] == nil {
  879. p2["bidstatus"] = info.SubType
  880. }
  881. p1[k] = []map[string]interface{}{p2}
  882. }
  883. }
  884. return p1
  885. }
  886. //计算预算(budget)、中标金额(bidamount)
  887. func CountAmount(project *ProjectInfo, info *Info) {
  888. if info.HasPackage {
  889. budget := 0.0
  890. for _, v := range project.Package{
  891. v1, _ := v.([]map[string]interface{})
  892. for _, v2 := range v1{
  893. b1 := qu.Float64All(v2["budget"])
  894. if b1 > 0 {
  895. budget = budget + b1
  896. break
  897. }
  898. }
  899. }
  900. project.Budget = budget
  901. }else {
  902. //招标没有多包
  903. k := KeyPackage.FindStringSubmatch(project.ProjectName)
  904. if len(k) > 0 {
  905. //招标是单包
  906. if len(project.Package) > 0 {
  907. //项目有多包
  908. flag := false
  909. for _, v := range project.Package{
  910. v1, _ := v.([]map[string]interface{})
  911. if len(v1) > 0 && v1[0]["name"] == info.ProjectName {
  912. flag = true
  913. }
  914. }
  915. if !flag {
  916. project.Budget = project.Budget + info.Budget
  917. }
  918. }else {
  919. //项目没有多包
  920. if info.Budget > 0 {
  921. project.Budget = project.Budget + info.Budget
  922. }
  923. }
  924. }else {
  925. //招标不是单包
  926. if project.Budget < info.Budget {
  927. project.Budget = info.Budget
  928. }
  929. }
  930. }
  931. if info.SubType == "中标" || info.SubType == "成交" || info.SubType == "合同" {
  932. if info.HasPackage {
  933. bidamount := 0.0
  934. for _, v := range project.Package{
  935. v1, _ := v.([]map[string]interface{})
  936. for _, v2 := range v1{
  937. b1 := qu.Float64All(v2["bidamount"])
  938. if b1 > 0 {
  939. bidamount = bidamount + b1
  940. break
  941. }
  942. }
  943. }
  944. project.Bidamount = bidamount
  945. }else {
  946. //招标没有多包
  947. k := KeyPackage.FindStringSubmatch(project.ProjectName)
  948. if len(k) > 0 {
  949. //招标是单包
  950. if len(project.Package) > 0 {
  951. //项目有多包
  952. flag := false
  953. for _, v := range project.Package{
  954. v1, _ := v.([]map[string]interface{})
  955. if len(v1) > 0 {
  956. flag = true
  957. }
  958. }
  959. if !flag {
  960. project.Bidamount = project.Bidamount + info.Bidamount
  961. }
  962. }else {
  963. //项目没有多包
  964. if info.Bidamount > 0 {
  965. project.Bidamount = project.Bidamount + info.Bidamount
  966. }
  967. }
  968. }else {
  969. //招标不是单包
  970. if project.Bidamount < info.Bidamount {
  971. project.Bidamount = info.Bidamount
  972. }
  973. }
  974. }
  975. }
  976. //if info.Budget > 0 {
  977. // //项目中第一条招标信息是否是包/段项目
  978. // key := KeyPackage.FindStringSubmatch(project.ProjectName)
  979. // if len(key) > 0 {
  980. // //判断项目中是否已经计算过该包/段的预算
  981. // if !isCount(project, info.ProjectName) {
  982. // project.Budget = project.Budget + info.Budget
  983. // }
  984. // }else {
  985. // if project.Budget < info.Budget {
  986. // project.Budget = info.Budget
  987. // }
  988. // }
  989. //}
  990. ////中标、成交、合同 处理中标金额(bidamount)
  991. //if info.SubType == "中标" || info.SubType == "成交" || info.SubType == "合同" {
  992. // if info.Bidamount > 0 {
  993. // key := KeyPackage.FindStringSubmatch(project.ProjectName)
  994. // if len(key) > 0 {
  995. // if !isCount(project, info.ProjectName) {
  996. // project.Bidamount = project.Bidamount + info.Bidamount
  997. // }
  998. // }else {
  999. // if project.Bidamount > info.Bidamount {
  1000. // project.Bidamount = info.Bidamount
  1001. // }
  1002. // }
  1003. // }
  1004. //}
  1005. //
  1006. ////保存信息到IdStatusInfo
  1007. //if project.IdStatusInfo != nil {
  1008. // project.IdStatusInfo[info.Id] = map[string]interface{}{
  1009. // "projectname": info.ProjectName,
  1010. // "bidstatus": info.SubType,
  1011. // "budget": info.Budget,
  1012. // "bidamount": info.Bidamount,
  1013. // }
  1014. //}else {
  1015. // project.IdStatusInfo = map[string]map[string]interface{}{}
  1016. // project.IdStatusInfo[info.Id] = map[string]interface{}{
  1017. // "projectname": info.ProjectName,
  1018. // "bidstatus": info.SubType,
  1019. // "budget": info.Budget,
  1020. // "bidamount": info.Bidamount,
  1021. // }
  1022. //}
  1023. }