project.go 25 KB

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