getSportData.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. const Parse = getApp().Parse;
  2. const company = getApp().globalData.company;
  3. const uid = Parse.User.current()?.id
  4. let request = require('../../utils/request')
  5. const dateF = require('../../utils/date')
  6. /**
  7. * 计算运动累加状况-页面使用
  8. * @param {*} column 累加字段
  9. */
  10. async function getwalk(column) {
  11. let typeList = ['walk', 'run']
  12. let distance = 0
  13. for (let i in typeList) {
  14. let type = typeList[i]
  15. let d = await getData(column, type) || 0
  16. console.log(d)
  17. distance = distance + d
  18. }
  19. return distance < 0 ? 0 : distance
  20. }
  21. /**
  22. * 计算运动累加状况-getwalk使用
  23. * @param {*} column 累加字段 (未知则'distance')
  24. * @param {*} type 运动类型 walk/run(未知则不限制)
  25. */
  26. async function getData(column, type) {
  27. let todate = new Date(new Date().setHours(0, 0, 0, 0))
  28. let tomorrow = new Date(new Date(todate).setDate(todate.getDate() + 1))
  29. let todaySql = dateF.formatTime(`YYYY-mm-dd HH:MM:SS`, todate)
  30. let yestodaySql = dateF.formatTime(`YYYY-mm-dd HH:MM:SS`, tomorrow)
  31. let sql = `SELECT SUM(t1."num")- SUM(t1."ago_num") AS "sum"
  32. FROM(SELECT MAX(al."${column||'distance'}") AS "num" ,al."actData",(
  33. SELECT MAX(al2."${column||'distance'}")
  34. FROM "ActivityRunLog" al2
  35. WHERE al2."actData"=al."actData"
  36. AND al2."isDeleted" IS NOT TRUE
  37. ${type?`AND al2."type"='${type}'`:''}
  38. AND al2."createdAt" < '${todaySql}'
  39. AND al2."isDeleted" IS NOT TRUE
  40. )AS "ago_num"
  41. FROM "ActivityRunLog" al
  42. LEFT JOIN "ActivityData" ad ON ad."objectId"=al."actData"
  43. WHERE al."isDeleted" IS NOT TRUE
  44. AND al."createdAt" > '${todaySql}'
  45. AND al."createdAt" < '${yestodaySql}'
  46. AND al."company"='${company}'
  47. AND al."user"='${uid}'
  48. ${type?`AND al."type"='${type}'`:''}
  49. AND al."isDeleted" IS NOT TRUE
  50. AND ad."isDeleted" IS NOT TRUE
  51. ${type?`AND ad."type"='${type}'`:''}
  52. GROUP BY al."actData")t1`
  53. // console.log(sql)
  54. let data = await request.customSQL(sql)
  55. // console.log(data)
  56. return data[0].sum || 0
  57. }
  58. /**查看活动运动数据(当前活动对应运动是否结束) */
  59. async function getActSport(actId) {
  60. let actQuery = new Parse.Query('Activity')
  61. let act = await actQuery.get(actId)
  62. if (act?.get('endDate')) {
  63. }
  64. }
  65. /**对于活动超时未结束的运动,设置stage为end */
  66. async function setEndSport(actId) {
  67. let regQuery = new Parse.Query('ActivityRegister')
  68. regQuery.equalTo('company', company)
  69. regQuery.equalTo('user', uid)
  70. regQuery.notEqualTo('isDeleted', true)
  71. regQuery.equalTo('activity', actId)
  72. regQuery.select('booking')
  73. let reg = await regQuery.find()
  74. let overtimeReg = reg?.filter(item => {
  75. let now = new Date()
  76. let endTime = item?.get('booking')?.to
  77. if (!endTime) return true
  78. return now >= endTime
  79. })?.map(item => item?.id)
  80. console.log(overtimeReg)
  81. if (overtimeReg?.length <= 0) return
  82. let actQuery = new Parse.Query('ActivityData')
  83. actQuery.equalTo('company', company)
  84. actQuery.equalTo('user', uid)
  85. actQuery.notEqualTo('isDeleted', true)
  86. actQuery.containedIn('activity', overtimeReg)
  87. actQuery.select('objectId')
  88. let actData = await actQuery.find()
  89. for (let i in actData) {
  90. let act = actData[i]
  91. //获取最后一条运动过程记录
  92. let logQuery = new Parse.Query('ActivityRunLog')
  93. logQuery.equalTo('company', company)
  94. logQuery.equalTo('user', uid)
  95. logQuery.notEqualTo('isDeleted', true)
  96. logQuery.equalTo('actData', act?.id)
  97. logQuery.descending('createdAt')
  98. let endLog = await logQuery.first()
  99. endLog.set('stage', 'end')
  100. await endLog.save()
  101. act.set('steps', endLog?.get('steps') || 0)
  102. act.set('distance', endLog?.get('distance') || 0)
  103. act.set('matchSpeed', endLog?.get('matchSpeed') || 0)
  104. act.set('sportDate', endLog?.get('sportDate') || 0)
  105. act.set('burnCalories', endLog?.get('burnCalories') || 0)
  106. act.set('status', 'end')
  107. act.set('endDate', new Date())
  108. await act.save()
  109. }
  110. }
  111. /**
  112. * 获取排行榜
  113. * @param {*} type 运动类型
  114. * @param {*} fromto 时间范围 today/toweek/tomonth/空
  115. * @param {*} limit 默认20
  116. * @param {*} skip 默认0
  117. * @param {*} order 默认 DESC
  118. */
  119. async function getRanking(type, fromto, limit, skip,order) {
  120. let fromtoSql = ``
  121. switch (fromto) {
  122. case 'today':
  123. let todate = new Date(new Date().setHours(0, 0, 0, 0))
  124. let tomorrow = new Date(new Date(todate).setDate(todate.getDate() + 1))
  125. let todaySql = dateF.formatTime(`YYYY-mm-dd HH:MM:SS`, todate)
  126. let yestodaySql = dateF.formatTime(`YYYY-mm-dd HH:MM:SS`, tomorrow)
  127. fromtoSql = `AND ad."endDate" >= '${todaySql}' AND ad."endDate" < '${yestodaySql}'`
  128. break;
  129. case 'toweek':
  130. const today = new Date();
  131. const dayOfWeek = today.getDay();
  132. const thisSun = new Date(today);
  133. thisSun.setDate(today.getDate() + (0 - dayOfWeek));
  134. thisSun.setHours(0, 0, 0, 0);
  135. const nextSun = new Date(thisSun);
  136. nextSun.setDate(thisSun.getDate() + 7);
  137. let thisSunSql = dateF.formatTime(`YYYY-mm-dd HH:MM:SS`, thisSun)
  138. let nextSunSql = dateF.formatTime(`YYYY-mm-dd HH:MM:SS`, nextSun)
  139. fromtoSql = `AND ad."endDate" >= '${thisSunSql}' AND ad."endDate" < '${nextSunSql}'`
  140. break;
  141. case 'tomonth':
  142. const now = new Date();
  143. const toMon = new Date(now.getFullYear(), now.getMonth(), 1, 0, 0, 0);
  144. const nextMon = new Date(now.getFullYear(), now.getMonth() + 1, 1, 0, 0, 0);
  145. let toMonSql = dateF.formatTime(`YYYY-mm-dd HH:MM:SS`, toMon)
  146. let nextMonSql = dateF.formatTime(`YYYY-mm-dd HH:MM:SS`, nextMon)
  147. fromtoSql = `AND ad."endDate" >= '${toMonSql}' AND ad."endDate" < '${nextMonSql}'`
  148. break;
  149. default:
  150. break;
  151. }
  152. let sql = `SELECT SUM(ad."steps") AS "totalSteps",SUM(ad."distance") AS "distance",SUM(ad."burnCalories") AS "burnCalories",SUM(ad."sportDate") AS "sportDate",ad."user",u."nickname",u."avatar"
  153. FROM "ActivityData" ad
  154. LEFT JOIN "_User" u ON u."objectId"=ad."user"
  155. WHERE ad."company"='${company}'
  156. AND ad."isDeleted" IS NOT TRUE
  157. AND ad."status" = 'end'
  158. ${type?`AND ad."type" = '${type}'`:''}
  159. ${fromtoSql}
  160. GROUP BY ad."user",u."nickname",u."avatar"
  161. ORDER BY SUM(ad."steps") ${order||'DESC'}
  162. LIMIT ${limit||20} OFFSET ${skip||0}`
  163. // console.log(sql)
  164. let data = await request.customSQL(sql)
  165. // console.log(data)
  166. return data
  167. }
  168. module.exports = {
  169. getwalk,
  170. setEndSport,
  171. getRanking
  172. };