Files
iotlcm/server/internal/library/allocationunits/units.go
T

142 lines
3.9 KiB
Go

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)
}