setting.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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", "letterName"},
  37. })
  38. CreateCRUD(router, "/info", &CRUDOption{
  39. Collection: "infos",
  40. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  41. entity := &model.Setting{}
  42. c.ShouldBindJSON(entity)
  43. entity.UpdateTime = time.Now()
  44. return entity, nil
  45. },
  46. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  47. return &model.Setting{}
  48. },
  49. JWT: true,
  50. SearchProject: []string{"companyName", "updateTime", "address", "phone"},
  51. })
  52. //计价策略
  53. CreateCRUD(router, "/calc", &CRUDOption{
  54. Collection: "calcs",
  55. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  56. entity := &model.PriceCalc{}
  57. c.ShouldBindJSON(entity)
  58. entity.CreateTime = time.Now()
  59. entity.UpdateTime = time.Now()
  60. return entity, nil
  61. },
  62. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  63. return &model.PriceCalc{}
  64. },
  65. JWT: true,
  66. OnUpdate: func(c *gin.Context, apictx *ApiSession, entity interface{}) {
  67. calc := entity.(*model.PriceCalc)
  68. calc.UpdateTime = time.Now()
  69. if calc.IsDefault != nil && *calc.IsDefault { //设为默认把其他的所有默认清除
  70. repo.RepoUpdateSetDocsProps(apictx.CreateRepoCtx(),
  71. &repo.DocFilterOptions{
  72. CollectName: "calcs",
  73. Query: repo.Map{"category": calc.Category},
  74. }, bson.M{"isDefault": false})
  75. }
  76. },
  77. SearchProject: []string{"name", "updateTime", "isDefault", "category", "remark", "param1", "param2", "param3", "param4", "param5"},
  78. })
  79. }