package budget import ( "errors" "fmt" "zhiyuan/pkg/db" "github.com/gin-gonic/gin" ) type ItemType struct { ID int64 `json:"id" prop:"add:false"` Name string `json:"name" label:"类别名称" type:"string" prop:"add edit" search:"like"` Pid int64 `json:"pid" type:"int" prop:"add" search:"="` Path string `json:"path" type:"string" prop:"add:false"` Header string `json:"header" type:"string" prop:"edit" default:""` OrderAt int64 `json:"order_at" prop:"add:false select:false"` DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"` CreatedAt int64 `json:"created_at" prop:"add:false"` UpdatedAt int64 `json:"updated_at" prop:"add:false"` db.BaseModel } func (ItemType) TableName() string { return "zy_budget_item_type" } func (model ItemType) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { typeIds := SilceIds(db.ToString(data["typeIds"])) if len(typeIds) > 0 { ids := AllItemTypes(typeIds) if len(ids) != 0 { s.Where = append(s.Where, fmt.Sprintf("`%s`.`id` in %s", model.TableName(), s.Param(ids))) } } return true } func (ItemType) OnePrivilege(c *gin.Context, id int64) bool { return true } func (ItemType) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error { pid, _ := db.ToInt64(data["pid"]) if pid != 0 { var ptyp ItemType db.GetModel(map[string]interface{}{ "id": pid, "deleted_at": 0, }, &ptyp) if ptyp.ID == 0 { return errors.New("没有权限") } data["path"] = ptyp.Path + db.ToString(ptyp.ID) + "," } return nil } func (ItemType) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error { return nil } func (ItemType) DelPrivilege(c *gin.Context, id int64) error { return nil } func (ItemType) OrderField() string { return "order_at" } func (ItemType) Page() bool { return false } func (ItemType) Count() bool { return true } func AllItemTypes(typeIds []int64) []int64 { ids := make([]int64, 0) ids = append(ids, typeIds...) if len(ids) == 0 { return ids } var types []ItemType db.GetModel(map[string]interface{}{ "id in": ids, "deleted_at": 0, }, &types) if len(types) == 0 { return ids } paths := "" for i, typ := range types { if i != 0 { paths += " AND " } paths += "`path` like \"" + typ.Path + db.ToString(typ.ID) + ",%\"" } ctypes := make([]ItemType, 0) db.GetModel(map[string]interface{}{ "": paths, "deleted_at": 0, }, &ctypes) for _, v := range ctypes { ids = append(ids, v.ID) } return ids }