package budget import ( "errors" "fmt" "strings" "zhiyuan/pkg/db" "zhiyuan/pkg/utils" "github.com/gin-gonic/gin" ) type Item struct { ID int64 `json:"id" prop:"add:false"` TypeId int64 `json:"typeId" type:"int" prop:"add edit"` Name string `json:"name" label:"项目名称" type:"string" prop:"add edit" search:"like"` ParentIds string `json:"parentIds" label:"继承" type:"string" prop:"edit" default:""` PathIds string `json:"pathIds" type:"string" prop:"add:false"` Property string `json:"property" label:"属性" type:"string" prop:"edit"` 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 select:false"` UpdatedAt int64 `json:"updated_at" prop:"add:false select:false"` Parents []Item `json:"parents" prop:"ignore"` Prop map[string]string `json:"prop" prop:"ignore"` ParentProp map[string]string `json:"parentProp" prop:"ignore"` ParentNames string `json:"parentNames" prop:"ignore"` db.BaseModel } func (Item) TableName() string { return "zy_budget_item" } func (model Item) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { if typeid, ok := data["typeId"]; ok { typid, _ := db.ToInt64(typeid) ids := AllItemTypes([]int64{typid}) if len(ids) != 0 { s.Where = append(s.Where, fmt.Sprintf("`%s`.`typeId` in %s", model.TableName(), s.Param(ids))) } } 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`.`typeId` in %s", model.TableName(), s.Param(ids))) } } excludeIds := SilceIds(db.ToString(data["exclude_ids"])) if len(excludeIds) != 0 { s.Where = append(s.Where, fmt.Sprintf("`%s`.`id` not in %s", model.TableName(), s.Param(excludeIds))) } return true } func (model Item) ListAfter(c *gin.Context, data map[string]interface{}, list []map[string]interface{}) []map[string]interface{} { //ids := make([]int64, 0) pathIds := make([]int64, 0) for n, item := range list { //id, _ := db.ToInt64(item["id"]) //ids = append(ids, id) pathIds = append(pathIds, SilceIds(db.ToString(item["pathIds"]))...) prop := make(map[string]string) utils.JsonDecode(db.ToString(item["property"])).To(&prop) list[n]["prop"] = prop } var parents []Item db.GetModel(map[string]interface{}{ "id in": pathIds, "deleted_at": 0, }, &parents) for n, item := range list { list[n]["parents"], list[n]["parentProp"], list[n]["parentNames"] = findParentItems(db.ToString(item["parentIds"]), parents) } return list } func (Item) OnePrivilege(c *gin.Context, id int64) bool { return true } func (Item) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error { if pathIds, ok := ItemParentIdsToPathIds(db.ToString(data["parentIds"]), true); ok { data["pathIds"] = pathIds } else { return errors.New("没有权限") } return nil } func (Item) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error { if _, ok := data["parentIds"]; ok { if pathIds, ok := ItemParentIdsToPathIds(db.ToString(data["parentIds"]), true); ok { for _, pid := range SilceIds(pathIds) { if pid == id { return errors.New("没有权限") } } data["pathIds"] = pathIds } else { return errors.New("没有权限") } } return nil } func (Item) EditAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) { if _, ok := data["parentIds"]; ok { var items []Item db.GetModel(map[string]interface{}{ "pathIds find_in_set": id, "deleted_at": 0, }, &items) UpdateItemPathIds(id, items) } } func (Item) DelPrivilege(c *gin.Context, id int64) error { return nil } func (Item) OrderField() string { return "order_at" } func (Item) Page() bool { return false } func (Item) Count() bool { return true } func (model Item) GetPathIds() []int64 { return SilceIds(model.PathIds) } func (model Item) GetParentIds() []int64 { return SilceIds(model.ParentIds) } func (model Item) GetProperty() map[string]string { props := make(map[string]string) utils.JsonDecode(model.Property).To(&props) return props } func (model Item) GetID() int64 { return model.ID } func (model Item) GetProp(name string) (expression string, ok bool) { expression, ok = model.Prop[name] if !ok { expression, ok = model.ParentProp[name] } return } func (model Item) GetSubs() *[]QuoteData { return nil } func (model Item) GetName() string { return model.Name } func (model Item) GetHeaders() *[]QuoteData { return nil } func (model Item) GetItem() *Item { return nil } func (model Item) GetValue() string { return "" } func (model Item) GetQuote() *Quote { return nil } func (model Item) GetTable() *Table { return nil } func (model Item) GetHeader() *Header { return nil } func (model Item) GetModule() *Module { return nil } func (model Item) GetGroup() *Group { return nil } func (model Item) GetRow() *Row { return nil } func (model Item) GetMyItem() *Item { return &model } func SilceIds(sids string) []int64 { ids := make([]int64, 0) for _, v := range strings.Split(sids, ",") { if p, ok := db.ToInt64(v); ok && p != 0 { ids = append(ids, p) } } return ids } func ItemParentIdsToPathIds(parentIds string, isOk bool) (string, bool) { pids := SilceIds(parentIds) if len(pids) == 0 { return "", true } var pitems []Item db.GetModel(map[string]interface{}{ "id in": pids, "deleted_at": 0, }, &pitems) if len(pids) != len(pitems) && isOk { return "", false } pathids := make([]int64, 0) pathids = append(pathids, pids...) for _, v := range pitems { for _, p := range v.GetPathIds() { mid := int64(0) for i, id := range pathids { mid = id if p == id { break } if p < id { pathids = append(pathids[:i], append([]int64{p}, pathids[i:]...)...) break } } if p > mid { pathids = append(pathids, p) } } } pathidss := make([]string, 0) for _, id := range pathids { pathidss = append(pathidss, db.ToString(id)) } return strings.Join(pathidss, ","), true } func UpdateItemPathIds(id int64, items []Item) { for _, v := range items { isChild := false for _, pid := range SilceIds(v.ParentIds) { if pid == id { isChild = true break } } if isChild { pathIds, _ := ItemParentIdsToPathIds(v.ParentIds, false) db.UpdateModel(db.Type(items), v.ID, map[string]interface{}{ "pathIds": pathIds, }) UpdateItemPathIds(v.ID, items) } } } func findParentItems(parentIds string, list []Item) ([]Item, map[string]string, string) { items := make([]Item, 0) prop := make(map[string]string) names := make([]string, 0) for _, pid := range SilceIds(parentIds) { for _, v := range list { if v.ID == pid { v.Parents, v.ParentProp, v.ParentNames = findParentItems(v.ParentIds, list) v.Prop = v.GetProperty() items = append(items, v) for k, v := range v.ParentProp { prop[k] = v } for k, v := range v.Prop { prop[k] = v } names = append(names, v.Name) } } } return items, prop, strings.Join(names, ",") } func GetItemModels(where map[string]interface{}) (items []Item, datas []QuoteData) { db.GetModel(where, &items) pathIds := make([]int64, 0) for _, item := range items { pathIds = append(pathIds, SilceIds(item.PathIds)...) } var parents []Item db.GetModel(map[string]interface{}{ "id in": pathIds, "deleted_at": 0, }, &parents) for i, item := range items { items[i].Parents, items[i].ParentProp, items[i].ParentNames = findParentItems(item.ParentIds, parents) items[i].Prop = item.GetProperty() datas = append(datas, items[i]) } return }