sync: 对齐小程序 v1.0.2 生产代码并脱敏
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
@@ -20,26 +21,30 @@ const (
|
||||
)
|
||||
|
||||
type appCustomerCardStat struct {
|
||||
DeptId int64 `orm:"dept_id"`
|
||||
TerminalId int64 `orm:"terminal_id"`
|
||||
CardCount int `orm:"card_count"`
|
||||
NormalCardCount int `orm:"normal_card_count"`
|
||||
PackageStatus int `orm:"package_status"`
|
||||
PackageStatusCounts []model.AppCustomerStatusCount
|
||||
BossCardStatus int `orm:"boss_card_status"`
|
||||
CardStatusCounts []model.AppCustomerStatusCount
|
||||
DeptId int64 `orm:"dept_id"`
|
||||
TerminalId int64 `orm:"terminal_id"`
|
||||
CardCount int `orm:"card_count"`
|
||||
NormalCardCount int `orm:"normal_card_count"`
|
||||
}
|
||||
|
||||
type appTerminalCustomerIdRow struct {
|
||||
TerminalId int64 `orm:"terminal_id"`
|
||||
}
|
||||
|
||||
func (s *sAppUser) CustomerList(ctx context.Context, param *model.AppCustomerListParam) (items []*model.AppCustomerModel, total int, err error) {
|
||||
func (s *sAppUser) CustomerList(ctx context.Context, param *model.AppCustomerListParam) (items []*model.AppCustomerModel, total int, summary *model.AppCustomerSummary, err error) {
|
||||
if param == nil {
|
||||
param = &model.AppCustomerListParam{}
|
||||
}
|
||||
user, err := appCurrentUser(ctx)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
if err = appEnsureEnterpriseCustomerScope(ctx, user); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
if param.Page <= 0 {
|
||||
param.Page = 1
|
||||
@@ -53,23 +58,65 @@ func (s *sAppUser) CustomerList(ctx context.Context, param *model.AppCustomerLis
|
||||
|
||||
customerType := appNormalizeCustomerType(param.CustomerType)
|
||||
if customerType == appCustomerTypeTerminal {
|
||||
return nil, 0, gerror.New("暂不支持查看终端客户")
|
||||
return nil, 0, nil, gerror.New("暂不支持查看终端客户")
|
||||
}
|
||||
|
||||
list, err := s.enterpriseCustomerList(ctx, user, param)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
total = len(list)
|
||||
start := (param.Page - 1) * param.PageSize
|
||||
if start >= total {
|
||||
return []*model.AppCustomerModel{}, total, nil
|
||||
summary = appCustomerSummary(list)
|
||||
items = appCustomerPage(list, param.Page, param.PageSize)
|
||||
if len(items) == 0 {
|
||||
return items, total, summary, nil
|
||||
}
|
||||
end := start + param.PageSize
|
||||
if end > total {
|
||||
end = total
|
||||
deptIds := make([]int64, 0, len(items))
|
||||
for _, item := range items {
|
||||
deptIds = append(deptIds, item.CustomerId)
|
||||
}
|
||||
return list[start:end], total, nil
|
||||
stats, err := appCardStatsByDept(ctx, user.TenantId, deptIds)
|
||||
if err != nil {
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
for _, item := range items {
|
||||
if stat := stats[item.CustomerId]; stat != nil {
|
||||
item.CardCount = stat.CardCount
|
||||
item.NormalCardCount = stat.NormalCardCount
|
||||
item.CardStatusCounts = stat.CardStatusCounts
|
||||
item.PackageStatusCounts = stat.PackageStatusCounts
|
||||
}
|
||||
}
|
||||
return items, total, summary, nil
|
||||
}
|
||||
|
||||
func appCustomerPage(items []*model.AppCustomerModel, page, pageSize int) []*model.AppCustomerModel {
|
||||
if page <= 0 || pageSize <= 0 || len(items) == 0 || page > (len(items)-1)/pageSize+1 {
|
||||
return []*model.AppCustomerModel{}
|
||||
}
|
||||
start := (page - 1) * pageSize
|
||||
remaining := len(items) - start
|
||||
if remaining < pageSize {
|
||||
return items[start:]
|
||||
}
|
||||
return items[start : start+pageSize]
|
||||
}
|
||||
|
||||
func appCustomerSummary(items []*model.AppCustomerModel) *model.AppCustomerSummary {
|
||||
summary := &model.AppCustomerSummary{}
|
||||
for _, item := range items {
|
||||
if item == nil {
|
||||
continue
|
||||
}
|
||||
summary.Total++
|
||||
if item.Status == consts.SysDeptStatusNormal {
|
||||
summary.NormalCount++
|
||||
}
|
||||
if item.RealNameStatus != "已录入实名资料" {
|
||||
summary.PendingProfileCount++
|
||||
}
|
||||
}
|
||||
return summary
|
||||
}
|
||||
|
||||
func (s *sAppUser) CustomerDetail(ctx context.Context, param *model.AppCustomerDetailParam) (*model.AppCustomerDetailModel, error) {
|
||||
@@ -95,16 +142,12 @@ func (s *sAppUser) CustomerDetail(ctx context.Context, param *model.AppCustomerD
|
||||
}
|
||||
|
||||
func (s *sAppUser) enterpriseCustomerList(ctx context.Context, user *entity.SysUser, param *model.AppCustomerListParam) ([]*model.AppCustomerModel, error) {
|
||||
depts, err := appEnterpriseDirectCustomerDepts(ctx, user)
|
||||
depts, err := appEnterpriseAuthorizedCustomerDepts(ctx, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
depts = appFilterEnterpriseDepts(depts, strings.TrimSpace(param.Keyword), strings.TrimSpace(param.Status))
|
||||
deptIds := appDeptIds(depts)
|
||||
stats, err := appCardStatsByDept(ctx, user.TenantId, deptIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
profiles, err := appEnterpriseProfileMap(ctx, user.TenantId, deptIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -113,8 +156,7 @@ func (s *sAppUser) enterpriseCustomerList(ctx context.Context, user *entity.SysU
|
||||
list := make([]*model.AppCustomerModel, 0, len(depts))
|
||||
for _, dept := range depts {
|
||||
profile := profiles[dept.DeptId]
|
||||
stat := stats[dept.DeptId]
|
||||
item := appEnterpriseCustomerModel(dept, profile, 0, stat)
|
||||
item := appEnterpriseCustomerModel(dept, profile, 0, nil)
|
||||
list = append(list, item)
|
||||
}
|
||||
return list, nil
|
||||
@@ -125,7 +167,7 @@ func (s *sAppUser) enterpriseCustomerDetail(ctx context.Context, user *entity.Sy
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if dept == nil || dept.DeptId == 0 {
|
||||
if dept == nil || dept.DeptId == 0 || dept.DeptType != 1 {
|
||||
return nil, gerror.New("企业客户不存在")
|
||||
}
|
||||
visible, err := appEnterpriseCustomerDeptVisible(ctx, user, dept.DeptId)
|
||||
@@ -251,6 +293,7 @@ func appDirectChildDepts(ctx context.Context, user *entity.SysUser) ([]*entity.S
|
||||
err := dao.SysDept.Ctx(ctx).
|
||||
Where(dao.SysDept.Columns().TenantId, user.TenantId).
|
||||
Where(dao.SysDept.Columns().ParentId, user.DeptId).
|
||||
Where(dao.SysDept.Columns().DeptType, 1).
|
||||
WhereNull(dao.SysDept.Columns().DeletedAt).
|
||||
OrderAsc(dao.SysDept.Columns().OrderNum).
|
||||
OrderAsc(dao.SysDept.Columns().DeptId).
|
||||
@@ -324,28 +367,67 @@ func appEnterpriseSubCustomerDepts(ctx context.Context, user *entity.SysUser) ([
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func appEnterpriseCustomerQuery(ctx context.Context, user *entity.SysUser) (*gdb.Model, error) {
|
||||
scope, err := appResolveEnterpriseDataScope(ctx, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mod := dao.SysDept.Ctx(ctx).
|
||||
Where(dao.SysDept.Columns().TenantId, user.TenantId).
|
||||
Where(dao.SysDept.Columns().DeptType, 1)
|
||||
if scope.Configured {
|
||||
if !scope.All {
|
||||
if len(scope.DeptIds) == 0 {
|
||||
return mod.Where("1 = 0"), nil
|
||||
}
|
||||
mod = mod.WhereIn(dao.SysDept.Columns().DeptId, scope.DeptIds)
|
||||
}
|
||||
} else {
|
||||
ids, err := appEnterpriseDeptAndSubIds(ctx, user.TenantId, user.DeptId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mod = mod.WhereIn(dao.SysDept.Columns().DeptId, ids)
|
||||
}
|
||||
excluded, err := appEnterpriseCustomerExcludedDeptIds(ctx, user.TenantId, user.DeptId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
excludedIds := []int64{user.DeptId}
|
||||
for id := range excluded {
|
||||
excludedIds = append(excludedIds, id)
|
||||
}
|
||||
mod = mod.WhereNotIn(dao.SysDept.Columns().DeptId, excludedIds)
|
||||
return mod, nil
|
||||
}
|
||||
|
||||
func appEnterpriseAuthorizedCustomerDepts(ctx context.Context, user *entity.SysUser) ([]*entity.SysDept, error) {
|
||||
mod, err := appEnterpriseCustomerQuery(ctx, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var rows []*entity.SysDept
|
||||
err = mod.OrderAsc(dao.SysDept.Columns().OrderNum).
|
||||
OrderAsc(dao.SysDept.Columns().DeptId).Scan(&rows)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询授权企业客户失败")
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func appEnterpriseCustomerDeptVisible(ctx context.Context, user *entity.SysUser, deptId int64) (bool, error) {
|
||||
if user == nil || user.DeptId <= 0 || deptId <= 0 {
|
||||
return false, nil
|
||||
}
|
||||
if deptId == user.DeptId {
|
||||
return true, nil
|
||||
}
|
||||
excludeIds, err := appEnterpriseCustomerExcludedDeptIds(ctx, user.TenantId, user.DeptId)
|
||||
mod, err := appEnterpriseCustomerQuery(ctx, user)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, excluded := excludeIds[deptId]; excluded {
|
||||
return false, nil
|
||||
}
|
||||
dept, err := appLoadCustomerDept(ctx, user.TenantId, deptId)
|
||||
count, err := mod.Where(dao.SysDept.Columns().DeptId, deptId).Count()
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, gerror.Wrap(err, "校验企业客户范围失败")
|
||||
}
|
||||
if dept == nil || dept.DeptId <= 0 || dept.ParentId != user.DeptId {
|
||||
return false, nil
|
||||
}
|
||||
return appEnterpriseDeptVisibleByDataScope(ctx, user, dept.DeptId)
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func appEnterpriseCustomerExcludedDeptIds(ctx context.Context, tenantId string, currentDeptId int64) (map[int64]struct{}, error) {
|
||||
@@ -461,6 +543,16 @@ func appDeptIds(depts []*entity.SysDept) []int64 {
|
||||
return ids
|
||||
}
|
||||
|
||||
func appAccumulateCustomerStatusCount(counts []model.AppCustomerStatusCount, status, count int) []model.AppCustomerStatusCount {
|
||||
for index := range counts {
|
||||
if counts[index].Status == status {
|
||||
counts[index].Count += count
|
||||
return counts
|
||||
}
|
||||
}
|
||||
return append(counts, model.AppCustomerStatusCount{Status: status, Count: count})
|
||||
}
|
||||
|
||||
func appCardStatsByDept(ctx context.Context, tenantId string, deptIds []int64) (map[int64]*appCustomerCardStat, error) {
|
||||
result := make(map[int64]*appCustomerCardStat)
|
||||
if len(deptIds) == 0 {
|
||||
@@ -474,16 +566,28 @@ func appCardStatsByDept(ctx context.Context, tenantId string, deptIds []int64) (
|
||||
Fields(cardTable+"."+dao.TSimCardInfo.Columns().DeptId+" AS dept_id").
|
||||
Fields("COUNT(1) AS card_count").
|
||||
Fields(normalCountExpr).
|
||||
Fields("COALESCE("+cardTable+"."+dao.TSimCardInfo.Columns().BossCardStatus+", 0) AS boss_card_status").
|
||||
Fields("COALESCE("+cardTable+"."+dao.TSimCardInfo.Columns().PackageStatus+", -1) AS package_status").
|
||||
Where(cardTable+"."+dao.TSimCardInfo.Columns().TenantId, tenantId).
|
||||
WhereIn(cardTable+"."+dao.TSimCardInfo.Columns().DeptId, deptIds).
|
||||
Group(cardTable + "." + dao.TSimCardInfo.Columns().DeptId).
|
||||
Group(cardTable + "." + dao.TSimCardInfo.Columns().DeptId + ", COALESCE(" + cardTable + "." + dao.TSimCardInfo.Columns().BossCardStatus + ", 0), COALESCE(" + cardTable + "." + dao.TSimCardInfo.Columns().PackageStatus + ", -1)").
|
||||
OrderAsc("boss_card_status").
|
||||
OrderAsc("package_status").
|
||||
Scan(&rows)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "统计企业客户卡数量失败")
|
||||
}
|
||||
for _, row := range rows {
|
||||
if row != nil {
|
||||
result[row.DeptId] = row
|
||||
stat := result[row.DeptId]
|
||||
if stat == nil {
|
||||
stat = &appCustomerCardStat{DeptId: row.DeptId}
|
||||
result[row.DeptId] = stat
|
||||
}
|
||||
stat.CardCount += row.CardCount
|
||||
stat.NormalCardCount += row.NormalCardCount
|
||||
stat.CardStatusCounts = appAccumulateCustomerStatusCount(stat.CardStatusCounts, row.BossCardStatus, row.CardCount)
|
||||
stat.PackageStatusCounts = appAccumulateCustomerStatusCount(stat.PackageStatusCounts, row.PackageStatus, row.CardCount)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
@@ -680,6 +784,8 @@ func appEnterpriseCustomerModel(dept *entity.SysDept, profile *entity.TAgentEnte
|
||||
if stat != nil {
|
||||
item.CardCount = stat.CardCount
|
||||
item.NormalCardCount = stat.NormalCardCount
|
||||
item.CardStatusCounts = stat.CardStatusCounts
|
||||
item.PackageStatusCounts = stat.PackageStatusCounts
|
||||
}
|
||||
return item
|
||||
}
|
||||
@@ -710,6 +816,8 @@ func appTerminalCustomerModel(terminal *entity.TTerminal, stat *appCustomerCardS
|
||||
if stat != nil {
|
||||
item.CardCount = stat.CardCount
|
||||
item.NormalCardCount = stat.NormalCardCount
|
||||
item.CardStatusCounts = stat.CardStatusCounts
|
||||
item.PackageStatusCounts = stat.PackageStatusCounts
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user