bill.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package controllers
  2. import (
  3. "fmt"
  4. "hongze/hongze_api/models"
  5. "hongze/hongze_api/utils"
  6. "time"
  7. )
  8. //年度账单
  9. type BillController struct {
  10. BaseAuthController
  11. }
  12. // @Title 年度账单接口
  13. // @Description 年度账单接口
  14. // @Success 200 {object} models.BillDetailResp
  15. // @router /detail [get]
  16. func (this *BillController) Detail() {
  17. br := new(models.BaseResponse).Init()
  18. defer func() {
  19. this.Data["json"] = br
  20. this.ServeJSON()
  21. }()
  22. user := this.User
  23. if user == nil {
  24. br.Msg = "请登录"
  25. br.ErrMsg = "请登录,用户信息为空"
  26. br.Ret = 408
  27. return
  28. }
  29. startDate := "2020-01-01 00:00:00"
  30. endDate := time.Now().Format(utils.FormatDate)
  31. uid := user.UserId
  32. var realName, togetherDay, createDate string
  33. resp := new(models.BillDetailResp)
  34. if user.RealName == "" {
  35. realName = user.NickName
  36. } else {
  37. realName = user.RealName
  38. }
  39. if !user.CreatedTime.IsZero() {
  40. sub := time.Now().Sub(user.CreatedTime)
  41. if sub < 0 {
  42. sub = 1
  43. }
  44. expireDay := fmt.Sprintf("%v", int(sub.Hours()/24))
  45. togetherDay = expireDay
  46. createDate = user.CreatedTime.Format("2006年01月02日")
  47. } else {
  48. sub := time.Now().Sub(user.LastUpdatedTime)
  49. if sub < 0 {
  50. sub = 1
  51. }
  52. expireDay := fmt.Sprintf("%v", int(sub.Hours()/24))
  53. togetherDay = expireDay
  54. createDate = user.LastUpdatedTime.Format("2006年01月02日")
  55. }
  56. //uid = 41555
  57. firstReadReportType, firstReadReportTitle, err := models.GetFirstReportInfo(uid, startDate)
  58. if err != nil && err.Error() != utils.ErrNoRow() {
  59. br.Msg = "获取数据失败"
  60. br.ErrMsg = "获取数据失败GetFirstReportInfo,Err:" + err.Error()
  61. return
  62. }
  63. listenCount, listenVideoPlaySeconds, err := models.GetListenInfo(uid, startDate)
  64. if err != nil && err.Error() != utils.ErrNoRow() {
  65. br.Msg = "获取数据失败"
  66. br.ErrMsg = "获取数据失败GetListenInfo,Err:" + err.Error()
  67. return
  68. }
  69. maxReadReportCount, maxReadReportDate, err := models.GetMaxReadReportInfo(uid, startDate)
  70. if err != nil && err.Error() != utils.ErrNoRow() {
  71. br.Msg = "获取数据失败"
  72. br.ErrMsg = "获取数据失败GetMaxReadReportInfo,Err:" + err.Error()
  73. return
  74. }
  75. latestTime, latestCreateTime, err := models.GetLatestReadReportInfo(uid, startDate)
  76. if err != nil && err.Error() != utils.ErrNoRow() {
  77. br.Msg = "获取数据失败"
  78. br.ErrMsg = "获取数据失败GetLatestReadReportInfo,Err:" + err.Error()
  79. return
  80. }
  81. var lastestCreateDate string
  82. var VideoPlaySeconds float64
  83. if !latestCreateTime.IsZero() {
  84. lastestCreateDate = latestCreateTime.Format(utils.FormatDate)
  85. }
  86. if lastestCreateDate != "" {
  87. rddpReadCount, rddpVideoPlaySeconds, err := models.GetRddpReadReportCountByDate(uid, lastestCreateDate)
  88. if err != nil && err.Error() != utils.ErrNoRow() {
  89. br.Msg = "获取数据失败"
  90. br.ErrMsg = "获取数据失败GetRddpReadReportCountByDate,Err:" + err.Error()
  91. return
  92. }
  93. weekReadCount, err := models.GetWeekReadReportCountByDate(uid, lastestCreateDate)
  94. if err != nil && err.Error() != utils.ErrNoRow() {
  95. br.Msg = "获取数据失败"
  96. br.ErrMsg = "获取数据失败GetWeekReadReportCountByDate,Err:" + err.Error()
  97. return
  98. }
  99. var weekVideoPlaySeconds float64
  100. if rddpReadCount <= 0 {
  101. weekVideoPlaySeconds = float64(utils.GetRandInt(3, 300)) / float64(10.00)
  102. } else {
  103. weekVideoPlaySeconds = float64(weekReadCount) * (rddpVideoPlaySeconds / float64(rddpReadCount))
  104. }
  105. VideoPlaySeconds = utils.FixFloat(rddpVideoPlaySeconds+weekVideoPlaySeconds, 2)
  106. }
  107. maxOpenReportClassify, maxOpenReportCount, err := models.GetOpenReadReportInfo(uid, startDate)
  108. if err != nil && err.Error() != utils.ErrNoRow() {
  109. br.Msg = "获取数据失败"
  110. br.ErrMsg = "获取数据失败GetOpenReadReportInfo,Err:" + err.Error()
  111. return
  112. }
  113. rddpTotalPlaySeconds, rddpTotal, err := models.GetRddpTotalReadDuration(uid, startDate)
  114. if err != nil && err.Error() != utils.ErrNoRow() {
  115. br.Msg = "获取数据失败"
  116. br.ErrMsg = "获取数据失败GetRddpTotalReadDuration,Err:" + err.Error()
  117. return
  118. }
  119. weekTotal, err := models.GetWeekTotalRead(uid, startDate)
  120. if err != nil && err.Error() != utils.ErrNoRow() {
  121. br.Msg = "获取数据失败"
  122. br.ErrMsg = "获取数据失败GetRddpTotalReadDuration,Err:" + err.Error()
  123. return
  124. }
  125. totalReadDuration := 0.00
  126. if rddpTotal <= 0 {
  127. totalReadDuration = rddpTotalPlaySeconds + float64(weekTotal)*(float64(utils.GetRandInt(3, 300))/float64(10.00))
  128. } else {
  129. totalReadDuration = rddpTotalPlaySeconds + (rddpTotalPlaySeconds/float64(rddpTotal))*float64(weekTotal)
  130. }
  131. dayType := `day`
  132. dayTotal, err := models.GetWeekTotalReadByType(uid, dayType, startDate)
  133. if err != nil && err.Error() != utils.ErrNoRow() {
  134. br.Msg = "获取数据失败"
  135. br.ErrMsg = "获取数据失败GetWeekTotalReadByType,Err:" + err.Error() + ";report_type:" + dayType
  136. return
  137. }
  138. weekType := `week`
  139. weekReportReadTotal, err := models.GetWeekTotalReadByType(uid, weekType, startDate)
  140. if err != nil && err.Error() != utils.ErrNoRow() {
  141. br.Msg = "获取数据失败"
  142. br.ErrMsg = "获取数据失败GetWeekTotalReadByType,Err:" + err.Error() + ";report_type:" + weekType
  143. return
  144. }
  145. twoWeekType := `two_week`
  146. twoWeekTotal, err := models.GetWeekTotalReadByType(uid, twoWeekType, startDate)
  147. if err != nil && err.Error() != utils.ErrNoRow() {
  148. br.Msg = "获取数据失败"
  149. br.ErrMsg = "获取数据失败GetWeekTotalReadByType,Err:" + err.Error() + ";report_type:" + twoWeekType
  150. return
  151. }
  152. monthType := `month`
  153. monthTotal, err := models.GetWeekTotalReadByType(uid, dayType, startDate)
  154. if err != nil && err.Error() != utils.ErrNoRow() {
  155. br.Msg = "获取数据失败"
  156. br.ErrMsg = "获取数据失败GetWeekTotalReadByType,Err:" + err.Error() + ";report_type:" + monthType
  157. return
  158. }
  159. rddpReadTotal, err := models.GetRddpTotalReadByType(uid, startDate)
  160. if err != nil && err.Error() != utils.ErrNoRow() {
  161. br.Msg = "获取数据失败"
  162. br.ErrMsg = "获取数据失败GetRddpTotalReadByType,Err:" + err.Error() + ";report_type:" + monthType
  163. return
  164. }
  165. var learnDay int
  166. rddpLearnDay, err := models.GetRddpLearnDay(uid, startDate, endDate)
  167. if err != nil && err.Error() != utils.ErrNoRow() {
  168. br.Msg = "获取数据失败"
  169. br.ErrMsg = "获取数据失败GetRddpLearnDay,Err:" + err.Error() + ";report_type:" + monthType
  170. return
  171. }
  172. weekLearnDay, err := models.GetWeekpLearnDay(uid, startDate)
  173. if err != nil && err.Error() != utils.ErrNoRow() {
  174. br.Msg = "获取数据失败"
  175. br.ErrMsg = "获取数据失败GetWeekpLearnDay,Err:" + err.Error() + ";report_type:" + monthType
  176. return
  177. }
  178. if rddpLearnDay >= weekLearnDay {
  179. learnDay = rddpLearnDay
  180. } else {
  181. learnDay = weekLearnDay
  182. }
  183. if learnDay <= 0 {
  184. learnDay = 1
  185. }
  186. resp.RealName = realName
  187. resp.TogetherDay = togetherDay
  188. resp.CreateDate = createDate
  189. if firstReadReportType == "rddp" {
  190. resp.FirstReadReportType = "日度点评"
  191. } else if firstReadReportType == "day" {
  192. resp.FirstReadReportType = "晨报"
  193. } else if firstReadReportType == "week" {
  194. resp.FirstReadReportType = "周报"
  195. } else if firstReadReportType == "two_week" {
  196. resp.FirstReadReportType = "双周报"
  197. } else if firstReadReportType == "month" {
  198. resp.FirstReadReportType = "月报"
  199. } else {
  200. resp.FirstReadReportType = "其他"
  201. }
  202. resp.FirstReadReportTitle = firstReadReportTitle
  203. resp.ListenReportCount = listenCount
  204. resp.ListenReportDuration = utils.FixFloat((listenVideoPlaySeconds / 60.00), 2)
  205. resp.MaxReadReportCount = maxReadReportCount
  206. resp.MaxReadReportDate = maxReadReportDate.Format("2006年01月02日")
  207. if lastestCreateDate != "" {
  208. resp.LatestReadReportDate = ""
  209. } else {
  210. lastestDate, _ := time.Parse("2006-01-02", lastestCreateDate)
  211. resp.LatestReadReportDate = lastestDate.Format("2006年01月02日")
  212. }
  213. resp.LatestReadReportTime = latestTime
  214. resp.LatestReadReportDateDuration = VideoPlaySeconds
  215. resp.MaxOpenReportClassify = maxOpenReportClassify
  216. resp.MaxOpenReportCount = maxOpenReportCount
  217. resp.TotalReadDuration = totalReadDuration
  218. resp.TotalReportDayCount = dayTotal
  219. resp.TotalReportWeekCount = weekReportReadTotal
  220. resp.TotalReportTwoWeekCount = twoWeekTotal
  221. resp.TotalReportMonthCount = monthTotal
  222. resp.TotalReportRddpCount = rddpReadTotal
  223. resp.LearnDay = learnDay
  224. resp.TotalReport = dayTotal + weekReportReadTotal + twoWeekTotal + monthTotal + rddpReadTotal
  225. br.Ret = 200
  226. br.Success = true
  227. br.Msg = "获取成功"
  228. br.Data = resp
  229. }
  230. //
  231. //func init() {
  232. // fmt.Println("start")
  233. // uid:=12008
  234. // startDate:=`2020-01-01 00:00:00`
  235. // latestTime, latestCreateTime, err := models.GetLatestReadReportInfo(uid, startDate)
  236. // fmt.Println(latestTime, latestCreateTime)
  237. // if err != nil && err.Error() != utils.ErrNoRow() {
  238. // return
  239. // }
  240. // var lastestCreateDate string
  241. // if !latestCreateTime.IsZero() {
  242. // fmt.Println("line 256")
  243. // lastestCreateDate = latestCreateTime.Format(utils.FormatDate)
  244. // }else{
  245. // fmt.Println("line 259")
  246. // }
  247. //
  248. // fmt.Println(latestTime,lastestCreateDate)
  249. //
  250. // fmt.Println("end")
  251. //}