2024-08-24 21:58:43 +08:00

275 lines
10 KiB
Go

package gonganxitong
import (
"path"
"reflect"
"shylinux.com/x/ice"
icebergs "shylinux.com/x/icebergs"
"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
User(m *ice.Message, arg ...string)
}
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) Init(m *ice.Message, arg ...string) {
s.Table.Init(m, arg...).GoSleep("3s", func() {
kit.If(m.GetCommand().Icon, func() { Portal{}.Show(m) })
})
}
func (s Table) Exit(m *ice.Message, arg ...string) {
s.Table.Exit(m, arg...)
}
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...)
case ctx.INDEX:
mdb.HashInputs(m.Message, 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) 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) List(m *ice.Message, arg ...string) {
if m.IsTech() {
if len(arg) == 0 {
s.Select(m)
} else if len(arg) == 2 {
s.SelectDetail(m, model.UID, arg[1])
}
}
}
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) Select(m *ice.Message, arg ...string) *ice.Message {
defer s.Display(m, "")
return s.Table.Select(m, arg...)
}
func (s Table) SelectDetail(m *ice.Message, arg ...string) *ice.Message {
defer s.Display(m, "")
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) 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 {
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 = OrderStatus(kit.Int(value)).String()
case model.MESSAGE_STATUS:
value = MessageStatus(kit.Int(value)).String()
}
return value
})
return m
}
func (s Table) CheckRole(m *ice.Message, arg ...string) {
m.ErrorNotImplement()
}
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) 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) 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) RecordEvent(m *ice.Message, place_uid, info string, arg ...string) {
event{}.Record(m.Spawn(kit.Dict(model.PLACE_UID, place_uid)), info, arg...)
}
func (s Table) RecordEventWithName(m *ice.Message, place_uid, info string, arg ...string) {
uid := kit.Select(m.Result(), m.Option(model.UID))
kit.If(info == "", func() { info = m.ActionCmdTitle() })
s.RecordEvent(m, place_uid, kit.JoinWord(info, kit.Cut(uid, 6), m.Option(model.NAME)), uid)
}
func (s Table) AddRecent(m *ice.Message, arg ...string) {
cmd := m.GetCommand()
m.Cmd(recent{}, s.Create, mdb.NAME, cmd.Help+" "+ice.Info.Title(), mdb.ICON, m.Resource(kit.Select(ice.Info.NodeIcon, cmd.Icon)), web.SPACE, m.Option(ice.MSG_USERPOD), ctx.INDEX, m.PrefixKey(), ctx.ARGS, kit.Join(arg))
}
func (s Table) SendMessage(m *ice.Message, from_user_uid, to_user_uid string, arg ...string) {
cmd := m.GetCommand()
kit.If(len(arg) == 0, func() { arg = append(arg, m.Option(s.Keys(s.Place, model.UID)), m.Option(model.UID)) })
m.Spawn(ice.Maps{db.DB: ""}).Cmd(message{}, s.Create, model.FROM_USER_UID, from_user_uid, model.TO_USER_UID, to_user_uid, model.PLACE_NAME, m.Option("_place_name"),
mdb.NAME, cmd.Help, mdb.ICON, cmd.Icon, web.SPACE, m.Option(ice.MSG_USERPOD), ctx.INDEX, m.PrefixKey(), ctx.ARGS, kit.Join(arg))
}
func (s Table) DoneMessage(m *ice.Message, arg ...string) {
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, user_uid, title string, arg ...string) {
if m.IsErr() {
return
}
name := kit.Select("", arg, 0)
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, 1), kit.Select("", arg, 2)
kit.If(place_uid == "", func() { place_uid = m.Option(s.Keys(s.Place, model.UID)) })
link := Portal{}.Link(m, place_uid, m.PrefixKey(), uid)
m.Cmd(user{}, s.SendTemplate, from, user_uid, link, title, name, kit.Cut(uid, 6))
}
func (s Table) Display(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")
}
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) 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("qrcode", qrcode{Tables: Tables{Table: table}})
cmd("event", event{Table: table})
cmd("apply", apply{Table: table})
cmd("allow", allow{Table: table})
cmd("member", placeUser{Tables: Tables{Table: table}})
cmd("message", Message{})
cmd("recent", recent{})
cmd("service", service{Table: table})
cmd("support", support{})
}