chore: 并入 iotlcm-app 历史到 app/(原提交 f40bda9)
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
package allocationunits
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"strings"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/dao"
|
||||
)
|
||||
|
||||
// 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)
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package allocationunits
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSnapshotAndLegacyFallback(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
if err := gdb.SetConfig(gdb.Config{"default": gdb.ConfigGroup{{Type: "sqlite", Link: "sqlite::@file(:memory:)", MaxOpenConnCount: 1}}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = g.DB().Close(ctx) })
|
||||
for _, sql := range []string{"CREATE TABLE sys_dept(dept_id INTEGER,tenant_id TEXT,dept_key TEXT,dept_name TEXT,deleted_at TEXT)", "INSERT INTO sys_dept VALUES(1,'A','a','划拨单位',NULL),(2,'A','b','接收单位',NULL),(3,'B','other','其他租户',NULL),(4,'A','gone','已删除单位','2026-01-01')"} {
|
||||
if _, err := g.DB().Exec(ctx, sql); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
snapshot, err := Snapshot(ctx, "A", 1, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := g.DB().Exec(ctx, "UPDATE sys_dept SET dept_name='新名称' WHERE dept_id=2"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
frozen := Resolve(snapshot, 0, 0)
|
||||
legacy := Resolve(nil, 1, 2)
|
||||
foreign := Resolve(nil, 3, 4)
|
||||
if err := Fill(ctx, "A", []*Fields{&frozen, &legacy, &foreign}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if frozen.TargetDeptName != "接收单位" || legacy.TargetDeptName != "新名称" || foreign.SourceDeptName != "" || foreign.TargetDeptName != "" {
|
||||
t.Fatalf("快照/回退/隔离错误: %+v %+v %+v", frozen, legacy, foreign)
|
||||
}
|
||||
if f := Resolve(gjson.New(map[string]any{"sourceDeptName": "历史名称"}), 1, 0); f.TargetDeptMode != "per_row" {
|
||||
t.Fatal(f)
|
||||
}
|
||||
encoded, err := json.Marshal(frozen)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(encoded), "sourceDeptName") {
|
||||
t.Fatal("JSON契约缺少单位字段")
|
||||
}
|
||||
merged := Merge(gjson.New(map[string]any{"jobRole": "allocate_parent"}), snapshot)
|
||||
if merged.Get("jobRole").String() != "allocate_parent" || merged.Get("sourceDeptName").String() != "划拨单位" {
|
||||
t.Fatal(merged)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user