dispatch_site_collect.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package dispatch
  2. import (
  3. "errors"
  4. "fmt"
  5. "zhiyuan/pkg/db"
  6. "zhiyuan/services/admin"
  7. "github.com/gin-gonic/gin"
  8. )
  9. type DispatchSiteCollect struct {
  10. ID int64 `json:"id" prop:"add:false"`
  11. ShopID int64 `json:"shop_id" label:"门店" type:"int" prop:"add:false" default:"0" search:"="`
  12. Total float64 `json:"total" label:"合计金额" type:"float" prop:"add:false" search:"="`
  13. State int64 `json:"state" label:"状态" type:"int" prop:"add:false" default:"0"`
  14. CreatedId int64 `json:"created_id" label:"创建人员" type:"int" prop:"add:false" search:"="`
  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 (DispatchSiteCollect) TableName() string {
  21. return "zy_dispatch_site_collect"
  22. }
  23. func (DispatchSiteCollect) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
  24. signature := db.ToString(post["signature"])
  25. if signature == "" {
  26. return errors.New("没有权限")
  27. }
  28. type DispatchSiteTableCollects struct {
  29. ShopId int64 `json:"shop_id" label:"门店" type:"int" prop:"select:dispatchsite.shop_id" search:"="`
  30. Total float64 `json:"total" label:"合计" type:"float" prop:"select:ROUND(sum(dispatchsiteitem.price*num))"`
  31. DispatchSiteTableCollect
  32. }
  33. if tableIds, ok := db.ToArray(post["table_ids"]); ok {
  34. models := make([]DispatchSiteTableCollects, 0)
  35. db.GetModel(map[string]interface{}{
  36. "id in": tableIds,
  37. }, &models)
  38. if len(models) == 0 {
  39. return errors.New("没有权限")
  40. }
  41. shopId := models[0].ShopId
  42. total := float64(0)
  43. for _, v := range models {
  44. if v.ShopId != shopId {
  45. return errors.New("没有权限")
  46. }
  47. total += v.Total
  48. }
  49. data["shop_id"] = shopId
  50. data["total"] = total
  51. data["state"] = 3
  52. data["created_id"] = c.GetInt("adminID")
  53. return nil
  54. }
  55. return errors.New("没有权限")
  56. }
  57. func (DispatchSiteCollect) AddAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) {
  58. signature := db.ToString(post["signature"])
  59. explain := db.ToString(post["explain"])
  60. pictures := db.ToString(post["pictures"])
  61. db.InsertModel(db.Type(DispatchSiteProcess{}), map[string]interface{}{
  62. "collect_id": id,
  63. "explain": explain,
  64. "pictures": pictures,
  65. "type": 2,
  66. "signature": signature,
  67. "state": 0,
  68. "created_id": c.GetInt("adminID"),
  69. })
  70. if tableIds, ok := db.ToArray(post["table_ids"]); ok {
  71. db.UpdateModels(db.Type(DispatchSiteTable{}), map[string]interface{}{
  72. "id in": tableIds,
  73. }, map[string]interface{}{
  74. "state": 3,
  75. "collect_id": id,
  76. })
  77. }
  78. }
  79. func (model DispatchSiteCollect) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  80. if !admin.IsSuperAdmin(c.GetInt("adminID")) {
  81. adminID := s.Param(c.GetInt("adminID"))
  82. where := fmt.Sprintf("`%s`.`created_id` = %s", model.TableName(), adminID)
  83. if admin.CheckAuth([]string{"dispatch:verify"}, c.GetInt("adminID")) {
  84. info, _ := admin.GetInfoByID(c.GetInt("adminID"), nil, nil)
  85. if info == nil {
  86. return false
  87. }
  88. where = fmt.Sprintf("%s OR FIND_IN_SET(`%s`.`shop_id`, %s)", where, model.TableName(), s.Param(info.ShopIds))
  89. }
  90. s.Where = append(s.Where, fmt.Sprintf("(%s)", where))
  91. }
  92. if state, ok := data["state"]; ok {
  93. if n, ok := db.ToInt64(state); ok {
  94. if n != 0 {
  95. s.Having = append(s.Having, fmt.Sprintf("`%s`.`state` = %s", model.TableName(), s.Param(n)))
  96. }
  97. }
  98. }
  99. return true
  100. }
  101. func (DispatchSiteCollect) OnePrivilege(c *gin.Context, id int64) bool {
  102. return true
  103. }
  104. func (DispatchSiteCollect) Page() bool {
  105. return false
  106. }
  107. func (DispatchSiteCollect) Count() bool {
  108. return true
  109. }
  110. type DispatchSiteCollectMobile struct {
  111. CreatedName string `json:"created_name" prop:"select:admin.username"`
  112. CreatedPhone string `json:"created_phone" prop:"select:admin.phone"`
  113. ShopName string `json:"shop_name" prop:"select:shop.shop_name"`
  114. DispatchSiteCollect
  115. }
  116. func (model DispatchSiteCollectMobile) GroupBy() string {
  117. return fmt.Sprintf("`%s`.`id`", model.TableName())
  118. }
  119. type JoinShop struct {
  120. DispatchSiteCollect
  121. }
  122. func (JoinShop) TableName() string {
  123. return "zy_shop"
  124. }
  125. func (model DispatchSiteCollectMobile) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  126. return append(model.DispatchSiteCollect.LeftJoin(data, s), db.JoinModel{
  127. Model: JoinShop{},
  128. As: "shop",
  129. On: []string{"`shop`.`id` = " + model.TableName() + ".`shop_id`"},
  130. }, db.JoinModel{
  131. Model: JoinAdmin{},
  132. As: "admin",
  133. On: []string{"`admin`.`id` = " + model.TableName() + ".`created_id`"},
  134. })
  135. }