signature.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "fmt"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "go.mongodb.org/mongo-driver/bson/primitive"
  12. )
  13. // 签名管理
  14. func Signature(r *GinRouter) {
  15. // 创建签名
  16. r.POST("/signature/create", SignatureAdd)
  17. // 获取签名详情
  18. r.GET("/signature/detail/:id", SignatureDetail)
  19. // 获取签名列表
  20. r.GET("/signature/list", SignatureList)
  21. // 更新签名
  22. r.POST("/signature/update", SignatureUpdate)
  23. // 删除签名
  24. r.POST("/signature/delete/:id", SignatureDel)
  25. }
  26. // 创建签名
  27. func SignatureAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  28. var signature model.Signature
  29. err := c.ShouldBindJSON(&signature)
  30. if err != nil {
  31. fmt.Println(err)
  32. return nil, errors.New("参数错误!")
  33. }
  34. ctx := apictx.CreateRepoCtx()
  35. // if signature.UserId.IsZero() {
  36. // return nil, errors.New("签名用户为空")
  37. // }
  38. signature.CreateTime = time.Now()
  39. signature.UpdateTime = time.Now()
  40. return repo.RepoAddDoc(ctx, repo.CollectionSignature, &signature)
  41. }
  42. // 获取签名详情
  43. func SignatureDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  44. signatureId := c.Param("id")
  45. id, err := primitive.ObjectIDFromHex(signatureId)
  46. if err != nil {
  47. return nil, errors.New("非法id")
  48. }
  49. var signature model.Signature
  50. option := &repo.DocSearchOptions{
  51. CollectName: repo.CollectionSignature,
  52. Query: repo.Map{"_id": id},
  53. }
  54. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &signature)
  55. if !found || err != nil {
  56. log.Info(err)
  57. return nil, errors.New("数据未找到")
  58. }
  59. return signature, nil
  60. }
  61. // 获取签名列表
  62. func SignatureList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  63. page, size, query := UtilQueryPageSize(c)
  64. option := &repo.PageSearchOptions{
  65. CollectName: repo.CollectionSignature,
  66. Query: query,
  67. Page: page,
  68. Size: size,
  69. Sort: bson.M{"sort": 1},
  70. }
  71. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  72. }
  73. // 更新签名
  74. func SignatureUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  75. var signature model.Signature
  76. err := c.ShouldBindJSON(&signature)
  77. if err != nil {
  78. return nil, errors.New("参数错误")
  79. }
  80. if signature.Id.Hex() == "" {
  81. return nil, errors.New("id的为空")
  82. }
  83. signature.UpdateTime = time.Now()
  84. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSignature, signature.Id.Hex(), &signature)
  85. }
  86. // 删除签名
  87. func SignatureDel(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  88. signatureId := c.Param("id")
  89. if signatureId == "" {
  90. return nil, errors.New("id为空")
  91. }
  92. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSignature, signatureId)
  93. }