project.go 31 KB

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