package gonganxitong import ( "path" "reflect" "strconv" "strings" "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" "shylinux.com/x/icebergs/base/web/html" 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"` market string `name:"market domain_uid* title* content" role:"leader"` } func (s Table) LoadTrans(m *ice.Message, arg ...string) { base := kit.FileLine(1, 100) if m.PrefixKey() != "web.team.gonganxitong.portal" { ice.LoadTrans(m.Spawn(kit.Dict("_template", path.Join(path.Dir(base), "portal.json"))).Message, m.CommandKey(), m.GetCommand().Command) } h := ctx.GetCmdFile(m.Message, m.PrefixKey()) kit.If(strings.HasPrefix(h, "/p/src/"), func() { h = strings.TrimPrefix(h, "/p/") }) if path.Dir(h) != path.Dir(base) && m.CommandKey() != web.PORTAL { ice.LoadTrans(m.Spawn(kit.Dict("_template", path.Join(path.Dir(h), "portal.json"))).Message, m.CommandKey(), m.GetCommand().Command) } } func (s Table) Init(m *ice.Message, arg ...string) { s.LoadTrans(m, arg...) s.Table.Init(m, arg...) } 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) Remove(m *ice.Message, arg ...string) { s.Table.Remove(m, arg...) } func (s Table) Modify(m *ice.Message, arg ...string) { s.Table.Modify(m, arg...) } func (s Table) ValueModel(m *ice.Message, action ice.Any, arg ...string) *ice.Message { defer m.Options(db.TARGET, m.CommandKey()).Set(ice.MSG_OPTION, db.TARGET) defer m.Options(db.MODEL, m.Configv(db.MODEL)).Set(ice.MSG_OPTION, db.MODEL) defer m.Options(db.DB, m.Configv(db.DB)).Set(ice.MSG_OPTION, db.DB) return m.Cmdy(m.Prefix(web.PORTAL), action, arg) } func (s Table) ValueCreate(m *ice.Message, arg ...string) { s.ValueModel(m, s.ValueCreate, arg...) s.RecordEventWithName(m, "") } func (s Table) ValueRemove(m *ice.Message, arg ...string) { s.ValueModel(m, s.ValueRemove, arg...) s.RecordEventWithName(m, "") } func (s Table) ValueModify(m *ice.Message, arg ...string) { s.ValueModel(m, s.ValueModify, arg...) s.RecordEventWithName(m, "") } func (s Table) ValueList(m *ice.Message, arg []string, fields ...ice.Any) *ice.Message { if len(arg) > 0 { kit.If(len(fields) == 0, func() { fields = kit.TransArgs(kit.Split(m.Config(mdb.FIELDS))) }) defer m.Options(db.FIELDS, fields).Set(ice.MSG_OPTION, db.FIELDS) } defer m.Options(db.TARGET, m.CommandKey()).Set(ice.MSG_OPTION, db.TARGET) s.ValueModel(m, s.ValueList, arg...).PushAction(s.Remove).Action(s.Create) if len(arg) > 0 { kit.If(m.Length() > 0, func() { s.UserPlaceInit(m) }) s.Button(m, "") } return 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, arg ...string) *ice.Message { kit.If(len(arg) == 0, func() { arg = append(arg, model.NAME, model.AVATAR) }) return s.SelectJoin(m, user{}, arg...) } 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 { defer m.Options(ice.MSG_USERROLE, aaa.VOID) return s.SelectJoin(m, s.FindSpaceCmd(m, web.TEAM_RENZHENGSHOUQUAN_AUTH), model.NAME, model.STATUS, model.DASHBOARD_UID) } 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.AUTH_STATUS: value = AuthStatus(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 ice.Any, fields ...ice.Any) *ice.Message { if len(arg) == 0 { s.Select(m, m.OptionSimple(s.Keys(place, model.UID))...) return m } 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.Options(db.DB, "", db.MODEL, "")) } 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) CheckRole(m *ice.Message, arg ...string) { role := UserPlaceRole(s.UserPlaceRole(m)) m.WarnNotRight(!kit.IsIn(role.String(), append(arg, UserPlaceCreator.String())...), role.String()) } func (s Table) UserPlaceRole(m *ice.Message) int { return kit.Int(m.Cmdx(s.Prefix(m, Portal{}), s.UserPlaceRole)) } 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) Market(m *ice.Message, arg ...string) { s.MarketInsert(m, arg...) } func (s Table) MarketInsert(m *ice.Message, arg ...string) { m.Cmdy(s.Prefix(m, Portal{}), s.MarketInsert, arg) } func (s Portal) MarketInsert(m *ice.Message, arg ...string) { m.Option(model.PLACE_UID, m.Option(s.Keys(s.Place, model.UID))) m.Option(ctx.ARGS, kit.Join([]string{m.Option(model.PLACE_UID), m.Option(model.UID)})) m.Cmdy(s.Prefix(m, market{}), s.Create, arg) } 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 { m.OptionDefault(model.UID, m.Result()) 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(s.Prefix(m, 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(s.Prefix(m, 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(s.Prefix(m, Portal{}), s.SendTemplate, m.PrefixKey(), from, to, title, arg) } func (s Portal) SendTemplate(m *ice.Message, arg ...string) { name := kit.Select("", arg, 4) if name == "" { name = kit.JoinWord(m.Option(model.CITY_NAME), m.Option(model.STREET_NAME), m.Option(s.Keys(s.Place, model.NAME))) } 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 := s.Link(m, place_uid, arg[0], uid) m.Cmd(s.FindSpaceCmd(m, user{}), s.SendTemplate, arg[1], arg[2], link, kit.JoinWord(m.Option(model.PORTAL_NAME), arg[3]), name, kit.Cut(uid, 6)) } func (s Table) Button(m *ice.Message, info string, arg ...ice.Any) *ice.Message { kit.If(!m.IsErr() && m.Length() == 0, func() { m.Option(ctx.STYLE, html.FORM) m.EchoInfoButton(info, arg...) }) return m } 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(`%s`, 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) ProcessPodCmd(m, msg *ice.Message, arg ...string) *ice.Message { m.ProcessPodCmd(msg.Append(web.SPACE), msg.Append(ctx.INDEX), kit.Split(msg.Append(ctx.ARGS))) m.RewriteAppend(func(value, key string, index int) string { kit.If(key == ctx.STYLE, func() { value = html.OUTPUT }) kit.If(key == web.SPACE, func() { value = m.Appendv("_space")[index] }) return value }) if msg.Append(web.SPACE) != "" { m.Optionv(ice.FIELD_PREFIX, ctx.ACTION, ctx.RUN, kit.Join(kit.Slice(kit.Split(msg.Append(ctx.INDEX), "."), 0, -1), ".")+".portal", ctx.ACTION, ctx.RUN, msg.Append(ctx.INDEX)) } m.Push("field.option", kit.Format(kit.Dict(arg))) m.Option("field.option", kit.Format(kit.Dict(arg))) return m } 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) FindSpaceCmd(m *ice.Message, cmd ice.Any) ice.Any { route := map[string]string{ "web.team.dashboard.portal": "20240903-operation", "web.team.dashboard.summary": "20240903-operation", "web.team.renzhengshouquan.portal": "20240724-community", web.TEAM_RENZHENGSHOUQUAN_AUTH: "20240724-community", web.TEAM_GONGANXITONG_USER: "20240724-community", web.CODE_MYSQL_CLIENT: "20240724-community", } cmds := ice.GetTypeKey(cmd) if space, ok := route[cmds]; ok && space != ice.Info.NodeName { m.Options(ice.MSG_USERROLE, aaa.TECH) return []string{web.SPACE, kit.Keys(ice.OPS, space), cmds} } return cmd } 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)) 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(func(m *icebergs.Message, arg ...string) { mdb.Config(m, db.DOMAIN, kit.PathName(1)) }, cmd.Actions[ice.CTX_INIT].Hand) } 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("notice", notice{Table: table}) cmd("domain", domain{Table: table}) cmd("member", member{Tables: Tables{Table: table}}) cmd("market", market{Table: table}) cmd("message", message{Table: table}) cmd("recent", recent{Table: table}) cmd("service", service{Table: table}) cmd("support", support{Table: table}) }