setting.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "time"
  6. "github.com/gin-gonic/gin"
  7. "go.mongodb.org/mongo-driver/bson"
  8. )
  9. func Setting(router *GinRouter) {
  10. CreateCRUD(router, "/units", &CRUDOption{
  11. Collection: "units",
  12. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  13. entity := &model.Unit{}
  14. c.ShouldBindJSON(entity)
  15. entity.CreateTime = time.Now()
  16. return entity, nil
  17. },
  18. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  19. return &model.Unit{}
  20. },
  21. JWT: true,
  22. SearchProject: []string{"name", "createTime"},
  23. })
  24. CreateCRUD(router, "/cates", &CRUDOption{
  25. Collection: "cates",
  26. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  27. entity := &model.Category{}
  28. c.ShouldBindJSON(entity)
  29. entity.CreateTime = time.Now()
  30. return entity, nil
  31. },
  32. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  33. return &model.Category{}
  34. },
  35. JWT: true,
  36. SearchProject: []string{"name", "createTime"},
  37. })
  38. //计价策略
  39. CreateCRUD(router, "/calc", &CRUDOption{
  40. Collection: "calcs",
  41. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  42. entity := &model.PriceCalc{}
  43. c.ShouldBindJSON(entity)
  44. entity.CreateTime = time.Now()
  45. entity.UpdateTime = time.Now()
  46. return entity, nil
  47. },
  48. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  49. return &model.PriceCalc{}
  50. },
  51. JWT: true,
  52. OnUpdate: func(c *gin.Context, apictx *ApiSession, entity interface{}) {
  53. calc := entity.(*model.PriceCalc)
  54. calc.UpdateTime = time.Now()
  55. if calc.IsDefault != nil && *calc.IsDefault { //设为默认把其他的所有默认清除
  56. repo.RepoUpdateSetDocsProps(apictx.CreateRepoCtx(),
  57. &repo.DocFilterOptions{
  58. CollectName: "calcs",
  59. Query: repo.Map{"category": calc.Category},
  60. }, bson.M{"isDefault": false})
  61. }
  62. },
  63. SearchProject: []string{"name", "updateTime", "isDefault", "category", "remark", "param1", "param2", "param3", "param4", "param5"},
  64. })
  65. }