project.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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 bidtype = map[string]string{
  405. "招标": "招标",
  406. "询价": "询价",
  407. "竞谈": "竞谈",
  408. "单一": "单一",
  409. "竞价": "竞价",
  410. "变更": "变更",
  411. "邀标": "邀标",
  412. }
  413. var bidstatus = map[string]string{
  414. "预告": "预告",
  415. "中标": "中标",
  416. "成交": "成交",
  417. "废标": "废标",
  418. "流标": "流标",
  419. "合同": "合同",
  420. }
  421. //招标时间zbtime、中标时间jgtime、项目状态bidstatus、招标类型bidtype、最后发布时间lasttime、首次发布时间firsttime
  422. func (p *ProjectTask) NewProject(tmp map[string]interface{}, thisinfo *Info) (string, *ProjectInfo) {
  423. pId := primitive.NewObjectID() //NewObjectId()
  424. set := map[string]interface{}{}
  425. set["_id"] = pId
  426. for _, f := range FIELDS {
  427. if tmp[f] != nil {
  428. set[f] = tmp[f]
  429. }
  430. }
  431. if tmp["budget"] != nil {
  432. set["budget"] = thisinfo.Budget
  433. }
  434. if tmp["bidamount"] != nil {
  435. set["bidamount"] = thisinfo.Bidamount
  436. }
  437. bidopentime := qu.Int64All(tmp["bidopentime"])
  438. if bidopentime > 0 {
  439. set["bidopentime"] = bidopentime
  440. }
  441. if thisinfo.ProjectName != "" {
  442. set["s_projectname"] = tmp["projectname"]
  443. }
  444. now := time.Now().Unix()
  445. set["createtime"] = now
  446. set["sourceinfoid"] = thisinfo.Id
  447. set["sourceinfourl"] = tmp["href"]
  448. set["firsttime"] = tmp["publishtime"]
  449. set["lasttime"] = tmp["publishtime"]
  450. set["pici"] = p.pici
  451. set["ids"] = []string{thisinfo.Id}
  452. if thisinfo.TopType == "招标" {
  453. set["zbtime"] = tmp["publishtime"]
  454. } else if thisinfo.TopType == "结果" {
  455. set["jgtime"] = tmp["publishtime"]
  456. }
  457. //异常标记
  458. if thisinfo.TopType != "招标" && thisinfo.TopType != "拟建" && thisinfo.TopType != "预告" {
  459. set["exception"] = 1
  460. }
  461. //projecthref保存
  462. if jsonData, ok := tmp["jsondata"].(map[string]interface{}); ok {
  463. if jsonData != nil && jsonData["projecthref"] != "" {
  464. set["projecthref"] = jsonData["projecthref"]
  465. }
  466. }
  467. //招标类型
  468. p.mapBidLock.Lock()
  469. bt := bidtype[thisinfo.SubType]
  470. p.mapBidLock.Unlock()
  471. if bt == "" {
  472. bt = "招标"
  473. }
  474. set["bidtype"] = bt
  475. set["bidstatus"] = thisinfo.SubType
  476. p1, pkg := p.NewCachePinfo(pId, thisinfo, bt)
  477. //招标信息是中标或者成交,保存bidstatus、budget、bidamount
  478. if thisinfo.SubType == "中标" || thisinfo.SubType == "成交" {
  479. p1.IdStatusInfo = map[string]map[string]interface{}{
  480. thisinfo.Id: {
  481. "projectname": thisinfo.ProjectName,
  482. "bidstatus": thisinfo.SubType,
  483. "budget": thisinfo.Budget,
  484. "bidamount": thisinfo.Bidamount,
  485. },
  486. }
  487. }
  488. if len(thisinfo.Subscopeclass) > 0 {
  489. s_subscopeclass := strings.Join(thisinfo.Subscopeclass, ",")
  490. set["s_subscopeclass"] = s_subscopeclass
  491. }
  492. if len(thisinfo.Winners) > 0 {
  493. set["s_winner"] = strings.Join(thisinfo.Winners, ",")
  494. p1.Winners = thisinfo.Winners
  495. }
  496. if thisinfo.HasPackage {
  497. set["multipackage"] = 1
  498. set["package"] = pkg
  499. } else {
  500. set["multipackage"] = 0
  501. }
  502. push := p.PushListInfo(tmp, thisinfo.Id)
  503. set["list"] = []bson.M{
  504. push,
  505. }
  506. //p.savePool <- set
  507. p.updatePool <- []map[string]interface{}{
  508. {
  509. "_id": pId,
  510. },
  511. {
  512. "$set": set,
  513. },
  514. }
  515. return pId.Hex(), &p1
  516. }
  517. var INFOFIELDS = []string{
  518. "projectname",
  519. "projectcode",
  520. "title",
  521. "href",
  522. "publishtime",
  523. "comeintime",
  524. "bidopentime",
  525. "toptype",
  526. "subtype",
  527. "buyer",
  528. "buyerclass",
  529. "agency",
  530. "winner",
  531. "budget",
  532. "bidamount",
  533. "topscopeclass",
  534. "subscopclass",
  535. "infoformat",
  536. "buyerperson",
  537. "buyertel",
  538. "area",
  539. "city",
  540. "list",
  541. }
  542. //项目中list的信息
  543. func (p *ProjectTask) PushListInfo(tmp map[string]interface{}, infoid string) bson.M {
  544. res := bson.M{
  545. "infoid": infoid,
  546. }
  547. for _, k := range INFOFIELDS {
  548. if tmp[k] != nil {
  549. res[k] = tmp[k]
  550. }
  551. }
  552. return res
  553. }
  554. //生成存放在内存中的对象
  555. func (p *ProjectTask) NewCachePinfo(id primitive.ObjectID, thisinfo *Info, bidtype string) (ProjectInfo, map[string]interface{}) {
  556. pkg := map[string]interface{}{}
  557. if thisinfo.HasPackage {
  558. pkg = PackageFormat(thisinfo, nil)
  559. }
  560. p1 := ProjectInfo{
  561. Id: id,
  562. Ids: []string{thisinfo.Id},
  563. ProjectName: thisinfo.ProjectName,
  564. ProjectCode: thisinfo.ProjectCode,
  565. Buyer: thisinfo.Buyer,
  566. Buyerclass: thisinfo.Buyerclass,
  567. Buyerperson: thisinfo.Buyerperson,
  568. Buyertel: thisinfo.Buyertel,
  569. Topscopeclass: thisinfo.Topscopeclass,
  570. Subscopeclass: thisinfo.Subscopeclass,
  571. Agency: thisinfo.Agency,
  572. Area: thisinfo.Area,
  573. City: thisinfo.City,
  574. District: thisinfo.District,
  575. MPN: []string{},
  576. MPC: []string{},
  577. FirstTime: thisinfo.Publishtime,
  578. LastTime: thisinfo.Publishtime,
  579. Budget: thisinfo.Budget,
  580. Package: pkg,
  581. Bidamount: thisinfo.Bidamount,
  582. Bidstatus: thisinfo.SubType,
  583. Bidtype: bidtype,
  584. }
  585. if thisinfo.LenPTC > 5 {
  586. p1.MPC = append(p1.MPC, thisinfo.PTC)
  587. }
  588. return p1, pkg
  589. }
  590. //更新项目
  591. func (p *ProjectTask) UpdateProject(tmp map[string]interface{}, thisinfo *Info, pInfo *ProjectInfo, weight int, comStr string, ex int) {
  592. if p.currentType != "ql" && p.currentType != "updateInfo" {
  593. if BinarySearch(pInfo.Ids, thisinfo.Id) > -1 {
  594. log.Println("repeat", thisinfo.Id)
  595. return
  596. }
  597. }
  598. set := map[string]interface{}{}
  599. pInfo.Ids = append(pInfo.Ids, thisinfo.Id)
  600. //1--firsttime
  601. if thisinfo.Publishtime < pInfo.FirstTime && thisinfo.Publishtime > 0 {
  602. pInfo.FirstTime = thisinfo.Publishtime
  603. set["firsttime"] = thisinfo.Publishtime
  604. if thisinfo.TopType == "招标" {
  605. set["zbtime"] = tmp["publishtime"]
  606. }
  607. }
  608. //2--lasttime
  609. if thisinfo.Publishtime > pInfo.LastTime {
  610. pInfo.LastTime = thisinfo.Publishtime
  611. set["lasttime"] = thisinfo.Publishtime
  612. p.mapBidLock.Lock()
  613. defer p.mapBidLock.Unlock()
  614. bt := bidtype[thisinfo.SubType]
  615. if bt != "" {
  616. set["bidtype"] = bt
  617. }
  618. bs, _ := tmp["subtype"].(string)
  619. if bidstatus[bs] != "" {
  620. set["bidstatus"] = thisinfo.SubType
  621. if bidstatus[bs] != "预告" && bidstatus[bs] != "合同" {
  622. set["jgtime"] = tmp["publishtime"]
  623. }
  624. } else if tmp["infoformat"] == 2 {
  625. set["bidstatus"] = "拟建"
  626. } else if tmp["subytpe"] == "招标" {
  627. set["bidstatus"] = thisinfo.TopType
  628. } else {
  629. set["bidstatus"] = "其它"
  630. }
  631. }
  632. //废标、流标 处理时间
  633. if thisinfo.SubType == "流标" || thisinfo.SubType == "废标" {
  634. pInfo.FirstTime = thisinfo.Publishtime
  635. pInfo.Bidopentime = int64(0)
  636. pInfo.LastTime = thisinfo.Publishtime
  637. set["firsttime"] = thisinfo.Publishtime
  638. set["zbtime"] = int64(0)
  639. set["publishtime"] = thisinfo.Publishtime
  640. set["bidopentime"] = int64(0)
  641. }
  642. //异常标记
  643. if ex > 0 {
  644. set["exception"] = ex
  645. }
  646. //3\4\5--省、市、县
  647. if thisinfo.Area != "全国" {
  648. if pInfo.Area == "全国" {
  649. pInfo.Area = thisinfo.Area
  650. set["area"] = thisinfo.Area
  651. } else if pInfo.Area != thisinfo.Area {
  652. //xt = false
  653. }
  654. if pInfo.City == "" && thisinfo.City != "" {
  655. pInfo.City = thisinfo.City
  656. set["city"] = thisinfo.City
  657. } else if pInfo.City != thisinfo.City {
  658. //xt = false
  659. }
  660. if thisinfo.District != "" && pInfo.District == "" {
  661. pInfo.District = thisinfo.District
  662. set["district"] = thisinfo.District
  663. }
  664. }
  665. //6--项目名称
  666. if (thisinfo.ProjectName != "" && pInfo.ProjectName == "") || (len([]rune(pInfo.ProjectName)) < 6 && thisinfo.LenPN > 6) {
  667. pInfo.ProjectName = thisinfo.ProjectName
  668. set["projectname"] = thisinfo.ProjectName
  669. }
  670. //7--项目编号
  671. if (pInfo.ProjectCode == "" && thisinfo.ProjectCode != "") || (len([]rune(pInfo.ProjectCode)) < 6 && len([]rune(thisinfo.ProjectCode)) > 6) {
  672. pInfo.ProjectCode = thisinfo.ProjectCode
  673. set["projectcode"] = thisinfo.ProjectCode
  674. }
  675. //7--采购单位
  676. if (pInfo.Buyer == "" && thisinfo.Buyer != "") || (len([]rune(pInfo.Buyer)) < 5 && len([]rune(thisinfo.Buyer)) > 5) {
  677. pInfo.Buyer = thisinfo.Buyer
  678. set["buyer"] = thisinfo.Buyer
  679. pInfo.Buyerclass = thisinfo.Buyerclass
  680. set["buyerclass"] = thisinfo.Buyerclass
  681. }
  682. if pInfo.Buyer == "" {
  683. set["buyerclass"] = ""
  684. }
  685. //8--代理机构
  686. if (pInfo.Agency == "" && thisinfo.Agency != "") || (len([]rune(pInfo.Agency)) < 5 && len([]rune(thisinfo.Agency)) > 5) {
  687. pInfo.Agency = thisinfo.Agency
  688. set["agency"] = thisinfo.Agency
  689. }
  690. //9--采购单位联系人
  691. if thisinfo.Buyerperson != "" && strings.Index(pInfo.Buyerperson, thisinfo.Buyerperson) < 0 {
  692. pInfo.Buyerperson = thisinfo.Buyerperson
  693. set["buyerperson"] = pInfo.Buyerperson
  694. }
  695. //10--采购单位電話
  696. if thisinfo.Buyertel != "" && strings.Index(pInfo.Buyertel, thisinfo.Buyertel) < 0 {
  697. pInfo.Buyertel = thisinfo.Buyertel
  698. set["buyertel"] = pInfo.Buyertel
  699. }
  700. if thisinfo.Bidopentime > pInfo.Bidopentime {
  701. pInfo.Bidopentime = thisinfo.Bidopentime
  702. set["bidopentime"] = pInfo.Bidopentime
  703. }
  704. if len(thisinfo.Topscopeclass) > 0 {
  705. sort.Strings(pInfo.Topscopeclass)
  706. for _, k := range thisinfo.Topscopeclass {
  707. if BinarySearch(pInfo.Topscopeclass, k) == -1 {
  708. pInfo.Topscopeclass = append(pInfo.Topscopeclass, k)
  709. sort.Strings(pInfo.Topscopeclass)
  710. }
  711. }
  712. set["topscopeclass"] = pInfo.Topscopeclass
  713. }
  714. if len(thisinfo.Subscopeclass) > 0 {
  715. sort.Strings(pInfo.Subscopeclass)
  716. for _, k := range thisinfo.Subscopeclass {
  717. if BinarySearch(pInfo.Subscopeclass, k) == -1 {
  718. pInfo.Subscopeclass = append(pInfo.Subscopeclass, k)
  719. sort.Strings(pInfo.Subscopeclass)
  720. }
  721. }
  722. set["subscopeclass"] = pInfo.Subscopeclass
  723. set["s_subscopeclass"] = strings.Join(pInfo.Subscopeclass, ",")
  724. }
  725. //winner
  726. if len(thisinfo.Winners) > 0 {
  727. sort.Strings(pInfo.Winners)
  728. for _, k := range thisinfo.Winners {
  729. if BinarySearch(pInfo.Winners, k) == -1 {
  730. pInfo.Winners = append(pInfo.Winners, k)
  731. sort.Strings(pInfo.Winners)
  732. }
  733. }
  734. //set["winners"] = pInfo.Winners
  735. set["s_winner"] = strings.Join(pInfo.Winners, ",")
  736. }
  737. if thisinfo.HasPackage { //多包处理
  738. set["multipackage"] = 1
  739. pkg := PackageFormat(thisinfo, pInfo)
  740. pInfo.Package = pkg
  741. set["package"] = pInfo.Package
  742. } else {
  743. set["multipackage"] = 0
  744. }
  745. //处理多包后,计算预算金额、中标金额
  746. CountAmount(pInfo, thisinfo)
  747. set["budget"] = pInfo.Budget
  748. set["bidamount"] = pInfo.Bidamount
  749. //
  750. set["mpn"] = pInfo.MPN
  751. set["mpc"] = pInfo.MPC
  752. set["pici"] = p.pici
  753. update := map[string]interface{}{}
  754. if len(set) > 0 {
  755. update["$set"] = set
  756. }
  757. //保留原数据吧
  758. push := p.PushListInfo(tmp, thisinfo.Id)
  759. push["compareStr"] = comStr
  760. push["resVal"] = pInfo.resVal
  761. push["pjVal"] = pInfo.pjVal
  762. update["$push"] = map[string]interface{}{
  763. "list": push,
  764. "ids": thisinfo.Id,
  765. }
  766. if len(update) > 0 {
  767. updateInfo := []map[string]interface{}{
  768. {
  769. "_id": pInfo.Id,
  770. },
  771. update,
  772. }
  773. p.updatePool <- updateInfo
  774. }
  775. }
  776. /**
  777. * 更新项目时,项目状态的处理
  778. * 返回是否新增项目,异常标记
  779. * 1、项目时,新项目时,招标信息的状态(toptype)不是招标、拟建、预告 异常:1
  780. * 异常1是在项目新建的时候才会产生
  781. * 3、项目合并时,项目状态是”流标“/”废标“,招标信息状态不是”招标“ 异常:2
  782. * 4、项目合并时,项目状态是”合同“/”其它“,招标信息类型是”结果“ 异常:3
  783. */
  784. func (p *ProjectTask) CompareStatus(project *ProjectInfo, info *Info) (bool, int) {
  785. if info.TopType == "拟建" || info.TopType == "预告" || info.TopType == "招标" {
  786. if project.Bidstatus == "拟建" || project.Bidstatus == "预告" || project.Bidstatus == "招标" {
  787. return false, 0
  788. } else {
  789. return true, 0
  790. }
  791. } else if info.TopType == "结果" {
  792. if project.Bidstatus == "拟建" || project.Bidstatus == "预告" || project.Bidstatus == "招标" {
  793. return false, 0
  794. } else if project.Bidstatus == info.SubType {
  795. //状态一样,根据发布时间判断是否合并
  796. if (info.Publishtime - project.FirstTime) > p.statusTime {
  797. return true, 0
  798. } else {
  799. return true, 0
  800. }
  801. } else if project.Bidstatus == "成交" && info.SubType == "中标" {
  802. return true, 0
  803. } else if project.Bidstatus == "流标" || project.Bidstatus == "废标" {
  804. return false, 2
  805. } else if project.Bidstatus == "合同" || project.Bidstatus == "其它" {
  806. return false, 3
  807. } else {
  808. return false, 0
  809. }
  810. } else {
  811. return false, 0
  812. }
  813. }
  814. /*
  815. * 对比地区(省、市、区),存在且不同,不能合并
  816. * 返回是否新建项目
  817. */
  818. func ComparePlace(project *ProjectInfo, info *Info) bool {
  819. if info.Area == "全国" || info.Area == "" {
  820. return false
  821. }
  822. if project.Area == "全国" || project.Area == "" {
  823. return false
  824. }
  825. if info.Area == project.Area {
  826. if info.City == "" {
  827. return false
  828. } else if info.City == project.City {
  829. if info.District == "" || info.District == project.District {
  830. return false
  831. } else {
  832. return true
  833. }
  834. } else {
  835. return true
  836. }
  837. } else {
  838. return true
  839. }
  840. }
  841. var PackageEle = []string{
  842. "origin",
  843. "name",
  844. "text",
  845. "budget",
  846. "winner",
  847. "bidamount",
  848. "bidamounttype",
  849. "currency",
  850. "bidstatus",
  851. }
  852. func packageEle(map1 map[string]interface{}, id string) map[string]interface{} {
  853. p2 := map[string]interface{}{}
  854. for _, k := range PackageEle {
  855. if map1[k] != nil {
  856. p2[k] = map1[k]
  857. }
  858. infoid := p2["infoid"]
  859. if infoid == nil {
  860. p2["infoid"] = id
  861. }
  862. }
  863. return p2
  864. }
  865. func PackageFormat(info *Info, project *ProjectInfo) map[string]interface{} {
  866. budget := 0
  867. bidamount := 0
  868. p1 := map[string]interface{}{}
  869. if project != nil && project.Package != nil && len(project.Package) > 0 {
  870. p1 = project.Package
  871. for k, v := range info.Package {
  872. if v1, ok := v.(map[string]interface{}); ok {
  873. v2 := map[string]interface{}{}
  874. v2 = packageEle(v1, info.Id)
  875. if v2["bidstatus"] == nil {
  876. v2["bidstatus"] = info.SubType
  877. }
  878. if isCount(project, v2[""]) {
  879. }
  880. if v2["budget"] != nil {
  881. budget = budget + v2["budget"].(float64)
  882. }
  883. if v2["bidamount"] != nil {
  884. bidamount = bidamount + v2["bidamount"].(float64)
  885. }
  886. addFlag := false
  887. for k1, v3 := range p1 {
  888. if v4, ok := v3.([]map[string]interface{}); ok {
  889. if qu.ObjToString(v4[0]["origin"]) == qu.ObjToString(v2["origin"]) && qu.ObjToString(v4[0]["name"]) == qu.ObjToString(v2["name"]) {
  890. v4 = append(v4, v2)
  891. p1[k1] = v4
  892. addFlag = true
  893. break
  894. }
  895. }
  896. }
  897. if !addFlag {
  898. p1[k] = []map[string]interface{}{v2}
  899. }
  900. }
  901. }
  902. } else {
  903. for k, v := range info.Package {
  904. v1, _ := v.(map[string]interface{})
  905. p2 := map[string]interface{}{}
  906. p2 = packageEle(v1, info.Id)
  907. if p2["bidstatus"] == nil {
  908. p2["bidstatus"] = info.SubType
  909. }
  910. if p2["budget"] != nil {
  911. budget = p2["budget"].(float64)
  912. }
  913. if p2["bidamount"] != nil {
  914. bidamount = p2["bidamount"].(float64)
  915. }
  916. p1[k] = []map[string]interface{}{p2}
  917. }
  918. }
  919. info.Budget = budget
  920. info.Bidamount = bidamount
  921. return p1
  922. }
  923. //计算预算(budget)、中标金额(bidamount)
  924. func CountAmount(project *ProjectInfo, info *Info) {
  925. //if project!= nil && project.Package != nil && len(project.Package) > 0 {
  926. ////暂时未考虑太多情况,简单处理
  927. //}
  928. if info.Budget > 0 {
  929. //项目中第一条招标信息是否是包/段项目
  930. key := titleGetPc.FindStringSubmatch(project.ProjectName)
  931. if len(key) > 0 {
  932. //判断项目中是否已经计算过该包/段的预算
  933. if !isCount(project, info.ProjectName) {
  934. project.Budget = project.Budget + info.Budget
  935. }
  936. }else {
  937. if project.Budget < info.Budget {
  938. project.Budget = info.Budget
  939. }
  940. }
  941. }
  942. //中标、成交、合同 处理中标金额(bidamount)
  943. if info.SubType == "中标" || info.SubType == "成交" || info.SubType == "合同" {
  944. if info.Bidamount > 0 {
  945. key := titleGetPc.FindStringSubmatch(project.ProjectName)
  946. if len(key) > 0 {
  947. if !isCount(project, info.ProjectName) {
  948. project.Bidamount = project.Bidamount + info.Bidamount
  949. }
  950. }else {
  951. if project.Bidamount < project.Bidamount {
  952. project.Bidamount = info.Bidamount
  953. }
  954. }
  955. }
  956. }
  957. //保存信息到IdStatusInfo
  958. project.IdStatusInfo[info.Id] = map[string]interface{}{
  959. "projectname": info.ProjectName,
  960. "bidstatus": info.SubType,
  961. "budget": info.Budget,
  962. "bidamount": info.Bidamount,
  963. }
  964. }
  965. func isCount(project *ProjectInfo, infoName string) bool {
  966. if project.IdStatusInfo != nil && len(project.IdStatusInfo) > 0 {
  967. for _, v := range project.IdStatusInfo{
  968. if v["projectname"] == infoName {
  969. return true
  970. }
  971. }
  972. }
  973. return false
  974. }