timetask.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package entity
  2. import (
  3. util "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jybase/mongodb"
  6. "bp.jydev.jianyu360.cn/BaseService/biService/vipTask/config"
  7. "fmt"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. "time"
  11. )
  12. type TimeTask struct {
  13. }
  14. func (t *TimeTask) Run() {
  15. go util.SimpleCrontab(false, config.DbConf.StatisticTime, func() {
  16. t.DataHandle(config.DbConf.DailyConfig)
  17. })
  18. }
  19. func (t *TimeTask) DataHandle(allocation []*config.ExpireConfig) {
  20. for _, expireConfig := range allocation {
  21. sourceMap := config.Subjectdb.FindOne("clue_info_source", map[string]interface{}{
  22. "judge": expireConfig.EndDays,
  23. }, "id", "")
  24. if sourceMap == nil {
  25. logx.Info(expireConfig.EndDays, ":查不到来源")
  26. return
  27. }
  28. sourceId := gconv.Int64((*sourceMap)["id"])
  29. //数据查询处理
  30. //个人数据处理
  31. PersonExpireHandle(expireConfig, sourceId)
  32. //企业数据处理
  33. EntExpireHandle(expireConfig, sourceId)
  34. }
  35. }
  36. // 分批处理数据
  37. func PersonExpireHandle(allocation *config.ExpireConfig, sourceId int64) {
  38. //按时间查询数据
  39. endTime := GetFutureDateTime(allocation.EndDays)
  40. startTime := GetFutureDateTime(allocation.StartDays)
  41. sess := config.UserMgo.GetMgoConn()
  42. defer config.UserMgo.DestoryMongoConn(sess)
  43. it := sess.DB("qfw").C("user").Find(map[string]interface{}{
  44. "l_vip_endtime": map[string]interface{}{
  45. "$gte": startTime,
  46. "$lte": endTime,
  47. },
  48. "i_vip_status": map[string]interface{}{
  49. "$gt": 0,
  50. },
  51. }).Select(map[string]interface{}{
  52. "s_phone": 1,
  53. "s_m_phone": 1,
  54. "_id": 1,
  55. "l_vip_endtime": 1,
  56. "l_vip_starttime": 1,
  57. "s_jyname": 1,
  58. "s_nickname": 1,
  59. "s_company": 1,
  60. }).Iter()
  61. for m := make(map[string]interface{}); it.Next(&m); {
  62. //logx.Info("个人数据处理", allocation, m)
  63. userId := mongodb.BsonIdToSId(m["_id"])
  64. /*endTime := gconv.Int64(m["l_vip_endtime"])
  65. startTime := gconv.Int64(m["l_vip_starttime"])*/
  66. company := gconv.String(m["s_company"])
  67. phone := gconv.String(m["s_phone"])
  68. nickname := gconv.String(m["s_nickname"])
  69. if nickname == "" {
  70. nickname = gconv.String(m["s_jyname"])
  71. }
  72. if phone == "" {
  73. phone = gconv.String(m["s_m_phone"])
  74. }
  75. //if TimeHandle(startTime, endTime, allocation.CycleDays) {
  76. //周期符合条件
  77. //查看新增还是修改
  78. if config.Subjectdb.Count("clue_info", map[string]interface{}{
  79. "userId": userId,
  80. }) > 0 {
  81. //修改
  82. config.Subjectdb.Update("clue_info", map[string]interface{}{
  83. "userId": userId,
  84. }, map[string]interface{}{
  85. "sourceId": sourceId,
  86. "updatetime": time.Now().Format(date.Date_Full_Layout),
  87. })
  88. } else {
  89. //新增
  90. config.Subjectdb.Insert("clue_info", map[string]interface{}{
  91. "sourceId": sourceId,
  92. "updatetime": time.Now().Format(date.Date_Full_Layout),
  93. "phone": phone,
  94. "name": nickname,
  95. "companyName": company,
  96. "createtime": time.Now().Format(date.Date_Full_Layout),
  97. "userId": userId,
  98. })
  99. }
  100. //}
  101. }
  102. }
  103. func EntExpireHandle(allocation *config.ExpireConfig, sourceId int64) {
  104. //按时间查询数据
  105. endTime := GetFutureDateTimeStr(allocation.EndDays)
  106. startTime := GetFutureDateTimeStr(allocation.StartDays)
  107. config.JianYu.SelectByBath(1, func(l *[]map[string]interface{}) bool {
  108. go func(m map[string]interface{}) {
  109. //logx.Info("企业数据处理", allocation, m)
  110. //企业开始结束时间查询
  111. endId := gconv.Int64(m["ent_id"])
  112. if endId == 86268 {
  113. logx.Info(1111)
  114. }
  115. orderList := config.JianYu.SelectBySql("select start_time,end_time from entniche_wait_empower where ent_id=? ORDER BY end_time desc", endId)
  116. if (*orderList) == nil || len(*orderList) == 0 {
  117. return
  118. }
  119. endTime := int64(0)
  120. startTime := int64(0)
  121. if len(*orderList) == 1 {
  122. time1, _ := time.Parse("2006-01-02 15:04:05", gconv.String((*orderList)[0]["start_time"]))
  123. time2, _ := time.Parse("2006-01-02 15:04:05", gconv.String((*orderList)[0]["end_time"]))
  124. startTime = time1.Unix()
  125. endTime = time2.Unix()
  126. } else {
  127. startTime, endTime = CalculateSuperOrderPeriod(orderList)
  128. }
  129. userId := ""
  130. nickname := ""
  131. logx.Info("企业数据处理", allocation, m, time.Unix(startTime, 0), time.Unix(endTime, 0))
  132. if TimeHandle(startTime, endTime, allocation.CycleDays) {
  133. //周期符合条件
  134. //查看新增还是修改
  135. //先查询企业信息
  136. entMap := config.JianYu.FindOne("entniche_info", map[string]interface{}{
  137. "id": endId,
  138. }, "name,phone", "")
  139. if entMap == nil {
  140. logx.Info(endId, ":企业查询不到")
  141. }
  142. company := gconv.String((*entMap)["name"])
  143. phone := gconv.String((*entMap)["phone"])
  144. //用户信息查询
  145. userData, ok := config.UserMgo.Find("user", map[string]interface{}{
  146. "i_appid": 2,
  147. "$or": []map[string]interface{}{
  148. {"s_phone": phone},
  149. {"s_m_phone": phone}},
  150. }, `{"s_phone":-1}`, `{"_id":1,"s_nickname":1,"s_jyname":1}`, false, -1, -1)
  151. if ok && userData != nil && len(*userData) > 0 {
  152. nickname = gconv.String((*userData)[0]["s_nickname"])
  153. if nickname == "" {
  154. nickname = gconv.String((*userData)[0]["s_jyname"])
  155. }
  156. userId = gconv.String(mongodb.BsonIdToSId((*userData)[0]["_id"]))
  157. }
  158. if config.Subjectdb.Count("clue_info", map[string]interface{}{
  159. "userId": userId,
  160. }) > 0 {
  161. //修改
  162. config.Subjectdb.Update("clue_info", map[string]interface{}{
  163. "userId": userId,
  164. }, map[string]interface{}{
  165. "sourceId": sourceId,
  166. "name": nickname,
  167. "companyName": company,
  168. "updatetime": time.Now().Format(date.Date_Full_Layout),
  169. })
  170. } else {
  171. //新增
  172. config.Subjectdb.Insert("clue_info", map[string]interface{}{
  173. "sourceId": sourceId,
  174. "updatetime": time.Now().Format(date.Date_Full_Layout),
  175. "phone": phone,
  176. "name": nickname,
  177. "companyName": company,
  178. "createtime": time.Now().Format(date.Date_Full_Layout),
  179. "userId": userId,
  180. })
  181. }
  182. }
  183. }((*l)[0])
  184. return true
  185. }, ` select * from ( select ent_id,MAX(end_time) end_time from entniche_wait_empower where product_type="VIP订阅" GROUP BY ent_id ) a where a.end_time<=? and a.end_time>=?`, endTime, startTime)
  186. fmt.Println(` select * from ( select ent_id,MAX(end_time) end_time from entniche_wait_empower GROUP BY ent_id ) a where a.end_time<=? and a.end_time>=?`, endTime, startTime)
  187. }
  188. // 获取几天以后的时间
  189. func GetFutureDateTime(days int64) int64 {
  190. // 获取当前时间
  191. now := time.Now()
  192. // 计算未来的时间
  193. future := now.AddDate(0, 0, gconv.Int(days))
  194. // 设置时间为23点59分59秒
  195. future = time.Date(future.Year(), future.Month(), future.Day(), 23, 59, 59, 0, future.Location())
  196. return future.Unix()
  197. }
  198. // 获取几天以后的时间
  199. func GetFutureDateTimeStr(days int64) string {
  200. // 获取当前时间
  201. now := time.Now()
  202. // 计算未来的时间
  203. future := now.AddDate(0, 0, gconv.Int(days))
  204. // 设置时间为23点59分59秒
  205. future = time.Date(future.Year(), future.Month(), future.Day(), 23, 59, 59, 0, future.Location())
  206. return future.Format("2006-01-02 15:04:05")
  207. }
  208. // 时间处理
  209. func TimeHandle(startTime, endTime, cycleDays int64) bool {
  210. if endTime-startTime > cycleDays*24*60*60 {
  211. return true
  212. }
  213. return false
  214. }
  215. type Order struct {
  216. StartDate time.Time
  217. EndDate time.Time
  218. }
  219. // 时间对比
  220. func CalculateSuperOrderPeriod(orders *[]map[string]interface{}) (int64, int64) {
  221. startDate, _ := time.Parse("2006-01-02 15:04:05", gconv.String((*orders)[0]["start_time"]))
  222. endDate, _ := time.Parse("2006-01-02 15:04:05", gconv.String((*orders)[0]["end_time"]))
  223. for i, order := range *orders {
  224. if i == 0 {
  225. continue
  226. }
  227. time1, _ := time.Parse("2006-01-02 15:04:05", gconv.String(order["start_time"]))
  228. time2, _ := time.Parse("2006-01-02 15:04:05", gconv.String(order["end_time"]))
  229. //先查看有没有交集
  230. if startDate.Unix() <= time2.Unix() || (startDate.Unix() > time2.Unix() && startDate.Unix()-time2.Unix() < 24*60*60) {
  231. if startDate.Unix() >= time1.Unix() {
  232. startDate = time1
  233. }
  234. } else {
  235. break
  236. }
  237. }
  238. return startDate.Unix(), endDate.Unix()
  239. }