project.go 27 KB

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