home.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package service
  2. import (
  3. "gas-cylinder-api/app/admin/model"
  4. "gas-cylinder-api/common/actions"
  5. "gas-cylinder-api/conf"
  6. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  7. "gogs.baozhida.cn/zoie/OAuth-core/service"
  8. )
  9. type Home struct {
  10. service.Service
  11. }
  12. // 获取每个市的门店数量
  13. func (e *Home) GetStoreStat(p *actions.DataPermission) (res []map[string]interface{}, err error) {
  14. if p.DeptId == 0 {
  15. err = e.Orm.Model(&model.SysDept{}).
  16. Select("JSON_UNQUOTE(JSON_EXTRACT(prov_store, '$.city')) AS city,COUNT(id) AS count").
  17. Where("JSON_LENGTH(prov_store) > 0 and type = 1").
  18. Group("JSON_EXTRACT(prov_store, '$.city')").Scan(&res).Error
  19. } else {
  20. err = e.Orm.Model(&model.SysDept{}).Select("JSON_UNQUOTE(JSON_EXTRACT(prov_store, '$.city')) AS city,COUNT(id) AS count").
  21. Where("id in (select id from sys_dept where path like ?)", "%/"+pkg.IntToString(p.DeptId)+"/%").
  22. Where("JSON_LENGTH(prov_store) > 0 and type = 1").
  23. Group("JSON_EXTRACT(prov_store, '$.city')").Scan(&res).Error
  24. }
  25. if err != nil {
  26. e.Log.Errorf("db error:%s", err)
  27. }
  28. for _, re := range res {
  29. re["city_name"] = conf.CityDataMap[re["city"].(string)]
  30. }
  31. return res, err
  32. }
  33. func (e *Home) GetStoreCount(p *actions.DataPermission) (count int64, err error) {
  34. if p.DeptId == 0 {
  35. err = e.Orm.Model(&model.SysDept{}).
  36. Where("type = 1").
  37. Count(&count).Error
  38. } else {
  39. err = e.Orm.Model(&model.SysDept{}).
  40. Where("id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
  41. Where("type = 1").
  42. Count(&count).Error
  43. }
  44. if err != nil {
  45. e.Log.Errorf("db error:%s", err)
  46. }
  47. return count, err
  48. }
  49. func (e *Home) GetGasCylinderStat(p *actions.DataPermission) (res []map[string]interface{}, err error) {
  50. if p.DeptId == 0 {
  51. err = e.Orm.Model(&model.GasCylinder{}).
  52. Select("status,COUNT(id) AS count").
  53. Group("status").Scan(&res).Error
  54. } else {
  55. err = e.Orm.Model(&model.GasCylinder{}).
  56. Select("status,COUNT(id) AS count").
  57. Where("dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
  58. Group("status").Scan(&res).Error
  59. }
  60. if err != nil {
  61. e.Log.Errorf("db error:%s", err)
  62. }
  63. return res, err
  64. }
  65. func (e *Home) GetGasCylinderCount(p *actions.DataPermission) (count int64, err error) {
  66. if p.DeptId == 0 {
  67. err = e.Orm.Model(&model.GasCylinder{}).
  68. Count(&count).Error
  69. if err != nil {
  70. e.Log.Errorf("db error:%s", err)
  71. }
  72. return count, err
  73. }
  74. err = e.Orm.Model(&model.GasCylinder{}).
  75. Where("dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
  76. Count(&count).Error
  77. if err != nil {
  78. e.Log.Errorf("db error:%s", err)
  79. }
  80. return count, err
  81. }
  82. func (e *Home) GetGasCylinderDeptStat(p *actions.DataPermission) (res []map[string]interface{}, err error) {
  83. if p.DeptId == 0 {
  84. err = e.Orm.Model(&model.GasCylinder{}).
  85. Select("sys_dept.name,gas_cylinder.dept_id,COUNT(gas_cylinder.id) AS count").
  86. Joins("left join sys_dept on sys_dept.id = gas_cylinder.dept_id").
  87. Group("gas_cylinder.dept_id").Scan(&res).Error
  88. } else {
  89. err = e.Orm.Model(&model.GasCylinder{}).
  90. Select("sys_dept.name,gas_cylinder.dept_id,COUNT(gas_cylinder.id) AS count").
  91. Joins("left join sys_dept on sys_dept.id = gas_cylinder.dept_id").
  92. Where("gas_cylinder.dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
  93. Group("gas_cylinder.dept_id").Scan(&res).Error
  94. }
  95. if err != nil {
  96. e.Log.Errorf("db error:%s", err)
  97. }
  98. return res, err
  99. }
  100. func (e *Home) GetGasCylinderMonthStat(p *actions.DataPermission) (res []map[string]interface{}, err error) {
  101. if p.DeptId == 0 {
  102. err = e.Orm.Model(&model.GasCylinder{}).
  103. Select("DATE_FORMAT(created_at, '%Y-%m') as month,COUNT(id) AS count").
  104. Where("created_at >= DATE_SUB(CURRENT_DATE(), INTERVAL 1 YEAR)").
  105. Group("DATE_FORMAT(created_at, '%Y-%m')").Scan(&res).Error
  106. } else {
  107. err = e.Orm.Model(&model.GasCylinder{}).
  108. Select("DATE_FORMAT(created_at, '%Y-%m') as month,COUNT(id) AS count").
  109. Where("created_at >= DATE_SUB(CURRENT_DATE(), INTERVAL 1 YEAR)").
  110. Where("dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
  111. Group("DATE_FORMAT(created_at, '%Y-%m')").Scan(&res).Error
  112. }
  113. if err != nil {
  114. e.Log.Errorf("db error:%s", err)
  115. }
  116. return res, err
  117. }
  118. // 燃气户数
  119. func (e *Home) GetCustomerCount(p *actions.DataPermission) (count int64, err error) {
  120. if p.DeptId == 0 {
  121. err = e.Orm.Model(&model.Customer{}).
  122. Count(&count).Error
  123. } else {
  124. err = e.Orm.Model(&model.Customer{}).
  125. Where("dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
  126. Count(&count).Error
  127. }
  128. if err != nil {
  129. e.Log.Errorf("db error:%s", err)
  130. }
  131. return count, err
  132. }
  133. // 送气工数
  134. func (e *Home) GetDeliveryCount(p *actions.DataPermission) (count int64, err error) {
  135. if p.DeptId == 0 {
  136. err = e.Orm.Model(&model.SysUser{}).
  137. Where("JSON_EXTRACT(prov_user, '$.userType') = 3 and JSON_EXTRACT(prov_user, '$.isorders') = 1").
  138. Count(&count).Error
  139. } else {
  140. err = e.Orm.Model(&model.SysUser{}).
  141. Where("JSON_EXTRACT(prov_user, '$.userType') = 3 and JSON_EXTRACT(prov_user, '$.isorders') = 1").
  142. Where("dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
  143. Count(&count).Error
  144. }
  145. if err != nil {
  146. e.Log.Errorf("db error:%s", err)
  147. }
  148. return count, err
  149. }