2ed5dfa6a1
iotlcm-app:小程序端 + 用户端服务。 - app/:uni-app 小程序前端(基于 unibest,MIT) - 微信 AppID → YOUR_WX_APPID,uni-app AppID → __UNI__YOURAPPID - 内网后端地址 → admin.iotlcm.example.com - 应用标题与 unibest 模板 README 重写(保留上游致谢与 MIT 声明) - server/:GoFrame 用户端服务(基于 XiuAdmin,Apache-2.0) - module xiuadmin → git.5iot.com/iotlcm-public/iotlcm-app(360 文件) - 支付回调地址 → app.iotlcm.example.com,商户名与应用名泛化 - 真实手机号 → 合法 11 位编造号(保留号段前 3 位 + 后 4 位) - 依赖号码尾部的断言同步更新 - 补 server/LICENSE(Apache-2.0) - 移除内部资产:CI 触发器 - 补根 README,LF 行尾 + .gitattributes 已知:TestSysDept_RefreshDeptAncestors 在源仓同一提交上同样失败,属既有缺陷。
756 lines
24 KiB
Go
756 lines
24 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/consts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/dao"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/entity"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
)
|
|
|
|
const (
|
|
appCustomerTypeAll = "all"
|
|
appCustomerTypeEnterprise = "enterprise"
|
|
appCustomerTypeTerminal = "terminal"
|
|
)
|
|
|
|
type appCustomerCardStat struct {
|
|
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) {
|
|
if param == nil {
|
|
param = &model.AppCustomerListParam{}
|
|
}
|
|
user, err := appCurrentUser(ctx)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if err = appEnsureEnterpriseCustomerScope(ctx, user); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if param.Page <= 0 {
|
|
param.Page = 1
|
|
}
|
|
if param.PageSize <= 0 {
|
|
param.PageSize = 20
|
|
}
|
|
if param.PageSize > 200 {
|
|
param.PageSize = 200
|
|
}
|
|
|
|
customerType := appNormalizeCustomerType(param.CustomerType)
|
|
if customerType == appCustomerTypeTerminal {
|
|
return nil, 0, gerror.New("暂不支持查看终端客户")
|
|
}
|
|
|
|
list, err := s.enterpriseCustomerList(ctx, user, param)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
total = len(list)
|
|
start := (param.Page - 1) * param.PageSize
|
|
if start >= total {
|
|
return []*model.AppCustomerModel{}, total, nil
|
|
}
|
|
end := start + param.PageSize
|
|
if end > total {
|
|
end = total
|
|
}
|
|
return list[start:end], total, nil
|
|
}
|
|
|
|
func (s *sAppUser) CustomerDetail(ctx context.Context, param *model.AppCustomerDetailParam) (*model.AppCustomerDetailModel, error) {
|
|
if param == nil {
|
|
param = &model.AppCustomerDetailParam{}
|
|
}
|
|
user, err := appCurrentUser(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err = appEnsureEnterpriseCustomerScope(ctx, user); err != nil {
|
|
return nil, err
|
|
}
|
|
customerType := appNormalizeCustomerType(param.CustomerType)
|
|
switch customerType {
|
|
case appCustomerTypeEnterprise:
|
|
return s.enterpriseCustomerDetail(ctx, user, param.CustomerId)
|
|
case appCustomerTypeTerminal:
|
|
return nil, gerror.New("暂不支持查看终端客户")
|
|
default:
|
|
return nil, gerror.New("客户类型不正确")
|
|
}
|
|
}
|
|
|
|
func (s *sAppUser) enterpriseCustomerList(ctx context.Context, user *entity.SysUser, param *model.AppCustomerListParam) ([]*model.AppCustomerModel, error) {
|
|
depts, err := appEnterpriseDirectCustomerDepts(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
|
|
}
|
|
|
|
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)
|
|
list = append(list, item)
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func (s *sAppUser) enterpriseCustomerDetail(ctx context.Context, user *entity.SysUser, deptId int64) (*model.AppCustomerDetailModel, error) {
|
|
dept, err := appLoadCustomerDept(ctx, user.TenantId, deptId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if dept == nil || dept.DeptId == 0 {
|
|
return nil, gerror.New("企业客户不存在")
|
|
}
|
|
visible, err := appEnterpriseCustomerDeptVisible(ctx, user, dept.DeptId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !visible {
|
|
return nil, gerror.New("企业客户不在当前企业可查看范围内")
|
|
}
|
|
profile, err := appEnterpriseProfileByDeptId(ctx, user.TenantId, dept.DeptId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stats, err := appCardStatsByDept(ctx, user.TenantId, []int64{dept.DeptId})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
item := appEnterpriseCustomerModel(dept, profile, 0, stats[dept.DeptId])
|
|
return &model.AppCustomerDetailModel{
|
|
AppCustomerModel: *item,
|
|
EnterpriseProfile: appEnterpriseProfileModel(dept, profile),
|
|
}, nil
|
|
}
|
|
|
|
func (s *sAppUser) terminalCustomerList(ctx context.Context, user *entity.SysUser, param *model.AppCustomerListParam) ([]*model.AppCustomerModel, error) {
|
|
visibleDeptIds, err := appVisibleCustomerDeptIds(ctx, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
terminalIds, err := appTerminalIdsByVisibleCards(ctx, user.TenantId, visibleDeptIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(terminalIds) == 0 {
|
|
return []*model.AppCustomerModel{}, nil
|
|
}
|
|
terminals, err := appTerminalMap(ctx, user.TenantId, terminalIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stats, err := appCardStatsByTerminal(ctx, user.TenantId, visibleDeptIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
keyword := strings.TrimSpace(param.Keyword)
|
|
status := strings.TrimSpace(param.Status)
|
|
list := make([]*model.AppCustomerModel, 0, len(terminals))
|
|
for _, terminalId := range terminalIds {
|
|
terminal := terminals[terminalId]
|
|
if terminal == nil || terminal.TerminalId == 0 {
|
|
continue
|
|
}
|
|
if status != "" && strings.TrimSpace(terminal.Status) != status {
|
|
continue
|
|
}
|
|
if keyword != "" && !appTerminalMatchesKeyword(terminal, keyword) {
|
|
continue
|
|
}
|
|
item := appTerminalCustomerModel(terminal, stats[terminal.TerminalId])
|
|
list = append(list, item)
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func (s *sAppUser) terminalCustomerDetail(ctx context.Context, user *entity.SysUser, terminalId int64) (*model.AppCustomerDetailModel, error) {
|
|
visibleDeptIds, err := appVisibleCustomerDeptIds(ctx, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
visible, err := appTerminalHasVisibleCard(ctx, user.TenantId, terminalId, visibleDeptIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !visible {
|
|
return nil, gerror.New("终端客户不在当前企业可查看范围内")
|
|
}
|
|
terminals, err := appTerminalMap(ctx, user.TenantId, []int64{terminalId})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
terminal := terminals[terminalId]
|
|
if terminal == nil || terminal.TerminalId == 0 {
|
|
return nil, gerror.New("终端客户不存在")
|
|
}
|
|
stats, err := appCardStatsByTerminal(ctx, user.TenantId, visibleDeptIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
item := appTerminalCustomerModel(terminal, stats[terminal.TerminalId])
|
|
return &model.AppCustomerDetailModel{
|
|
AppCustomerModel: *item,
|
|
TerminalProfile: appTerminalProfileModel(terminal),
|
|
}, nil
|
|
}
|
|
|
|
func appEnsureEnterpriseCustomerScope(ctx context.Context, user *entity.SysUser) error {
|
|
if user == nil {
|
|
return gerror.New("登录已过期")
|
|
}
|
|
if appIsTerminalUser(ctx, user) {
|
|
return gerror.New("终端用户不能查看客户管理")
|
|
}
|
|
if user.DeptId <= 0 {
|
|
return gerror.New("当前账号未绑定企业,不能查看客户管理")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func appNormalizeCustomerType(customerType string) string {
|
|
switch strings.TrimSpace(customerType) {
|
|
case appCustomerTypeEnterprise:
|
|
return appCustomerTypeEnterprise
|
|
case appCustomerTypeTerminal:
|
|
return appCustomerTypeTerminal
|
|
default:
|
|
return appCustomerTypeAll
|
|
}
|
|
}
|
|
|
|
func appDirectChildDepts(ctx context.Context, user *entity.SysUser) ([]*entity.SysDept, error) {
|
|
var rows []*entity.SysDept
|
|
err := dao.SysDept.Ctx(ctx).
|
|
Where(dao.SysDept.Columns().TenantId, user.TenantId).
|
|
Where(dao.SysDept.Columns().ParentId, user.DeptId).
|
|
WhereNull(dao.SysDept.Columns().DeletedAt).
|
|
OrderAsc(dao.SysDept.Columns().OrderNum).
|
|
OrderAsc(dao.SysDept.Columns().DeptId).
|
|
Scan(&rows)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "查询直属下级企业客户失败")
|
|
}
|
|
return rows, nil
|
|
}
|
|
|
|
func appEnterpriseDirectCustomerDepts(ctx context.Context, user *entity.SysUser) ([]*entity.SysDept, error) {
|
|
rows, err := appDirectChildDepts(ctx, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
excludeIds, err := appEnterpriseCustomerExcludedDeptIds(ctx, user.TenantId, user.DeptId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]*entity.SysDept, 0, len(rows))
|
|
for _, row := range rows {
|
|
if row == nil || row.DeptId <= 0 {
|
|
continue
|
|
}
|
|
if _, excluded := excludeIds[row.DeptId]; excluded {
|
|
continue
|
|
}
|
|
result = append(result, row)
|
|
}
|
|
return appFilterDeptsByEnterpriseDataScope(ctx, user, result)
|
|
}
|
|
|
|
func appEnterpriseSubCustomerDepts(ctx context.Context, user *entity.SysUser) ([]*entity.SysDept, error) {
|
|
if user == nil || user.DeptId <= 0 {
|
|
return []*entity.SysDept{}, nil
|
|
}
|
|
visibleIds, err := appEnterpriseDeptAndSubIds(ctx, user.TenantId, user.DeptId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
excludeIds, err := appEnterpriseCustomerExcludedDeptIds(ctx, user.TenantId, user.DeptId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
deptIds := make([]int64, 0, len(visibleIds))
|
|
for _, deptId := range visibleIds {
|
|
if deptId <= 0 || deptId == user.DeptId {
|
|
continue
|
|
}
|
|
if _, excluded := excludeIds[deptId]; excluded {
|
|
continue
|
|
}
|
|
deptIds = append(deptIds, deptId)
|
|
}
|
|
if len(deptIds) == 0 {
|
|
return []*entity.SysDept{}, nil
|
|
}
|
|
|
|
var rows []*entity.SysDept
|
|
err = dao.SysDept.Ctx(ctx).
|
|
Where(dao.SysDept.Columns().TenantId, user.TenantId).
|
|
WhereIn(dao.SysDept.Columns().DeptId, deptIds).
|
|
WhereNull(dao.SysDept.Columns().DeletedAt).
|
|
OrderAsc(dao.SysDept.Columns().ParentId).
|
|
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)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if _, excluded := excludeIds[deptId]; excluded {
|
|
return false, nil
|
|
}
|
|
dept, err := appLoadCustomerDept(ctx, user.TenantId, deptId)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if dept == nil || dept.DeptId <= 0 || dept.ParentId != user.DeptId {
|
|
return false, nil
|
|
}
|
|
return appEnterpriseDeptVisibleByDataScope(ctx, user, dept.DeptId)
|
|
}
|
|
|
|
func appEnterpriseCustomerExcludedDeptIds(ctx context.Context, tenantId string, currentDeptId int64) (map[int64]struct{}, error) {
|
|
excludeIds := make(map[int64]struct{})
|
|
registerDeptId := appRegisterDeptOrDefault(ctx, tenantId)
|
|
if registerDeptId <= 0 {
|
|
return excludeIds, nil
|
|
}
|
|
registerDeptIds, err := appEnterpriseDeptAndSubIds(ctx, tenantId, registerDeptId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, deptId := range registerDeptIds {
|
|
if deptId == currentDeptId {
|
|
return excludeIds, nil
|
|
}
|
|
}
|
|
for _, deptId := range registerDeptIds {
|
|
if deptId > 0 {
|
|
excludeIds[deptId] = struct{}{}
|
|
}
|
|
}
|
|
return excludeIds, nil
|
|
}
|
|
|
|
func appVisibleCustomerDeptIds(ctx context.Context, user *entity.SysUser) ([]int64, error) {
|
|
children, err := appEnterpriseSubCustomerDepts(ctx, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ids := make([]int64, 0, len(children)+1)
|
|
ids = append(ids, user.DeptId)
|
|
for _, child := range children {
|
|
if child != nil && child.DeptId > 0 {
|
|
ids = append(ids, child.DeptId)
|
|
}
|
|
}
|
|
depts := make([]*entity.SysDept, 0, len(ids))
|
|
for _, deptId := range ids {
|
|
depts = append(depts, &entity.SysDept{DeptId: deptId})
|
|
}
|
|
filtered, err := appFilterDeptsByEnterpriseDataScope(ctx, user, depts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return appDeptIds(filtered), nil
|
|
}
|
|
|
|
func appFilterEnterpriseDepts(depts []*entity.SysDept, keyword string, status string) []*entity.SysDept {
|
|
result := make([]*entity.SysDept, 0, len(depts))
|
|
for _, dept := range depts {
|
|
if dept == nil || dept.DeptId == 0 {
|
|
continue
|
|
}
|
|
if status != "" && strings.TrimSpace(dept.Status) != status {
|
|
continue
|
|
}
|
|
if keyword != "" && !appDeptMatchesKeyword(dept, keyword) {
|
|
continue
|
|
}
|
|
result = append(result, dept)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func appDeptMatchesKeyword(dept *entity.SysDept, keyword string) bool {
|
|
if dept == nil {
|
|
return false
|
|
}
|
|
fields := []string{
|
|
dept.DeptName,
|
|
dept.DeptKey,
|
|
dept.Phone,
|
|
dept.Email,
|
|
dept.CreditCode,
|
|
strconv.FormatInt(dept.DeptId, 10),
|
|
}
|
|
for _, field := range fields {
|
|
if strings.Contains(strings.TrimSpace(field), keyword) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func appTerminalMatchesKeyword(terminal *entity.TTerminal, keyword string) bool {
|
|
if terminal == nil {
|
|
return false
|
|
}
|
|
fields := []string{
|
|
terminal.TerminalName,
|
|
terminal.Phone,
|
|
terminal.Email,
|
|
terminal.FullName,
|
|
terminal.CreditCode,
|
|
strconv.FormatInt(terminal.TerminalId, 10),
|
|
}
|
|
for _, field := range fields {
|
|
if strings.Contains(strings.TrimSpace(field), keyword) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func appDeptIds(depts []*entity.SysDept) []int64 {
|
|
ids := make([]int64, 0, len(depts))
|
|
for _, dept := range depts {
|
|
if dept != nil && dept.DeptId > 0 {
|
|
ids = append(ids, dept.DeptId)
|
|
}
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func appCardStatsByDept(ctx context.Context, tenantId string, deptIds []int64) (map[int64]*appCustomerCardStat, error) {
|
|
result := make(map[int64]*appCustomerCardStat)
|
|
if len(deptIds) == 0 {
|
|
return result, nil
|
|
}
|
|
var rows []*appCustomerCardStat
|
|
cardTable := dao.TSimCardInfo.Table()
|
|
normalCountExpr := "SUM(CASE WHEN " + cardTable + "." + dao.TSimCardInfo.Columns().BossCardStatus + " = " + strconv.Itoa(consts.V3ApiSimBossCardStatusActivated) +
|
|
" AND " + cardTable + "." + dao.TSimCardInfo.Columns().PackageStatus + " <> " + strconv.Itoa(consts.PackageStatusExpired) + " THEN 1 ELSE 0 END) AS normal_card_count"
|
|
err := dao.TSimCardInfo.Ctx(ctx).
|
|
Fields(cardTable+"."+dao.TSimCardInfo.Columns().DeptId+" AS dept_id").
|
|
Fields("COUNT(1) AS card_count").
|
|
Fields(normalCountExpr).
|
|
Where(cardTable+"."+dao.TSimCardInfo.Columns().TenantId, tenantId).
|
|
WhereIn(cardTable+"."+dao.TSimCardInfo.Columns().DeptId, deptIds).
|
|
Group(cardTable + "." + dao.TSimCardInfo.Columns().DeptId).
|
|
Scan(&rows)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "统计企业客户卡数量失败")
|
|
}
|
|
for _, row := range rows {
|
|
if row != nil {
|
|
result[row.DeptId] = row
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func appCardStatsByTerminal(ctx context.Context, tenantId string, visibleDeptIds []int64) (map[int64]*appCustomerCardStat, error) {
|
|
result := make(map[int64]*appCustomerCardStat)
|
|
if len(visibleDeptIds) == 0 {
|
|
return result, nil
|
|
}
|
|
bindTable := dao.TSimTerminal.Table()
|
|
cardTable := dao.TSimCardInfo.Table()
|
|
var rows []*appCustomerCardStat
|
|
normalCountExpr := "SUM(CASE WHEN " + cardTable + "." + dao.TSimCardInfo.Columns().BossCardStatus + " = " + strconv.Itoa(consts.V3ApiSimBossCardStatusActivated) +
|
|
" AND " + cardTable + "." + dao.TSimCardInfo.Columns().PackageStatus + " <> " + strconv.Itoa(consts.PackageStatusExpired) + " THEN 1 ELSE 0 END) AS normal_card_count"
|
|
err := dao.TSimTerminal.Ctx(ctx).
|
|
Fields(bindTable+"."+dao.TSimTerminal.Columns().TerminalId+" AS terminal_id").
|
|
Fields("COUNT(DISTINCT "+bindTable+"."+dao.TSimTerminal.Columns().Iccid+") AS card_count").
|
|
Fields(normalCountExpr).
|
|
InnerJoin(cardTable, cardTable+"."+dao.TSimCardInfo.Columns().Iccid+"="+bindTable+"."+dao.TSimTerminal.Columns().Iccid+" AND "+cardTable+"."+dao.TSimCardInfo.Columns().TenantId+"="+bindTable+"."+dao.TSimTerminal.Columns().TenantId).
|
|
Where(bindTable+"."+dao.TSimTerminal.Columns().TenantId, tenantId).
|
|
WhereIn(cardTable+"."+dao.TSimCardInfo.Columns().DeptId, visibleDeptIds).
|
|
Group(bindTable + "." + dao.TSimTerminal.Columns().TerminalId).
|
|
Scan(&rows)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "统计终端客户卡数量失败")
|
|
}
|
|
for _, row := range rows {
|
|
if row != nil {
|
|
result[row.TerminalId] = row
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func appAgentBalanceMap(ctx context.Context, tenantId string, deptIds []int64) (map[int64]float64, error) {
|
|
result := make(map[int64]float64)
|
|
if len(deptIds) == 0 {
|
|
return result, nil
|
|
}
|
|
var rows []*entity.TAgentBalance
|
|
err := dao.TAgentBalance.Ctx(ctx).
|
|
Where(dao.TAgentBalance.Columns().TenantId, tenantId).
|
|
WhereIn(dao.TAgentBalance.Columns().DeptId, deptIds).
|
|
Scan(&rows)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "查询企业客户余额失败")
|
|
}
|
|
for _, row := range rows {
|
|
if row != nil {
|
|
result[row.DeptId] = appRoundAmount(row.Balance)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func appEnterpriseProfileMap(ctx context.Context, tenantId string, deptIds []int64) (map[int64]*entity.TAgentEnterprise, error) {
|
|
result := make(map[int64]*entity.TAgentEnterprise)
|
|
if len(deptIds) == 0 {
|
|
return result, nil
|
|
}
|
|
var rows []*entity.TAgentEnterprise
|
|
err := dao.TAgentEnterprise.Ctx(ctx).
|
|
Where(dao.TAgentEnterprise.Columns().TenantId, tenantId).
|
|
WhereIn(dao.TAgentEnterprise.Columns().DeptId, deptIds).
|
|
Scan(&rows)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "查询企业客户实名资料失败")
|
|
}
|
|
for _, row := range rows {
|
|
if row != nil {
|
|
result[row.DeptId] = row
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func appEnterpriseProfileByDeptId(ctx context.Context, tenantId string, deptId int64) (*entity.TAgentEnterprise, error) {
|
|
var profile *entity.TAgentEnterprise
|
|
err := dao.TAgentEnterprise.Ctx(ctx).
|
|
Where(dao.TAgentEnterprise.Columns().TenantId, tenantId).
|
|
Where(dao.TAgentEnterprise.Columns().DeptId, deptId).
|
|
Limit(1).
|
|
Scan(&profile)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "查询企业客户实名资料失败")
|
|
}
|
|
return profile, nil
|
|
}
|
|
|
|
func appTerminalIdsByVisibleCards(ctx context.Context, tenantId string, visibleDeptIds []int64) ([]int64, error) {
|
|
if len(visibleDeptIds) == 0 {
|
|
return []int64{}, nil
|
|
}
|
|
bindTable := dao.TSimTerminal.Table()
|
|
cardTable := dao.TSimCardInfo.Table()
|
|
var rows []*appTerminalCustomerIdRow
|
|
err := dao.TSimTerminal.Ctx(ctx).
|
|
Fields(bindTable+"."+dao.TSimTerminal.Columns().TerminalId+" AS terminal_id").
|
|
InnerJoin(cardTable, cardTable+"."+dao.TSimCardInfo.Columns().Iccid+"="+bindTable+"."+dao.TSimTerminal.Columns().Iccid+" AND "+cardTable+"."+dao.TSimCardInfo.Columns().TenantId+"="+bindTable+"."+dao.TSimTerminal.Columns().TenantId).
|
|
Where(bindTable+"."+dao.TSimTerminal.Columns().TenantId, tenantId).
|
|
WhereIn(cardTable+"."+dao.TSimCardInfo.Columns().DeptId, visibleDeptIds).
|
|
Group(bindTable + "." + dao.TSimTerminal.Columns().TerminalId).
|
|
OrderAsc(bindTable + "." + dao.TSimTerminal.Columns().TerminalId).
|
|
Scan(&rows)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "查询终端客户范围失败")
|
|
}
|
|
ids := make([]int64, 0, len(rows))
|
|
for _, row := range rows {
|
|
if row != nil && row.TerminalId > 0 {
|
|
ids = append(ids, row.TerminalId)
|
|
}
|
|
}
|
|
return ids, nil
|
|
}
|
|
|
|
func appTerminalMap(ctx context.Context, tenantId string, terminalIds []int64) (map[int64]*entity.TTerminal, error) {
|
|
result := make(map[int64]*entity.TTerminal)
|
|
if len(terminalIds) == 0 {
|
|
return result, nil
|
|
}
|
|
var rows []*entity.TTerminal
|
|
err := dao.TTerminal.Ctx(ctx).
|
|
Where(dao.TTerminal.Columns().TenantId, tenantId).
|
|
WhereIn(dao.TTerminal.Columns().TerminalId, terminalIds).
|
|
Scan(&rows)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "查询终端客户资料失败")
|
|
}
|
|
for _, row := range rows {
|
|
if row != nil {
|
|
result[row.TerminalId] = row
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func appTerminalHasVisibleCard(ctx context.Context, tenantId string, terminalId int64, visibleDeptIds []int64) (bool, error) {
|
|
if terminalId <= 0 || len(visibleDeptIds) == 0 {
|
|
return false, nil
|
|
}
|
|
bindTable := dao.TSimTerminal.Table()
|
|
cardTable := dao.TSimCardInfo.Table()
|
|
count, err := dao.TSimTerminal.Ctx(ctx).
|
|
InnerJoin(cardTable, cardTable+"."+dao.TSimCardInfo.Columns().Iccid+"="+bindTable+"."+dao.TSimTerminal.Columns().Iccid+" AND "+cardTable+"."+dao.TSimCardInfo.Columns().TenantId+"="+bindTable+"."+dao.TSimTerminal.Columns().TenantId).
|
|
Where(bindTable+"."+dao.TSimTerminal.Columns().TenantId, tenantId).
|
|
Where(bindTable+"."+dao.TSimTerminal.Columns().TerminalId, terminalId).
|
|
WhereIn(cardTable+"."+dao.TSimCardInfo.Columns().DeptId, visibleDeptIds).
|
|
Count()
|
|
if err != nil {
|
|
return false, gerror.Wrap(err, "校验终端客户范围失败")
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
func appLoadCustomerDept(ctx context.Context, tenantId string, deptId int64) (*entity.SysDept, error) {
|
|
var dept *entity.SysDept
|
|
err := dao.SysDept.Ctx(ctx).
|
|
Where(dao.SysDept.Columns().TenantId, tenantId).
|
|
Where(dao.SysDept.Columns().DeptId, deptId).
|
|
Limit(1).
|
|
Scan(&dept)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "查询企业客户失败")
|
|
}
|
|
return dept, nil
|
|
}
|
|
|
|
func appEnterpriseCustomerModel(dept *entity.SysDept, profile *entity.TAgentEnterprise, balance float64, stat *appCustomerCardStat) *model.AppCustomerModel {
|
|
item := &model.AppCustomerModel{
|
|
CustomerType: appCustomerTypeEnterprise,
|
|
CustomerTypeName: "企业客户",
|
|
CustomerId: dept.DeptId,
|
|
DeptId: dept.DeptId,
|
|
ParentDeptId: dept.ParentId,
|
|
CustomerCode: strings.TrimSpace(dept.DeptKey),
|
|
CustomerName: strings.TrimSpace(dept.DeptName),
|
|
DeptCategory: strings.TrimSpace(dept.DeptCategory),
|
|
Phone: appMaskPhone(dept.Phone),
|
|
Email: appMaskEmail(dept.Email),
|
|
Status: strings.TrimSpace(dept.Status),
|
|
StatusText: appDeptStatusText(dept.Status),
|
|
Balance: balance,
|
|
Address: appMaskAddress(dept.Address),
|
|
CreatedAt: appProfileDate(dept.CreatedAt),
|
|
}
|
|
if profile != nil {
|
|
item.FullName = strings.TrimSpace(profile.FullName)
|
|
item.RealNameStatus = appRealNameStatus(profile.FullName, profile.LegalPersonName, profile.LegalPersonIdCard)
|
|
} else {
|
|
item.RealNameStatus = "实名资料待完善"
|
|
}
|
|
if stat != nil {
|
|
item.CardCount = stat.CardCount
|
|
item.NormalCardCount = stat.NormalCardCount
|
|
}
|
|
return item
|
|
}
|
|
|
|
func appTerminalCustomerModel(terminal *entity.TTerminal, stat *appCustomerCardStat) *model.AppCustomerModel {
|
|
item := &model.AppCustomerModel{
|
|
CustomerType: appCustomerTypeTerminal,
|
|
CustomerTypeName: "终端客户",
|
|
CustomerId: terminal.TerminalId,
|
|
TerminalId: terminal.TerminalId,
|
|
CustomerName: strings.TrimSpace(terminal.TerminalName),
|
|
Phone: appMaskPhone(terminal.Phone),
|
|
Email: appMaskEmail(terminal.Email),
|
|
Status: strings.TrimSpace(terminal.Status),
|
|
StatusText: appTerminalStatusText(terminal.Status),
|
|
RealNameStatus: appRealNameStatus(terminal.FullName, terminal.LegalPersonName, terminal.LegalPersonIdCard),
|
|
Balance: appRoundAmount(terminal.Balance),
|
|
Address: appMaskAddress(terminal.Address),
|
|
FullName: strings.TrimSpace(terminal.FullName),
|
|
CreatedAt: appProfileDate(terminal.CreatedAt),
|
|
}
|
|
if item.CustomerName == "" {
|
|
item.CustomerName = item.FullName
|
|
}
|
|
if item.CustomerName == "" {
|
|
item.CustomerName = "终端客户" + strconv.FormatInt(terminal.TerminalId, 10)
|
|
}
|
|
if stat != nil {
|
|
item.CardCount = stat.CardCount
|
|
item.NormalCardCount = stat.NormalCardCount
|
|
}
|
|
return item
|
|
}
|
|
|
|
func appEnterpriseProfileModel(dept *entity.SysDept, enterprise *entity.TAgentEnterprise) *model.AppEnterpriseProfileModel {
|
|
if dept == nil || dept.DeptId == 0 {
|
|
return nil
|
|
}
|
|
profile := &model.AppEnterpriseProfileModel{
|
|
DeptId: dept.DeptId,
|
|
DeptName: strings.TrimSpace(dept.DeptName),
|
|
DeptKey: strings.TrimSpace(dept.DeptKey),
|
|
Phone: appMaskPhone(dept.Phone),
|
|
Email: appMaskEmail(dept.Email),
|
|
StatusText: appDeptStatusText(dept.Status),
|
|
CreditCode: appMaskCreditCode(dept.CreditCode),
|
|
AreaCode: strings.TrimSpace(dept.AreaCode),
|
|
Address: appMaskAddress(dept.Address),
|
|
FullName: strings.TrimSpace(dept.DeptName),
|
|
RealNameStatus: "实名资料待完善",
|
|
}
|
|
if enterprise != nil && enterprise.Id > 0 {
|
|
profile.FullName = appFirstNotEmpty(enterprise.FullName, dept.DeptName)
|
|
profile.Industry = strings.TrimSpace(enterprise.Industry)
|
|
profile.CompanyType = strings.TrimSpace(enterprise.CompanyType)
|
|
profile.EstablishTime = appProfileDate(enterprise.EstablishTime)
|
|
profile.RealNameStatus = appRealNameStatus(enterprise.FullName, enterprise.LegalPersonName, enterprise.LegalPersonIdCard)
|
|
}
|
|
return profile
|
|
}
|
|
|
|
func appDeptStatusText(status string) string {
|
|
switch strings.TrimSpace(status) {
|
|
case consts.SysDeptStatusNormal:
|
|
return "正常"
|
|
case consts.SysDeptStatusDisable:
|
|
return "停用"
|
|
case "":
|
|
return "未设置"
|
|
default:
|
|
return "未知"
|
|
}
|
|
}
|