2024-09-02 17:34:18 +08:00

339 lines
12 KiB
Go

package gonganxitong
import (
"path"
"reflect"
"strconv"
"shylinux.com/x/ice"
icebergs "shylinux.com/x/icebergs"
"shylinux.com/x/icebergs/base/aaa"
"shylinux.com/x/icebergs/base/ctx"
"shylinux.com/x/icebergs/base/mdb"
"shylinux.com/x/icebergs/base/web"
kit "shylinux.com/x/toolkits"
"shylinux.com/x/community/src/gonganxitong/model"
"shylinux.com/x/mysql-story/src/db"
)
type UserPlacer interface {
Placer
}
type Placer interface {
Inputs(m *ice.Message, arg ...string)
Select(m *ice.Message, arg ...string) *ice.Message
RewriteAppend(m *ice.Message, arg ...string) *ice.Message
TransValue(m *ice.Message, key string, arg ...string) string
TransRole(m *ice.Message, arg ...string) string
}
type Container interface {
FindOrCreateByName(m *ice.Message, arg ...string)
}
type Table struct {
db.Table
UserPlace UserPlacer
Place Placer
Street Container
checkRole string `name:"checkRole role"`
inputs string `name:"inputs" role:"void"`
list string `name:"list place_uid uid auto" role:"void"`
}
func (s Table) BeforeMigrate(m *ice.Message, arg ...string) {
s.Table.BeforeMigrate(m, arg...)
kit.If(m.GetCommand().Icon, func() { Portal{}.Show(m) })
}
func (s Table) AfterMigrate(m *ice.Message, arg ...string) {
kit.If(m.GetCommand().Icon, func() { Portal{}.Show(m) })
}
func (s Table) Inputs(m *ice.Message, arg ...string) {
switch arg[0] {
case model.USER_PLACE_ROLE:
s.InputsListRole(m, UserPlaceRoleList, arg...)
case model.PLACE_TYPE:
s.InputsList(m, PlaceTypeList, arg...)
default:
s.Table.Inputs(m, arg...)
}
}
func (s Table) InputsList(m *ice.Message, list ice.Any, arg ...string) {
it := reflect.ValueOf(list).MapRange()
for it.Next() {
m.Push(arg[0], it.Key().Int()).Push(model.NAME, it.Value().String())
}
m.SortInt(arg[0]).DisplayInputKeyNameIconTitle()
}
func (s Table) InputsListValue(m *ice.Message, list ice.Any, key string) string {
k, e := strconv.ParseInt(key, 10, 64)
if e != nil {
return key
}
it := reflect.ValueOf(list).MapRange()
for it.Next() {
if it.Key().Int() == k {
return it.Value().String()
}
}
return ""
}
func (s Table) InputsListRole(m *ice.Message, list ice.Any, arg ...string) {
it := reflect.ValueOf(list).MapRange()
for it.Next() {
if it.Key().Int() > 1 {
m.Push(arg[0], it.Key().Int()).Push(model.NAME, it.Value().String())
}
}
m.SortInt(arg[0]).DisplayInputKeyNameIconTitle()
}
func (s Table) Create(m *ice.Message, arg ...string) {
s.Table.Create(m, arg...)
s.UserPlaceInit(m)
}
func (s Table) List(m *ice.Message, arg ...string) *ice.Message {
if m.IsTech() {
if len(arg) == 0 {
s.Select(m)
} else if len(arg) == 2 {
s.SelectDetail(m, model.UID, arg[1])
}
}
return m
}
func (s Table) Select(m *ice.Message, arg ...string) *ice.Message {
s.Table.Select(m, arg...)
kit.If(m.Length() > 0, func() { s.UserPlaceInit(m) })
return m
}
func (s Table) SelectDetail(m *ice.Message, arg ...string) *ice.Message {
return s.Table.SelectDetail(m, arg...)
}
func (s Table) SelectJoinUser(m *ice.Message) *ice.Message {
return s.SelectJoin(m, user{}, model.NAME, model.AVATAR)
}
func (s Table) SelectJoinCity(m *ice.Message) *ice.Message {
return s.SelectJoin(m, city{}, model.NAME)
}
func (s Table) SelectJoinAuth(m *ice.Message) *ice.Message {
if ice.Info.NodeName == "20240724-community" {
return s.SelectJoin(m, "web.team.renzhengshouquan.auth", model.STATUS)
} else {
defer m.Options(ice.MSG_USERROLE, aaa.TECH).Options(ice.MSG_USERROLE, aaa.VOID)
return s.SelectJoin(m, []string{web.SPACE, "ops.20240724-community", "web.team.renzhengshouquan.auth"}, model.STATUS)
}
}
func (s Table) RenameAppend(m *ice.Message, arg ...string) Table {
m.RenameAppend(arg...)
return s
}
func (s Table) RewriteAppend(m *ice.Message, arg ...string) *ice.Message {
m.RewriteAppend(func(value, key string, index int) string {
if _, e := strconv.ParseInt(value, 10, 64); e != nil {
return value
}
switch key {
case model.USER_PLACE_ROLE:
value = UserPlaceRole(kit.Int(value)).String()
case model.PLACE_TYPE:
value = PlaceType(kit.Int(value)).String()
case model.APPLY_STATUS:
value = ApplyStatus(kit.Int(value)).String()
case model.ALLOW_STATUS:
value = AllowStatus(kit.Int(value)).String()
case model.MESSAGE_STATUS:
value = MessageStatus(kit.Int(value)).String()
}
return value
})
return m
}
func (s Table) TablesWithRole(m *ice.Message, arg []string, userPlace UserPlacer, place Placer, target Placer, fields ...ice.Any) *ice.Message {
s.Tables(m, kit.JoinWord("INNER JOIN", s.TableName(kit.TypeName(userPlace)),
"ON", s.Key(target, model.USER_UID), "=", s.Key(userPlace, model.USER_UID),
"AND", s.Key(target, s.Keys(place, model.UID)), "=", s.Key(userPlace, s.Keys(place, model.UID)),
"AND", s.Key(userPlace, model.DELETED_AT), "IS NULL",
)).FieldsWithCreatedAT(m, target, append([]ice.Any{s.Key(target, model.USER_UID), s.AS(s.Key(userPlace, model.ROLE), s.Keys(userPlace, model.ROLE))}, fields...)...)
if len(arg) == 1 {
s.Table.Select(m, s.Key(target, s.Keys(place, model.UID)), arg[0])
} else if len(arg) == 2 {
s.Table.SelectDetail(m, s.Key(target, s.Keys(place, model.UID)), arg[0], s.Key(target, model.UID), arg[1])
} else {
return m
}
return s.SelectJoinUser(m)
}
func (s Table) Update(m *ice.Message, data ice.Map, arg ...string) {
data[model.OPERATOR] = m.Option(model.USER_UID)
s.Table.Update(m, data, arg...)
}
func (s Table) UpdateAuth(m *ice.Message, arg ...string) {
s.Update(m, kit.Dict(model.AUTH_UID, arg[0]), arg[1:]...)
}
func (s Table) ChangeStatus(m *ice.Message, uid string, from, to int, arg ...string) *ice.Message {
msg := s.Select(m.Spawn(), model.UID, uid)
if !m.WarnNotValid(kit.Int(msg.Append(mdb.STATUS)) != int(from)) {
s.Update(m, kit.Dict(mdb.STATUS, to, arg), model.UID, uid)
}
return m
}
func (s Table) UserPlaceRole(m *ice.Message) int {
return kit.Int(m.Cmdx(s.Prefix(m, Portal{}), "userPlaceRole"))
}
func (s Table) CheckRole(m *ice.Message, arg ...string) {
m.ErrorNotImplement()
}
func (s Table) UserPlaceInit(m *ice.Message, arg ...string) {
if m.Option(model.PLACE_INIT) == "" {
return
}
m.Cmd(s.Prefix(m, Portal{}), "placeCmd", "addCount", model.INIT, 1, m.Option(model.PLACE_UID), m.Option(model.PLACE_INIT))
}
func (s Table) RecordEvent(m *ice.Message, info string, arg ...string) {
m.Cmd(s.Prefix(m, Portal{}), s.RecordEvent, info, arg)
}
func (s Table) RecordEventWithName(m *ice.Message, info string, arg ...string) {
uid := kit.Select(m.Result(), m.Option(model.UID))
kit.If(info == "", func() {
switch info = m.ActionCmdTitle(); m.ActionKey() {
case "placeCreate", mdb.CREATE:
info = "✅ " + info
case "placeRemove", mdb.REMOVE:
info = "❌ " + info
case "placeAuth":
info = "🕑 " + info
}
})
s.RecordEvent(m, kit.JoinWord(info, kit.Cut(uid, 6), kit.Select(m.Option(model.TITLE), m.Option(model.NAME))), uid)
}
func (s Table) GetCommands(m *ice.Message, key string, arg ...string) []string {
kit.If(len(arg) == 0, func() { arg = append(arg, m.Option(s.Keys(s.Place, model.UID)), m.Option(model.UID)) })
return kit.Simple(m.OptionSimple(key), ctx.ARGS, kit.Join(arg), m.OptionSimple(model.CITY_NAME, model.STREET_NAME, model.PLACE_NAME))
}
func (s Table) SendMessage(m *ice.Message, from_user_uid, to_user_uid string, arg ...string) {
m.Spawn(ice.Maps{db.DB: ""}).Cmd(message{}, s.Create, model.FROM_USER_UID, from_user_uid, model.TO_USER_UID, to_user_uid, s.GetCommands(m, model.COMMAND_UID, arg...))
}
func (s Table) DoneMessage(m *ice.Message, arg ...string) {
if m.Option(model.MESSAGE_UID) != "" {
m.Spawn(ice.Maps{db.DB: ""}).Cmd(message{}, message{}.Done, kit.Dict(model.UID, m.Option(model.MESSAGE_UID)))
}
}
func (s Table) SendTemplate(m *ice.Message, from, to, title string, arg ...string) {
if m.IsErr() {
return
}
m.Cmd(Portal{}, m.PrefixKey(), from, to, title, arg)
}
func (s Portal) SendTemplate(m *ice.Message, arg ...string) {
name := kit.Select("", arg, 4)
if name == "" {
name = m.Option(s.Keys(s.Place, model.NAME)) + " " + s.Place.TransValue(m, s.Keys(s.UserPlace, model.ROLE))
}
uid, place_uid := kit.Select(m.Option(model.UID), arg, 5), kit.Select("", arg, 6)
kit.If(place_uid == "", func() { place_uid = m.Option(s.Keys(s.Place, model.UID)) })
link := Portal{}.Link(m, place_uid, arg[0], uid)
m.Cmd(user{}, s.SendTemplate, arg[1], arg[2], link, arg[3], name, kit.Cut(uid, 6))
}
func (s Table) DisplayBase(m *ice.Message, file string) *ice.Message {
if s.Place != nil {
m.Option("_place_uid", s.ToLower(kit.TypeName(s.Place))+"_uid")
m.Option("_place_name", s.ToLower(kit.TypeName(s.Place))+"_name")
m.Option("_place_type", s.ToLower(kit.TypeName(s.Place))+"_type")
m.Option("_user_place_role", s.ToLower(kit.TypeName(s.UserPlace))+"_role")
}
if s.Street != nil {
m.Option("_street_name", s.ToLower(kit.TypeName(s.Street))+"_name")
}
m.Display(m.Resource(path.Join(path.Dir(kit.FileLine(1, 100)), kit.Select(m.CommandKey()+".js", file))))
return m
}
func (s Table) Display(m *ice.Message, file string) *ice.Message {
s.DisplayBase(m, file)
base, sub := path.Dir(kit.FileLine(1, 100)), path.Dir(kit.FileLine(-1, 100))
return m.Display(kit.Format(mdb.Cache(m.Message, kit.Keys(m.PrefixKey(), ctx.DISPLAY, file), func() ice.Any {
if file == "" {
file = m.Resource(path.Join(sub, m.CommandKey()+".js"))
}
if m.Template(file) == "" {
if m.CommandKey() == web.PORTAL {
file = m.Resource(path.Join(base, "portal.js"))
} else {
file = m.Resource(path.Join(base, "common.js"))
}
}
return file
})))
}
func (s Table) TransValue(m *ice.Message, key string, arg ...string) string {
if value := kit.Select(m.Option(key), arg, 0); m.IsEnglish() {
return value
} else {
return kit.Select(value, kit.Value(m.Target().Commands[m.CommandKey()].Meta, "_trans.value."+key+"."+value))
}
}
func (s Table) TransRole(m *ice.Message, arg ...string) string {
value := kit.Select(m.Option(s.Keys(s.UserPlace, model.ROLE)), arg, 0)
role := s.Place.TransValue(m, s.Keys(s.UserPlace, model.ROLE), arg...)
return kit.Format(`<span class="role %s">%s</span>`, value, role)
}
func (s Table) Prefix(m *ice.Message, target ice.Any) string {
return m.Prefix(kit.TypeName(target))
}
func (s Table) PrefixPortal(m *ice.Message) string {
return m.Prefix(kit.TypeName(Portal{}))
}
func (s Table) SpaceCmd(m *ice.Message, space string, arg ...ice.Any) *ice.Message {
if space == ice.Info.NodeName {
return m.Cmd(arg...)
} else {
return m.Cmd(append([]ice.Any{web.SPACE, kit.Keys(ice.OPS, space)}, arg...)...)
}
}
func (s Table) Link(m *ice.Message, arg ...string) string {
return Portal{}.Link(m, arg...)
}
type Tables struct{ Table }
func (s Tables) BeforeMigrate(m *ice.Message, arg ...string) {}
func newTable() Table { return Table{UserPlace: userPlace{}, Place: place{}, Street: street{}} }
func newTables() Tables { return Tables{Table: newTable()} }
func NewTable(userPlace UserPlacer, place Placer, street Container) Table {
return Table{UserPlace: userPlace, Place: place, Street: street}
}
func NewTables(userPlace UserPlacer, place Placer, street Container) Tables {
return Tables{Table: NewTable(userPlace, place, street)}
}
func NewPortal(userPlace UserPlacer, place Placer, street Container) Portal {
return Portal{Table: Table{UserPlace: userPlace, Place: place, Street: street}}
}
func (s Portal) getTable() Table { return s.Table }
func PortalCmd(portal ice.Any) {
h := kit.FileLine(-1, 100)
p := kit.Keys("web.team", kit.ModPath(-1))
base := kit.FileLine(1, 100)
cmd := func(name string, data ice.Any) {
_, cmd := ice.Cmd(kit.Keys(p, name), data)
cmd.RawHand = path.Join(path.Dir(h), name+".go")
cmd.Actions[ice.CTX_INIT].Hand = icebergs.MergeHand(cmd.Actions[ice.CTX_INIT].Hand, func(m *icebergs.Message, arg ...string) {
ice.LoadTrans(m.Spawn(kit.Dict("_template", path.Join(path.Dir(base), "portal.json"))), m.CommandKey(), cmd)
ice.LoadTrans(m, m.CommandKey(), cmd)
mdb.Config(m, db.DOMAIN, "gonganxitong")
})
}
table := portal.(interface{ getTable() Table }).getTable()
cmd("portal", portal)
cmd("search", search{Tables: Tables{Table: table}})
cmd("qrcode", qrcode{Tables: Tables{Table: table}})
cmd("event", event{Table: table})
cmd("apply", apply{Table: table})
cmd("allow", allow{Table: table})
cmd("member", member{Tables: Tables{Table: table}})
cmd("message", message{})
cmd("recent", recent{})
cmd("service", service{Table: table})
cmd("support", support{})
}