123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package api
- import (
- "box-cost/db/model"
- "box-cost/db/repo"
- "bytes"
- "fmt"
- "net/http"
- "time"
- "github.com/thecodingmachine/gotenberg-go-client/v7"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- func generateSerial(ctx *ApiSession, typeName string) (serial string, err error) {
- // 获取类型
- cate := &model.Category{}
- found, err := repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: "cates",
- Project: []string{"letterName"},
- Query: repo.Map{"name": typeName},
- Sort: bson.M{"_id": -1},
- }, cate)
- if !found || err != nil {
- return "", fmt.Errorf("未找到该类型")
- }
- // 自增器 increment index加1
- increment := &model.Increment{}
- found, _ = repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionIncrement,
- Query: repo.Map{"type": cate.LetterName},
- Project: []string{"index"},
- Sort: bson.M{"_id": -1},
- }, increment)
- if !found {
- repo.RepoAddDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, &model.Increment{
- Type: cate.LetterName,
- Index: 1,
- })
- return fmt.Sprintf("%s-%06d", cate.LetterName, 1), nil
- }
- index := increment.Index + 1
- repo.RepoUpdateSetDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, increment.Id.Hex(), &model.Increment{Index: index})
- // 拼接为序号
- return fmt.Sprintf("%s-%06d", cate.LetterName, index), nil
- }
- func searchBillTypeById(ctx *ApiSession, collectName string, id primitive.ObjectID) (string, error) {
- found, curbill := repo.RepoSeachDocMap(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: collectName,
- Project: []string{"type"},
- Query: repo.Map{"_id": id},
- Sort: bson.M{"_id": -1},
- })
- if !found {
- return "", fmt.Errorf("未找到该类型")
- }
- return curbill["type"].(string), nil
- }
- func excelToPdf(formBody *bytes.Buffer, pdfHost string) (*http.Response, error) {
- httpClient := &http.Client{
- Timeout: time.Duration(5) * time.Second,
- }
- client := &gotenberg.Client{Hostname: pdfHost, HTTPClient: httpClient}
- doc, err := gotenberg.NewDocumentFromBytes("foo.xlsx", formBody.Bytes())
- if err != nil {
- fmt.Println(" to pdf read data err:", err)
- return nil, err
- }
- req := gotenberg.NewOfficeRequest(doc)
- req.Landscape(true)
- return client.Post(req)
- }
- func isManager(roles []string) bool {
- if len(roles) > 0 {
- for _, role := range roles {
- if role == "manager" {
- return true
- }
- }
- }
- return false
- }
- func getUserById(apictx *ApiSession, id primitive.ObjectID) (*model.UserSmaple, error) {
- fmt.Println(id.Hex())
- user := &model.UserSmaple{}
- _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- Db: "box-user",
- CollectName: repo.CollectionUsers,
- Query: repo.Map{"_id": id},
- Project: []string{"name", "avatar", "city", "loginName", "roles"},
- }, user)
- return user, err
- }
- // 获取一天的起始终止时间
- // func getDayRange(t time.Time) (start, end time.Time) {
- // loc, _ := time.LoadLocation("Local")
- // date := t.Format("2006-01-02")
- // startDate := date + " 00:00:00"
- // startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startDate, loc)
- // endDate := date + " 23:59:59"
- // endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endDate, loc)
- // return startTime, endTime
- // }
- // 获取时间跨度的起始终止时间
- // startDate/endDate 2023-01-31
- func getTimeRange(startDate, endDate string) (start, end time.Time) {
- loc, _ := time.LoadLocation("Local")
- startDateTime := startDate + " 00:00:00"
- endDateTime := endDate + " 23:59:59"
- start, _ = time.ParseInLocation("2006-01-02 15:04:05", startDateTime, loc)
- end, _ = time.ParseInLocation("2006-01-02 15:04:05", endDateTime, loc)
- return
- }
|