Files
iotlcm/server/internal/library/allocationunits/units.go
T
MiFaZhan 59eb34976a chore: 初始开源发布
iotlcm-admin-svr:管理后台服务。

- Go module:iotlcm → git.5iot.com/iotlcm-public/iotlcm-admin-svr(729 文件)
- 凭据与地址脱敏:JWT signingKey(含代码里的 MustGet 默认值)、
  MySQL DSN、Redis 口令、告警邮件发件人邮箱全部改为占位;
  私网 IP → 文档地址段;库名 xiujie_admin → iotlcm
- 真实手机号/邮箱 → 合法 11 位编造号 / example.com
- 移除内部资产:CI 触发器、过时 DDL(sql/db_iotlcm.sql)、
  私有依赖 go get 行、明文 registry 登录口令
- 启动 banner 统一 IOTLCM 方块字 + 社区惯例署名
- Dockerfile 改用公共基础镜像
- 拼错的 RADEME.md → README.md,README 重写
- 需要真实数据库的用例改为可用 IOTLCM_TEST_WITH_DB=1 显式启用
- 补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes

数据库 schema 不随本仓发布(生产 DDL 已过时),README 已说明。
2026-10-23 14:48:30 +08:00

142 lines
3.9 KiB
Go

package allocationunits
import (
"context"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
"strings"
)
// Fields 是日志展示契约,不增加数据库字段。
type Fields struct {
SourceDeptId int64 `json:"sourceDeptId" orm:"-"`
SourceDeptName string `json:"sourceDeptName" orm:"-"`
TargetDeptId int64 `json:"targetDeptId" orm:"-"`
TargetDeptName string `json:"targetDeptName" orm:"-"`
TargetDeptMode string `json:"targetDeptMode" orm:"-"`
}
type Unit struct {
Id int64 `orm:"dept_id"`
Key string `orm:"dept_key"`
Name string `orm:"dept_name"`
}
func Load(ctx context.Context, tenant string, ids []int64, tx ...gdb.TX) (map[int64]Unit, error) {
unique := map[int64]bool{}
for _, id := range ids {
if id > 0 {
unique[id] = true
}
}
result := map[int64]Unit{}
if len(unique) == 0 {
return result, nil
}
ids = nil
for id := range unique {
ids = append(ids, id)
}
var rows []Unit
if len(tx) > 0 {
result, err := tx[0].GetAll("SELECT dept_id,dept_key,dept_name FROM sys_dept WHERE tenant_id=? AND deleted_at IS NULL AND dept_id IN (?)", tenant, ids)
if err != nil {
return nil, gerror.Wrap(err, "读取划拨单位信息失败")
}
if err := result.Structs(&rows); err != nil {
return nil, err
}
units := map[int64]Unit{}
for _, row := range rows {
units[row.Id] = row
}
return units, nil
}
mod := dao.SysDept.Ctx(ctx)
if err := mod.WhereNull("deleted_at").Where("tenant_id", tenant).WhereIn("dept_id", ids).Fields("dept_id", "dept_key", "dept_name", "deleted_at").Scan(&rows); err != nil {
return nil, gerror.Wrap(err, "读取划拨单位信息失败")
}
for _, row := range rows {
result[row.Id] = row
}
return result, nil
}
// Snapshot 名称由服务端查询,仅在受理时记录,执行时不覆盖。
func Snapshot(ctx context.Context, tenant string, source, target int64, tx ...gdb.TX) (*gjson.Json, error) {
units, err := Load(ctx, tenant, []int64{source, target}, tx...)
if err != nil {
return nil, err
}
mode := "single"
if target <= 0 {
mode = "per_row"
}
return gjson.New(map[string]any{"sourceDeptId": source, "sourceDeptKey": units[source].Key, "sourceDeptName": units[source].Name, "targetDeptId": target, "targetDeptKey": units[target].Key, "targetDeptName": units[target].Name, "targetDeptMode": mode}), nil
}
func Resolve(info *gjson.Json, source, target int64) Fields {
f := Fields{SourceDeptId: source, TargetDeptId: target}
if info != nil {
if id := info.Get("sourceDeptId").Int64(); id > 0 {
f.SourceDeptId = id
}
if id := info.Get("targetDeptId").Int64(); id > 0 {
f.TargetDeptId = id
}
f.SourceDeptName = strings.TrimSpace(info.Get("sourceDeptName").String())
f.TargetDeptName = strings.TrimSpace(info.Get("targetDeptName").String())
f.TargetDeptMode = info.Get("targetDeptMode").String()
}
if f.TargetDeptMode == "" {
if f.TargetDeptId > 0 {
f.TargetDeptMode = "single"
} else {
f.TargetDeptMode = "per_row"
}
}
return f
}
// Fill 一页只补查一次旧数据的单位名称,不扫描卡片当前归属,不覆盖历史快照。
func Fill(ctx context.Context, tenant string, fields []*Fields) error {
ids := []int64{}
for _, f := range fields {
if f.SourceDeptName == "" {
ids = append(ids, f.SourceDeptId)
}
if f.TargetDeptName == "" {
ids = append(ids, f.TargetDeptId)
}
}
units, err := Load(ctx, tenant, ids)
if err != nil {
return err
}
for _, f := range fields {
if f.SourceDeptName == "" {
f.SourceDeptName = units[f.SourceDeptId].Name
}
if f.TargetDeptName == "" {
f.TargetDeptName = units[f.TargetDeptId].Name
}
}
return nil
}
func Merge(info, units *gjson.Json) *gjson.Json {
data := map[string]any{}
if info != nil {
for k, v := range info.Map() {
data[k] = v
}
}
if units != nil {
for k, v := range units.Map() {
data[k] = v
}
}
return gjson.New(data)
}