new_project.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package main
  2. import (
  3. qu "qfw/util"
  4. "strings"
  5. "time"
  6. "gopkg.in/mgo.v2/bson"
  7. )
  8. var FIELDS = []string{"area", "city", "district", "projectname", "projectcode", "buyer", "winner", "budget", "bidamount", "bidstatus", "agency", "projectscope"}
  9. func NewProject(tmp map[string]interface{}, thisinfo *Info) (string, *ProjectInfo) {
  10. set := map[string]interface{}{}
  11. for _, f := range FIELDS {
  12. if tmp[f] != nil {
  13. set[f] = tmp[f]
  14. }
  15. }
  16. set["s_projectname"] = thisinfo.ProjectName
  17. set["createtime"] = time.Now().Unix()
  18. set["sourceinfoid"] = qu.BsonIdToSId(tmp["_id"])
  19. set["sourceinfourl"] = tmp["href"]
  20. set["topscopeclass"] = thisinfo.Topscopeclass
  21. set["subscopeclass"] = thisinfo.Subscopeclass
  22. if thisinfo.Buyerperson != "" {
  23. set["buyerperson"] = thisinfo.Buyerperson
  24. }
  25. if thisinfo.Buyertel != "" {
  26. set["buyertel"] = thisinfo.Buyertel
  27. }
  28. if thisinfo.Buyerclass != "" {
  29. set["buyerclass"] = thisinfo.Buyerclass
  30. }
  31. if thisinfo.Bidopentime > 0 {
  32. set["bidopentime"] = thisinfo.Bidopentime
  33. }
  34. if len(thisinfo.Winnerorder) > 0 {
  35. set["winnerorder"] = thisinfo.Winnerorder
  36. }
  37. s_subscopeclass := strings.Join(thisinfo.Subscopeclass, ",")
  38. set["s_subscopeclass"] = s_subscopeclass
  39. s_winner := strings.Join(thisinfo.Winners, ",")
  40. set["s_winner"] = s_winner
  41. if tmp["package"] != nil {
  42. set["package"] = tmp["package"] //没定义优先级
  43. }
  44. push := NewPushInfo(tmp)
  45. set["list"] = []bson.M{
  46. push,
  47. }
  48. pId := bson.NewObjectId()
  49. set["_id"] = pId
  50. ///id := MongoTool.Save(ProjectColl, set)
  51. p1 := NewPinfo(pId, thisinfo, set)
  52. return pId.Hex(), &p1
  53. }
  54. func NewPushInfo(tmp map[string]interface{}) bson.M {
  55. res := bson.M{
  56. "comeintime": tmp["comeintime"],
  57. "publishtime": tmp["publishtime"],
  58. "title": tmp["title"],
  59. "toptype": tmp["toptype"],
  60. "subtype": tmp["subtype"],
  61. "infoformat": tmp["infoformat"],
  62. "infoid": qu.BsonIdToSId(tmp["_id"]),
  63. "area": tmp["area"],
  64. "city": tmp["city"],
  65. "projectname": tmp["projectname"],
  66. "projectcode": tmp["projectcode"],
  67. "buyer": tmp["buyer"],
  68. "href": tmp["href"],
  69. }
  70. for _, k := range []string{"winner", "budget", "bidamount"} {
  71. if tmp[k] != nil {
  72. res[k] = tmp[k]
  73. }
  74. }
  75. return res
  76. }
  77. //生成存放在redis数组中的对象
  78. func NewPinfo(id bson.ObjectId, thisinfo *Info, set map[string]interface{}) ProjectInfo {
  79. p1 := ProjectInfo{
  80. Id: id,
  81. Ids: []string{thisinfo.Id},
  82. Topscopeclass: thisinfo.Topscopeclass,
  83. Subscopeclass: thisinfo.Subscopeclass,
  84. Winners: thisinfo.Winners,
  85. ProjectName: thisinfo.ProjectName,
  86. ProjectCode: thisinfo.ProjectCode,
  87. Buyer: thisinfo.Buyer,
  88. Agency: thisinfo.Agency,
  89. Area: thisinfo.Area,
  90. City: thisinfo.City,
  91. District: thisinfo.District,
  92. MPN: []string{},
  93. MPC: []string{},
  94. HasPackage: thisinfo.HasPackage,
  95. Package: map[string]interface{}{},
  96. Buyerclass: thisinfo.Buyerclass,
  97. Bidopentime: thisinfo.Bidopentime,
  98. Winnerorder: thisinfo.Winnerorder,
  99. FirstTime: thisinfo.Publishtime,
  100. LastTime: thisinfo.Publishtime,
  101. Budget: thisinfo.Budget,
  102. Bidamount: thisinfo.Bidamount,
  103. }
  104. if thisinfo.LenPTC > 5 {
  105. p1.MPC = append(p1.MPC, thisinfo.PTC)
  106. }
  107. //savePool <- set
  108. MongoTool.SaveByOriID(ProjectColl, set)
  109. return p1
  110. }
  111. var updatePool = make(chan []map[string]interface{}, 30)
  112. //var savePool = make(chan map[string]interface{}, 6)
  113. //func SaveQueue() {
  114. // arr := []map[string]interface{}{}
  115. // sp := make(chan bool, 3)
  116. // for {
  117. // select {
  118. // case <-beforUpdate:
  119. // if len(arr) > 0 {
  120. // MongoTool.SaveBulk(ProjectColl, arr...)
  121. // arr = []map[string]interface{}{}
  122. // }
  123. // case v := <-savePool:
  124. // arr = append(arr, v)
  125. // if len(arr) > 50 {
  126. // sp <- true
  127. // go func(arr []map[string]interface{}) {
  128. // MongoTool.SaveBulk(ProjectColl, arr...)
  129. // <-sp
  130. // }(arr)
  131. // arr = []map[string]interface{}{}
  132. // }
  133. // case <-time.After(80 * time.Millisecond):
  134. // if len(arr) > 0 {
  135. // sp <- true
  136. // go func(arr []map[string]interface{}) {
  137. // MongoTool.SaveBulk(ProjectColl, arr...)
  138. // <-sp
  139. // }(arr)
  140. // arr = []map[string]interface{}{}
  141. // }
  142. // }
  143. // }
  144. //}
  145. func updateQueue() {
  146. arr := [][]map[string]interface{}{}
  147. sp := make(chan bool, 3)
  148. for {
  149. select {
  150. case v := <-updatePool:
  151. arr = append(arr, v)
  152. if len(arr) > 200 {
  153. sp <- true
  154. go func(arr [][]map[string]interface{}) {
  155. MongoTool.UpdateBulk(ProjectColl, arr...)
  156. <-sp
  157. }(arr)
  158. arr = [][]map[string]interface{}{}
  159. }
  160. case <-time.After(500 * time.Millisecond):
  161. if len(arr) > 0 {
  162. sp <- true
  163. go func(arr [][]map[string]interface{}) {
  164. MongoTool.UpdateBulk(ProjectColl, arr...)
  165. <-sp
  166. }(arr)
  167. arr = [][]map[string]interface{}{}
  168. }
  169. }
  170. }
  171. }