package gonganxitong import ( "shylinux.com/x/ice" kit "shylinux.com/x/toolkits" "shylinux.com/x/community/src/gonganxitong/model" ) type apply struct { Table order string `data:"502"` role string `data:"leader"` create string `name:"create user_place_role*:select begin_time:select@date end_time:select@date" role:"void"` cancel string `name:"cancel" role:"void"` submit string `name:"submit" role:"void"` } func (s apply) Init(m *ice.Message, arg ...string) { USER_PLACE_ROLE := s.Keys(s.UserPlace, model.ROLE) m.Design(s.Create, "", kit.JoinWord(USER_PLACE_ROLE+"*:select", "begin_time:select@date", "end_time:select@date")) s.Table.Init(m, arg...) } func (s apply) Inputs(m *ice.Message, arg ...string) { s.UserPlace.Inputs(m, arg...) } func (s apply) Create(m *ice.Message, arg ...string) { PLACE_UID, USER_PLACE_ROLE := s.Keys(s.Place, model.UID), s.Keys(s.UserPlace, model.ROLE) arg = kit.TransArgKeys(arg, PLACE_UID, model.PLACE_UID, USER_PLACE_ROLE, model.ROLE) s.Insert(m, kit.Simple(arg, model.PLACE_UID, m.Option(PLACE_UID), m.OptionSimple(model.USER_UID))...) m.ProcessRewrite(model.UID, m.Result()) } func (s apply) List(m *ice.Message, arg ...string) { s.FieldsWithCreatedAT(m, s, model.USER_UID, model.APPLY_STATUS, s.AS(s.Key(s, model.ROLE), s.Keys(s.UserPlace, model.ROLE)), model.BEGIN_TIME, model.END_TIME, ) if len(arg) == 1 { s.Select(m, model.PLACE_UID, arg[0], model.USER_UID, m.Option(model.USER_UID)) } else if len(arg) == 2 { s.SelectDetail(m, model.PLACE_UID, arg[0], model.USER_UID, m.Option(model.USER_UID), model.UID, arg[1]) switch ApplyStatus(kit.Int(m.Append(model.APPLY_STATUS))) { case ApplyCreate: m.PushButton(s.Cancel, s.Submit) case ApplySubmit: m.EchoQRCode(s.Link(m, arg[0], s.Prefix(m, allow{}), arg[1])).Echo("请让管理员扫码或等待审批") defer func() { m.Echo("", m.Resource(m.Append(model.USER_AVATAR))) }() case ApplyRejected, ApplyApproved: s.DoneMessage(m) } } else { return } s.SelectJoinUser(m) s.DisplayBase(m, "") } func (s apply) Cancel(m *ice.Message, arg ...string) { s.changeStatus(m, ApplyCreate, ApplyCancel) } func (s apply) Submit(m *ice.Message, arg ...string) { msg := s.changeStatus(m, ApplyCreate, ApplySubmit) if m.WarnNotValid(msg.IsErr()) { return } role, style := UserPlaceCreator, "" switch UserPlaceRole(kit.Int(msg.Append(model.ROLE))) { case UserPlaceLandlord: role, style = UserPlaceCreator, "danger" default: role, style = UserPlaceLandlord, "notice" } 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.Option(model.FROM_USER_UID, m.Option(model.USER_UID)) m.Cmd(s.Prefix(m, allow{}), s.Create, model.APPLY_UID, m.Option(model.UID), model.PLACE_UID, m.Option(s.Keys(s.Place, model.UID)), msg.AppendSimple(model.USER_UID), model.STATUS, AllowCreate) s.RecordEvent(m, kit.JoinWord("🕑", m.Trans("apply submit", "权限申请 已提交"), kit.Cut(m.Option(model.UID), 6), s.TransRole(m, "", style), m.Option(model.USER_NAME)), m.Option(model.UID)) s.SendMessage(m, msg.Append(model.USER_UID), m.Option(model.USER_UID)) } func (s apply) Reject(m *ice.Message, arg ...string) { msg := s.changeStatus(m, ApplySubmit, ApplyRejected) s.SendTemplate(msg, "", "", m.Trans("apply rejected", "权限申请 已驳回")) m.Option(model.ROLE, msg.Append(model.ROLE)) } func (s apply) Approve(m *ice.Message, arg ...string) { msg := s.changeStatus(m, ApplySubmit, ApplyApproved) if msg.IsErr() { return } m.Cmd(s.UserPlace, s.Insert, s.Keys(s.Place, model.UID), msg.Append(model.PLACE_UID), msg.AppendSimple(model.USER_UID, model.ROLE)) s.SendTemplate(msg, "", "", m.Trans("apply approved", "权限申请 已通过")) m.Option(model.ROLE, msg.Append(model.ROLE)) } func init() { ice.TeamCtxCmd(apply{Table: newTable()}) } func (s apply) changeStatus(m *ice.Message, from, to ApplyStatus, arg ...string) *ice.Message { msg := s.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.Update(m, kit.Dict(model.STATUS, to), 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] }