123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package service
- import (
- "gas-cylinder-api/app/admin/model"
- "gas-cylinder-api/common/actions"
- "gas-cylinder-api/conf"
- "gogs.baozhida.cn/zoie/OAuth-core/pkg"
- "gogs.baozhida.cn/zoie/OAuth-core/service"
- )
- type Home struct {
- service.Service
- }
- // 获取每个市的门店数量
- func (e *Home) GetStoreStat(p *actions.DataPermission) (res []map[string]interface{}, err error) {
- if p.DeptId == 0 {
- err = e.Orm.Model(&model.SysDept{}).
- Select("JSON_UNQUOTE(JSON_EXTRACT(prov_store, '$.city')) AS city,COUNT(id) AS count").
- Where("JSON_LENGTH(prov_store) > 0 and type = 1").
- Group("JSON_EXTRACT(prov_store, '$.city')").Scan(&res).Error
- } else {
- err = e.Orm.Model(&model.SysDept{}).Select("JSON_UNQUOTE(JSON_EXTRACT(prov_store, '$.city')) AS city,COUNT(id) AS count").
- Where("id in (select id from sys_dept where path like ?)", "%/"+pkg.IntToString(p.DeptId)+"/%").
- Where("JSON_LENGTH(prov_store) > 0 and type = 1").
- Group("JSON_EXTRACT(prov_store, '$.city')").Scan(&res).Error
- }
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- }
- for _, re := range res {
- re["city_name"] = conf.CityDataMap[re["city"].(string)]
- }
- return res, err
- }
- func (e *Home) GetStoreCount(p *actions.DataPermission) (count int64, err error) {
- if p.DeptId == 0 {
- err = e.Orm.Model(&model.SysDept{}).
- Where("type = 1").
- Count(&count).Error
- } else {
- err = e.Orm.Model(&model.SysDept{}).
- Where("id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
- Where("type = 1").
- Count(&count).Error
- }
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- }
- return count, err
- }
- func (e *Home) GetGasCylinderStat(p *actions.DataPermission) (res []map[string]interface{}, err error) {
- if p.DeptId == 0 {
- err = e.Orm.Model(&model.GasCylinder{}).
- Select("status,COUNT(id) AS count").
- Group("status").Scan(&res).Error
- } else {
- err = e.Orm.Model(&model.GasCylinder{}).
- Select("status,COUNT(id) AS count").
- Where("dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
- Group("status").Scan(&res).Error
- }
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- }
- return res, err
- }
- func (e *Home) GetGasCylinderCount(p *actions.DataPermission) (count int64, err error) {
- if p.DeptId == 0 {
- err = e.Orm.Model(&model.GasCylinder{}).
- Count(&count).Error
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- }
- return count, err
- }
- err = e.Orm.Model(&model.GasCylinder{}).
- Where("dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
- Count(&count).Error
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- }
- return count, err
- }
- func (e *Home) GetGasCylinderDeptStat(p *actions.DataPermission) (res []map[string]interface{}, err error) {
- if p.DeptId == 0 {
- err = e.Orm.Model(&model.GasCylinder{}).
- Select("sys_dept.name,gas_cylinder.dept_id,COUNT(gas_cylinder.id) AS count").
- Joins("left join sys_dept on sys_dept.id = gas_cylinder.dept_id").
- Group("gas_cylinder.dept_id").Scan(&res).Error
- } else {
- err = e.Orm.Model(&model.GasCylinder{}).
- Select("sys_dept.name,gas_cylinder.dept_id,COUNT(gas_cylinder.id) AS count").
- Joins("left join sys_dept on sys_dept.id = gas_cylinder.dept_id").
- Where("gas_cylinder.dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
- Group("gas_cylinder.dept_id").Scan(&res).Error
- }
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- }
- return res, err
- }
- func (e *Home) GetGasCylinderMonthStat(p *actions.DataPermission) (res []map[string]interface{}, err error) {
- if p.DeptId == 0 {
- err = e.Orm.Model(&model.GasCylinder{}).
- Select("DATE_FORMAT(created_at, '%Y-%m') as month,COUNT(id) AS count").
- Where("created_at >= DATE_SUB(CURRENT_DATE(), INTERVAL 1 YEAR)").
- Group("DATE_FORMAT(created_at, '%Y-%m')").Scan(&res).Error
- } else {
- err = e.Orm.Model(&model.GasCylinder{}).
- Select("DATE_FORMAT(created_at, '%Y-%m') as month,COUNT(id) AS count").
- Where("created_at >= DATE_SUB(CURRENT_DATE(), INTERVAL 1 YEAR)").
- Where("dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
- Group("DATE_FORMAT(created_at, '%Y-%m')").Scan(&res).Error
- }
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- }
- return res, err
- }
- // 燃气户数
- func (e *Home) GetCustomerCount(p *actions.DataPermission) (count int64, err error) {
- if p.DeptId == 0 {
- err = e.Orm.Model(&model.Customer{}).
- Count(&count).Error
- } else {
- err = e.Orm.Model(&model.Customer{}).
- Where("dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
- Count(&count).Error
- }
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- }
- return count, err
- }
- // 送气工数
- func (e *Home) GetDeliveryCount(p *actions.DataPermission) (count int64, err error) {
- if p.DeptId == 0 {
- err = e.Orm.Model(&model.SysUser{}).
- Where("JSON_EXTRACT(prov_user, '$.userType') = 3 and JSON_EXTRACT(prov_user, '$.isorders') = 1").
- Count(&count).Error
- } else {
- err = e.Orm.Model(&model.SysUser{}).
- Where("JSON_EXTRACT(prov_user, '$.userType') = 3 and JSON_EXTRACT(prov_user, '$.isorders') = 1").
- Where("dept_id in (select id from sys_dept where path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
- Count(&count).Error
- }
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- }
- return count, err
- }
|