final_site_process.go 5.3 KB

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