item_type.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package budget
  2. import (
  3. "errors"
  4. "fmt"
  5. "zhiyuan/pkg/db"
  6. "github.com/gin-gonic/gin"
  7. )
  8. type ItemType struct {
  9. ID int64 `json:"id" prop:"add:false"`
  10. Name string `json:"name" label:"类别名称" type:"string" prop:"add edit" search:"like"`
  11. Pid int64 `json:"pid" type:"int" prop:"add" search:"="`
  12. Path string `json:"path" type:"string" prop:"add:false"`
  13. Header string `json:"header" type:"string" prop:"edit" default:""`
  14. OrderAt int64 `json:"order_at" prop:"add:false select:false"`
  15. DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
  16. CreatedAt int64 `json:"created_at" prop:"add:false"`
  17. UpdatedAt int64 `json:"updated_at" prop:"add:false"`
  18. db.BaseModel
  19. }
  20. func (ItemType) TableName() string {
  21. return "zy_budget_item_type"
  22. }
  23. func (model ItemType) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  24. typeIds := SilceIds(db.ToString(data["typeIds"]))
  25. if len(typeIds) > 0 {
  26. ids := AllItemTypes(typeIds)
  27. if len(ids) != 0 {
  28. s.Where = append(s.Where, fmt.Sprintf("`%s`.`id` in %s", model.TableName(), s.Param(ids)))
  29. }
  30. }
  31. return true
  32. }
  33. func (ItemType) OnePrivilege(c *gin.Context, id int64) bool {
  34. return true
  35. }
  36. func (ItemType) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
  37. pid, _ := db.ToInt64(data["pid"])
  38. if pid != 0 {
  39. var ptyp ItemType
  40. db.GetModel(map[string]interface{}{
  41. "id": pid,
  42. "deleted_at": 0,
  43. }, &ptyp)
  44. if ptyp.ID == 0 {
  45. return errors.New("没有权限")
  46. }
  47. data["path"] = ptyp.Path + db.ToString(ptyp.ID) + ","
  48. }
  49. return nil
  50. }
  51. func (ItemType) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error {
  52. return nil
  53. }
  54. func (ItemType) DelPrivilege(c *gin.Context, id int64) error {
  55. return nil
  56. }
  57. func (ItemType) OrderField() string {
  58. return "order_at"
  59. }
  60. func (ItemType) Page() bool {
  61. return false
  62. }
  63. func (ItemType) Count() bool {
  64. return true
  65. }
  66. func AllItemTypes(typeIds []int64) []int64 {
  67. ids := make([]int64, 0)
  68. ids = append(ids, typeIds...)
  69. if len(ids) == 0 {
  70. return ids
  71. }
  72. var types []ItemType
  73. db.GetModel(map[string]interface{}{
  74. "id in": ids,
  75. "deleted_at": 0,
  76. }, &types)
  77. if len(types) == 0 {
  78. return ids
  79. }
  80. paths := ""
  81. for i, typ := range types {
  82. if i != 0 {
  83. paths += " AND "
  84. }
  85. paths += "`path` like \"" + typ.Path + db.ToString(typ.ID) + ",%\""
  86. }
  87. ctypes := make([]ItemType, 0)
  88. db.GetModel(map[string]interface{}{
  89. "": paths,
  90. "deleted_at": 0,
  91. }, &ctypes)
  92. for _, v := range ctypes {
  93. ids = append(ids, v.ID)
  94. }
  95. return ids
  96. }