project.go 29 KB

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