task.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "qfw/util"
  6. "regexp"
  7. "sync"
  8. //"strings"
  9. "time"
  10. )
  11. const (
  12. InitMinTime = int64(1325347200) //最小时间位置2012
  13. )
  14. //全量合并
  15. func taskQl(udpInfo map[string]interface{}) {
  16. defer util.Catch()
  17. //1、检查pubilshtime索引
  18. db, _ := udpInfo["db"].(string)
  19. if db == "" {
  20. db = MongoTool.DbName
  21. }
  22. coll, _ := udpInfo["coll"].(string)
  23. if coll == "" {
  24. coll = ExtractColl
  25. }
  26. sess := MongoTool.GetMgoConn()
  27. bcon := false
  28. if sess.DB(db).C(coll).EnsureIndexKey("publishtime_1", "publishtime_-1") == nil {
  29. bcon = true
  30. } else {
  31. log.Println("publishtime_1索引不存在")
  32. }
  33. MongoTool.DestoryMongoConn(sess)
  34. thread := util.IntAllDef(udpInfo["thread"], 1)
  35. if bcon {
  36. //go SaveQueue()
  37. go updateQueue()
  38. go clearMem()
  39. //获取起始时间
  40. startTime, END := int64(0), int64(0)
  41. sts, bres := MongoTool.Find(ExtractColl, `{}`, "publishtime", `{"publishtime":1}`, true, 0, 1)
  42. if bres && sts != nil && len(*sts) == 1 {
  43. startTime = util.Int64All((*sts)[0]["publishtime"])
  44. sts, bres = MongoTool.Find(ExtractColl, `{}`, "-publishtime", `{"publishtime":1}`, true, 0, 1)
  45. if bres && sts != nil && len(*sts) == 1 {
  46. END = util.Int64All((*sts)[0]["publishtime"])
  47. }
  48. log.Println("查询到的起始时间", startTime, END)
  49. } else {
  50. return
  51. }
  52. startTime -= 1
  53. sum := 0
  54. if startTime < InitMinTime {
  55. q := map[string]interface{}{
  56. "publishtime": map[string]interface{}{
  57. "$gt": startTime,
  58. "$lte": InitMinTime,
  59. },
  60. }
  61. sum = Mql(q, thread, db, coll, sum)
  62. startTime = InitMinTime
  63. }
  64. for {
  65. if startTime >= END {
  66. break
  67. }
  68. et := startTime + 50*86400
  69. if et >= END {
  70. et = END
  71. }
  72. q := map[string]interface{}{
  73. "publishtime": map[string]interface{}{
  74. "$gt": startTime,
  75. "$lte": et,
  76. },
  77. }
  78. sum = Mql(q, thread, db, coll, sum)
  79. startTime = et
  80. time.Sleep(1 * time.Second)
  81. }
  82. }
  83. log.Println("task over!!!")
  84. }
  85. var wg = sync.WaitGroup{}
  86. func Mql(q map[string]interface{}, thread int, db, coll string, sum int) int {
  87. defer util.Catch()
  88. sess := MongoTool.GetMgoConn()
  89. defer MongoTool.DestoryMongoConn(sess)
  90. query := sess.DB(db).C(coll).Find(q).Sort("publishtime").Iter()
  91. pool := make(chan bool, thread)
  92. count := 0
  93. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  94. info := ParseInfo(tmp)
  95. if info != nil && !((info.pnbval == 1 && info.Buyer != "") || info.pnbval == 0) {
  96. pool <- true
  97. go func(info *Info, tmp map[string]interface{}) {
  98. defer func() {
  99. currentTime = info.Publishtime
  100. <-pool
  101. }()
  102. startProjectMerge(info, tmp)
  103. }(info, tmp)
  104. } else {
  105. //log.Println("info err:", tmp["_id"], tmp["title"], tmp["buyer"])
  106. }
  107. if sum%1000 == 0 {
  108. log.Println("current", sum)
  109. }
  110. sum++
  111. tmp = make(map[string]interface{})
  112. }
  113. //阻塞
  114. for n := 0; n < thread; n++ {
  115. pool <- true
  116. }
  117. //完成
  118. log.Println("sontask over:", count, sum, q)
  119. return sum
  120. }
  121. var (
  122. titleGetPc = regexp.MustCompile("^([-0-9a-zA-Z第号采招政询电审竞#]{8,}[-0-9a-zA-Z#]+)")
  123. titleGetPc1 = regexp.MustCompile("[\\[【((](.{0,6}(编号|编码|项号|包号|代码|标段?号)[::为])?([-0-9a-zA-Z第号采招政询电审竞#]{5,}([\\[\\]()()][-0-9a-zA-Z第号采招审竞#]+[\\[\\]()()][-0-9a-zA-Z第号采招审竞#]+)?)[\\]】))]")
  124. titleGetPc2 = regexp.MustCompile("([-0-9a-zA-Z第号采政招询电审竞#]{8,}[-0-9a-zA-Z#]+)(.{0,5}公告)?$")
  125. pcReplace = regexp.MustCompile("([\\[【((〖〔《{﹝{](重|第?[二三四再]次.{0,4})[\\]】))〗〕》}﹞}])$|[\\[\\]【】()()〖〗〔〕《》{}﹝﹞-{}–  ]+|(号|重|第?[二三四五再]次(招标)?)$")
  126. )
  127. func ParseInfo(tmp map[string]interface{}) (info *Info) {
  128. bys, _ := json.Marshal(tmp)
  129. var thisinfo *Info
  130. json.Unmarshal(bys, &thisinfo)
  131. if thisinfo == nil {
  132. return nil
  133. }
  134. if len(thisinfo.Topscopeclass) == 0 {
  135. thisinfo.Topscopeclass = []string{}
  136. }
  137. if len(thisinfo.Subscopeclass) == 0 {
  138. thisinfo.Subscopeclass = []string{}
  139. }
  140. res := titleGetPc.FindStringSubmatch(thisinfo.Title)
  141. if len(res) > 1 && len(res[1]) > 6 && thisinfo.ProjectCode != res[1] && !numCheckPc.MatchString(res[1]) && !_zimureg1.MatchString(res[1]) {
  142. thisinfo.PTC = res[1]
  143. thisinfo.pnbval++
  144. } else {
  145. res = titleGetPc1.FindStringSubmatch(thisinfo.Title)
  146. if len(res) > 3 && len(res[3]) > 6 && thisinfo.ProjectCode != res[3] && !numCheckPc.MatchString(res[3]) && !_zimureg1.MatchString(res[3]) {
  147. thisinfo.PTC = res[3]
  148. thisinfo.pnbval++
  149. } else {
  150. res = titleGetPc2.FindStringSubmatch(thisinfo.Title)
  151. if len(res) > 1 && len(res[1]) > 6 && thisinfo.ProjectCode != res[1] && !numCheckPc.MatchString(res[1]) && !_zimureg1.MatchString(res[1]) {
  152. thisinfo.PTC = res[1]
  153. thisinfo.pnbval++
  154. }
  155. }
  156. }
  157. if thisinfo.ProjectName != "" && len([]rune(thisinfo.ProjectName)) > 0 {
  158. // thisinfo.ProjectName = strings.Replace(thisinfo.ProjectName, "(", "(", -1)
  159. // thisinfo.ProjectName = strings.Replace(thisinfo.ProjectName, ")", ")", -1)
  160. // thisinfo.ProjectName = strings.Replace(thisinfo.ProjectName, "-", "", -1)
  161. thisinfo.ProjectName = pcReplace.ReplaceAllString(thisinfo.ProjectName, "")
  162. if thisinfo.ProjectName != "" {
  163. thisinfo.pnbval++
  164. }
  165. }
  166. if thisinfo.ProjectCode != "" || thisinfo.PTC != "" {
  167. if thisinfo.ProjectCode != "" {
  168. thisinfo.ProjectCode = pcReplace.ReplaceAllString(thisinfo.ProjectCode, "")
  169. // thisinfo.ProjectCode = strings.Replace(thisinfo.ProjectCode, "(", "(", -1)
  170. // thisinfo.ProjectCode = strings.Replace(thisinfo.ProjectCode, ")", ")", -1)
  171. // thisinfo.ProjectCode = strings.Replace(thisinfo.ProjectCode, "-", "", -1)
  172. } else {
  173. thisinfo.PTC = pcReplace.ReplaceAllString(thisinfo.PTC, "")
  174. // thisinfo.PTC = strings.Replace(thisinfo.PTC, "(", "(", -1)
  175. // thisinfo.PTC = strings.Replace(thisinfo.PTC, ")", ")", -1)
  176. // thisinfo.PTC = strings.Replace(thisinfo.PTC, "-", "", -1)
  177. }
  178. if thisinfo.ProjectCode != "" || thisinfo.PTC != "" {
  179. thisinfo.pnbval++
  180. }
  181. }
  182. if thisinfo.ProjectCode == thisinfo.PTC {
  183. thisinfo.PTC = ""
  184. }
  185. if thisinfo.Buyer != "" && len([]rune(thisinfo.Buyer)) > 2 {
  186. thisinfo.pnbval++
  187. } else {
  188. thisinfo.Buyer = ""
  189. }
  190. thisinfo.LenPC = len([]rune(thisinfo.ProjectCode))
  191. thisinfo.LenPTC = len([]rune(thisinfo.PTC))
  192. thisinfo.LenPN = len([]rune(thisinfo.ProjectName))
  193. return thisinfo
  194. }