170 lines
5.6 KiB
Go
170 lines
5.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/exportcenter"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
)
|
|
|
|
// agentExportColumns 只允许列表字段,列标题和映射由服务端确定。
|
|
func agentExportColumns(raw string, defaults []model.ExportColumn) ([]model.ExportColumn, error) {
|
|
if strings.TrimSpace(raw) == "" {
|
|
return nil, gerror.New("请选择导出列")
|
|
}
|
|
var requested []model.ExportColumn
|
|
if err := json.Unmarshal([]byte(raw), &requested); err != nil {
|
|
return nil, gerror.New("导出列格式错误")
|
|
}
|
|
allowed := make(map[string]model.ExportColumn, len(defaults))
|
|
for _, col := range defaults {
|
|
allowed[col.Field] = col
|
|
}
|
|
result := make([]model.ExportColumn, 0, len(requested))
|
|
seen := make(map[string]bool)
|
|
for _, col := range requested {
|
|
safe, ok := allowed[col.Field]
|
|
if !ok {
|
|
return nil, gerror.New("包含不允许导出的字段")
|
|
}
|
|
if !seen[col.Field] {
|
|
result = append(result, safe)
|
|
seen[col.Field] = true
|
|
}
|
|
}
|
|
if len(result) == 0 {
|
|
return nil, gerror.New("请选择导出列")
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *sAgentUser) UserExport(ctx context.Context, in *model.AgentUserListParam) error {
|
|
columns, err := agentExportColumns(in.ExportColumnsJson, []model.ExportColumn{
|
|
{Field: "userName", Label: "用户名称"}, {Field: "nickName", Label: "用户昵称"},
|
|
{Field: "roles", Label: "角色"}, {Field: "phonenumber", Label: "联系电话"},
|
|
{Field: "email", Label: "邮箱"}, {Field: "status", Label: "状态"}, {Field: "createdAt", Label: "创建时间"},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = exportcenter.Submit(ctx, &exportcenter.Request{
|
|
BizKey: "agentUser", BizName: "用户管理", PermCode: "cpm:agent:user:export", QueryParam: in, Columns: columns,
|
|
RowFields: agentExportFields(columns),
|
|
ToRow: func(value interface{}) []interface{} {
|
|
row := value.(map[string]interface{})
|
|
values := make([]interface{}, 0, len(columns))
|
|
for _, column := range columns {
|
|
values = append(values, row[column.Field])
|
|
}
|
|
return values
|
|
},
|
|
Tags: func() ([]string, error) {
|
|
labels := make([]string, 0, len(columns))
|
|
for _, col := range columns {
|
|
labels = append(labels, col.Label)
|
|
}
|
|
return labels, nil
|
|
},
|
|
Fetch: func(ctx context.Context, page, pageSize int) ([]interface{}, int, error) {
|
|
query := *in
|
|
query.Page = page
|
|
query.PageSize = pageSize
|
|
list, total, err := s.UserList(ctx, &query)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
rows := make([]interface{}, 0, len(list))
|
|
for _, item := range list {
|
|
roles := make([]string, 0, len(item.Roles))
|
|
for _, role := range item.Roles {
|
|
if role != nil && role.RoleName != "" {
|
|
roles = append(roles, role.RoleName)
|
|
}
|
|
}
|
|
roleNames := strings.Join(roles, "、")
|
|
if roleNames == "" {
|
|
roleNames = "-"
|
|
}
|
|
createdAt := ""
|
|
if item.CreatedAt != nil {
|
|
createdAt = item.CreatedAt.Format("Y-m-d H:i:s")
|
|
}
|
|
rows = append(rows, map[string]interface{}{"userName": item.UserName, "nickName": item.NickName, "roles": roleNames, "phonenumber": item.Phonenumber, "email": item.Email, "status": agentExportStatus(item.Status), "createdAt": createdAt})
|
|
}
|
|
return rows, total, nil
|
|
},
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (s *sAgentDept) DeptExport(ctx context.Context, in *model.AgentDeptListParam) error {
|
|
columns, err := agentExportColumns(in.ExportColumnsJson, []model.ExportColumn{
|
|
{Field: "deptName", Label: "企业名称"}, {Field: "userName", Label: "登录账号"}, {Field: "nickName", Label: "负责人"},
|
|
{Field: "phone", Label: "联系电话"}, {Field: "email", Label: "联系人邮箱"}, {Field: "areaCode", Label: "组织所在地"},
|
|
{Field: "address", Label: "详细地址"}, {Field: "createdAt", Label: "创建时间"}, {Field: "status", Label: "状态"},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = exportcenter.Submit(ctx, &exportcenter.Request{
|
|
BizKey: "agentDept", BizName: "企业客户", PermCode: "cpm:agent:dept:export", QueryParam: in, Columns: columns,
|
|
RowFields: agentExportFields(columns),
|
|
ToRow: func(value interface{}) []interface{} {
|
|
row := value.(map[string]interface{})
|
|
values := make([]interface{}, 0, len(columns))
|
|
for _, column := range columns {
|
|
values = append(values, row[column.Field])
|
|
}
|
|
return values
|
|
},
|
|
Tags: func() ([]string, error) {
|
|
labels := make([]string, 0, len(columns))
|
|
for _, col := range columns {
|
|
labels = append(labels, col.Label)
|
|
}
|
|
return labels, nil
|
|
},
|
|
Fetch: func(ctx context.Context, page, pageSize int) ([]interface{}, int, error) {
|
|
query := *in
|
|
query.Page = page
|
|
query.PageSize = pageSize
|
|
list, total, err := s.DeptList(ctx, &query)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
rows := make([]interface{}, 0, len(list))
|
|
for _, item := range list {
|
|
createdAt := ""
|
|
if item.CreatedAt != nil {
|
|
createdAt = item.CreatedAt.Format("Y-m-d H:i:s")
|
|
}
|
|
rows = append(rows, map[string]interface{}{"deptName": item.DeptName, "userName": item.UserName, "nickName": item.NickName, "phone": item.Phone, "email": item.Email, "areaCode": item.AreaCode, "address": item.Address, "createdAt": createdAt, "status": agentExportStatus(item.Status)})
|
|
}
|
|
return rows, total, nil
|
|
},
|
|
})
|
|
return err
|
|
}
|
|
|
|
func agentExportFields(columns []model.ExportColumn) []string {
|
|
fields := make([]string, 0, len(columns))
|
|
for _, column := range columns {
|
|
fields = append(fields, column.Field)
|
|
}
|
|
return fields
|
|
}
|
|
|
|
func agentExportStatus(status string) string {
|
|
if status == "0" {
|
|
return "正常"
|
|
}
|
|
if status == "1" {
|
|
return "停用"
|
|
}
|
|
return "未知"
|
|
}
|