jingganjiaoyu bf9fb9afcd opt some
2024-08-05 02:09:58 +08:00

145 lines
5.4 KiB
Go

package gonganxitong
import (
"shylinux.com/x/ice"
kit "shylinux.com/x/toolkits"
"shylinux.com/x/community/src/gonganxitong/model"
)
type Apply struct {
Table
user user
event event
portal string `data:"true"`
domain string `data:"gonganxitong"`
create string `name:"create user_place_role*:select begin_time:select@date end_time:select@date" role:"void"`
list string `name:"list place_uid uid auto" help:"权限申请" icon:"https://img.icons8.com/officel/80/edit-property.png" role:"void"`
cancel string `name:"cancel" role:"void"`
submit string `name:"submit" role:"void"`
}
func (s Apply) Init(m *ice.Message, arg ...string) {
s.Table.Init(m, arg...)
place_uid, user_place_role := s.Keys(s.Place, model.UID), s.Keys(s.UserPlace, model.ROLE)
m.Design(s.Create, "", user_place_role+"*:select")
m.Design(s.List, "", place_uid+" uid auto")
}
func (s Apply) Inputs(m *ice.Message, arg ...string) {
s.Place.(interface{ Inputs(*ice.Message, ...string) }).Inputs(m, arg...)
}
func (s Apply) Create(m *ice.Message, arg ...string) {
arg = kit.TransArgKeys(arg, s.Keys(s.Place, model.UID), model.PLACE_UID)
arg = kit.TransArgKeys(arg, s.Keys(s.UserPlace, model.ROLE), model.ROLE)
arg = kit.TransArgValueTime(arg, model.BEGIN_TIME, model.END_TIME)
s.Table.Create(m, kit.Simple(model.PLACE_UID, m.Option(s.Keys(s.Place, model.UID)), arg, m.OptionSimple(model.USER_UID), model.TABLES, s.Keys(s.Place, model.UID), model.STATUS, ApplyCreate)...)
}
func (s Apply) List(m *ice.Message, arg ...string) {
s.Tables(m, s.user).FieldsWithCreatedAT(m, s, model.USER_AVATAR, model.USER_NAME,
s.AS(s.Key(s, model.ROLE), s.Keys(s.UserPlace, model.ROLE)), model.APPLY_STATUS,
model.BEGIN_TIME, model.END_TIME, model.PLACE_UID,
)
if len(arg) == 0 {
return
if m.IsTech() {
s.Select(m)
}
} else if len(arg) == 1 {
if s.Table.Select(m, model.USER_UID, m.Option(model.USER_UID), model.PLACE_UID, arg[0]); m.Length() == 0 {
m.EchoInfoButton(m.Trans("please create apply", "请创建权限申请"), s.Create)
return
}
defer m.Sort("apply_status,created_at", []string{
ApplyCreate.String(),
ApplySubmit.String(),
ApplyApproved.String(),
ApplyRejected.String(),
ApplyCancel.String(),
}, ice.STR_R)
} else if len(arg) == 2 {
s.Table.SelectDetail(m, model.USER_UID, m.Option(model.USER_UID), model.PLACE_UID, arg[0], s.Key(s, model.UID), arg[1])
if ApplyStatus(kit.Int(m.Append(model.APPLY_STATUS))) == ApplySubmit {
m.EchoQRCode(Portal{}.Link(m, arg[0], m.Prefix("order"), arg[1]))
}
}
m.RenameAppend(model.PLACE_UID, s.Keys(s.Place, model.UID))
s.SelectJoin(m, s.Place, model.NAME, model.TYPE)
m.Table(func(value ice.Maps) {
switch ApplyStatus(kit.Int(value[model.APPLY_STATUS])) {
case ApplyCreate:
m.PushButton(s.Cancel, s.Submit)
default:
m.PushButton()
}
})
s.UserPlace.RewriteAppend(m)
s.Display(m)
}
func (s Apply) Cancel(m *ice.Message, arg ...string) {
msg := s.status(m, ApplyCreate, ApplyCancel)
s.SendTemplate(m, "", msg.Append(model.USER_UID), m.Trans("role apply cancel", "权限申请 已取消"))
}
func (s Apply) Submit(m *ice.Message, arg ...string) {
msg := s.status(m, ApplyCreate, ApplySubmit)
if m.WarnNotValid(msg.IsErr()) {
return
}
defer m.ToastProcess()()
role := UserPlaceCreator
switch UserPlaceRole(kit.Int(msg.Append(model.ROLE))) {
case UserPlaceLandlord:
role = UserPlaceCreator
case UserPlaceTenant, UserPlaceAdmin:
role = UserPlaceLandlord
}
msg = m.Cmd(s.UserPlace, s.Select, m.OptionSimple(s.Keys(s.Place, model.UID)), model.ROLE, role)
if m.WarnNotFound(msg.Length() == 0, role.String()) {
return
}
m.Cmd(Order{}, s.Create, msg.AppendSimple(model.USER_UID), model.APPLY_UID, m.Option(model.UID), model.STATUS, OrderCreate)
admin := m.Cmd(s.user, s.Select, model.UID, msg.Append(model.USER_UID)).Append(model.NAME)
s.event.Record(m, kit.Format("🕑 %s"+m.Trans(" apply submit", "权限申请提交"), s.TransRole(m)), m.Option(model.UID))
s.SendTemplate(m, admin, m.Option(model.USER_UID), m.Trans("role apply submit", "权限申请 已提交"))
}
func (s Apply) Reject(m *ice.Message, arg ...string) {
msg := s.status(m, ApplySubmit, ApplyRejected)
s.SendTemplate(m, "", msg.Append(model.USER_UID), m.Trans("role apply rejected", "权限申请 已驳回"))
}
func (s Apply) Approve(m *ice.Message, arg ...string) {
msg := s.status(m, ApplySubmit, ApplyApproved)
m.Cmd(s.UserPlace, s.Create, msg.AppendSimple(model.USER_UID, model.ROLE), s.Keys(s.Place, model.UID), msg.Append(model.PLACE_UID))
s.SendTemplate(m, "", msg.Append(model.USER_UID), m.Trans("role apply approved", "权限申请 已通过"))
}
func init() { ice.TeamCtxCmd(Apply{Table: newTable()}) }
func (s Apply) status(m *ice.Message, from, todo ApplyStatus, arg ...string) *ice.Message {
msg := s.Table.Select(m.Spawn(), m.OptionSimple(model.UID)...)
if !m.WarnNotFound(msg.Length() == 0, m.Option(model.UID)) {
if status := ApplyStatus(kit.Int(msg.Append(model.STATUS))); !m.WarnNotValid(status != from, status.String()) {
s.Table.Update(m, kit.Dict(model.STATUS, todo), m.OptionSimple(model.UID)...)
}
}
return msg
}
type ApplyStatus int
const (
ApplyCreate ApplyStatus = iota
ApplyCancel
ApplySubmit
ApplyRejected
ApplyApproved
)
var ApplyStatusList = map[ApplyStatus]string{
ApplyCreate: "create",
ApplyCancel: "cancel",
ApplySubmit: "submit",
ApplyRejected: "rejected",
ApplyApproved: "approved",
}
func (s ApplyStatus) String() string { return ApplyStatusList[s] }