project_es.go 7.1 KB

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