project_es.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. "esindex/config"
  7. "go.uber.org/zap"
  8. "math"
  9. "reflect"
  10. "regexp"
  11. "strconv"
  12. )
  13. var (
  14. regLetter = regexp.MustCompile("[a-z]*")
  15. )
  16. func projectTask(data []byte, mapInfo map[string]interface{}) {
  17. defer util.Catch()
  18. q, _ := mapInfo["query"].(map[string]interface{})
  19. if q == nil {
  20. q = map[string]interface{}{
  21. "_id": map[string]interface{}{
  22. "$gt": mongodb.StringTOBsonId(mapInfo["gtid"].(string)),
  23. "$lte": mongodb.StringTOBsonId(mapInfo["lteid"].(string)),
  24. },
  25. }
  26. } else {
  27. if q["pici"] == nil {
  28. idMap, _ := q["_id"].(map[string]interface{})
  29. if idMap != nil {
  30. tmpQ := map[string]interface{}{}
  31. for c, id := range idMap {
  32. if idStr, ok := id.(string); ok && id != "" {
  33. tmpQ[c] = mongodb.StringTOBsonId(idStr)
  34. }
  35. }
  36. q["_id"] = tmpQ
  37. }
  38. }
  39. }
  40. conn := MgoP.GetMgoConn()
  41. defer MgoP.DestoryMongoConn(conn)
  42. count, _ := conn.DB(MgoP.DbName).C(config.Conf.DB.MongoP.Coll).Find(&q).Count()
  43. log.Info("projectTask", zap.String("coll", config.Conf.DB.MongoP.Coll), zap.Any("查询语句:", q), zap.Int64("同步总数:", count))
  44. query := conn.DB(MgoP.DbName).C(config.Conf.DB.MongoP.Coll).Find(q).Iter()
  45. n := 0
  46. for tmp := make(map[string]interface{}); query.Next(tmp); n++ {
  47. if n%2000 == 0 {
  48. log.Info("current", zap.Int("count", n))
  49. }
  50. newTmp := make(map[string]interface{})
  51. for f, ftype := range ProjectField {
  52. if tmp[f] != nil {
  53. if f == "package" {
  54. pp := map[string]map[string]interface{}{}
  55. if packages, ok := tmp["package"].(map[string]interface{}); ok {
  56. for _, pks := range packages {
  57. if pk, ok := pks.([]interface{}); ok {
  58. for _, v := range pk {
  59. if p, ok := v.(map[string]interface{}); ok {
  60. winner := util.ObjToString(p["winner"])
  61. bidamount := util.Float64All((p["bidamount"]))
  62. if len(winner) > 4 && bidamount > 0 {
  63. p := map[string]interface{}{
  64. "winner": winner,
  65. "bidamount": bidamount,
  66. }
  67. pp[winner] = p
  68. }
  69. }
  70. }
  71. }
  72. }
  73. } else {
  74. winner := util.ObjToString(tmp["winner"])
  75. bidamount := util.Float64All(tmp["bidamount"])
  76. if len(winner) > 4 && bidamount > 0 {
  77. p := map[string]interface{}{
  78. "winner": winner,
  79. "bidamount": bidamount,
  80. }
  81. pp[winner] = p
  82. }
  83. }
  84. pk1 := []map[string]interface{}{}
  85. for _, v := range pp {
  86. pk1 = append(pk1, v)
  87. }
  88. if len(pk1) > 0 {
  89. newTmp["package1"] = pk1
  90. }
  91. } else if f == "topscopeclass" {
  92. if topscopeclass, ok := tmp["topscopeclass"].([]interface{}); ok {
  93. tc := []string{}
  94. m2 := map[string]bool{}
  95. for _, v := range topscopeclass {
  96. str := util.ObjToString(v)
  97. str = regLetter.ReplaceAllString(str, "") // 去除字母
  98. if !m2[str] {
  99. m2[str] = true
  100. tc = append(tc, str)
  101. }
  102. }
  103. newTmp["topscopeclass"] = tc
  104. }
  105. } else if f == "list" {
  106. if list, ok := tmp[f].([]interface{}); ok {
  107. var newList []map[string]interface{}
  108. for _, item := range list {
  109. item1 := item.(map[string]interface{})
  110. listm := make(map[string]interface{})
  111. for f1, ftype1 := range ProjectListF {
  112. if item1[f1] != nil {
  113. if f == "topscopeclass" || f == "subscopeclass" {
  114. listm[f] = item1[f1]
  115. } else {
  116. if fieldval := item1[f1]; reflect.TypeOf(fieldval).String() != ftype1 {
  117. continue
  118. } else {
  119. if fieldval != "" {
  120. listm[f1] = fieldval
  121. }
  122. }
  123. }
  124. }
  125. }
  126. newList = append(newList, listm)
  127. }
  128. newTmp[f] = newList
  129. }
  130. } else if f == "budget" || f == "bidamount" || f == "sortprice" {
  131. if tmp[f] != nil && util.Float64All(f) <= 1000000000 {
  132. newTmp[f] = tmp[f]
  133. }
  134. } else if f == "projectscope" {
  135. projectscopeRune := []rune(util.ObjToString(tmp[f]))
  136. if len(projectscopeRune) > 1000 {
  137. newTmp[f] = util.ObjToString(tmp[f])[:1000]
  138. } else {
  139. newTmp[f] = tmp[f]
  140. }
  141. } else if f == "s_projectname" {
  142. newTmp[f] = tmp["projectname"]
  143. } else if f == "ids" || f == "_id" || f == "mpc" || f == "mpn" || f == "review_experts" || f == "winnerorder" ||
  144. f == "entidlist" || f == "first_cooperation" {
  145. newTmp[f] = tmp[f]
  146. } else {
  147. if fieldval := tmp[f]; reflect.TypeOf(fieldval).String() != ftype {
  148. continue
  149. } else {
  150. if fieldval != "" {
  151. newTmp[f] = fieldval
  152. }
  153. }
  154. }
  155. }
  156. }
  157. budget := util.Float64All(newTmp["budget"])
  158. bidamount := util.Float64All(newTmp["bidamount"])
  159. if float64(budget) > 0 && float64(bidamount) > 0 {
  160. rate := float64(1) - float64(bidamount)/float64(budget)
  161. f, _ := strconv.ParseFloat(strconv.FormatFloat(rate, 'f', 4, 64), 64)
  162. //不在0~0.6之间,不生成费率;只生成预算,中标金额舍弃,索引增加折扣率异常标识
  163. if f < 0 || f > 0.6 {
  164. delete(newTmp, "bidamount")
  165. newTmp["prate_flag"] = 1
  166. } else {
  167. newTmp["project_rate"] = f
  168. }
  169. }
  170. bidopentime := util.Int64All(tmp["bidopentime"]) //开标日期
  171. fzb_publishtime := int64(0) //记录第一个招标信息的publishtime
  172. bidcycle_flag := false //判断是否已计算出标书表编制周期
  173. list := tmp["list"].([]interface{})
  174. for _, m := range list {
  175. tmpM := m.(map[string]interface{})
  176. if bidamount, ok := tmpM["bidamount"].(string); ok && len(bidamount) > 0 { //bidamount为string类型,转成float
  177. tmpB := util.Float64All(tmpM["bidamount"])
  178. tmpM["bidamount"] = tmpB
  179. }
  180. //计算bidcycle标书表编制周期字段
  181. if !bidcycle_flag && bidopentime > 0 { //bidopentime>0证明list中有bidopentime,无则不用计算bidcycle
  182. if toptype := util.ObjToString(tmpM["toptype"]); toptype == "招标" {
  183. zb_bidopentime := util.Int64All(tmpM["bidopentime"])
  184. zb_publishtime := util.Int64All(tmpM["publishtime"])
  185. if zb_publishtime > 0 {
  186. if zb_bidopentime > 0 {
  187. if tmpTime := zb_bidopentime - zb_publishtime; tmpTime > 0 {
  188. f_day := float64(tmpTime) / float64(86400)
  189. day := math.Ceil(f_day)
  190. tmp["bidcycle"] = int(day)
  191. bidcycle_flag = true
  192. }
  193. }
  194. if fzb_publishtime == 0 { //仅赋值第一个招标信息的publishtime
  195. fzb_publishtime = zb_publishtime
  196. }
  197. }
  198. }
  199. }
  200. }
  201. //计算bidcycle标书表编制周期字段
  202. //list中招标信息中未能计算出bidcycle,用第一个招标信息的fzb_publishtime和外围bidopentime计算
  203. if !bidcycle_flag && bidopentime > 0 && fzb_publishtime > 0 {
  204. if tmpTime := bidopentime - fzb_publishtime; tmpTime > 0 {
  205. f_day := float64(tmpTime) / float64(86400)
  206. day := math.Ceil(f_day)
  207. newTmp["bidcycle"] = int(day)
  208. }
  209. }
  210. saveProjectEsPool <- newTmp
  211. tmp = make(map[string]interface{})
  212. }
  213. log.Info("create project index...over", zap.Any("mapInfo", mapInfo), zap.Int("count", n))
  214. }