project.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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. log.Println("pid", findPid)
  194. choose, ex := CompareStatus(resN[0], info)
  195. if !choose {
  196. p.UpdateProject(tmp, info, resN[0], kv+1, resN[0].comStr, ex)
  197. }else {
  198. bFindProject = false
  199. }
  200. break
  201. }
  202. }
  203. if !bFindProject {
  204. id, p1 := p.NewProject(tmp, info)
  205. p.AllIdsMapLock.Lock()
  206. p.AllIdsMap[id] = &ID{Id: id, P: p1}
  207. p.AllIdsMapLock.Unlock()
  208. for _, m := range pids {
  209. m.Arr = append(m.Arr, id)
  210. }
  211. }
  212. }
  213. func (p *ProjectTask) compareBCTABB(info *Info, cp *ProjectInfo, diffTime int64, score int) (compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount string, score2 int) {
  214. compareBuyer = "D"
  215. if len([]rune(info.Buyer)) > 3 && len([]rune(cp.Buyer)) > 3 {
  216. v := CheckContain(info.Buyer, cp.Buyer)
  217. if v == 1 {
  218. compareBuyer = "A"
  219. score += 3
  220. } else {
  221. //v1 := CosineSimilar(info.Buyer, cp.Buyer)
  222. if v == 2 {
  223. compareBuyer = "B"
  224. score += 1
  225. } else {
  226. compareBuyer = "C"
  227. }
  228. }
  229. }
  230. //---------------------------------------
  231. compareCity = ""
  232. if info.Area != "全国" && info.Area != "" && info.Area == cp.Area {
  233. compareCity += "A"
  234. score += 2
  235. } else if info.Area == "全国" || cp.Area == "全国" {
  236. compareCity += "B"
  237. score += 1
  238. } else {
  239. compareCity += "C"
  240. }
  241. if compareCity != "C" {
  242. if info.City != "" && info.City == cp.City {
  243. compareCity += "A"
  244. score += 2
  245. } else {
  246. if info.Area == "全国" || cp.Area == "全国" {
  247. compareCity += "B"
  248. } else if info.City == compareCity {
  249. compareCity += "B"
  250. } else {
  251. compareCity += "C"
  252. }
  253. }
  254. } else {
  255. compareCity += "C"
  256. }
  257. score2 = 0
  258. if compareCity == "AA" {
  259. if info.District != "" && info.District == cp.District {
  260. score2 = 1
  261. }
  262. }
  263. compareTime = "D"
  264. if diffTime < 45*86400 {
  265. compareTime = "A"
  266. score += 2
  267. } else if diffTime < 90*86400 {
  268. compareTime = "B"
  269. score += 1
  270. }
  271. compareAgency = "D"
  272. if info.Agency != "" {
  273. if info.Agency == cp.Agency {
  274. compareAgency = "A"
  275. score += 2
  276. score2 += 1
  277. } else if cp.Agency != "" {
  278. if strings.Contains(info.Agency, cp.Agency) || strings.Contains(cp.Agency, info.Agency) {
  279. compareAgency = "B"
  280. score += 1
  281. score2 += 1
  282. } else {
  283. compareAgency = "C"
  284. }
  285. }
  286. }
  287. compareBudget = "C"
  288. if info.Budget > 0 && (info.Budget == cp.Budget || (cp.Bidamount > 0 && info.Budget > cp.Bidamount && (info.Budget-cp.Bidamount) < (0.15*info.Budget))) {
  289. compareBudget = "A"
  290. score += 1
  291. score2 += 1
  292. }
  293. // else if info.Budget == 0 && cp.Budget == 0 {
  294. // compareBudget = "B"
  295. // }
  296. compareBidmount = "C"
  297. if info.Bidamount > 0 && (info.Bidamount == cp.Bidamount || (cp.Budget > 0 && cp.Budget > info.Bidamount && (cp.Budget-info.Bidamount) < 0.15*cp.Budget)) {
  298. compareBidmount = "A"
  299. score += 1
  300. score2 += 1
  301. }
  302. // else if info.Bidamount == 0 && cp.Bidamount == 0 {
  303. // compareBidmount = "B"
  304. // }
  305. cp.score = score
  306. return
  307. }
  308. func compareResult(resVal, pjVal, score2 int, comStr, compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount string) int {
  309. eqV := 0
  310. switch resVal {
  311. case 3:
  312. if pjVal == 3 && comStr[3:] != "CCCDCCC" {
  313. eqV = 1
  314. } else if compareBuyer < "C" {
  315. if pjVal > 1 {
  316. eqV = 1
  317. } else { //if (compareCity[1:1] != "C" || compareTime != "D") && score2 > 0
  318. eqV = 2
  319. }
  320. } else if compareBuyer == "D" {
  321. if pjVal > 1 && (compareCity[1:1] != "C" || score2 > 0) {
  322. eqV = 2
  323. } else if compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  324. eqV = 3
  325. }
  326. } else {
  327. if pjVal == 3 && (score2 > 0 || compareCity[1:1] != "C") {
  328. eqV = 2
  329. } else if pjVal == 2 && compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  330. eqV = 3
  331. } else if compareCity == "AA" && compareTime == "A" && score2 > 0 {
  332. eqV = 3
  333. }
  334. }
  335. case 2:
  336. if compareBuyer < "C" {
  337. if pjVal > 1 {
  338. eqV = 2
  339. } else if compareCity[1:1] != "C" && compareTime == "A" || score2 > 0 {
  340. eqV = 3
  341. }
  342. } else if compareBuyer == "D" {
  343. if pjVal > 1 && (score2 > 0 || compareCity[1:1] != "C") {
  344. eqV = 2
  345. } else if compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  346. eqV = 3
  347. }
  348. } else {
  349. if pjVal > 1 && compareTime == "A" && (score2 > 0 || compareCity[1:1] != "C") {
  350. eqV = 2
  351. } else if compareCity[1:1] != "C" && compareTime == "A" && (compareAgency == "A" || score2 > 0) && (compareBudget == "A" || compareBidmount == "A") {
  352. eqV = 3
  353. }
  354. }
  355. case 1:
  356. if compareBuyer < "C" {
  357. if pjVal > 1 && (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. } else if compareBuyer == "D" {
  363. if pjVal > 1 && compareTime == "A" && (score2 > 0 || compareCity[1:1] != "C") {
  364. eqV = 2
  365. } else if compareCity[1:1] != "C" && compareTime == "A" && (compareAgency == "A" || score2 > 0) && (compareBudget == "A" || compareBidmount == "A") {
  366. eqV = 3
  367. }
  368. } else {
  369. if pjVal > 1 && compareTime == "A" && score2 > 0 && (compareBudget == "A" || compareBidmount == "A") && compareCity[1:1] != "C" {
  370. eqV = 3
  371. }
  372. }
  373. }
  374. return eqV
  375. }
  376. var FIELDS = []string{
  377. "area",
  378. "city",
  379. "district",
  380. "projectname",
  381. "projectcode",
  382. "buyer",
  383. "buyerclass",
  384. "buyerperson",
  385. "buyertel",
  386. "winner",
  387. //"budget",
  388. //"bidamount",
  389. //"bidstatus",
  390. "agency",
  391. "projectscope",
  392. "topscopeclass",
  393. "subscopeclass",
  394. "winnerorder",
  395. "package",
  396. }
  397. var bidtype = map[string]string{
  398. "招标": "招标",
  399. "询价": "询价",
  400. "竞谈": "竞谈",
  401. "单一": "单一",
  402. "竞价": "竞价",
  403. "变更": "变更",
  404. "邀标": "邀标",
  405. }
  406. var bidstatus = map[string]string{
  407. "预告": "预告",
  408. "中标": "中标",
  409. "成交": "成交",
  410. "废标": "废标",
  411. "流标": "流标",
  412. "合同": "合同",
  413. }
  414. //招标时间zbtime、中标时间jgtime、项目状态bidstatus、招标类型bidtype、最后发布时间lasttime、首次发布时间firsttime
  415. func (p *ProjectTask) NewProject(tmp map[string]interface{}, thisinfo *Info) (string, *ProjectInfo) {
  416. pId := primitive.NewObjectID() //NewObjectId()
  417. set := map[string]interface{}{}
  418. set["_id"] = pId
  419. for _, f := range FIELDS {
  420. if tmp[f] != nil {
  421. set[f] = tmp[f]
  422. }
  423. }
  424. if tmp["budget"] != nil {
  425. set["budget"] = thisinfo.Budget
  426. }
  427. if tmp["bidamount"] != nil {
  428. set["bidamount"] = thisinfo.Bidamount
  429. }
  430. bidopentime := qu.Int64All(tmp["bidopentime"])
  431. if bidopentime > 0 {
  432. set["bidopentime"] = bidopentime
  433. }
  434. if thisinfo.ProjectName != "" {
  435. set["s_projectname"] = tmp["projectname"] //兼容老版本
  436. }
  437. now := time.Now().Unix()
  438. set["createtime"] = now
  439. set["sourceinfoid"] = thisinfo.Id
  440. set["sourceinfourl"] = tmp["href"]
  441. set["firsttime"] = tmp["publishtime"]
  442. set["lasttime"] = tmp["publishtime"]
  443. set["pici"] = p.pici
  444. set["ids"] = []string{thisinfo.Id}
  445. if thisinfo.TopType == "招标" {
  446. set["zbtime"] = tmp["publishtime"]
  447. } else if thisinfo.TopType == "结果" {
  448. set["jgtime"] = tmp["publishtime"]
  449. }
  450. //异常标记
  451. if thisinfo.TopType != "招标" && thisinfo.TopType != "拟建" && thisinfo.TopType != "预告" {
  452. set["exception"] = 1
  453. }
  454. //projecthref保存
  455. if jsonData, ok := tmp["jsondata"].(map[string]interface{}); ok {
  456. if jsonData != nil && jsonData["projecthref"] != "" {
  457. set["projecthref"] = jsonData["projecthref"]
  458. }
  459. }
  460. //招标类型
  461. bt := bidtype[thisinfo.SubType]
  462. if bt == "" {
  463. bt = "招标"
  464. }
  465. set["bidtype"] = bt
  466. bs, _ := tmp["bidstatus"].(string)
  467. if bidstatus[bs] != "" {
  468. set["bidstatus"] = bs
  469. }
  470. if set["bidstatus"] == nil && thisinfo.TopType == "结果" {
  471. bs = thisinfo.SubType
  472. set["bidstatus"] = thisinfo.SubType
  473. }
  474. p1, pkg := p.NewCachePinfo(pId, thisinfo, bs, 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, bidstatus, 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: bidstatus,
  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. return true, 0
  778. }else if project.Bidstatus == "成交" {
  779. if info.SubType == "中标" {
  780. return true, 0
  781. }else {
  782. return false, 0
  783. }
  784. }else if project.Bidstatus == "流标" || project.Bidstatus == "废标" {
  785. return false, 2
  786. }else if project.Bidstatus == "合同" || project.Bidstatus == "其它" {
  787. return false, 3
  788. }else {
  789. return false, 0
  790. }
  791. }else {
  792. return false, 0
  793. }
  794. }
  795. /*
  796. * 对比地区(省、市、区),存在且不同,不能合并
  797. * 返回是否新建项目
  798. */
  799. func ComparePlace(project *ProjectInfo, info *Info) bool {
  800. if info.Area == "全国" || info.Area == "" {
  801. return false
  802. }
  803. if project.Area == "全国" || project.Area == "" {
  804. return false
  805. }
  806. if info.Area == project.Area {
  807. if info.City == "" {
  808. return false
  809. }else if info.City == project.City {
  810. if info.District == "" || info.District == project.District {
  811. return false
  812. }else {
  813. return true
  814. }
  815. }else {
  816. return true
  817. }
  818. }else {
  819. return true
  820. }
  821. }
  822. var PackageEle = []string{
  823. "origin",
  824. "name",
  825. "text",
  826. "budget",
  827. "winner",
  828. "bidamount",
  829. "bidamounttype",
  830. "currency",
  831. "bidstatus",
  832. }
  833. func packageEle(map1 map[string]interface{}) (map[string]interface{}, float64, float64) {
  834. budget := 0.0
  835. bidamount := 0.0
  836. p2 := map[string]interface{}{}
  837. for _, k := range PackageEle{
  838. if map1[k] != nil {
  839. p2[k] = map1[k]
  840. }
  841. if p2["budget"] != nil {
  842. budget = budget + p2["budget"].(float64)
  843. }
  844. if p2["bidamount"] != nil {
  845. bidamount = bidamount + p2["bidamount"].(float64)
  846. }
  847. }
  848. return p2, budget, bidamount
  849. }
  850. func PackageFormat(info *Info, project *ProjectInfo) (map[string]interface{}, float64, float64) {
  851. budget := 0.0
  852. bidamount := 0.0
  853. p1 := map[string]interface{}{}
  854. if project != nil && project.Package != nil && len(project.Package) > 0 {
  855. budget = project.Budget
  856. bidamount = project.Bidamount
  857. p1 = project.Package
  858. ids := p1["ids"].([]string)
  859. ids = append(ids, info.Id)
  860. p1["ids"] = ids
  861. for _, v := range info.Package {
  862. if v1, ok := v.(map[string]interface{}); ok {
  863. v2 := map[string]interface{}{}
  864. v2, budget, bidamount = packageEle(v1)
  865. if v2["bidstatus"] == nil {
  866. v2["bidstatus"] = info.SubType
  867. }
  868. n := 0
  869. addFlag := false
  870. for k, v3 := range p1 {
  871. if k == "ids" {
  872. continue
  873. }
  874. if v4, ok := v3.([]map[string]interface{}); ok {
  875. if v4[0]["origin"].(string) == v2["origin"].(string) && v4[0]["name"].(string) == v2["name"].(string) {
  876. v4 = append(v4, v2)
  877. p1[k] = v4
  878. addFlag = true
  879. break
  880. }
  881. }
  882. }
  883. if !addFlag {
  884. n++
  885. m := len(ids) + n
  886. p1[strconv.Itoa(m)] = []map[string]interface{}{v2}
  887. }
  888. }
  889. }
  890. }else {
  891. p1["ids"] = []string{info.Id}
  892. for k, v := range info.Package {
  893. v1 := v.(map[string]interface{})
  894. p2 := map[string]interface{}{}
  895. p2, budget, bidamount = packageEle(v1)
  896. if p2["bidstatus"] == nil {
  897. p2["bidstatus"] = info.SubType
  898. }
  899. p1[k] = []map[string]interface{}{p2}
  900. }
  901. }
  902. return p1, budget, bidamount
  903. }