智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
7.9 KiB

  1. package hdl
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/admin/svc"
  5. "applet/app/db"
  6. "applet/app/e"
  7. "applet/app/enum"
  8. "applet/app/utils"
  9. "applet/app/utils/cache"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "time"
  14. )
  15. func HomePageIndex(c *gin.Context) {
  16. now := time.Now()
  17. todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Format("2006-01-02 15:04:05") //今日开始时间
  18. todayEnd := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location()).Format("2006-01-02 15:04:05") //今日结束时间
  19. sql1 := fmt.Sprintf("SELECT sum(trade_amount) as total FROM self_support_for_school_ord where order_status = 2 and face_time >= '%s' and face_time < '%s'", todayStart, todayEnd) //统计今日"自营-学校"收益
  20. sql2 := fmt.Sprintf("SELECT sum(total_price) as total FROM central_kitchen_for_school_package_ord where state = 1 and create_at >= '%s' and create_at < '%s'", todayStart, todayEnd) //统计今日"央厨-学校"收益
  21. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  22. if err != nil {
  23. e.OutErr(c, e.ERR, err.Error())
  24. return
  25. }
  26. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  27. if err != nil {
  28. e.OutErr(c, e.ERR, err.Error())
  29. return
  30. }
  31. var result1, result2, todayIncome float64
  32. if mapArr1 != nil {
  33. result1 = utils.StrToFloat64(mapArr1[0]["total"])
  34. }
  35. if mapArr2 != nil {
  36. result2 = utils.StrToFloat64(mapArr2[0]["total"])
  37. }
  38. todayIncome = result1 + result2
  39. sql3 := fmt.Sprintf("SELECT COUNT(*) as total,kind FROM `enterprise` GROUP BY kind") //统计校企类型数量
  40. mapArr3, err := db.QueryNativeString(db.Db, sql3)
  41. if err != nil {
  42. e.OutErr(c, e.ERR, err.Error())
  43. return
  44. }
  45. var totalCentralKitchenForSchoolNums, totalCentralKitchenForFactoryNums, totalSelfSupportForSchoolNums, totalSelfSupportForFactoryNums int
  46. if mapArr3 != nil {
  47. for _, v := range mapArr3 {
  48. if v["kind"] == utils.IntToStr(enum.EnterpriseKindByCentralKitchenForSchool) {
  49. totalCentralKitchenForSchoolNums = utils.StrToInt(v["total"])
  50. }
  51. if v["kind"] == utils.IntToStr(enum.EnterpriseKindByCentralKitchenForFactory) {
  52. totalCentralKitchenForFactoryNums = utils.StrToInt(v["total"])
  53. }
  54. if v["kind"] == utils.IntToStr(enum.EnterpriseKindBySelfSupportForSchool) {
  55. totalSelfSupportForSchoolNums = utils.StrToInt(v["total"])
  56. }
  57. if v["kind"] == utils.IntToStr(enum.EnterpriseKindBySelfSupportForFactory) {
  58. totalSelfSupportForFactoryNums = utils.StrToInt(v["total"])
  59. }
  60. }
  61. }
  62. var totalCentralKitchenForSchoolUserIdentityNums, totalCentralKitchenForFactoryUserIdentityNums, totalSelfSupportForSchoolUserIdentityNums, totalSelfSupportForFactoryUserIdentityNums int
  63. sql4 := fmt.Sprintf("select count(*) as total from `user_identity` where identity in (%d, %d) ", enum.UserIdentityForCentralKitchenForStudent, enum.UserIdentityForCentralKitchenForTeacher) //统计"央厨-学校"用户数
  64. sql5 := fmt.Sprintf("select count(*) as total from `user_identity` where identity in (%d, %d, %d) ", enum.UserIdentityForSelfSupportForStudent, enum.UserIdentityForSelfSupportForTeacher, enum.UserIdentityForSelfSupportForWorker) //统计"自营-学校"用户数
  65. mapArr4, err := db.QueryNativeString(db.Db, sql4)
  66. if err != nil {
  67. e.OutErr(c, e.ERR, err.Error())
  68. return
  69. }
  70. if mapArr4 != nil {
  71. totalCentralKitchenForSchoolUserIdentityNums = utils.StrToInt(mapArr4[0]["total"])
  72. }
  73. mapArr5, err := db.QueryNativeString(db.Db, sql5)
  74. if err != nil {
  75. e.OutErr(c, e.ERR, err.Error())
  76. return
  77. }
  78. if mapArr5 != nil {
  79. totalSelfSupportForSchoolUserIdentityNums = utils.StrToInt(mapArr5[0]["total"])
  80. }
  81. e.OutSuc(c, map[string]interface{}{
  82. "data": map[string]interface{}{
  83. "todayIncome": utils.Float64ToStr(todayIncome),
  84. "totalCentralKitchenForSchoolNums": totalCentralKitchenForSchoolNums,
  85. "totalCentralKitchenForFactoryNums": totalCentralKitchenForFactoryNums,
  86. "totalSelfSupportForSchoolNums": totalSelfSupportForSchoolNums,
  87. "totalSelfSupportForFactoryNums": totalSelfSupportForFactoryNums,
  88. "totalCentralKitchenForSchoolUserIdentityNums": totalCentralKitchenForSchoolUserIdentityNums,
  89. "totalCentralKitchenForFactoryUserIdentityNums": totalCentralKitchenForFactoryUserIdentityNums,
  90. "totalSelfSupportForSchoolUserIdentityNums": totalSelfSupportForSchoolUserIdentityNums,
  91. "totalSelfSupportForFactoryUserIdentityNums": totalSelfSupportForFactoryUserIdentityNums,
  92. },
  93. }, nil)
  94. return
  95. }
  96. func ThisWeekData(c *gin.Context) {
  97. now := time.Now()
  98. var list []map[string]interface{}
  99. for i := -6; i <= 0; i++ {
  100. day := now.AddDate(0, 0, i).Format("2006-01-02") //7日前
  101. dayStart := now.AddDate(0, 0, i).Format("2006-01-02")
  102. dayEnd := now.AddDate(0, 0, i+1).Format("2006-01-02")
  103. sql1 := fmt.Sprintf("SELECT sum(trade_amount) as total FROM self_support_for_school_ord where order_status = 2 and face_time >= '%s' and face_time < '%s'", dayStart, dayEnd) //统计今日"自营-学校"收益
  104. sql2 := fmt.Sprintf("SELECT sum(total_price) as total FROM central_kitchen_for_school_package_ord where state = 1 and create_at >= '%s' and create_at < '%s'", dayStart, dayEnd) //统计今日"央厨-学校"收益
  105. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  106. if err != nil {
  107. e.OutErr(c, e.ERR, err.Error())
  108. return
  109. }
  110. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  111. if err != nil {
  112. e.OutErr(c, e.ERR, err.Error())
  113. return
  114. }
  115. var result1, result2, todayIncome float64
  116. if mapArr1 != nil {
  117. result1 = utils.StrToFloat64(mapArr1[0]["total"])
  118. }
  119. if mapArr2 != nil {
  120. result2 = utils.StrToFloat64(mapArr2[0]["total"])
  121. }
  122. todayIncome = result1 + result2
  123. list = append(list, map[string]interface{}{
  124. "date": day,
  125. "income": todayIncome,
  126. })
  127. }
  128. e.OutSuc(c, map[string]interface{}{
  129. "data": list,
  130. }, nil)
  131. return
  132. }
  133. func ThisMonthData(c *gin.Context) {
  134. var list []map[string]interface{}
  135. admin := svc.GetUser(c)
  136. cacheKey := fmt.Sprintf(md.AdminHomePageDataKey, utils.GetIP(c.Request), utils.AnyToString(admin.AdmId))
  137. var adminHomePageDataString string
  138. adminHomePageDataString, _ = cache.GetString(cacheKey)
  139. if adminHomePageDataString != "" {
  140. if err := json.Unmarshal([]byte(adminHomePageDataString), &list); err != nil {
  141. e.OutErr(c, e.ERR, err.Error())
  142. return
  143. }
  144. } else {
  145. now := time.Now()
  146. for i := -29; i <= 0; i++ {
  147. day := now.AddDate(0, 0, i).Format("2006-01-02") //7日前
  148. dayStart := now.AddDate(0, 0, i).Format("2006-01-02 15:04:05")
  149. dayEnd := now.AddDate(0, 0, i+1).Format("2006-01-02 15:04:05")
  150. sql1 := fmt.Sprintf("SELECT count(*) as total FROM self_support_for_school_ord where order_status = 2 and face_time >= '%s' and face_time < '%s'", dayStart, dayEnd) //统计今日"自营-学校"收益
  151. sql2 := fmt.Sprintf("SELECT count(*) as total FROM central_kitchen_for_school_package_ord where state = 1 and create_at >= '%s' and create_at < '%s'", dayStart, dayEnd) //统计今日"央厨-学校"收益
  152. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  153. if err != nil {
  154. e.OutErr(c, e.ERR, err.Error())
  155. return
  156. }
  157. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  158. if err != nil {
  159. e.OutErr(c, e.ERR, err.Error())
  160. return
  161. }
  162. var result1, result2 int
  163. if mapArr1 != nil {
  164. result1 = utils.StrToInt(mapArr1[0]["total"])
  165. }
  166. if mapArr2 != nil {
  167. result2 = utils.StrToInt(mapArr2[0]["total"])
  168. }
  169. list = append(list, map[string]interface{}{
  170. "date": day,
  171. "self_support_for_school": result1,
  172. "central_kitchen_for_school": result2,
  173. })
  174. }
  175. marshal, err := json.Marshal(list)
  176. if err != nil {
  177. e.OutErr(c, e.ERR, err.Error())
  178. return
  179. }
  180. adminHomePageDataString = string(marshal)
  181. _, err = cache.SetEx(cacheKey, adminHomePageDataString, md.AdminHomePageDataCacheTime)
  182. }
  183. e.OutSuc(c, map[string]interface{}{
  184. "data": list,
  185. }, nil)
  186. return
  187. }