project.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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 _, res := range resN {
  164. choose, e := p.CompareStatus(resN[0], info)
  165. if !choose {
  166. ex = e
  167. resArr = append(resArr, res)
  168. }
  169. }
  170. if len(resArr) > 0 {
  171. bFindProject = true
  172. findPid = resArr[0].Id.Hex()
  173. for k2, bv := range []int{bpn, bpc, bptc, bpb} {
  174. if bv > -1 {
  175. pids[bv].Arr = append(pids[bv].Arr, findPid)
  176. if k2 == 0 {
  177. if resArr[0].ProjectName == "" {
  178. resArr[0].ProjectName = info.ProjectName
  179. } else {
  180. if resArr[0].MPN == nil {
  181. resArr[0].MPN = []string{info.ProjectName}
  182. } else {
  183. resArr[0].MPN = append(resArr[0].MPN, info.ProjectName)
  184. }
  185. }
  186. } else if k2 < 3 {
  187. if resArr[0].ProjectCode == "" {
  188. resArr[0].ProjectCode = qu.If(k2 == 1, info.ProjectCode, info.PTC).(string)
  189. } else {
  190. if resArr[0].MPC == nil {
  191. resArr[0].MPC = []string{qu.If(k2 == 1, info.ProjectCode, info.PTC).(string)}
  192. } else {
  193. resArr[0].MPC = append(resArr[0].MPC, qu.If(k2 == 1, info.ProjectCode, info.PTC).(string))
  194. }
  195. }
  196. } else {
  197. if resArr[0].Buyer == "" {
  198. resArr[0].Buyer = info.Buyer
  199. }
  200. }
  201. }
  202. }
  203. p.UpdateProject(tmp, info, resArr[0], kv+1, resArr[0].comStr, ex)
  204. } else {
  205. bFindProject = false
  206. findPid = ""
  207. }
  208. break
  209. }
  210. }
  211. if !bFindProject {
  212. id, p1 := p.NewProject(tmp, info)
  213. p.AllIdsMapLock.Lock()
  214. p.AllIdsMap[id] = &ID{Id: id, P: p1}
  215. p.AllIdsMapLock.Unlock()
  216. for _, m := range pids {
  217. m.Arr = append(m.Arr, id)
  218. }
  219. }
  220. }
  221. func (p *ProjectTask) compareBCTABB(info *Info, cp *ProjectInfo, diffTime int64, score int) (compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount string, score2 int) {
  222. compareBuyer = "D"
  223. if len([]rune(info.Buyer)) > 3 && len([]rune(cp.Buyer)) > 3 {
  224. v := CheckContain(info.Buyer, cp.Buyer)
  225. if v == 1 {
  226. compareBuyer = "A"
  227. score += 3
  228. } else {
  229. //v1 := CosineSimilar(info.Buyer, cp.Buyer)
  230. if v == 2 {
  231. compareBuyer = "B"
  232. score += 1
  233. } else {
  234. compareBuyer = "C"
  235. }
  236. }
  237. }
  238. //---------------------------------------
  239. compareCity = ""
  240. if info.Area != "全国" && info.Area != "" && info.Area == cp.Area {
  241. compareCity += "A"
  242. score += 2
  243. } else if info.Area == "全国" || cp.Area == "全国" {
  244. compareCity += "B"
  245. score += 1
  246. } else {
  247. compareCity += "C"
  248. }
  249. if compareCity != "C" {
  250. if info.City != "" && info.City == cp.City {
  251. compareCity += "A"
  252. score += 2
  253. } else {
  254. if info.Area == "全国" || cp.Area == "全国" {
  255. compareCity += "B"
  256. } else if info.City == compareCity {
  257. compareCity += "B"
  258. } else {
  259. compareCity += "C"
  260. }
  261. }
  262. } else {
  263. compareCity += "C"
  264. }
  265. score2 = 0
  266. if compareCity == "AA" {
  267. if info.District != "" && info.District == cp.District {
  268. score2 = 1
  269. }
  270. }
  271. compareTime = "D"
  272. if diffTime < 45*86400 {
  273. compareTime = "A"
  274. score += 2
  275. } else if diffTime < 90*86400 {
  276. compareTime = "B"
  277. score += 1
  278. }
  279. compareAgency = "D"
  280. if info.Agency != "" {
  281. if info.Agency == cp.Agency {
  282. compareAgency = "A"
  283. score += 2
  284. score2 += 1
  285. } else if cp.Agency != "" {
  286. if strings.Contains(info.Agency, cp.Agency) || strings.Contains(cp.Agency, info.Agency) {
  287. compareAgency = "B"
  288. score += 1
  289. score2 += 1
  290. } else {
  291. compareAgency = "C"
  292. }
  293. }
  294. }
  295. compareBudget = "C"
  296. if info.Budget > 0 && (info.Budget == cp.Budget || (cp.Bidamount > 0 && info.Budget > cp.Bidamount && (info.Budget-cp.Bidamount) < (0.15*info.Budget))) {
  297. compareBudget = "A"
  298. score += 1
  299. score2 += 1
  300. }
  301. // else if info.Budget == 0 && cp.Budget == 0 {
  302. // compareBudget = "B"
  303. // }
  304. compareBidmount = "C"
  305. if info.Bidamount > 0 && (info.Bidamount == cp.Bidamount || (cp.Budget > 0 && cp.Budget > info.Bidamount && (cp.Budget-info.Bidamount) < 0.15*cp.Budget)) {
  306. compareBidmount = "A"
  307. score += 1
  308. score2 += 1
  309. }
  310. // else if info.Bidamount == 0 && cp.Bidamount == 0 {
  311. // compareBidmount = "B"
  312. // }
  313. cp.score = score
  314. return
  315. }
  316. func compareResult(resVal, pjVal, score2 int, comStr, compareBuyer, compareCity, compareTime, compareAgency, compareBudget, compareBidmount string) int {
  317. eqV := 0
  318. switch resVal {
  319. case 3:
  320. if pjVal == 3 && comStr[3:] != "CCCDCCC" {
  321. eqV = 1
  322. } else if compareBuyer < "C" {
  323. if pjVal > 1 {
  324. eqV = 1
  325. } else { //if (compareCity[1:1] != "C" || compareTime != "D") && score2 > 0
  326. eqV = 2
  327. }
  328. } else if compareBuyer == "D" {
  329. if pjVal > 1 && (compareCity[1:1] != "C" || score2 > 0) {
  330. eqV = 2
  331. } else if compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  332. eqV = 3
  333. }
  334. } else {
  335. if pjVal == 3 && (score2 > 0 || compareCity[1:1] != "C") {
  336. eqV = 2
  337. } else if pjVal == 2 && compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  338. eqV = 3
  339. } else if compareCity == "AA" && compareTime == "A" && score2 > 0 {
  340. eqV = 3
  341. }
  342. }
  343. case 2:
  344. if compareBuyer < "C" {
  345. if pjVal > 1 {
  346. eqV = 2
  347. } else if compareCity[1:1] != "C" && compareTime == "A" || score2 > 0 {
  348. eqV = 3
  349. }
  350. } else if compareBuyer == "D" {
  351. if pjVal > 1 && (score2 > 0 || compareCity[1:1] != "C") {
  352. eqV = 2
  353. } else if compareCity[1:1] != "C" && compareTime == "A" && score2 > 0 {
  354. eqV = 3
  355. }
  356. } else {
  357. if pjVal > 1 && compareTime == "A" && (score2 > 0 || compareCity[1:1] != "C") {
  358. eqV = 2
  359. } else if compareCity[1:1] != "C" && compareTime == "A" && (compareAgency == "A" || score2 > 0) && (compareBudget == "A" || compareBidmount == "A") {
  360. eqV = 3
  361. }
  362. }
  363. case 1:
  364. if compareBuyer < "C" {
  365. if pjVal > 1 && (score2 > 0 || compareCity[1:1] != "C") {
  366. eqV = 2
  367. } else if compareCity[1:1] != "C" && compareTime == "A" && (compareAgency == "A" || score2 > 0) && (compareBudget == "A" || compareBidmount == "A") {
  368. eqV = 3
  369. }
  370. } else if compareBuyer == "D" {
  371. if pjVal > 1 && compareTime == "A" && (score2 > 0 || compareCity[1:1] != "C") {
  372. eqV = 2
  373. } else if compareCity[1:1] != "C" && compareTime == "A" && (compareAgency == "A" || score2 > 0) && (compareBudget == "A" || compareBidmount == "A") {
  374. eqV = 3
  375. }
  376. } else {
  377. if pjVal > 1 && compareTime == "A" && score2 > 0 && (compareBudget == "A" || compareBidmount == "A") && compareCity[1:1] != "C" {
  378. eqV = 3
  379. }
  380. }
  381. }
  382. return eqV
  383. }
  384. //项目中的字段
  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. if bt == "招标" {
  478. set["projectscope"] = qu.ObjToString(tmp["projectscope"])
  479. set["bidtype"] = bidtype[bs]
  480. set["bidstatus"] = bs
  481. }else {
  482. set["bidtype"] = bt
  483. if bidstatus[bs] != "" {
  484. set["bidstatus"] = thisinfo.SubType
  485. } else if tmp["infoformat"] == 2 {
  486. set["bidstatus"] = "拟建"
  487. } else {
  488. set["bidstatus"] = "其它"
  489. }
  490. }
  491. p.mapBidLock.Unlock()
  492. p1, pkg := p.NewCachePinfo(pId, thisinfo, bt)
  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. return pId.Hex(), &p1
  536. }
  537. //招标信息字段
  538. var INFOFIELDS = []string{
  539. "projectname",
  540. "projectcode",
  541. "projectscope",
  542. "contractcode",
  543. "title",
  544. "href",
  545. "publishtime",
  546. "comeintime",
  547. "bidopentime",
  548. "toptype",
  549. "subtype",
  550. "buyer",
  551. "buyerclass",
  552. "agency",
  553. "winner",
  554. "budget",
  555. "bidamount",
  556. "topscopeclass",
  557. "subscopclass",
  558. "infoformat",
  559. "buyerperson",
  560. "buyertel",
  561. "area",
  562. "city",
  563. "spidercode",
  564. "site",
  565. }
  566. //项目中list的信息
  567. func (p *ProjectTask) PushListInfo(tmp map[string]interface{}, infoid string) bson.M {
  568. res := bson.M{
  569. "infoid": infoid,
  570. }
  571. for _, k := range INFOFIELDS {
  572. if tmp[k] != nil {
  573. res[k] = tmp[k]
  574. }
  575. }
  576. return res
  577. }
  578. //生成存放在内存中的对象
  579. func (p *ProjectTask) NewCachePinfo(id primitive.ObjectID, thisinfo *Info, bidtype string) (ProjectInfo, map[string]interface{}) {
  580. pkg := map[string]interface{}{}
  581. if thisinfo.HasPackage {
  582. pkg = PackageFormat(thisinfo, nil)
  583. }
  584. p1 := ProjectInfo{
  585. Id: id,
  586. Ids: []string{thisinfo.Id},
  587. ProjectName: thisinfo.ProjectName,
  588. ProjectCode: thisinfo.ProjectCode,
  589. ContractCode: thisinfo.ContractCode,
  590. Buyer: thisinfo.Buyer,
  591. Buyerclass: thisinfo.Buyerclass,
  592. Buyerperson: thisinfo.Buyerperson,
  593. Buyertel: thisinfo.Buyertel,
  594. Topscopeclass: thisinfo.Topscopeclass,
  595. Subscopeclass: thisinfo.Subscopeclass,
  596. Agency: thisinfo.Agency,
  597. Area: thisinfo.Area,
  598. City: thisinfo.City,
  599. District: thisinfo.District,
  600. MPN: []string{},
  601. MPC: []string{},
  602. FirstTime: thisinfo.Publishtime,
  603. LastTime: thisinfo.Publishtime,
  604. Budget: thisinfo.Budget,
  605. Package: pkg,
  606. Bidamount: thisinfo.Bidamount,
  607. Bidstatus: thisinfo.SubType,
  608. Bidtype: bidtype,
  609. Winners: thisinfo.Winners,
  610. }
  611. if thisinfo.LenPTC > 5 {
  612. p1.MPC = append(p1.MPC, thisinfo.PTC)
  613. }
  614. return p1, pkg
  615. }
  616. //更新项目
  617. func (p *ProjectTask) UpdateProject(tmp map[string]interface{}, thisinfo *Info, pInfo *ProjectInfo, weight int, comStr string, ex int) {
  618. if p.currentType != "ql" && p.currentType != "updateInfo" {
  619. if BinarySearch(pInfo.Ids, thisinfo.Id) > -1 {
  620. log.Println("repeat", thisinfo.Id)
  621. return
  622. }
  623. }
  624. set := map[string]interface{}{}
  625. pInfo.Ids = append(pInfo.Ids, thisinfo.Id)
  626. //1--firsttime
  627. if thisinfo.Publishtime < pInfo.FirstTime && thisinfo.Publishtime > 0 {
  628. pInfo.FirstTime = thisinfo.Publishtime
  629. set["firsttime"] = thisinfo.Publishtime
  630. if thisinfo.TopType == "招标" {
  631. set["zbtime"] = tmp["publishtime"]
  632. }
  633. }
  634. //2--lasttime
  635. pInfo.LastTime = thisinfo.Publishtime
  636. set["lasttime"] = thisinfo.Publishtime
  637. if thisinfo.TopType == "招标" {
  638. set["zbtime"] = tmp["publishtime"]
  639. } else if thisinfo.TopType == "结果" || thisinfo.SubType == "合同" {
  640. set["jgtime"] = tmp["publishtime"]
  641. }
  642. bt := qu.ObjToString(tmp["toptype"])
  643. bs := qu.ObjToString(tmp["subtype"])
  644. p.mapBidLock.Lock()
  645. if bt == "招标" {
  646. //招标状态,更新projectscope
  647. if tmp["projectscope"] != nil {
  648. set["projectscope"] = qu.ObjToString(tmp["projectscope"])
  649. }
  650. set["bidtype"] = bidtype[bs]
  651. set["bidstatus"] = bs
  652. }else {
  653. set["bidtype"] = bt
  654. if bidstatus[bs] != "" {
  655. set["bidstatus"] = thisinfo.SubType
  656. } else if tmp["infoformat"] == 2 {
  657. set["bidstatus"] = "拟建"
  658. } else {
  659. set["bidstatus"] = "其它"
  660. }
  661. }
  662. p.mapBidLock.Unlock()
  663. //废标、流标 处理时间
  664. if thisinfo.SubType == "流标" || thisinfo.SubType == "废标" {
  665. pInfo.FirstTime = thisinfo.Publishtime
  666. pInfo.Bidopentime = int64(0)
  667. pInfo.LastTime = thisinfo.Publishtime
  668. set["firsttime"] = thisinfo.Publishtime
  669. set["zbtime"] = int64(0)
  670. set["publishtime"] = thisinfo.Publishtime
  671. set["bidopentime"] = int64(0)
  672. }
  673. //异常标记
  674. if ex > 0 {
  675. set["exception"] = ex
  676. }
  677. //3\4\5--省、市、县
  678. if thisinfo.Area != "全国" {
  679. if pInfo.Area == "全国" {
  680. pInfo.Area = thisinfo.Area
  681. set["area"] = thisinfo.Area
  682. } else if pInfo.Area != thisinfo.Area {
  683. //xt = false
  684. }
  685. if pInfo.City == "" && thisinfo.City != "" {
  686. pInfo.City = thisinfo.City
  687. set["city"] = thisinfo.City
  688. } else if pInfo.City != thisinfo.City {
  689. //xt = false
  690. }
  691. if thisinfo.District != "" && pInfo.District == "" {
  692. pInfo.District = thisinfo.District
  693. set["district"] = thisinfo.District
  694. }
  695. }
  696. //6--项目名称
  697. if (thisinfo.ProjectName != "" && pInfo.ProjectName == "") || (len([]rune(pInfo.ProjectName)) < 6 && thisinfo.LenPN > 6) {
  698. pInfo.ProjectName = thisinfo.ProjectName
  699. set["projectname"] = thisinfo.ProjectName
  700. }
  701. //7--项目编号
  702. if (pInfo.ProjectCode == "" && thisinfo.ProjectCode != "") || (len([]rune(pInfo.ProjectCode)) < 6 && len([]rune(thisinfo.ProjectCode)) > 6) {
  703. pInfo.ProjectCode = thisinfo.ProjectCode
  704. set["projectcode"] = thisinfo.ProjectCode
  705. }
  706. //7--采购单位
  707. if (pInfo.Buyer == "" && thisinfo.Buyer != "") || (len([]rune(pInfo.Buyer)) < 5 && len([]rune(thisinfo.Buyer)) > 5) {
  708. pInfo.Buyer = thisinfo.Buyer
  709. set["buyer"] = thisinfo.Buyer
  710. pInfo.Buyerclass = thisinfo.Buyerclass
  711. set["buyerclass"] = thisinfo.Buyerclass
  712. }
  713. if pInfo.Buyer == "" {
  714. set["buyerclass"] = ""
  715. }
  716. if thisinfo.ContractCode != "" {
  717. set["contractcode"] = pInfo.ContractCode + ","+thisinfo.ContractCode
  718. }
  719. //8--代理机构
  720. if (pInfo.Agency == "" && thisinfo.Agency != "") || (len([]rune(pInfo.Agency)) < 5 && len([]rune(thisinfo.Agency)) > 5) {
  721. pInfo.Agency = thisinfo.Agency
  722. set["agency"] = thisinfo.Agency
  723. }
  724. //9--采购单位联系人
  725. if thisinfo.Buyerperson != "" && strings.Index(pInfo.Buyerperson, thisinfo.Buyerperson) < 0 {
  726. pInfo.Buyerperson = thisinfo.Buyerperson
  727. set["buyerperson"] = pInfo.Buyerperson
  728. }
  729. //10--采购单位電話
  730. if thisinfo.Buyertel != "" && strings.Index(pInfo.Buyertel, thisinfo.Buyertel) < 0 {
  731. pInfo.Buyertel = thisinfo.Buyertel
  732. set["buyertel"] = pInfo.Buyertel
  733. }
  734. if thisinfo.Bidopentime > pInfo.Bidopentime {
  735. pInfo.Bidopentime = thisinfo.Bidopentime
  736. set["bidopentime"] = pInfo.Bidopentime
  737. }
  738. if len(thisinfo.Topscopeclass) > 0 {
  739. sort.Strings(pInfo.Topscopeclass)
  740. for _, k := range thisinfo.Topscopeclass {
  741. if BinarySearch(pInfo.Topscopeclass, k) == -1 {
  742. pInfo.Topscopeclass = append(pInfo.Topscopeclass, k)
  743. sort.Strings(pInfo.Topscopeclass)
  744. }
  745. }
  746. set["topscopeclass"] = pInfo.Topscopeclass
  747. }
  748. if len(thisinfo.Subscopeclass) > 0 {
  749. sort.Strings(pInfo.Subscopeclass)
  750. for _, k := range thisinfo.Subscopeclass {
  751. if BinarySearch(pInfo.Subscopeclass, k) == -1 {
  752. pInfo.Subscopeclass = append(pInfo.Subscopeclass, k)
  753. sort.Strings(pInfo.Subscopeclass)
  754. }
  755. }
  756. set["subscopeclass"] = pInfo.Subscopeclass
  757. set["s_subscopeclass"] = strings.Join(pInfo.Subscopeclass, ",")
  758. }
  759. //winner
  760. if len(thisinfo.Winners) > 0 {
  761. sort.Strings(pInfo.Winners)
  762. for _, k := range thisinfo.Winners {
  763. if BinarySearch(pInfo.Winners, k) == -1 {
  764. pInfo.Winners = append(pInfo.Winners, k)
  765. sort.Strings(pInfo.Winners)
  766. }
  767. }
  768. set["s_winner"] = strings.Join(pInfo.Winners, ",")
  769. }
  770. if thisinfo.HasPackage { //多包处理
  771. set["multipackage"] = 1
  772. pkg := PackageFormat(thisinfo, pInfo)
  773. pInfo.Package = pkg
  774. set["package"] = pInfo.Package
  775. } else {
  776. set["multipackage"] = 0
  777. }
  778. //处理多包后,计算预算金额、中标金额
  779. CountAmount(pInfo, thisinfo)
  780. set["budget"] = pInfo.Budget
  781. set["bidamount"] = pInfo.Bidamount
  782. infofiled := InfoField{
  783. Budget: thisinfo.Budget,
  784. Bidamount: thisinfo.Bidamount,
  785. ContractCode: thisinfo.ContractCode,
  786. ProjectName: thisinfo.ProjectName,
  787. ProjectCode: thisinfo.ProjectCode,
  788. Bidstatus: bs,
  789. }
  790. copyMap := Copy(pInfo.InfoFiled).(map[string]InfoField)
  791. copyMap[thisinfo.Id] = infofiled
  792. tmpMap := make(map[string]interface{})
  793. for k, v := range copyMap{
  794. tmpMap[k] = StructToMap(v)
  795. }
  796. tmpMap[thisinfo.Id] = StructToMap(infofiled)
  797. pInfo.InfoFiled = copyMap
  798. set["infofield"] = tmpMap
  799. set["mpn"] = pInfo.MPN
  800. set["mpc"] = pInfo.MPC
  801. set["pici"] = p.pici
  802. update := map[string]interface{}{}
  803. if len(set) > 0 {
  804. update["$set"] = set
  805. }
  806. //保留原数据吧
  807. push := p.PushListInfo(tmp, thisinfo.Id)
  808. push["s_winner"] = strings.Join(thisinfo.Winners, ",")
  809. push["compareStr"] = comStr
  810. push["resVal"] = pInfo.resVal
  811. push["pjVal"] = pInfo.pjVal
  812. update["$push"] = map[string]interface{}{
  813. "list": push,
  814. "ids": thisinfo.Id,
  815. }
  816. if len(update) > 0 {
  817. updateInfo := []map[string]interface{}{
  818. {
  819. "_id": pInfo.Id,
  820. },
  821. update,
  822. }
  823. p.updatePool <- updateInfo
  824. }
  825. }
  826. /**
  827. * 更新项目时,项目状态的处理
  828. * 返回是否新增项目,异常标记
  829. * 1、项目时,新项目时,招标信息的状态(toptype)不是招标、拟建、预告 异常:1
  830. * 异常1是在项目新建的时候才会产生
  831. * 3、项目合并时,项目状态是”流标“/”废标“,招标信息状态不是”招标“ 异常:2
  832. * 4、项目合并时,项目状态是”合同“/”其它“,招标信息类型是”结果“ 异常:3
  833. */
  834. func (p *ProjectTask) CompareStatus(project *ProjectInfo, info *Info) (bool, int) {
  835. if info.TopType == "拟建" || info.TopType == "预告" || info.TopType == "招标" {
  836. if project.Bidstatus == "拟建" || project.Bidstatus == "预告" || project.Bidstatus == "招标" {
  837. return false, 0
  838. } else {
  839. return true, 0
  840. }
  841. } else if info.TopType == "结果" {
  842. if project.Bidstatus == "拟建" || project.Bidstatus == "预告" || project.Bidstatus == "招标" {
  843. return false, 0
  844. } else if project.Bidstatus == info.SubType {
  845. //状态一样,根据发布时间判断是否合并
  846. if (info.Publishtime - project.FirstTime) > p.statusTime {
  847. return true, 0
  848. } else {
  849. return false, 0
  850. }
  851. } else if project.Bidstatus == "成交" && info.SubType == "中标" {
  852. return true, 0
  853. } else if project.Bidstatus == "流标" || project.Bidstatus == "废标" {
  854. return false, 2
  855. } else if project.Bidstatus == "合同" || project.Bidstatus == "其它" {
  856. return false, 3
  857. } else {
  858. return false, 0
  859. }
  860. } else {
  861. return false, 0
  862. }
  863. }
  864. /*
  865. * 对比地区(省、市、区),存在且不同,不能合并
  866. * 返回是否新建项目
  867. */
  868. func ComparePlace(project *ProjectInfo, info *Info) bool {
  869. if info.Area == "全国" || info.Area == "" {
  870. return false
  871. }
  872. if project.Area == "全国" || project.Area == "" {
  873. return false
  874. }
  875. if info.Area == project.Area {
  876. if info.City == "" {
  877. return false
  878. } else if info.City == project.City {
  879. if info.District == "" || info.District == project.District {
  880. return false
  881. } else {
  882. return true
  883. }
  884. } else {
  885. return true
  886. }
  887. } else {
  888. return true
  889. }
  890. }
  891. var PackageEle = []string{
  892. "origin",
  893. "name",
  894. "text",
  895. "budget",
  896. "winner",
  897. "bidamount",
  898. "bidamounttype",
  899. "currency",
  900. "bidstatus",
  901. }
  902. func packageEle(map1 map[string]interface{}, id string) map[string]interface{} {
  903. p2 := map[string]interface{}{}
  904. for _, k := range PackageEle {
  905. if map1[k] != nil {
  906. p2[k] = map1[k]
  907. }
  908. infoid := p2["infoid"]
  909. if infoid == nil {
  910. p2["infoid"] = id
  911. }
  912. }
  913. return p2
  914. }
  915. func PackageFormat(info *Info, project *ProjectInfo) map[string]interface{} {
  916. p1 := map[string]interface{}{}
  917. packageCopy := make(map[string]interface{})
  918. if project != nil && project.Package != nil {
  919. packageCopy = Copy(project.Package).(map[string]interface{})
  920. }
  921. if packageCopy != nil && len(packageCopy) > 0 {
  922. p1 = packageCopy
  923. for k, v := range info.Package {
  924. if v1, ok := v.(map[string]interface{}); ok {
  925. v2 := map[string]interface{}{}
  926. v2 = packageEle(v1, info.Id)
  927. if v2["bidstatus"] == nil {
  928. v2["bidstatus"] = info.SubType
  929. }
  930. addFlag := false
  931. for k1, v3 := range p1 {
  932. if v4, ok := v3.([]map[string]interface{}); ok {
  933. if qu.ObjToString(v4[0]["origin"]) == qu.ObjToString(v2["origin"]) && qu.ObjToString(v4[0]["name"]) == qu.ObjToString(v2["name"]) {
  934. v4 = append(v4, v2)
  935. p1[k1] = v4
  936. addFlag = true
  937. break
  938. }
  939. }
  940. }
  941. if !addFlag {
  942. p1[k] = []map[string]interface{}{v2}
  943. }
  944. }
  945. }
  946. } else {
  947. for k, v := range packageCopy {
  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. p1 = packageCopy
  958. return p1
  959. }
  960. //计算预算(budget)、中标金额(bidamount)
  961. func CountAmount(project *ProjectInfo, info *Info) {
  962. if info.HasPackage {
  963. budget := 0.0
  964. for _, v := range project.Package{
  965. v1, _ := v.([]map[string]interface{})
  966. for _, v2 := range v1{
  967. b1 := qu.Float64All(v2["budget"])
  968. if b1 > 0 {
  969. budget = budget + b1
  970. break
  971. }
  972. }
  973. }
  974. project.Budget = budget
  975. }else {
  976. //招标没有多包
  977. k := KeyPackage.FindStringSubmatch(project.ProjectName)
  978. if len(k) > 0 {
  979. //招标是单包
  980. if len(project.Package) > 0 {
  981. //项目有多包
  982. flag := false
  983. for _, v := range project.Package{
  984. v1, _ := v.([]map[string]interface{})
  985. if len(v1) > 0 && v1[0]["name"] == info.ProjectName {
  986. flag = true
  987. }
  988. }
  989. if !flag {
  990. project.Budget = project.Budget + info.Budget
  991. }
  992. }else {
  993. //项目没有多包
  994. if info.Budget > 0 {
  995. project.Budget = project.Budget + info.Budget
  996. }
  997. }
  998. }else {
  999. //招标不是单包
  1000. //flag := false
  1001. //if project.InfoFiled != nil && len(project.InfoFiled) > 0 {
  1002. // for _, res := range project.InfoFiled {
  1003. // if res.ProjectName == info.ProjectName {
  1004. // if res.Budget < info.Budget {
  1005. // project.Budget = project.Budget - res.Budget + info.Budget
  1006. // }
  1007. // flag = true
  1008. // break
  1009. // }
  1010. // }
  1011. // if !flag {
  1012. // project.Budget = project.Budget + info.Budget
  1013. // }
  1014. //}
  1015. if project.Budget < info.Budget {
  1016. project.Budget = info.Budget
  1017. }
  1018. }
  1019. }
  1020. if info.SubType == "中标" || info.SubType == "成交" || info.SubType == "合同" {
  1021. if info.HasPackage {
  1022. bidamount := 0.0
  1023. for _, v := range project.Package{
  1024. v1, _ := v.([]map[string]interface{})
  1025. for _, v2 := range v1{
  1026. b1 := qu.Float64All(v2["bidamount"])
  1027. if b1 > 0 {
  1028. bidamount = bidamount + b1
  1029. break
  1030. }
  1031. }
  1032. }
  1033. project.Bidamount = bidamount
  1034. }else {
  1035. //招标没有多包
  1036. k := KeyPackage.FindStringSubmatch(project.ProjectName)
  1037. if len(k) > 0 {
  1038. //招标是单包
  1039. if len(project.Package) > 0 {
  1040. //项目有多包
  1041. flag := false
  1042. for _, v := range project.Package{
  1043. v1, _ := v.([]map[string]interface{})
  1044. if len(v1) > 0 {
  1045. flag = true
  1046. }
  1047. }
  1048. if !flag {
  1049. project.Bidamount = project.Bidamount + info.Bidamount
  1050. }
  1051. }else {
  1052. //项目没有多包
  1053. if info.Bidamount > 0 {
  1054. project.Bidamount = project.Bidamount + info.Bidamount
  1055. }
  1056. }
  1057. }else {
  1058. //招标不是单包
  1059. //flag := false
  1060. //if project.InfoFiled != nil && len(project.InfoFiled) > 0 {
  1061. // for _, res := range project.InfoFiled {
  1062. // if res.Bidstatus == "合同" && res.ContractCode != "" && info.SubType == "合同" && info.ContractCode != "" {
  1063. // if res.ContractCode == info.ContractCode {
  1064. // if res.Bidamount < info.Bidamount {
  1065. // project.Bidamount = project.Bidamount - res.Bidamount + info.Bidamount
  1066. // }
  1067. // flag = true
  1068. // break
  1069. // }
  1070. // }else {
  1071. // if res.ProjectName == info.ProjectName {
  1072. // if res.Bidamount < info.Bidamount {
  1073. // project.Bidamount = project.Bidamount - res.Bidamount + info.Bidamount
  1074. // }
  1075. // flag = true
  1076. // break
  1077. // }
  1078. // }
  1079. // }
  1080. // if !flag {
  1081. // project.Bidamount = project.Bidamount + info.Bidamount
  1082. // }
  1083. //}
  1084. if info.SubType == "中标" || info.SubType == "成交" {
  1085. if project.Bidamount < info.Bidamount {
  1086. project.Bidamount = info.Bidamount
  1087. }else {
  1088. flag := false
  1089. if project.InfoFiled != nil && len(project.InfoFiled) > 0 {
  1090. for _, res := range project.InfoFiled {
  1091. if res.ContractCode != "" && res.ContractCode == info.ContractCode {
  1092. flag = true
  1093. break
  1094. }
  1095. if res.Bidamount == project.Bidamount {
  1096. flag = true
  1097. break
  1098. }
  1099. }
  1100. if !flag {
  1101. project.Bidamount = project.Bidamount + info.Bidamount
  1102. }else {
  1103. if project.Bidamount < info.Bidamount {
  1104. project.Bidamount = info.Bidamount
  1105. }
  1106. }
  1107. }
  1108. }
  1109. }
  1110. }
  1111. }
  1112. }
  1113. }
  1114. //结构体转map
  1115. func StructToMap(filed InfoField) map[string]interface{} {
  1116. //先转json
  1117. result, err := json.Marshal(filed)
  1118. if err != nil {
  1119. return nil
  1120. }
  1121. //json转map
  1122. res := make(map[string]interface{})
  1123. err = json.Unmarshal(result, &res)
  1124. return res
  1125. }