面包店
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.

hdl_home_page.go 6.4 KiB

пре 7 месеци
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package hdl
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/utils"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. "time"
  9. )
  10. func HomeIndex(c *gin.Context) {
  11. now := time.Now()
  12. //1、统计公司数量
  13. mapArr1, err := db.QueryNativeString(db.Db, "SELECT COUNT(*) AS total FROM `company`")
  14. if err != nil {
  15. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  16. return
  17. }
  18. //2、统计校企数量
  19. mapArr2, err := db.QueryNativeString(db.Db, "SELECT COUNT(*) AS total FROM `enterprise`")
  20. if err != nil {
  21. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  22. return
  23. }
  24. //3、统计订单付款金额
  25. mapArr3, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) AS total FROM `order` where create_at >='%s' and state != 5", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05")))
  26. if err != nil {
  27. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  28. return
  29. }
  30. //4、统计订单取消金额
  31. mapArr4, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) as total FROM `order` AS total where create_at >='%s' and state = 5", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05")))
  32. if err != nil {
  33. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  34. return
  35. }
  36. var result1, result2, result3, result4 float64
  37. if mapArr1 != nil {
  38. result1 = utils.StrToFloat64(mapArr1[0]["total"])
  39. }
  40. if mapArr2 != nil {
  41. result2 = utils.StrToFloat64(mapArr2[0]["total"])
  42. }
  43. if mapArr3 != nil {
  44. result3 = utils.StrToFloat64(mapArr3[0]["total"])
  45. }
  46. if mapArr4 != nil {
  47. result4 = utils.StrToFloat64(mapArr4[0]["total"])
  48. }
  49. e.OutSuc(c, map[string]interface{}{
  50. "company_total_nums": result1,
  51. "enterprise_total_nums": result2,
  52. "order_total_amount_for_success": result3,
  53. "order_total_amount_for_cancel": result4,
  54. }, nil)
  55. return
  56. }
  57. func RepayBehaviorAnalysis(c *gin.Context) {
  58. now := time.Now()
  59. //1、统计订单数据
  60. mapArr1, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT COUNT(*) AS total, enterprise_id FROM `order` where create_at >='%s' and create_at < '%s' GROUP BY enterprise_id", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05")))
  61. if err != nil {
  62. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  63. return
  64. }
  65. //2、数据整理
  66. var dataMap = map[int]float64{}
  67. var totalNUms = len(mapArr1)
  68. for _, v := range mapArr1 {
  69. dataMap[utils.StrToInt(v["total"])]++
  70. }
  71. //3、数据计算
  72. var result []map[string]interface{}
  73. for k, v := range dataMap {
  74. result = append(result, map[string]interface{}{
  75. "key": utils.IntToStr(k) + "次购买",
  76. "value": v / float64(totalNUms),
  77. })
  78. }
  79. e.OutSuc(c, map[string]interface{}{
  80. "result": result,
  81. }, nil)
  82. return
  83. }
  84. func OperationalDataStatistics(c *gin.Context) {
  85. startDate := c.DefaultQuery("start_date", "")
  86. endDate := c.DefaultQuery("end_date", "")
  87. if startDate == "" || endDate == "" {
  88. e.OutErr(c, e.ERR_INVALID_ARGS, "时间范围不能为空")
  89. return
  90. }
  91. now := time.Now()
  92. //1、商品数量
  93. mapArr1, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT COUNT(*) AS total FROM `goods` where create_at >='%s' and create_at < '%s'", startDate, endDate))
  94. if err != nil {
  95. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  96. return
  97. }
  98. mapArr11, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT COUNT(*) AS total FROM `goods`"))
  99. if err != nil {
  100. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  101. return
  102. }
  103. //2、订单数量
  104. mapArr2, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT COUNT(*) AS total FROM `order` where create_at >='%s' and create_at < '%s'", startDate, endDate))
  105. if err != nil {
  106. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  107. return
  108. }
  109. mapArr22, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT COUNT(*) AS total FROM `order` where create_at >='%s' and create_at < '%s'", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05")))
  110. if err != nil {
  111. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  112. return
  113. }
  114. //3、统计订单付款金额
  115. mapArr3, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) AS total FROM `order` where create_at >='%s' and create_at < '%s' and state != 5", startDate, endDate))
  116. if err != nil {
  117. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  118. return
  119. }
  120. mapArr33, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) as total FROM `order` AS total where create_at >='%s' and create_at < '%s' and state != 5", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05")))
  121. fmt.Println(fmt.Sprintf("SELECT SUM(amount) as total FROM `order` AS total where create_at >='%s' and create_at < '%s' and state != 5", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05")))
  122. if err != nil {
  123. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  124. return
  125. }
  126. //4、统计订单取消金额
  127. mapArr4, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) AS total FROM `order` where create_at >='%s' and create_at < '%s' and state = 5", startDate, endDate))
  128. if err != nil {
  129. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  130. return
  131. }
  132. mapArr44, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) as total FROM `order` AS total where create_at >='%s' and create_at < '%s' and state = 5", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05")))
  133. if err != nil {
  134. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  135. return
  136. }
  137. var result1, result2, result3, result4, result11, result22, result33, result44 float64
  138. if mapArr1 != nil {
  139. result1 = utils.StrToFloat64(mapArr1[0]["total"])
  140. }
  141. if mapArr2 != nil {
  142. result2 = utils.StrToFloat64(mapArr2[0]["total"])
  143. }
  144. if mapArr3 != nil {
  145. result3 = utils.StrToFloat64(mapArr3[0]["total"])
  146. }
  147. if mapArr4 != nil {
  148. result4 = utils.StrToFloat64(mapArr4[0]["total"])
  149. }
  150. if mapArr11 != nil {
  151. result11 = utils.StrToFloat64(mapArr11[0]["total"])
  152. }
  153. if mapArr22 != nil {
  154. result22 = utils.StrToFloat64(mapArr22[0]["total"])
  155. }
  156. if mapArr33 != nil {
  157. result33 = utils.StrToFloat64(mapArr33[0]["total"])
  158. }
  159. if mapArr44 != nil {
  160. result44 = utils.StrToFloat64(mapArr44[0]["total"])
  161. }
  162. e.OutSuc(c, map[string]interface{}{
  163. "goods_increase_value": result1,
  164. "goods_total_value": result11,
  165. "order_increase_value": result2,
  166. "order_total_value": result22,
  167. "success_order_total_amount_increase_value": result3,
  168. "success_order_total_amount_total_value": result33,
  169. "cancel_order_total_amount_increase_value": result4,
  170. "cancel_order_total_amount_total_value": result44,
  171. }, nil)
  172. return
  173. }