123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package api
- import (
- "box-cost/db/model"
- "box-cost/db/repo"
- "box-cost/log"
- "errors"
- "fmt"
- "time"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- // 签名管理
- func Signature(r *GinRouter) {
- // 创建签名
- r.POST("/signature/create", SignatureAdd)
- // 获取签名详情
- r.GET("/signature/detail/:id", SignatureDetail)
- // 获取签名列表
- r.GET("/signature/list", SignatureList)
- // 更新签名
- r.POST("/signature/update", SignatureUpdate)
- // 删除签名
- r.POST("/signature/delete/:id", SignatureDel)
- }
- // 创建签名
- func SignatureAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var signature model.Signature
- err := c.ShouldBindJSON(&signature)
- if err != nil {
- fmt.Println(err)
- return nil, errors.New("参数错误!")
- }
- ctx := apictx.CreateRepoCtx()
- // if signature.UserId.IsZero() {
- // return nil, errors.New("签名用户为空")
- // }
- signature.CreateTime = time.Now()
- signature.UpdateTime = time.Now()
- return repo.RepoAddDoc(ctx, repo.CollectionSignature, &signature)
- }
- // 获取签名详情
- func SignatureDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- signatureId := c.Param("id")
- id, err := primitive.ObjectIDFromHex(signatureId)
- if err != nil {
- return nil, errors.New("非法id")
- }
- var signature model.Signature
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionSignature,
- Query: repo.Map{"_id": id},
- }
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &signature)
- if !found || err != nil {
- log.Info(err)
- return nil, errors.New("数据未找到")
- }
- return signature, nil
- }
- // 获取签名列表
- func SignatureList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- option := &repo.PageSearchOptions{
- CollectName: repo.CollectionSignature,
- Query: query,
- Page: page,
- Size: size,
- Sort: bson.M{"sort": 1},
- }
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
- }
- // 更新签名
- func SignatureUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var signature model.Signature
- err := c.ShouldBindJSON(&signature)
- if err != nil {
- return nil, errors.New("参数错误")
- }
- if signature.Id.Hex() == "" {
- return nil, errors.New("id的为空")
- }
- signature.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSignature, signature.Id.Hex(), &signature)
- }
- // 删除签名
- func SignatureDel(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- signatureId := c.Param("id")
- if signatureId == "" {
- return nil, errors.New("id为空")
- }
- return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSignature, signatureId)
- }
|