123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- 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
- }
|