behavior.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package v1
  2. import (
  3. "context"
  4. "designs/app/common/request"
  5. "designs/app/common/response"
  6. "designs/global"
  7. "designs/service"
  8. "encoding/json"
  9. "errors"
  10. "github.com/gin-gonic/gin"
  11. "go.mongodb.org/mongo-driver/v2/bson"
  12. "strconv"
  13. "time"
  14. )
  15. type AdData struct {
  16. Pid string `json:"pid" bson:"pid"`
  17. Aid string `json:"aid" bson:"aid"`
  18. Cid string `json:"cid" bson:"cid"`
  19. //ReportUrl string `json:"reportUrl" bson:"reportUrl"`
  20. Reported bool `json:"reported" bson:"reported"`
  21. Duration int64 `json:"duration" bson:"duration"`
  22. AdReqCount uint8 `json:"adReqCount" bson:"adReqCount"`
  23. AdEposedcount uint8 `json:"adEposedcount" bson:"adEposedcount"`
  24. CreateTime int `json:"createTime" bson:"createTime"`
  25. }
  26. type UserBehavior struct {
  27. Id string `bson:"_id,omitempty"`
  28. Gid string `bson:"gid" json:"gid"`
  29. Pf string `bson:"pf" json:"pf"`
  30. OpenId string `bson:"openId" json:"openId"`
  31. AdData interface{} `bson:"adData" json:"adData"`
  32. AdFromCount int `bson:"adFromCount" json:"adFromCount"`
  33. TotalDuration int `bson:"totalDuration" json:"totalDuration"`
  34. TotalAdReqCount int `bson:"totalAdReqCount" json:"totalAdReqCount"`
  35. TotalAdEposedCount int `bson:"totalAdEposedCount" json:"totalAdEposedCount"`
  36. }
  37. func ReceiveUserBehavior(c *gin.Context) {
  38. form := request.Check(c, &struct {
  39. Gid string `form:"gid" json:"gid" binding:"required"`
  40. Pf string `form:"pf" json:"pf" binding:"required"`
  41. OpenId string `form:"openId" json:"openId" binding:"required"`
  42. AdData interface{} `form:"adData" json:"adData" binding:""`
  43. AdFromCount int `form:"adFromCount" json:"adFromCount" binding:""`
  44. TotalDuration int `form:"totalDuration" json:"totalDuration" binding:""`
  45. TotalAdReqCount int `form:"totalAdReqCount" json:"totalAdReqCount" binding:""`
  46. TotalAdEposedCount int `form:"totalAdEposedCount" json:"totalAdEposedCount" binding:""`
  47. }{})
  48. //if form.AdData != nil {
  49. // var AdData map[string]interface{}
  50. //}
  51. behavior := UserBehavior{
  52. Id: form.Gid + "|" + form.Pf + "|" + form.OpenId,
  53. Gid: form.Gid,
  54. Pf: form.Pf,
  55. OpenId: form.OpenId,
  56. AdData: form.AdData,
  57. AdFromCount: form.AdFromCount,
  58. TotalDuration: form.TotalDuration,
  59. TotalAdReqCount: form.TotalAdReqCount,
  60. TotalAdEposedCount: form.TotalAdEposedCount,
  61. }
  62. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  63. filter := bson.M{"_id": behavior.Id}
  64. result := make(map[string]interface{})
  65. err := collection.FindOne(context.Background(), filter).Decode(&result)
  66. if len(result) == 0 {
  67. //新增
  68. _, err = collection.InsertOne(context.Background(), behavior)
  69. if err != nil {
  70. response.Fail(c, 1003, "写入数据失败"+err.Error())
  71. return
  72. }
  73. } else {
  74. var newAdData AdData
  75. adData, _ := json.Marshal(form.AdData)
  76. json.Unmarshal(adData, &newAdData)
  77. var oldAdData AdData
  78. oldAdDatas, _ := json.Marshal(result["adData"])
  79. json.Unmarshal(oldAdDatas, &oldAdData)
  80. newAdData.Reported = oldAdData.Reported
  81. //更新到MongoDB 中
  82. update := bson.M{
  83. "$set": struct {
  84. Gid string `bson:"gid" json:"gid"`
  85. Pf string `bson:"pf" json:"pf"`
  86. OpenId string `bson:"openId" json:"openId"`
  87. AdData interface{} `bson:"adData" json:"adData"`
  88. AdFromCount int `bson:"adFromCount" json:"adFromCount"`
  89. TotalDuration int `bson:"totalDuration" json:"totalDuration"`
  90. TotalAdReqCount int `bson:"totalAdReqCount" json:"totalAdReqCount"`
  91. TotalAdEposedCount int `bson:"totalAdEposedCount" json:"totalAdEposedCount"`
  92. }{
  93. Gid: behavior.Gid,
  94. Pf: behavior.Pf,
  95. OpenId: behavior.OpenId,
  96. AdData: newAdData,
  97. AdFromCount: behavior.AdFromCount,
  98. TotalDuration: behavior.TotalDuration,
  99. TotalAdReqCount: behavior.TotalAdReqCount,
  100. TotalAdEposedCount: behavior.TotalAdEposedCount,
  101. },
  102. }
  103. _, err = collection.UpdateOne(context.Background(), filter, update)
  104. if err != nil {
  105. response.Fail(c, 1003, "写入数据失败"+err.Error())
  106. return
  107. }
  108. }
  109. //如果是从广告获取进来的用户,则需要检测是否上传
  110. if form.AdData != nil {
  111. res := service.OceanReportAction(behavior.Id)
  112. if res.Code != 0 {
  113. //上传出现了问题,或者并没有上传
  114. global.App.Log.Error(res.Code, res.Message)
  115. }
  116. }
  117. response.Success(c, gin.H{})
  118. }
  119. func CheckUserBehavior(c *gin.Context) {
  120. gid := c.GetString("gid")
  121. pf := c.GetString("pf")
  122. openId := c.GetString("openid")
  123. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  124. //
  125. filter := bson.M{"_id": gid + "|" + pf + "|" + openId}
  126. result := make(map[string]interface{})
  127. collection.FindOne(context.Background(), filter).Decode(&result)
  128. if len(result) == 0 {
  129. response.Success(c, gin.H{
  130. "data": nil,
  131. })
  132. } else {
  133. response.Success(c, gin.H{
  134. "data": result,
  135. })
  136. }
  137. }
  138. func UpdateUserBehavior(c *gin.Context) {
  139. //查出所有需要更新的数据
  140. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  141. filters := bson.M{}
  142. filters["adData"] = bson.M{"$exists": true}
  143. var result []map[string]interface{}
  144. cur, err := collection.Find(context.Background(), filters)
  145. if err != nil {
  146. response.Fail(c, 1001, err.Error())
  147. }
  148. err = cur.All(context.Background(), &result)
  149. if err != nil {
  150. response.Fail(c, 1001, err.Error())
  151. return
  152. }
  153. for _, v := range result {
  154. var adData map[string]interface{}
  155. oldAdDatas, _ := json.Marshal(v["adData"])
  156. json.Unmarshal(oldAdDatas, &adData)
  157. if adData["createtime"] != nil {
  158. //错误数据,需要修正
  159. filter := bson.M{"_id": v["_id"]}
  160. //
  161. adData["createTime"] = adData["createtime"]
  162. adData["adReqCount"] = adData["adreqcount"]
  163. adData["adEposedcount"] = adData["adeposedcount"]
  164. adData["reportUrl"] = adData["reporturl"]
  165. delete(adData, "createtime")
  166. delete(adData, "adreqcount")
  167. delete(adData, "adeposedcount")
  168. delete(adData, "reporturl")
  169. //更新到MongoDB 中
  170. update := bson.M{
  171. "$set": struct {
  172. AdData map[string]interface{} `bson:"adData" json:"adData"`
  173. }{
  174. AdData: adData,
  175. },
  176. }
  177. _, err = collection.UpdateOne(context.Background(), filter, update)
  178. if err != nil {
  179. response.Fail(c, 1003, "写入数据失败"+err.Error())
  180. return
  181. }
  182. }
  183. }
  184. defer cur.Close(context.Background())
  185. //更新成正确的数据
  186. response.Success(c, gin.H{
  187. "data": result,
  188. })
  189. }
  190. func FindUserBehavior(id string) *UserBehavior {
  191. filter := bson.M{"_id": id}
  192. var result UserBehavior
  193. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  194. err := collection.FindOne(context.Background(), filter).Decode(&result)
  195. if err != nil {
  196. return nil
  197. }
  198. return &result
  199. }
  200. // 新建UserBehavior
  201. func CreateUserBehavior(gid string, pf string, openId string) error {
  202. id := gid + "|" + pf + "|" + openId
  203. Behavior := FindUserBehavior(id)
  204. if Behavior != nil {
  205. return errors.New("该用户数据已经存在")
  206. }
  207. behavior := UserBehavior{
  208. Id: id,
  209. Gid: gid,
  210. Pf: pf,
  211. OpenId: openId,
  212. AdData: nil,
  213. AdFromCount: 0,
  214. TotalDuration: 0,
  215. TotalAdReqCount: 0,
  216. TotalAdEposedCount: 0,
  217. }
  218. //新增
  219. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  220. _, err := collection.InsertOne(context.Background(), behavior)
  221. if err != nil {
  222. return err
  223. }
  224. return nil
  225. }
  226. // 更新UserBehavior 在线时间
  227. func UpdateUserBehaviorDuration(gid string, pf string, openId string, now time.Time, types int) error {
  228. id := gid + "|" + pf + "|" + openId
  229. Behavior := FindUserBehavior(id)
  230. if Behavior == nil {
  231. return errors.New("该用户数据未保存,无法计算在线时间")
  232. }
  233. lastTime, err := global.App.Redis.Get(context.Background(), id+"|online").Result()
  234. if err != nil {
  235. return err
  236. }
  237. //当状态为在线时,刷新redis数据,状态为2是离线,不刷新
  238. if types == 1 {
  239. global.App.Redis.Set(context.Background(), id+"|online", now.Unix(), 300)
  240. } else {
  241. global.App.Redis.Del(context.Background(), id+"|online")
  242. }
  243. //如果redis中没有查到在线状态,那就视为刚刚登录,不增加时间
  244. if lastTime == "" {
  245. return nil
  246. }
  247. //计算时间差
  248. lastTimeUnix, _ := strconv.ParseInt(lastTime, 10, 64)
  249. duration := now.Unix() - lastTimeUnix
  250. var adData interface{}
  251. if Behavior.AdData != nil {
  252. //存在adData
  253. var newAdData AdData
  254. oldAdDatas, _ := json.Marshal(Behavior.AdData)
  255. json.Unmarshal(oldAdDatas, &newAdData)
  256. newAdData.Duration = newAdData.Duration + duration
  257. adData = newAdData
  258. }
  259. //更新到MongoDB 中
  260. update := bson.M{
  261. "$set": struct {
  262. AdData interface{} `bson:"adData" json:"adData"`
  263. TotalDuration int `bson:"duration" json:"duration"`
  264. }{
  265. AdData: adData,
  266. TotalDuration: Behavior.TotalDuration + int(duration),
  267. },
  268. }
  269. filter := bson.M{"_id": id}
  270. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  271. _, err = collection.UpdateOne(context.Background(), filter, update)
  272. if err != nil {
  273. return err
  274. }
  275. return nil
  276. }
  277. // 更新UserBehavior 看广告的数据
  278. func UpdateUserBehaviorAdInfo(gid string, pf string, openId string, adsState int) error {
  279. id := gid + "|" + pf + "|" + openId
  280. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  281. filter := bson.M{"_id": id}
  282. Behavior := FindUserBehavior(id)
  283. if Behavior == nil {
  284. return errors.New("该用户数据未保存,无法计算在线时间")
  285. }
  286. var adData interface{}
  287. if Behavior.AdData != nil {
  288. //存在adData
  289. var newAdData AdData
  290. oldAdDatas, _ := json.Marshal(Behavior.AdData)
  291. json.Unmarshal(oldAdDatas, &newAdData)
  292. newAdData.AdReqCount++
  293. if adsState == 2 {
  294. newAdData.AdEposedcount++
  295. }
  296. adData = newAdData
  297. }
  298. var AdReqCount int
  299. var AdEposedcount int
  300. var AdFormCount int
  301. if adsState == 0 {
  302. //展示不成功
  303. AdReqCount = 1
  304. AdFormCount = 1
  305. } else if adsState == 1 {
  306. //展示成功
  307. AdReqCount = 1
  308. AdFormCount = 1
  309. } else if adsState == 2 {
  310. //展示成功并且看完
  311. AdReqCount = 1
  312. AdFormCount = 1
  313. AdEposedcount = 1
  314. }
  315. //更新到MongoDB 中
  316. update := bson.M{
  317. "$set": struct {
  318. AdData interface{} `bson:"adData" json:"adData"`
  319. AdFromCount int `bson:"adFromCount" json:"adFromCount"`
  320. TotalAdReqCount int `bson:"totalAdReqCount" json:"totalAdReqCount"`
  321. TotalAdEposedCount int `bson:"totalAdEposedCount" json:"totalAdEposedCount"`
  322. }{
  323. AdData: adData,
  324. AdFromCount: Behavior.AdFromCount + AdFormCount,
  325. TotalAdReqCount: Behavior.TotalAdReqCount + AdReqCount,
  326. TotalAdEposedCount: Behavior.TotalAdEposedCount + AdEposedcount,
  327. },
  328. }
  329. _, err := collection.UpdateOne(context.Background(), filter, update)
  330. if err != nil {
  331. return err
  332. }
  333. return nil
  334. }