project_es.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. newTmp["s_projectname"] = tmp["projectname"]
  52. for f, ftype := range ProjectField {
  53. if tmp[f] != nil {
  54. if f == "package" {
  55. pp := map[string]map[string]interface{}{}
  56. if packages, ok := tmp["package"].(map[string]interface{}); ok {
  57. for _, pks := range packages {
  58. if pk, ok := pks.([]interface{}); ok {
  59. for _, v := range pk {
  60. if p, ok := v.(map[string]interface{}); ok {
  61. winner := util.ObjToString(p["winner"])
  62. bidamount := util.Float64All((p["bidamount"]))
  63. if len(winner) > 4 && bidamount > 0 {
  64. p := map[string]interface{}{
  65. "winner": winner,
  66. "bidamount": bidamount,
  67. }
  68. pp[winner] = p
  69. }
  70. }
  71. }
  72. }
  73. }
  74. } else {
  75. winner := util.ObjToString(tmp["winner"])
  76. bidamount := util.Float64All(tmp["bidamount"])
  77. if len(winner) > 4 && bidamount > 0 {
  78. p := map[string]interface{}{
  79. "winner": winner,
  80. "bidamount": bidamount,
  81. }
  82. pp[winner] = p
  83. }
  84. }
  85. pk1 := []map[string]interface{}{}
  86. for _, v := range pp {
  87. pk1 = append(pk1, v)
  88. }
  89. if len(pk1) > 0 {
  90. newTmp["package1"] = pk1
  91. }
  92. } else if f == "topscopeclass" {
  93. if topscopeclass, ok := tmp["topscopeclass"].([]interface{}); ok {
  94. tc := []string{}
  95. m2 := map[string]bool{}
  96. for _, v := range topscopeclass {
  97. str := util.ObjToString(v)
  98. str = regLetter.ReplaceAllString(str, "") // 去除字母
  99. if !m2[str] {
  100. m2[str] = true
  101. tc = append(tc, str)
  102. }
  103. }
  104. newTmp["topscopeclass"] = tc
  105. }
  106. } else if f == "list" {
  107. if list, ok := tmp[f].([]interface{}); ok {
  108. var newList []map[string]interface{}
  109. for _, item := range list {
  110. item1 := item.(map[string]interface{})
  111. listm := make(map[string]interface{})
  112. for f1, ftype1 := range ProjectListF {
  113. if item1[f1] != nil {
  114. if f == "topscopeclass" || f == "subscopeclass" {
  115. listm[f] = item1[f1]
  116. } else {
  117. if fieldval := item1[f1]; reflect.TypeOf(fieldval).String() != ftype1 {
  118. continue
  119. } else {
  120. if fieldval != "" {
  121. listm[f1] = fieldval
  122. }
  123. }
  124. }
  125. }
  126. }
  127. newList = append(newList, listm)
  128. }
  129. newTmp[f] = newList
  130. }
  131. } else if f == "budget" || f == "bidamount" || f == "sortprice" {
  132. if tmp[f] != nil && util.Float64All(tmp[f]) <= 1000000000 {
  133. newTmp[f] = tmp[f]
  134. }
  135. } else if f == "projectscope" {
  136. projectscopeRune := []rune(util.ObjToString(tmp[f]))
  137. if len(projectscopeRune) > 1000 {
  138. newTmp[f] = util.ObjToString(tmp[f])[:1000]
  139. } else {
  140. newTmp[f] = tmp[f]
  141. }
  142. } else if f == "ids" || f == "_id" || f == "mpc" || f == "mpn" || f == "review_experts" || f == "winnerorder" ||
  143. f == "entidlist" || f == "first_cooperation" || f == "subscopeclass" {
  144. newTmp[f] = tmp[f]
  145. } else {
  146. if fieldval := tmp[f]; reflect.TypeOf(fieldval).String() != ftype {
  147. continue
  148. } else {
  149. if fieldval != "" {
  150. newTmp[f] = fieldval
  151. }
  152. }
  153. }
  154. }
  155. }
  156. budget := util.Float64All(newTmp["budget"])
  157. bidamount := util.Float64All(newTmp["bidamount"])
  158. if float64(budget) > 0 && float64(bidamount) > 0 {
  159. rate := float64(1) - float64(bidamount)/float64(budget)
  160. f, _ := strconv.ParseFloat(strconv.FormatFloat(rate, 'f', 4, 64), 64)
  161. //不在0~0.6之间,不生成费率;只生成预算,中标金额舍弃,索引增加折扣率异常标识
  162. if f < 0 || f > 0.6 {
  163. delete(newTmp, "bidamount")
  164. newTmp["prate_flag"] = 1
  165. } else {
  166. newTmp["project_rate"] = f
  167. }
  168. }
  169. bidopentime := util.Int64All(tmp["bidopentime"]) //开标日期
  170. fzb_publishtime := int64(0) //记录第一个招标信息的publishtime
  171. bidcycle_flag := false //判断是否已计算出标书表编制周期
  172. list := tmp["list"].([]interface{})
  173. for _, m := range list {
  174. tmpM := m.(map[string]interface{})
  175. if bidamount, ok := tmpM["bidamount"].(string); ok && len(bidamount) > 0 { //bidamount为string类型,转成float
  176. tmpB := util.Float64All(tmpM["bidamount"])
  177. tmpM["bidamount"] = tmpB
  178. }
  179. //计算bidcycle标书表编制周期字段
  180. if !bidcycle_flag && bidopentime > 0 { //bidopentime>0证明list中有bidopentime,无则不用计算bidcycle
  181. if toptype := util.ObjToString(tmpM["toptype"]); toptype == "招标" {
  182. zb_bidopentime := util.Int64All(tmpM["bidopentime"])
  183. zb_publishtime := util.Int64All(tmpM["publishtime"])
  184. if zb_publishtime > 0 {
  185. if zb_bidopentime > 0 {
  186. if tmpTime := zb_bidopentime - zb_publishtime; tmpTime > 0 {
  187. f_day := float64(tmpTime) / float64(86400)
  188. day := math.Ceil(f_day)
  189. tmp["bidcycle"] = int(day)
  190. bidcycle_flag = true
  191. }
  192. }
  193. if fzb_publishtime == 0 { //仅赋值第一个招标信息的publishtime
  194. fzb_publishtime = zb_publishtime
  195. }
  196. }
  197. }
  198. }
  199. }
  200. //计算bidcycle标书表编制周期字段
  201. //list中招标信息中未能计算出bidcycle,用第一个招标信息的fzb_publishtime和外围bidopentime计算
  202. if !bidcycle_flag && bidopentime > 0 && fzb_publishtime > 0 {
  203. if tmpTime := bidopentime - fzb_publishtime; tmpTime > 0 {
  204. f_day := float64(tmpTime) / float64(86400)
  205. day := math.Ceil(f_day)
  206. newTmp["bidcycle"] = int(day)
  207. }
  208. }
  209. saveProjectEsPool <- newTmp
  210. tmp = make(map[string]interface{})
  211. }
  212. log.Info("create project index...over", zap.Any("mapInfo", mapInfo), zap.Int("count", n))
  213. }