bill.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package controllers
  2. import (
  3. "fmt"
  4. "hongze/hongze_api/models"
  5. "hongze/hongze_api/services"
  6. "hongze/hongze_api/utils"
  7. "strconv"
  8. "time"
  9. )
  10. //年度账单
  11. type BillController struct {
  12. BaseAuthController
  13. }
  14. // @Title 年度账单接口
  15. // @Description 年度账单接口
  16. // @Success 200 {object} models.BillDetailResp
  17. // @router /detail [get]
  18. func (this *BillController) Detail() {
  19. br := new(models.BaseResponse).Init()
  20. defer func() {
  21. this.Data["json"] = br
  22. this.ServeJSON()
  23. }()
  24. user := this.User
  25. if user == nil {
  26. br.Msg = "请登录"
  27. br.ErrMsg = "请登录,用户信息为空"
  28. br.Ret = 600
  29. return
  30. }
  31. uid := user.UserId
  32. userInfo, err := models.GetWxUserItemByUserId(uid)
  33. if err != nil {
  34. br.Ret = 600
  35. br.Msg = "登录失败"
  36. br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
  37. return
  38. }
  39. permission, err := services.CheckUserPermission(uid)
  40. if err != nil {
  41. br.Msg = "登录失败"
  42. br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
  43. return
  44. }
  45. if permission != 0 {
  46. br.Msg = "判断用户是否有权限失败"
  47. br.ErrMsg = "判断用户是否有权限失败:uid:" + strconv.Itoa(uid)
  48. br.Ret = 600
  49. br.IsSendEmail = false
  50. return
  51. }
  52. startDate := "2020-01-01 00:00:00"
  53. endDate := time.Now().Format(utils.FormatDate)
  54. var realName, togetherDay, createDate string
  55. resp := new(models.BillDetailResp)
  56. if user.RealName == "" {
  57. realName = user.NickName
  58. } else {
  59. realName = user.RealName
  60. }
  61. if !user.CreatedTime.IsZero() {
  62. sub := time.Now().Sub(user.CreatedTime)
  63. if sub <= 0 {
  64. sub = 1
  65. }
  66. expireDay := fmt.Sprintf("%v", int(sub.Hours()/24))
  67. togetherDay = expireDay
  68. createDate = user.CreatedTime.Format("2006年01月02日")
  69. } else {
  70. sub := time.Now().Sub(user.LastUpdatedTime)
  71. if sub <= 0 {
  72. sub = 1
  73. }
  74. expireDay := fmt.Sprintf("%v", int(sub.Hours()/24))
  75. togetherDay = expireDay
  76. createDate = user.LastUpdatedTime.Format("2006年01月02日")
  77. }
  78. if togetherDay == "0" {
  79. togetherDay = "1"
  80. }
  81. //uid = 41555
  82. firstReadReportType, firstReadReportTitle, err := models.GetFirstReportInfo(uid, startDate)
  83. if err != nil && err.Error() != utils.ErrNoRow() {
  84. br.Msg = "获取数据失败"
  85. br.ErrMsg = "获取数据失败GetFirstReportInfo,Err:" + err.Error()
  86. return
  87. }
  88. listenCount, listenVideoPlaySeconds, err := models.GetListenInfo(uid, startDate)
  89. if err != nil && err.Error() != utils.ErrNoRow() {
  90. br.Msg = "获取数据失败"
  91. br.ErrMsg = "获取数据失败GetListenInfo,Err:" + err.Error()
  92. return
  93. }
  94. maxReadReportCount, maxReadReportDate, err := models.GetMaxReadReportInfo(uid, startDate)
  95. if err != nil && err.Error() != utils.ErrNoRow() {
  96. br.Msg = "获取数据失败"
  97. br.ErrMsg = "获取数据失败GetMaxReadReportInfo,Err:" + err.Error()
  98. return
  99. }
  100. latestTime, latestCreateTime, err := models.GetLatestReadReportInfo(uid, startDate)
  101. if err != nil && err.Error() != utils.ErrNoRow() {
  102. br.Msg = "获取数据失败"
  103. br.ErrMsg = "获取数据失败GetLatestReadReportInfo,Err:" + err.Error()
  104. return
  105. }
  106. var lastestCreateDate string
  107. var VideoPlaySeconds float64
  108. if !latestCreateTime.IsZero() {
  109. lastestCreateDate = latestCreateTime.Format(utils.FormatDate)
  110. addCreateTime := latestCreateTime
  111. hour := latestCreateTime.Hour()
  112. if hour > 6 || hour <= 18 {
  113. rand := utils.GetRandInt(18, 23)
  114. addHour := rand - hour
  115. if addHour >= 0 {
  116. hh, _ := time.ParseDuration(strconv.Itoa(addHour) + "h")
  117. hh1 := addCreateTime.Add(hh)
  118. latestTime = hh1.Format("15:04:05")
  119. }
  120. }
  121. }
  122. if lastestCreateDate != "" {
  123. rddpReadCount, rddpVideoPlaySeconds, err := models.GetRddpReadReportCountByDate(uid, lastestCreateDate)
  124. if err != nil && err.Error() != utils.ErrNoRow() {
  125. br.Msg = "获取数据失败"
  126. br.ErrMsg = "获取数据失败GetRddpReadReportCountByDate,Err:" + err.Error()
  127. return
  128. }
  129. weekReadCount, err := models.GetWeekReadReportCountByDate(uid, lastestCreateDate)
  130. if err != nil && err.Error() != utils.ErrNoRow() {
  131. br.Msg = "获取数据失败"
  132. br.ErrMsg = "获取数据失败GetWeekReadReportCountByDate,Err:" + err.Error()
  133. return
  134. }
  135. var weekVideoPlaySeconds float64
  136. if rddpReadCount <= 0 {
  137. weekVideoPlaySeconds = float64(utils.GetRandInt(3, 300)) / float64(10.00)
  138. } else {
  139. weekVideoPlaySeconds = float64(weekReadCount) * (rddpVideoPlaySeconds / float64(rddpReadCount))
  140. }
  141. VideoPlaySeconds = utils.FixFloat(rddpVideoPlaySeconds+weekVideoPlaySeconds, 2)
  142. }
  143. maxOpenReportClassify, maxOpenReportCount, err := models.GetOpenReadReportInfo(uid, startDate)
  144. if err != nil && err.Error() != utils.ErrNoRow() {
  145. br.Msg = "获取数据失败"
  146. br.ErrMsg = "获取数据失败GetOpenReadReportInfo,Err:" + err.Error()
  147. return
  148. }
  149. rddpTotalPlaySeconds, rddpTotal, err := models.GetRddpTotalReadDuration(uid, startDate)
  150. if err != nil && err.Error() != utils.ErrNoRow() {
  151. br.Msg = "获取数据失败"
  152. br.ErrMsg = "获取数据失败GetRddpTotalReadDuration,Err:" + err.Error()
  153. return
  154. }
  155. weekTotal, err := models.GetWeekTotalRead(uid, startDate)
  156. if err != nil && err.Error() != utils.ErrNoRow() {
  157. br.Msg = "获取数据失败"
  158. br.ErrMsg = "获取数据失败GetRddpTotalReadDuration,Err:" + err.Error()
  159. return
  160. }
  161. totalReadDuration := 0.00
  162. if rddpTotal <= 0 {
  163. totalReadDuration = rddpTotalPlaySeconds + float64(weekTotal)*float64(120.00) //(float64(utils.GetRandInt(150, 155))/float64(10.00))
  164. } else {
  165. totalReadDuration = rddpTotalPlaySeconds + float64(weekTotal)*float64(120.00) //(rddpTotalPlaySeconds/float64(rddpTotal))*float64(weekTotal)
  166. }
  167. fmt.Println(uid, rddpTotalPlaySeconds, weekTotal, totalReadDuration)
  168. dayType := `day`
  169. dayTotal, err := models.GetWeekTotalReadByType(uid, dayType, startDate)
  170. if err != nil && err.Error() != utils.ErrNoRow() {
  171. br.Msg = "获取数据失败"
  172. br.ErrMsg = "获取数据失败GetWeekTotalReadByType,Err:" + err.Error() + ";report_type:" + dayType
  173. return
  174. }
  175. weekType := `week`
  176. weekReportReadTotal, err := models.GetWeekTotalReadByType(uid, weekType, startDate)
  177. if err != nil && err.Error() != utils.ErrNoRow() {
  178. br.Msg = "获取数据失败"
  179. br.ErrMsg = "获取数据失败GetWeekTotalReadByType,Err:" + err.Error() + ";report_type:" + weekType
  180. return
  181. }
  182. twoWeekType := `two_week`
  183. twoWeekTotal, err := models.GetWeekTotalReadByType(uid, twoWeekType, startDate)
  184. if err != nil && err.Error() != utils.ErrNoRow() {
  185. br.Msg = "获取数据失败"
  186. br.ErrMsg = "获取数据失败GetWeekTotalReadByType,Err:" + err.Error() + ";report_type:" + twoWeekType
  187. return
  188. }
  189. monthType := `month`
  190. monthTotal, err := models.GetWeekTotalReadByType(uid, dayType, startDate)
  191. if err != nil && err.Error() != utils.ErrNoRow() {
  192. br.Msg = "获取数据失败"
  193. br.ErrMsg = "获取数据失败GetWeekTotalReadByType,Err:" + err.Error() + ";report_type:" + monthType
  194. return
  195. }
  196. rddpReadTotal, err := models.GetRddpTotalReadByType(uid, startDate)
  197. if err != nil && err.Error() != utils.ErrNoRow() {
  198. br.Msg = "获取数据失败"
  199. br.ErrMsg = "获取数据失败GetRddpTotalReadByType,Err:" + err.Error() + ";report_type:" + monthType
  200. return
  201. }
  202. var learnDay int
  203. rddpLearnDay, err := models.GetRddpLearnDay(uid, startDate, endDate)
  204. if err != nil && err.Error() != utils.ErrNoRow() {
  205. br.Msg = "获取数据失败"
  206. br.ErrMsg = "获取数据失败GetRddpLearnDay,Err:" + err.Error() + ";report_type:" + monthType
  207. return
  208. }
  209. weekLearnDay, err := models.GetWeekpLearnDay(uid, startDate)
  210. if err != nil && err.Error() != utils.ErrNoRow() {
  211. br.Msg = "获取数据失败"
  212. br.ErrMsg = "获取数据失败GetWeekpLearnDay,Err:" + err.Error() + ";report_type:" + monthType
  213. return
  214. }
  215. if rddpLearnDay >= weekLearnDay {
  216. learnDay = rddpLearnDay
  217. } else {
  218. learnDay = weekLearnDay
  219. }
  220. if learnDay <= 0 {
  221. learnDay = 1
  222. }
  223. resp.RealName = realName
  224. resp.TogetherDay = togetherDay
  225. resp.CreateDate = createDate
  226. if firstReadReportType == "rddp" {
  227. resp.FirstReadReportType = "日度点评"
  228. } else if firstReadReportType == "day" {
  229. resp.FirstReadReportType = "晨报"
  230. } else if firstReadReportType == "week" {
  231. resp.FirstReadReportType = "周报"
  232. } else if firstReadReportType == "two_week" {
  233. resp.FirstReadReportType = "双周报"
  234. } else if firstReadReportType == "month" {
  235. resp.FirstReadReportType = "月报"
  236. } else {
  237. resp.FirstReadReportType = "其他"
  238. }
  239. resp.FirstReadReportTitle = firstReadReportTitle
  240. resp.ListenReportCount = listenCount
  241. resp.ListenReportDuration = utils.FixFloat((listenVideoPlaySeconds / 60.00), 2)
  242. resp.MaxReadReportCount = maxReadReportCount
  243. resp.MaxReadReportDate = maxReadReportDate.Format("2006年01月02日")
  244. if lastestCreateDate == "" {
  245. resp.LatestReadReportDate = ""
  246. } else {
  247. lastestDate, _ := time.Parse("2006-01-02", lastestCreateDate)
  248. resp.LatestReadReportDate = lastestDate.Format("2006年01月02日")
  249. }
  250. resp.LatestReadReportTime = latestTime
  251. resp.LatestReadReportDateDuration = VideoPlaySeconds
  252. resp.MaxOpenReportClassify = maxOpenReportClassify
  253. resp.MaxOpenReportCount = maxOpenReportCount
  254. resp.TotalReadDuration = totalReadDuration
  255. resp.TotalReportDayCount = dayTotal
  256. resp.TotalReportWeekCount = weekReportReadTotal
  257. resp.TotalReportTwoWeekCount = twoWeekTotal
  258. resp.TotalReportMonthCount = monthTotal
  259. resp.TotalReportRddpCount = rddpReadTotal
  260. resp.LearnDay = learnDay
  261. resp.TotalReport = dayTotal + weekReportReadTotal + twoWeekTotal + monthTotal + rddpReadTotal
  262. {
  263. annualReport := new(models.AnnualReport)
  264. annualReport.AnnualReportDate = "2020"
  265. annualReport.UserId = uid
  266. annualReport.Mobile = userInfo.Mobile
  267. annualReport.Email = userInfo.Email
  268. annualReport.CompanyId = userInfo.CompanyId
  269. annualReport.RealName = realName
  270. annualReport.TogetherDay = togetherDay
  271. annualReport.CreateDate = createDate
  272. annualReport.FirstReadReportType = firstReadReportType
  273. annualReport.FirstReadReportTitle = firstReadReportTitle
  274. annualReport.ListenReportCount = listenCount
  275. annualReport.ListenReportDuration = listenVideoPlaySeconds
  276. annualReport.MaxReadReportDate = resp.MaxReadReportDate
  277. annualReport.MaxReadReportCount = resp.MaxReadReportCount
  278. annualReport.LatestReadReportDate = resp.LatestReadReportDate
  279. annualReport.LatestReadReportTime = resp.LatestReadReportTime
  280. annualReport.LatestReadReportDateDuration = resp.LatestReadReportDateDuration
  281. annualReport.MaxOpenReportClassify = resp.MaxOpenReportClassify
  282. annualReport.MaxOpenReportCount = resp.MaxOpenReportCount
  283. annualReport.TotalReadDuration = resp.TotalReadDuration
  284. annualReport.TotalReportDayCount = resp.TotalReportDayCount
  285. annualReport.TotalReportWeekCount = resp.TotalReportWeekCount
  286. annualReport.TotalReportMonthCount = resp.TotalReportMonthCount
  287. annualReport.TotalReportTwoWeekCount = resp.TotalReportTwoWeekCount
  288. annualReport.TotalReportRddpCount = resp.TotalReportRddpCount
  289. annualReport.TotalReport = resp.TotalReport
  290. annualReport.LearnDay = resp.LearnDay
  291. annualReport.CreateTime = time.Now()
  292. go models.AddAnnualReport(annualReport)
  293. }
  294. br.Ret = 200
  295. br.Success = true
  296. br.Msg = "获取成功"
  297. br.Data = resp
  298. }
  299. /*
  300. func init() {
  301. fmt.Println("start")
  302. uid := 12008
  303. startDate := `2020-01-01 00:00:00`
  304. latestTime, latestCreateTime, err := models.GetLatestReadReportInfo(uid, startDate)
  305. fmt.Println(latestTime, latestCreateTime)
  306. if err != nil && err.Error() != utils.ErrNoRow() {
  307. return
  308. }
  309. var lastestCreateDate string
  310. if !latestCreateTime.IsZero() {
  311. fmt.Println("line 256")
  312. addCreateTime := latestCreateTime
  313. lastestCreateDate = latestCreateTime.Format(utils.FormatDate)
  314. hour := latestCreateTime.Hour()
  315. fmt.Println("hour",hour)
  316. if hour > 6 || hour <= 18 {
  317. fmt.Println("line 280")
  318. rand := utils.GetRandInt(18, 23)
  319. addHour := rand - hour
  320. fmt.Println("line 283")
  321. fmt.Println(rand)
  322. fmt.Println(addHour)
  323. if addHour >= 0 {
  324. hh, _ := time.ParseDuration(strconv.Itoa(addHour)+"h")
  325. hh1 := addCreateTime.Add(hh)
  326. fmt.Println(hh1)
  327. fmt.Println(addCreateTime)
  328. fmt.Println("line 292")
  329. fmt.Println(hh1.Format("15:04:05"))
  330. }
  331. }
  332. } else {
  333. fmt.Println("line 259")
  334. }
  335. fmt.Println(latestTime, lastestCreateDate)
  336. fmt.Println("line 291")
  337. fmt.Println("end")
  338. }
  339. */
  340. //func init() {
  341. // fmt.Println("start")
  342. //
  343. // startDate:="2020-01-01 00:00:00"
  344. // latestTime, latestCreateTime, err := models.GetLatestReadReportInfo(34167, startDate)
  345. // if err != nil && err.Error() != utils.ErrNoRow() {
  346. // return
  347. // }
  348. // fmt.Println(latestTime,latestCreateTime)
  349. // fmt.Println("end")
  350. //}