project.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 := CompareStatus(resN[0], info)
  164. if !choose {
  165. ex = e
  166. resArr = append(resArr, res)
  167. }
  168. }
  169. if len(resArr) > 0 {
  170. bFindProject = true
  171. findPid = resArr[0].Id.Hex()
  172. for k2, bv := range []int{bpn, bpc, bptc, bpb} {
  173. if bv > -1 {
  174. pids[bv].Arr = append(pids[bv].Arr, findPid)
  175. if k2 == 0 {
  176. if resArr[0].ProjectName == "" {
  177. resArr[0].ProjectName = info.ProjectName
  178. } else {
  179. if resArr[0].MPN == nil {
  180. resArr[0].MPN = []string{info.ProjectName}
  181. } else {
  182. resArr[0].MPN = append(resArr[0].MPN, info.ProjectName)
  183. }
  184. }
  185. } else if k2 < 3 {
  186. if resArr[0].ProjectCode == "" {
  187. resArr[0].ProjectCode = qu.If(k2 == 1, info.ProjectCode, info.PTC).(string)
  188. } else {
  189. if resArr[0].MPC == nil {
  190. resArr[0].MPC = []string{qu.If(k2 == 1, info.ProjectCode, info.PTC).(string)}
  191. } else {
  192. resArr[0].MPC = append(resArr[0].MPC, qu.If(k2 == 1, info.ProjectCode, info.PTC).(string))
  193. }
  194. }
  195. } else {
  196. if resArr[0].Buyer == "" {
  197. resArr[0].Buyer = info.Buyer
  198. }
  199. }
  200. }
  201. }
  202. p.UpdateProject(tmp, info, resArr[0], kv+1, resArr[0].comStr, ex)
  203. } else {
  204. bFindProject = false
  205. findPid = ""
  206. }
  207. break
  208. }
  209. }
  210. if !bFindProject {
  211. id, p1 := p.NewProject(tmp, info)
  212. p.AllIdsMapLock.Lock()
  213. p.AllIdsMap[id] = &ID{Id: id, P: p1}
  214. p.AllIdsMapLock.Unlock()
  215. for _, m := range pids {
  216. m.Arr = append(m.Arr, id)
  217. }
  218. }
  219. }
  220. func (p *ProjectTask) compareBCTABB(info *Info, cp *ProjectInfo, diffTime int64, score int) (compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount string, score2 int) {
  221. compareBuyer = "D"
  222. if len([]rune(info.Buyer)) > 3 && len([]rune(cp.Buyer)) > 3 {
  223. v := CheckContain(info.Buyer, cp.Buyer)
  224. if v == 1 {
  225. compareBuyer = "A"
  226. score += 3
  227. } else {
  228. //v1 := CosineSimilar(info.Buyer, cp.Buyer)
  229. if v == 2 {
  230. compareBuyer = "B"
  231. score += 1
  232. } else {
  233. compareBuyer = "C"
  234. }
  235. }
  236. }
  237. //---------------------------------------
  238. compareCity = ""
  239. if info.Area != "全国" && info.Area != "" && info.Area == cp.Area {
  240. compareCity += "A"
  241. score += 2
  242. } else if info.Area == "全国" || cp.Area == "全国" {
  243. compareCity += "B"
  244. score += 1
  245. } else {
  246. compareCity += "C"
  247. }
  248. if compareCity != "C" {
  249. if info.City != "" && info.City == cp.City {
  250. compareCity += "A"
  251. score += 2
  252. } else {
  253. if info.Area == "全国" || cp.Area == "全国" {
  254. compareCity += "B"
  255. } else if info.City == compareCity {
  256. compareCity += "B"
  257. } else {
  258. compareCity += "C"
  259. }
  260. }
  261. } else {
  262. compareCity += "C"
  263. }
  264. score2 = 0
  265. if compareCity == "AA" {
  266. if info.District != "" && info.District == cp.District {
  267. score2 = 1
  268. }
  269. }
  270. compareTime = "D"
  271. if diffTime < 45*86400 {
  272. compareTime = "A"
  273. score += 2
  274. } else if diffTime < 90*86400 {
  275. compareTime = "B"
  276. score += 1
  277. }
  278. compareAgency = "D"
  279. if info.Agency != "" {
  280. if info.Agency == cp.Agency {
  281. compareAgency = "A"
  282. score += 2
  283. score2 += 1
  284. } else if cp.Agency != "" {
  285. if strings.Contains(info.Agency, cp.Agency) || strings.Contains(cp.Agency, info.Agency) {
  286. compareAgency = "B"
  287. score += 1
  288. score2 += 1
  289. } else {
  290. compareAgency = "C"
  291. }
  292. }
  293. }
  294. compareBudget = "C"
  295. if info.Budget > 0 && (info.Budget == cp.Budget || (cp.Bidamount > 0 && info.Budget > cp.Bidamount && (info.Budget-cp.Bidamount) < (0.15*info.Budget))) {
  296. compareBudget = "A"
  297. score += 1
  298. score2 += 1
  299. }
  300. // else if info.Budget == 0 && cp.Budget == 0 {
  301. // compareBudget = "B"
  302. // }
  303. compareBidmount = "C"
  304. if info.Bidamount > 0 && (info.Bidamount == cp.Bidamount || (cp.Budget > 0 && cp.Budget > info.Bidamount && (cp.Budget-info.Bidamount) < 0.15*cp.Budget)) {
  305. compareBidmount = "A"
  306. score += 1
  307. score2 += 1
  308. }
  309. // else if info.Bidamount == 0 && cp.Bidamount == 0 {
  310. // compareBidmount = "B"
  311. // }
  312. cp.score = score
  313. return
  314. }
  315. func compareResult(resVal, pjVal, score2 int, comStr, compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount string) int {
  316. eqV := 0
  317. switch resVal {
  318. case 3:
  319. if pjVal == 3 && comStr[3:] != "CCCDCCC" {
  320. eqV = 1
  321. } else if compareBuyer < "C" {
  322. if pjVal > 1 {
  323. eqV = 1
  324. } else { //if (compareCity[1:1] != "C" || compareTime != "D") && score2 > 0
  325. eqV = 2
  326. }
  327. } else if compareBuyer == "D" {
  328. if pjVal > 1 && (compareCity[1:1] != "C" || score2 > 0) {
  329. eqV = 2
  330. } else if compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  331. eqV = 3
  332. }
  333. } else {
  334. if pjVal == 3 && (score2 > 0 || compareCity[1:1] != "C") {
  335. eqV = 2
  336. } else if pjVal == 2 && compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  337. eqV = 3
  338. } else if compareCity == "AA" && compareTime == "A" && score2 > 0 {
  339. eqV = 3
  340. }
  341. }
  342. case 2:
  343. if compareBuyer < "C" {
  344. if pjVal > 1 {
  345. eqV = 2
  346. } else if compareCity[1:1] != "C" && compareTime == "A" || score2 > 0 {
  347. eqV = 3
  348. }
  349. } else if compareBuyer == "D" {
  350. if pjVal > 1 && (score2 > 0 || compareCity[1:1] != "C") {
  351. eqV = 2
  352. } else if compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  353. eqV = 3
  354. }
  355. } else {
  356. if pjVal > 1 && compareTime == "A" && (score2 > 0 || compareCity[1:1] != "C") {
  357. eqV = 2
  358. } else if compareCity[1:1] != "C" && compareTime == "A" && (compareAgency == "A" || score2 > 0) && (compareBudget == "A" || compareBidmount == "A") {
  359. eqV = 3
  360. }
  361. }
  362. case 1:
  363. if compareBuyer < "C" {
  364. if pjVal > 1 && (score2 > 0 || compareCity[1:1] != "C") {
  365. eqV = 2
  366. } else if compareCity[1:1] != "C" && compareTime == "A" && (compareAgency == "A" || score2 > 0) && (compareBudget == "A" || compareBidmount == "A") {
  367. eqV = 3
  368. }
  369. } else if compareBuyer == "D" {
  370. if pjVal > 1 && compareTime == "A" && (score2 > 0 || compareCity[1:1] != "C") {
  371. eqV = 2
  372. } else if compareCity[1:1] != "C" && compareTime == "A" && (compareAgency == "A" || score2 > 0) && (compareBudget == "A" || compareBidmount == "A") {
  373. eqV = 3
  374. }
  375. } else {
  376. if pjVal > 1 && compareTime == "A" && score2 > 0 && (compareBudget == "A" || compareBidmount == "A") && compareCity[1:1] != "C" {
  377. eqV = 3
  378. }
  379. }
  380. }
  381. return eqV
  382. }
  383. var FIELDS = []string{
  384. "area",
  385. "city",
  386. "district",
  387. "projectname",
  388. "projectcode",
  389. "buyer",
  390. "buyerclass",
  391. "buyerperson",
  392. "buyertel",
  393. "winner",
  394. //"budget",
  395. //"bidamount",
  396. //"bidstatus",
  397. "agency",
  398. "projectscope",
  399. "topscopeclass",
  400. "subscopeclass",
  401. "winnerorder",
  402. "package",
  403. }
  404. var bidtype = map[string]string{
  405. "招标": "招标",
  406. "询价": "询价",
  407. "竞谈": "竞谈",
  408. "单一": "单一",
  409. "竞价": "竞价",
  410. "变更": "变更",
  411. "邀标": "邀标",
  412. }
  413. var bidstatus = map[string]string{
  414. "预告": "预告",
  415. "中标": "中标",
  416. "成交": "成交",
  417. "废标": "废标",
  418. "流标": "流标",
  419. "合同": "合同",
  420. }
  421. //招标时间zbtime、中标时间jgtime、项目状态bidstatus、招标类型bidtype、最后发布时间lasttime、首次发布时间firsttime
  422. func (p *ProjectTask) NewProject(tmp map[string]interface{}, thisinfo *Info) (string, *ProjectInfo) {
  423. pId := primitive.NewObjectID() //NewObjectId()
  424. set := map[string]interface{}{}
  425. set["_id"] = pId
  426. for _, f := range FIELDS {
  427. if tmp[f] != nil {
  428. set[f] = tmp[f]
  429. }
  430. }
  431. if tmp["budget"] != nil {
  432. set["budget"] = thisinfo.Budget
  433. }
  434. if tmp["bidamount"] != nil {
  435. set["bidamount"] = thisinfo.Bidamount
  436. }
  437. bidopentime := qu.Int64All(tmp["bidopentime"])
  438. if bidopentime > 0 {
  439. set["bidopentime"] = bidopentime
  440. }
  441. if thisinfo.ProjectName != "" {
  442. set["s_projectname"] = tmp["projectname"] //兼容老版本
  443. }
  444. now := time.Now().Unix()
  445. set["createtime"] = now
  446. set["sourceinfoid"] = thisinfo.Id
  447. set["sourceinfourl"] = tmp["href"]
  448. set["firsttime"] = tmp["publishtime"]
  449. set["lasttime"] = tmp["publishtime"]
  450. set["pici"] = p.pici
  451. set["ids"] = []string{thisinfo.Id}
  452. if thisinfo.TopType == "招标" {
  453. set["zbtime"] = tmp["publishtime"]
  454. } else if thisinfo.TopType == "结果" {
  455. set["jgtime"] = tmp["publishtime"]
  456. }
  457. //异常标记
  458. if thisinfo.TopType != "招标" && thisinfo.TopType != "拟建" && thisinfo.TopType != "预告" {
  459. set["exception"] = 1
  460. }
  461. //projecthref保存
  462. if jsonData, ok := tmp["jsondata"].(map[string]interface{}); ok {
  463. if jsonData != nil && jsonData["projecthref"] != "" {
  464. set["projecthref"] = jsonData["projecthref"]
  465. }
  466. }
  467. //招标类型
  468. bt := bidtype[thisinfo.SubType]
  469. if bt == "" {
  470. bt = "招标"
  471. }
  472. set["bidtype"] = bt
  473. set["bidstatus"] = thisinfo.SubType
  474. p1, pkg := p.NewCachePinfo(pId, thisinfo, bt)
  475. if len(thisinfo.Subscopeclass) > 0 {
  476. s_subscopeclass := strings.Join(thisinfo.Subscopeclass, ",")
  477. set["s_subscopeclass"] = s_subscopeclass
  478. }
  479. if len(thisinfo.Winners) > 0 {
  480. set["s_winner"] = strings.Join(thisinfo.Winners, ",")
  481. p1.Winners = thisinfo.Winners
  482. }
  483. if thisinfo.HasPackage {
  484. set["multipackage"] = 1
  485. set["package"] = pkg
  486. } else {
  487. set["multipackage"] = 0
  488. }
  489. push := p.PushListInfo(tmp, thisinfo.Id)
  490. set["list"] = []bson.M{
  491. push,
  492. }
  493. //p.savePool <- set
  494. p.updatePool <- []map[string]interface{}{
  495. {
  496. "_id": pId,
  497. },
  498. {
  499. "$set": set,
  500. },
  501. }
  502. return pId.Hex(), &p1
  503. }
  504. var INFOFIELDS = []string{
  505. "projectname",
  506. "projectcode",
  507. "title",
  508. "href",
  509. "publishtime",
  510. "comeintime",
  511. "bidopentime",
  512. "toptype",
  513. "subtype",
  514. "buyer",
  515. "buyerclass",
  516. "agency",
  517. "winner",
  518. "budget",
  519. "bidamount",
  520. "topscopeclass",
  521. "subscopclass",
  522. "infoformat",
  523. "buyerperson",
  524. "buyertel",
  525. "area",
  526. "city",
  527. "list",
  528. }
  529. //项目中list的信息
  530. func (p *ProjectTask) PushListInfo(tmp map[string]interface{}, infoid string) bson.M {
  531. res := bson.M{
  532. "infoid": infoid,
  533. }
  534. for _, k := range INFOFIELDS {
  535. if tmp[k] != nil {
  536. res[k] = tmp[k]
  537. }
  538. }
  539. return res
  540. }
  541. //生成存放在内存中的对象
  542. func (p *ProjectTask) NewCachePinfo(id primitive.ObjectID, thisinfo *Info, bidtype string) (ProjectInfo, map[string]interface{}) {
  543. pkg := map[string]interface{}{}
  544. if thisinfo.HasPackage {
  545. pkg, _, _ = PackageFormat(thisinfo, nil)
  546. }
  547. p1 := ProjectInfo{
  548. Id: id,
  549. Ids: []string{thisinfo.Id},
  550. ProjectName: thisinfo.ProjectName,
  551. ProjectCode: thisinfo.ProjectCode,
  552. Buyer: thisinfo.Buyer,
  553. Buyerclass: thisinfo.Buyerclass,
  554. Buyerperson: thisinfo.Buyerperson,
  555. Buyertel: thisinfo.Buyertel,
  556. Topscopeclass: thisinfo.Topscopeclass,
  557. Subscopeclass: thisinfo.Subscopeclass,
  558. Agency: thisinfo.Agency,
  559. Area: thisinfo.Area,
  560. City: thisinfo.City,
  561. District: thisinfo.District,
  562. MPN: []string{},
  563. MPC: []string{},
  564. FirstTime: thisinfo.Publishtime,
  565. LastTime: thisinfo.Publishtime,
  566. Budget: thisinfo.Budget,
  567. Package: pkg,
  568. Bidamount: thisinfo.Bidamount,
  569. Bidstatus: thisinfo.SubType,
  570. Bidtype: bidtype,
  571. }
  572. if thisinfo.LenPTC > 5 {
  573. p1.MPC = append(p1.MPC, thisinfo.PTC)
  574. }
  575. return p1, pkg
  576. }
  577. //更新项目
  578. func (p *ProjectTask) UpdateProject(tmp map[string]interface{}, thisinfo *Info, pInfo *ProjectInfo, weight int, comStr string, ex int) {
  579. if p.currentType != "ql" && p.currentType != "updateInfo" {
  580. if BinarySearch(pInfo.Ids, thisinfo.Id) > -1 {
  581. log.Println("repeat", thisinfo.Id)
  582. return
  583. }
  584. }
  585. set := map[string]interface{}{}
  586. pInfo.Ids = append(pInfo.Ids, thisinfo.Id)
  587. //1--firsttime
  588. if thisinfo.Publishtime < pInfo.FirstTime && thisinfo.Publishtime > 0 {
  589. pInfo.FirstTime = thisinfo.Publishtime
  590. set["firsttime"] = thisinfo.Publishtime
  591. if thisinfo.TopType == "招标" {
  592. set["zbtime"] = tmp["publishtime"]
  593. }
  594. }
  595. //2--lasttime
  596. if thisinfo.Publishtime > pInfo.LastTime {
  597. pInfo.LastTime = thisinfo.Publishtime
  598. set["lasttime"] = thisinfo.Publishtime
  599. bt := bidtype[thisinfo.SubType]
  600. if bt != "" {
  601. set["bidtype"] = bt
  602. }
  603. bs, _ := tmp["subtype"].(string)
  604. if bidstatus[bs] != "" {
  605. set["bidstatus"] = thisinfo.SubType
  606. if bidstatus[bs] != "预告" && bidstatus[bs] != "合同" {
  607. set["jgtime"] = tmp["publishtime"]
  608. }
  609. } else if tmp["infoformat"] == 2 {
  610. set["bidstatus"] = "拟建"
  611. } else if tmp["subytpe"] == "招标" {
  612. set["bidstatus"] = thisinfo.TopType
  613. } else {
  614. set["bidstatus"] = "其它"
  615. }
  616. }
  617. //异常标记
  618. if ex > 0 {
  619. set["exception"] = ex
  620. }
  621. //3\4\5--省、市、县
  622. if thisinfo.Area != "全国" {
  623. if pInfo.Area == "全国" {
  624. pInfo.Area = thisinfo.Area
  625. set["area"] = thisinfo.Area
  626. } else if pInfo.Area != thisinfo.Area {
  627. //xt = false
  628. }
  629. if pInfo.City == "" && thisinfo.City != "" {
  630. pInfo.City = thisinfo.City
  631. set["city"] = thisinfo.City
  632. } else if pInfo.City != thisinfo.City {
  633. //xt = false
  634. }
  635. if thisinfo.District != "" && pInfo.District == "" {
  636. pInfo.District = thisinfo.District
  637. set["district"] = thisinfo.District
  638. }
  639. }
  640. //6--项目名称
  641. if (thisinfo.ProjectName != "" && pInfo.ProjectName == "") || (len([]rune(pInfo.ProjectName)) < 6 && thisinfo.LenPN > 6) {
  642. pInfo.ProjectName = thisinfo.ProjectName
  643. set["projectname"] = thisinfo.ProjectName
  644. }
  645. //7--项目编号
  646. if (pInfo.ProjectCode == "" && thisinfo.ProjectCode != "") || (len([]rune(pInfo.ProjectCode)) < 6 && len([]rune(thisinfo.ProjectCode)) > 6) {
  647. pInfo.ProjectCode = thisinfo.ProjectCode
  648. set["projectcode"] = thisinfo.ProjectCode
  649. }
  650. //7--采购单位
  651. if (pInfo.Buyer == "" && thisinfo.Buyer != "") || (len([]rune(pInfo.Buyer)) < 5 && len([]rune(thisinfo.Buyer)) > 5) {
  652. pInfo.Buyer = thisinfo.Buyer
  653. set["buyer"] = thisinfo.Buyer
  654. pInfo.Buyerclass = thisinfo.Buyerclass
  655. set["buyerclass"] = thisinfo.Buyerclass
  656. }
  657. if pInfo.Buyer == "" {
  658. set["buyerclass"] = ""
  659. }
  660. //if thisinfo.Buyerclass != "" && pInfo.Buyerclass == "" {
  661. // pInfo.Buyerclass = thisinfo.Buyerclass
  662. // set["buyerclass"] = pInfo.Buyerclass
  663. //}
  664. //8--代理机构
  665. if (pInfo.Agency == "" && thisinfo.Agency != "") || (len([]rune(pInfo.Agency)) < 5 && len([]rune(thisinfo.Agency)) > 5) {
  666. pInfo.Agency = thisinfo.Agency
  667. set["agency"] = thisinfo.Agency
  668. }
  669. //9--采购单位联系人
  670. if thisinfo.Buyerperson != "" && strings.Index(pInfo.Buyerperson, thisinfo.Buyerperson) < 0 {
  671. pInfo.Buyerperson = thisinfo.Buyerperson
  672. set["buyerperson"] = pInfo.Buyerperson
  673. }
  674. //10--采购单位電話
  675. if thisinfo.Buyertel != "" && strings.Index(pInfo.Buyertel, thisinfo.Buyertel) < 0 {
  676. pInfo.Buyertel = thisinfo.Buyertel
  677. set["buyertel"] = pInfo.Buyertel
  678. }
  679. if thisinfo.Bidopentime > pInfo.Bidopentime {
  680. pInfo.Bidopentime = thisinfo.Bidopentime
  681. set["bidopentime"] = pInfo.Bidopentime
  682. }
  683. if thisinfo.Bidamount > 0 {
  684. pInfo.Bidamount = pInfo.Bidamount + thisinfo.Bidamount
  685. set["bidamount"] = pInfo.Bidamount
  686. }
  687. if thisinfo.Budget > 0 {
  688. pInfo.Budget = pInfo.Budget + thisinfo.Budget
  689. set["budget"] = pInfo.Budget
  690. }
  691. if len(thisinfo.Topscopeclass) > 0 {
  692. sort.Strings(pInfo.Topscopeclass)
  693. for _, k := range thisinfo.Topscopeclass {
  694. if BinarySearch(pInfo.Topscopeclass, k) == -1 {
  695. pInfo.Topscopeclass = append(pInfo.Topscopeclass, k)
  696. sort.Strings(pInfo.Topscopeclass)
  697. }
  698. }
  699. set["topscopeclass"] = pInfo.Topscopeclass
  700. }
  701. if len(thisinfo.Subscopeclass) > 0 {
  702. sort.Strings(pInfo.Subscopeclass)
  703. for _, k := range thisinfo.Subscopeclass {
  704. if BinarySearch(pInfo.Subscopeclass, k) == -1 {
  705. pInfo.Subscopeclass = append(pInfo.Subscopeclass, k)
  706. sort.Strings(pInfo.Subscopeclass)
  707. }
  708. }
  709. set["subscopeclass"] = pInfo.Subscopeclass
  710. set["s_subscopeclass"] = strings.Join(pInfo.Subscopeclass, ",")
  711. }
  712. //winner
  713. if len(thisinfo.Winners) > 0 {
  714. sort.Strings(pInfo.Winners)
  715. for _, k := range thisinfo.Winners {
  716. if BinarySearch(pInfo.Winners, k) == -1 {
  717. pInfo.Winners = append(pInfo.Winners, k)
  718. sort.Strings(pInfo.Winners)
  719. }
  720. }
  721. //set["winners"] = pInfo.Winners
  722. set["s_winner"] = strings.Join(pInfo.Winners, ",")
  723. }
  724. if thisinfo.HasPackage { //多包处理
  725. set["multipackage"] = 1
  726. pkg, _, _ := PackageFormat(thisinfo, pInfo)
  727. pInfo.Package = pkg
  728. set["package"] = pInfo.Package
  729. } else {
  730. set["multipackage"] = 0
  731. }
  732. set["mpn"] = pInfo.MPN
  733. set["mpc"] = pInfo.MPC
  734. set["pici"] = p.pici
  735. update := map[string]interface{}{}
  736. if len(set) > 0 {
  737. update["$set"] = set
  738. }
  739. //保留原数据吧
  740. push := p.PushListInfo(tmp, thisinfo.Id)
  741. push["compareStr"] = comStr
  742. push["resVal"] = pInfo.resVal
  743. push["pjVal"] = pInfo.pjVal
  744. update["$push"] = map[string]interface{}{
  745. "list": push,
  746. "ids": thisinfo.Id,
  747. }
  748. if len(update) > 0 {
  749. updateInfo := []map[string]interface{}{
  750. map[string]interface{}{
  751. "_id": pInfo.Id,
  752. },
  753. update,
  754. }
  755. p.updatePool <- updateInfo
  756. }
  757. }
  758. /**
  759. * 更新项目时,项目状态的处理
  760. * 返回是否新增项目,异常标记
  761. * 1、项目时,新项目时,招标信息的状态(toptype)不是招标、拟建、预告 异常:1
  762. * 异常1是在项目新建的时候才会产生
  763. * 3、项目合并时,项目状态是”流标“/”废标“,招标信息状态不是”招标“ 异常:2
  764. * 4、项目合并时,项目状态是”合同“/”其它“,招标信息类型是”结果“ 异常:3
  765. */
  766. func CompareStatus(project *ProjectInfo, info *Info) (bool, int) {
  767. if info.TopType == "拟建" || info.TopType == "预告" || info.TopType == "招标" {
  768. if project.Bidstatus == "拟建" || project.Bidstatus == "预告" || project.Bidstatus == "招标" {
  769. return false, 0
  770. } else {
  771. return true, 0
  772. }
  773. } else if info.TopType == "结果" {
  774. if project.Bidstatus == "拟建" || project.Bidstatus == "预告" || project.Bidstatus == "招标" {
  775. return false, 0
  776. } else if project.Bidstatus == info.SubType {
  777. key := titleGetPc.FindStringSubmatch(info.ProjectName)
  778. if key != nil {
  779. return false, 0
  780. } else {
  781. return true, 0
  782. }
  783. } else if project.Bidstatus == "成交" && info.SubType == "中标" {
  784. return true, 0
  785. } else if project.Bidstatus == "流标" || project.Bidstatus == "废标" {
  786. return false, 2
  787. } else if project.Bidstatus == "合同" || project.Bidstatus == "其它" {
  788. return false, 3
  789. } else {
  790. return false, 0
  791. }
  792. } else {
  793. return false, 0
  794. }
  795. }
  796. /*
  797. * 对比地区(省、市、区),存在且不同,不能合并
  798. * 返回是否新建项目
  799. */
  800. func ComparePlace(project *ProjectInfo, info *Info) bool {
  801. if info.Area == "全国" || info.Area == "" {
  802. return false
  803. }
  804. if project.Area == "全国" || project.Area == "" {
  805. return false
  806. }
  807. if info.Area == project.Area {
  808. if info.City == "" {
  809. return false
  810. } else if info.City == project.City {
  811. if info.District == "" || info.District == project.District {
  812. return false
  813. } else {
  814. return true
  815. }
  816. } else {
  817. return true
  818. }
  819. } else {
  820. return true
  821. }
  822. }
  823. var PackageEle = []string{
  824. "origin",
  825. "name",
  826. "text",
  827. "budget",
  828. "winner",
  829. "bidamount",
  830. "bidamounttype",
  831. "currency",
  832. "bidstatus",
  833. }
  834. func packageEle(map1 map[string]interface{}, id string) (map[string]interface{}, float64, float64) {
  835. budget := 0.0
  836. bidamount := 0.0
  837. p2 := map[string]interface{}{}
  838. for _, k := range PackageEle {
  839. if map1[k] != nil {
  840. p2[k] = map1[k]
  841. }
  842. infoid := p2["infoid"]
  843. if infoid == nil {
  844. p2["infoid"] = id
  845. }
  846. if p2["budget"] != nil {
  847. budget = budget + p2["budget"].(float64)
  848. }
  849. if p2["bidamount"] != nil {
  850. bidamount = bidamount + p2["bidamount"].(float64)
  851. }
  852. }
  853. return p2, budget, bidamount
  854. }
  855. func PackageFormat(info *Info, project *ProjectInfo) (map[string]interface{}, float64, float64) {
  856. budget := 0.0
  857. bidamount := 0.0
  858. p1 := map[string]interface{}{}
  859. if project != nil && project.Package != nil && len(project.Package) > 0 {
  860. budget = project.Budget
  861. bidamount = project.Bidamount
  862. p1 = project.Package
  863. for k, v := range info.Package {
  864. if v1, ok := v.(map[string]interface{}); ok {
  865. v2 := map[string]interface{}{}
  866. v2, budget, bidamount = packageEle(v1, info.Id)
  867. if v2["bidstatus"] == nil {
  868. v2["bidstatus"] = info.SubType
  869. }
  870. addFlag := false
  871. for k1, v3 := range p1 {
  872. if v4, ok := v3.([]map[string]interface{}); ok {
  873. if qu.ObjToString(v4[0]["origin"]) == qu.ObjToString(v2["origin"]) && qu.ObjToString(v4[0]["name"]) == qu.ObjToString(v2["name"]) {
  874. v4 = append(v4, v2)
  875. p1[k1] = v4
  876. addFlag = true
  877. break
  878. }
  879. }
  880. }
  881. if !addFlag {
  882. p1[k] = []map[string]interface{}{v2}
  883. }
  884. }
  885. }
  886. } else {
  887. for k, v := range info.Package {
  888. v1, _ := v.(map[string]interface{})
  889. p2 := map[string]interface{}{}
  890. p2, budget, bidamount = packageEle(v1, info.Id)
  891. if p2["bidstatus"] == nil {
  892. p2["bidstatus"] = info.SubType
  893. }
  894. p1[k] = []map[string]interface{}{p2}
  895. }
  896. }
  897. return p1, budget, bidamount
  898. }