project.go 25 KB

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