project.go 29 KB

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