dispatch_site_process.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package dispatch
  2. import (
  3. "errors"
  4. "fmt"
  5. "zhiyuan/pkg/db"
  6. "github.com/gin-gonic/gin"
  7. )
  8. type DispatchSiteProcess struct {
  9. ID int64 `json:"id" prop:"add:false"`
  10. TableId int64 `json:"table_id" label:"表" type:"int"`
  11. CollectId int64 `json:"collect_id" label:"汇总" type:"int"`
  12. Explain string `json:"explain" label:"说明" type:"string" search:"like"`
  13. Pictures string `json:"pictures" label:"图片" type:"string" search:"like"`
  14. Type int64 `json:"type" label:"类型" type:"int" prop:"add" default:"0"`
  15. Signature string `json:"signature" label:"签名" prop:"add" type:"string"`
  16. State int64 `json:"state" label:"状态" type:"int" prop:"add" default:"0"`
  17. CreatedId int64 `json:"created_id" label:"创建人员" type:"int" prop:"add:false" search:"="`
  18. CreatedName string `json:"created_name" prop:"add:false select:created.username"`
  19. CreatedPhone string `json:"created_phone" prop:"add:false select:created.phone"`
  20. CreatedHeadimgurl string `json:"created_headimgurl" prop:"add:false select:created.headimgurl"`
  21. DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
  22. CreatedAt int64 `json:"created_at" prop:"add:false"`
  23. UpdatedAt int64 `json:"updated_at" prop:"add:false"`
  24. db.BaseModel
  25. }
  26. func (DispatchSiteProcess) TableName() string {
  27. return "zy_dispatch_site_process"
  28. }
  29. func (model DispatchSiteProcess) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  30. table_id, _ := db.ToInt64(data["table_id"])
  31. collect_id, _ := db.ToInt64(data["collect_id"])
  32. if table_id != 0 && collect_id != 0 {
  33. s.Where = append(s.Where, fmt.Sprintf("`%s`.`table_id` = %s OR `%s`.`collect_id` = %s", model.TableName(), s.Param(table_id), model.TableName(), s.Param(collect_id)))
  34. } else if table_id != 0 {
  35. s.Where = append(s.Where, fmt.Sprintf("`%s`.`table_id` = %s", model.TableName(), s.Param(table_id)))
  36. } else if collect_id != 0 {
  37. s.Where = append(s.Where, fmt.Sprintf("`%s`.`collect_id` = %s", model.TableName(), s.Param(collect_id)))
  38. }
  39. return true
  40. }
  41. func (DispatchSiteProcess) OnePrivilege(c *gin.Context, id int64) bool {
  42. return true
  43. }
  44. func (DispatchSiteProcess) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
  45. data["created_id"] = c.GetInt("adminID")
  46. typ, _ := db.ToInt64(data["type"])
  47. tableID, _ := db.ToInt64(data["table_id"])
  48. collectID, _ := db.ToInt64(data["collect_id"])
  49. state, _ := db.ToInt64(data["state"])
  50. if tableID != 0 {
  51. var model DispatchSiteTable
  52. db.GetModel(map[string]interface{}{
  53. "id": tableID,
  54. }, &model)
  55. if model.ID == 0 || model.State != typ {
  56. return errors.New("没有权限")
  57. }
  58. s := model.State + 1
  59. if state != 0 {
  60. s = -1
  61. }
  62. if s > 2 {
  63. return errors.New("没有权限")
  64. }
  65. err := db.UpdateModel(db.Type(model), tableID, map[string]interface{}{
  66. "state": s,
  67. })
  68. if err != nil {
  69. return errors.New("没有权限")
  70. }
  71. if s == -1 {
  72. tableid, err := db.InsertModel(db.Type(DispatchSiteTable{}), map[string]interface{}{
  73. "type_id": model.TypeId,
  74. "site_id": model.SiteId,
  75. "worker_id": model.WorkerId,
  76. "budget": model.Budget,
  77. "state": 0,
  78. })
  79. if err == nil {
  80. items, _ := db.GetModelMap(db.Type(DispatchTypeItem{}), map[string]interface{}{"type_id": model.TypeId}, nil)
  81. if items != nil {
  82. for _, v := range items {
  83. db.InsertModel(db.Type(DispatchSiteItem{}), map[string]interface{}{
  84. "table_id": tableid,
  85. "name": v["name"],
  86. "unit": v["unit"],
  87. "price": v["price"],
  88. "num": 0,
  89. })
  90. }
  91. }
  92. }
  93. }
  94. } else if collectID != 0 {
  95. var model DispatchSiteCollect
  96. db.GetModel(map[string]interface{}{
  97. "id": collectID,
  98. }, &model)
  99. if model.ID == 0 || model.State != typ {
  100. return errors.New("没有权限")
  101. }
  102. if state != 0 {
  103. return errors.New("没有权限")
  104. }
  105. s := model.State + 1
  106. err := db.UpdateModel(db.Type(model), collectID, map[string]interface{}{
  107. "state": s,
  108. })
  109. if err != nil {
  110. return errors.New("没有权限")
  111. }
  112. err = db.UpdateModels(db.Type(DispatchSiteTable{}), map[string]interface{}{
  113. "collect_id": collectID,
  114. }, map[string]interface{}{
  115. "state": s,
  116. })
  117. if err != nil {
  118. return errors.New("没有权限")
  119. }
  120. } else {
  121. return errors.New("没有权限")
  122. }
  123. return nil
  124. }
  125. func (DispatchSiteProcess) Page() bool {
  126. return false
  127. }
  128. func (model DispatchSiteProcess) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  129. return []db.JoinModel{
  130. {
  131. Model: JoinAdmin{},
  132. As: "created",
  133. On: []string{"`created`.`id` = " + model.TableName() + ".`created_id`"},
  134. },
  135. }
  136. }