item.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package calc
  2. import (
  3. "errors"
  4. "time"
  5. "zhiyuan/pkg/db"
  6. "github.com/gin-gonic/gin"
  7. )
  8. type Item struct {
  9. ID int64 `json:"id" prop:"add:false"`
  10. CalcId int64 `json:"calcId" type:"int" prop:"add" search:"="`
  11. Name string `json:"name" label:"项目名称" type:"string" prop:"add edit" search:"like"`
  12. TypeId int64 `json:"typeId" label:"产品类别" type:"int" prop:"edit" default:"0"`
  13. Min int64 `json:"min" label:"最小数量" type:"int" prop:"add edit" default:"0"`
  14. Max int64 `json:"max" label:"最大数量" type:"int" prop:"add edit" default:"0"`
  15. State int64 `json:"state" label:"状态" type:"int" prop:"edit" default:"0"`
  16. ParentId int64 `json:"parentId" type:"int" prop:"add" search:"=" default:"0"`
  17. OrderAt int64 `json:"order_at" prop:"add:false select:false"`
  18. DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
  19. CreatedAt int64 `json:"created_at" prop:"add:false select:false"`
  20. UpdatedAt int64 `json:"updated_at" prop:"add:false select:false"`
  21. Props []Property `json:"props" prop:"ignore"`
  22. Childs []Item `json:"childs" prop:"ignore"`
  23. db.BaseModel
  24. }
  25. func (Item) TableName() string {
  26. return "zy_calc_item"
  27. }
  28. func (Item) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  29. return true
  30. }
  31. func (Item) OnePrivilege(c *gin.Context, id int64) bool {
  32. return true
  33. }
  34. func (Item) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
  35. if data["parentId"] != nil && data["parentId"].(int64) != 0 {
  36. var p Item
  37. db.GetModel(map[string]interface{}{
  38. "id": data["parentId"].(int64),
  39. }, &p)
  40. if p.ID == 0 {
  41. return errors.New("没有权限")
  42. }
  43. }
  44. if data["min"].(int64) > data["max"].(int64) {
  45. return errors.New("没有权限")
  46. }
  47. return nil
  48. }
  49. func (Item) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error {
  50. return nil
  51. }
  52. func (Item) DelPrivilege(c *gin.Context, id int64) error {
  53. return nil
  54. }
  55. func (Item) OrderField() string {
  56. return "order_at"
  57. }
  58. func (Item) Page() bool {
  59. return false
  60. }
  61. func (Item) Count() bool {
  62. return false
  63. }
  64. func (Item) AddAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) {
  65. if use, ok := post["use"].(float64); ok && use != 0 {
  66. calc := GetCalculateModel(int64(use), true)
  67. if calc != nil {
  68. if calcId, ok := db.ToInt64(data["calcId"]); ok {
  69. order := time.Now().Unix()
  70. CopyPropertys(calc.Props, calcId, id, order)
  71. CopyItems(calc.Items, calcId, id, order)
  72. }
  73. }
  74. }
  75. }
  76. func findItems(itemId int64, list []Item, props []Property) []Item {
  77. items := make([]Item, 0)
  78. for _, v := range list {
  79. if v.ParentId == itemId {
  80. v.Props = findProps(v.ID, props)
  81. v.Childs = findItems(v.ID, list, props)
  82. items = append(items, v)
  83. }
  84. }
  85. return items
  86. }
  87. func GetItemModel(id int64, showAll bool) *Item {
  88. var item *Item
  89. db.GetModel(map[string]interface{}{"id": id}, &item)
  90. if item != nil {
  91. var items []Item
  92. var props []Property
  93. where := map[string]interface{}{"calcId": item.CalcId, "deleted_at": 0}
  94. if !showAll {
  95. where["state"] = 1
  96. }
  97. db.GetModel(where, &items)
  98. if !showAll {
  99. where["type !="] = 1
  100. }
  101. db.GetModel(where, &props)
  102. item.Props = findProps(item.ID, props)
  103. item.Childs = findItems(item.ID, items, props)
  104. }
  105. return item
  106. }
  107. func CopyItems(items []Item, calcId int64, parentId int64, order int64) error {
  108. for i, item := range items {
  109. _, err := item.Copy(item.Name, calcId, parentId, order+int64(len(items)-i))
  110. if err != nil {
  111. return err
  112. }
  113. }
  114. return nil
  115. }
  116. func (model Item) Copy(name string, calcId int64, parentId int64, order int64) (int64, error) {
  117. id, err := db.InsertModel(db.Type(model), map[string]interface{}{
  118. "calcId": calcId,
  119. "name": name,
  120. "typeId": model.TypeId,
  121. "min": model.Min,
  122. "max": model.Max,
  123. "state": model.State,
  124. "parentId": parentId,
  125. "order_at": order,
  126. })
  127. if err != nil {
  128. return 0, err
  129. }
  130. err = CopyPropertys(model.Props, calcId, id, order)
  131. if err != nil {
  132. return id, err
  133. }
  134. err = CopyItems(model.Childs, calcId, id, order)
  135. if err != nil {
  136. return id, err
  137. }
  138. return id, nil
  139. }