proTask.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package main
  2. import (
  3. util "app.yhyue.com/data_processing/common_utils"
  4. "app.yhyue.com/data_processing/common_utils/log"
  5. "app.yhyue.com/data_processing/common_utils/mongodb"
  6. "encoding/json"
  7. "fmt"
  8. "go.uber.org/zap"
  9. "proposed_project/config"
  10. "strings"
  11. "sync"
  12. "time"
  13. )
  14. func updateAllQueue() {
  15. arru := make([][]map[string]interface{}, saveSize)
  16. indexu := 0
  17. for {
  18. select {
  19. case v := <-updatePool:
  20. arru[indexu] = v
  21. indexu++
  22. if indexu == saveSize {
  23. updateSp <- true
  24. go func(arru [][]map[string]interface{}) {
  25. defer func() {
  26. <-updateSp
  27. }()
  28. MgoPro.UpSertBulk(config.Conf.Serve.ProColl, arru...)
  29. }(arru)
  30. arru = make([][]map[string]interface{}, saveSize)
  31. indexu = 0
  32. }
  33. case <-time.After(1 * time.Second):
  34. if indexu > 0 {
  35. updateSp <- true
  36. go func(arru [][]map[string]interface{}) {
  37. defer func() {
  38. <-updateSp
  39. }()
  40. MgoPro.UpSertBulk(config.Conf.Serve.ProColl, arru...)
  41. }(arru[:indexu])
  42. arru = make([][]map[string]interface{}, saveSize)
  43. indexu = 0
  44. }
  45. }
  46. }
  47. }
  48. func doTask(gtid, lteid string) {
  49. defer util.Catch()
  50. sess := MgoBid.GetMgoConn()
  51. defer MgoBid.DestoryMongoConn(sess)
  52. ch := make(chan bool, config.Conf.Serve.Thread)
  53. wg := &sync.WaitGroup{}
  54. q := map[string]interface{}{"_id": map[string]interface{}{
  55. "$gt": mongodb.StringTOBsonId(gtid),
  56. "$lte": mongodb.StringTOBsonId(lteid)}}
  57. f := map[string]interface{}{
  58. "contenthtml": 0,
  59. "field_source": 0,
  60. "nj_record": 0,
  61. "kvtext": 0,
  62. }
  63. log.Info("doTask", zap.Any("q", q))
  64. query := sess.DB(config.Conf.DB.MongoB.Dbname).C("nzj_bidding").Find(q).Select(f).Sort("publishtime").Iter()
  65. count := 0
  66. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  67. if count%5000 == 0 {
  68. log.Info(fmt.Sprintf("current --- %d", count))
  69. }
  70. //if util.ObjToString(tmp["toptype"]) != "拟建" {
  71. // continue
  72. //}
  73. ch <- true
  74. wg.Add(1)
  75. go func(temp map[string]interface{}) {
  76. defer func() {
  77. <-ch
  78. wg.Done()
  79. }()
  80. taskTag(temp) // 增量数据打标签
  81. info := ParseInfo(temp)
  82. if info.ProjectName != "" {
  83. MergeTask(temp, info)
  84. } else {
  85. log.Error("MergeTask", zap.Any("infoid", temp["_id"]))
  86. }
  87. }(tmp)
  88. tmp = make(map[string]interface{})
  89. }
  90. wg.Wait()
  91. log.Info(fmt.Sprintf("over --- %d", count), zap.String("LastId: ", LastId))
  92. TaskSingle = true
  93. }
  94. func MergeTask(tmp map[string]interface{}, info *Info) {
  95. if info.ApproveCode != "" && info.ApproveCode != "无" && info.ApproveCode != "暂无项目代码" {
  96. AllCodeMapLock.Lock()
  97. pid := AllCodeMap[info.ApproveCode]
  98. AllCodeMapLock.Unlock()
  99. if pid != "" {
  100. AllPidMapLock.Lock()
  101. res := AllPidMap[pid]
  102. AllPidMapLock.Unlock()
  103. if res != nil {
  104. comparePro := res.P
  105. updateProject(tmp, *info, comparePro)
  106. } else {
  107. startProjectMerge(tmp, info)
  108. }
  109. } else {
  110. pid = startProjectMerge(tmp, info)
  111. AllCodeMapLock.Lock()
  112. AllCodeMap[info.ApproveCode] = pid
  113. AllCodeMapLock.Unlock()
  114. }
  115. } else {
  116. startProjectMerge(tmp, info)
  117. }
  118. }
  119. func ParseInfo(tmp map[string]interface{}) (info *Info) {
  120. bys, _ := json.Marshal(tmp)
  121. var thisinfo *Info
  122. _ = json.Unmarshal(bys, &thisinfo)
  123. if thisinfo == nil {
  124. return nil
  125. }
  126. if len(thisinfo.Topscopeclass) == 0 {
  127. thisinfo.Topscopeclass = []string{}
  128. }
  129. if len(thisinfo.Subscopeclass) == 0 {
  130. thisinfo.Subscopeclass = []string{}
  131. }
  132. return thisinfo
  133. }
  134. func taskTag(tmp map[string]interface{}) {
  135. tag := taskFuc(tmp)
  136. if tag["nature"] != "" {
  137. tmp["nature_code"] = tag["nature"]
  138. } else {
  139. tmp["nature_code"] = "00"
  140. }
  141. if tag["project_stage"] != "" {
  142. tmp["project_stage_code"] = tag["project_stage"]
  143. } else {
  144. tmp["project_stage_code"] = "00"
  145. }
  146. if tag["owner"] != "" {
  147. tmp["ownerclass_code"] = tag["owner"]
  148. } else {
  149. tmp["ownerclass_code"] = "00"
  150. }
  151. if tag["sub_category"] != "" {
  152. tmp["category_code"] = tag["sub_category"]
  153. } else {
  154. if tag["top_category"] != "" {
  155. tmp["category_code"] = tag["top_category"]
  156. }
  157. }
  158. if util.ObjToString(tmp["category_code"]) == "" {
  159. tmp["category_code"] = "04"
  160. }
  161. }
  162. func doTask1(gtid, lteid string) {
  163. defer util.Catch()
  164. sess := MgoBid.GetMgoConn()
  165. defer MgoBid.DestoryMongoConn(sess)
  166. ch := make(chan bool, config.Conf.Serve.Thread)
  167. wg := &sync.WaitGroup{}
  168. q := map[string]interface{}{"_id": map[string]interface{}{
  169. "$gt": mongodb.StringTOBsonId(gtid),
  170. "$lte": mongodb.StringTOBsonId(lteid)}}
  171. log.Info("doTask", zap.Any("q", q))
  172. query := sess.DB(config.Conf.DB.MongoB.Dbname).C("bidding").Find(q).Select(nil).Iter()
  173. count := 0
  174. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  175. if count%20000 == 0 {
  176. log.Info(fmt.Sprintf("current --- %d", count))
  177. }
  178. if util.ObjToString(tmp["toptype"]) != "拟建" {
  179. continue
  180. }
  181. ch <- true
  182. wg.Add(1)
  183. go func(temp map[string]interface{}) {
  184. defer func() {
  185. <-ch
  186. wg.Done()
  187. }()
  188. //disField(temp)
  189. savePool <- temp
  190. }(tmp)
  191. tmp = make(map[string]interface{})
  192. }
  193. wg.Wait()
  194. log.Info(fmt.Sprintf("over --- %d", count))
  195. TaskSingle = true
  196. }
  197. func disField(tmp map[string]interface{}) {
  198. njInfo := map[string]int{
  199. "approvecode": 1,
  200. "approvedept": 1,
  201. "approvestatus": 1,
  202. "approvetime": 1,
  203. "approvenumber": 1,
  204. "approvecontent": 1,
  205. "projecttype": 1,
  206. "approvecity": 1,
  207. "countryprojectcode": 1,
  208. "total_investment": 1,
  209. "owner": 1,
  210. "projectperiod": 1,
  211. "project_person": 1,
  212. "projectaddr": 1,
  213. "project_scale": 1,
  214. }
  215. code := util.ObjToString(tmp["spidercode"])
  216. //只处理增量的lua采集的信息
  217. if !strings.Contains(code, "_njpc") {
  218. for f := range njInfo {
  219. if tmp[f] != nil {
  220. njInfo[f] = 0 //值为0表示该字段已抽取,不必再更新
  221. }
  222. }
  223. //projectinfo
  224. if projectinfo, ok := tmp["projectinfo"].(map[string]interface{}); ok && len(projectinfo) > 0 {
  225. flag := false
  226. for f, n := range njInfo {
  227. if n == 1 && projectinfo[f] != nil {
  228. tmp[f] = projectinfo[f]
  229. flag = true
  230. }
  231. }
  232. if !flag { //嵌套信息
  233. tmpTime := ""
  234. tmpK := "" //记录最终取哪个map
  235. for k, v := range projectinfo {
  236. if result, ok := v.(map[string]interface{}); ok && len(result) > 0 && k != "attachments" {
  237. approvetime := util.ObjToString(result["approvetime"])
  238. if tmpTime == "" || approvetime >= tmpTime { //取最新时间信息
  239. tmpTime = approvetime
  240. tmpK = k
  241. }
  242. }
  243. }
  244. if tmpK != "" {
  245. if resultTmp, ok := projectinfo[tmpK].(map[string]interface{}); ok && len(resultTmp) > 0 {
  246. for f, n := range njInfo {
  247. if n == 1 && resultTmp[f] != nil {
  248. tmp[f] = resultTmp[f]
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. //buyer
  256. if buyer := tmp["buyer"]; buyer != nil && tmp["owner"] == nil {
  257. tmp["owner"] = buyer
  258. }
  259. }
  260. }