project.go 31 KB

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