123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- package api
- import (
- "fmt"
- "sku3dweb/db/model"
- "sku3dweb/db/repo"
- "time"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- //订单
- func Order(r *GinRouter) {
- // 查看订单
- r.GETJWTKEY("/order/detail/:id", GetOrder, SKU3DUSER, SKU3DADMIN)
- // 查看订单列表
- r.GETJWTKEY("/order/list", GetOrders, SKU3DUSER, SKU3DADMIN)
- // 添加
- r.POSTJWTKEY("/order/create", CreateOrder, SKU3DUSER, SKU3DADMIN)
- // 修改订单
- r.POSTJWTKEY("/order/update", UpdateOrder, SKU3DUSER, SKU3DADMIN)
- // 删除订单
- r.POSTJWTKEY("/order/delete/:id", DeleteOrder, SKU3DUSER, SKU3DADMIN)
- }
- func GetOrder(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- idStr := c.Param("id")
- id, err := primitive.ObjectIDFromHex(idStr)
- if err != nil {
- return false, err
- }
- query := repo.Map{"_id": id}
- // 非管理员只能看自己的订单
- if apictx.User.Key == SKU3DUSER {
- pid, err := primitive.ObjectIDFromHex(apictx.User.Parent)
- if err != nil {
- return false, err
- }
- query["userId"] = pid
- }
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionOrder,
- Query: query,
- }
- found, m := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), option)
- if !found {
- return false, NewError("没有找到该订单")
- }
- return m, nil
- }
- func GetOrders(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- option := &repo.PageSearchOptions{
- CollectName: repo.CollectionOrder,
- Query: query,
- Page: page,
- Size: size,
- Sort: bson.M{"createTime": -1},
- }
- // 非管理员只能看自己的订单
- if apictx.User.Key == SKU3DUSER {
- pid, err := primitive.ObjectIDFromHex(apictx.User.Parent)
- if err != nil {
- return false, err
- }
- query["userId"] = pid
- }
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
- }
- func CreateOrder(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var order model.Order
- err := c.ShouldBindJSON(&order)
- if err != nil {
- fmt.Println(err)
- return false, err
- }
- ctx := apictx.CreateRepoCtx()
- pid, err := primitive.ObjectIDFromHex(apictx.User.Parent)
- if err != nil {
- return false, err
- }
- query := repo.Map{"userId": pid}
- // 申请免费版,或者购买企业版
- if order.Apply != nil && order.Member == nil {
- query["status"] = model.Applying
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionOrder,
- Query: query,
- }
- found, _ := repo.RepoSeachDocMap(ctx, option)
- if found {
- return false, NewError("已申请")
- }
- order.Status = &model.Applying
- } else if order.Apply == nil && order.Member != nil {
- query["status"] = model.WatiPay
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionOrder,
- Query: query,
- }
- found, _ := repo.RepoSeachDocMap(ctx, option)
- if found {
- return false, NewError("请先完成已有支付")
- }
- order.Status = &model.WatiPay
- // 查询member信息
- var m model.Member
- option1 := &repo.DocSearchOptions{
- CollectName: repo.CollectionMember,
- Query: repo.Map{"_id": order.Member.Id},
- }
- found, _ = repo.RepoSeachDoc(ctx, option1, &m)
- if !found {
- return false, NewError("不存在该会员信息")
- }
- m.Day = order.Member.Day
- order.Member.Member = &m
- // 直接购买/续费
- var total int
- if order.Member.Day != nil { //直接购买默认是5个席位
- // 席数*价格*年
- order.Member.UserNum = IntValue(5)
- m.UserNum = order.Member.UserNum
- total = 2000
- } else {
- // 添加席位
- // 查询多少过期时间
- var mu model.MemberUser
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionMemberUser,
- Query: repo.Map{"parentId": pid, "memberId": m.Id},
- Project: []string{"deadTime"},
- }
- found, _ := repo.RepoSeachDoc(ctx, option, &mu)
- if !found {
- return false, NewError("未购买过企业会员")
- }
- // 剩余天数
- day := int(time.Until(mu.DeadTime) / (time.Hour * 24))
- if day <= 0 {
- day = 1
- }
- // 计算一天需要多少钱
- dayPrice := *order.Member.Price / 365
- total = (*order.Member.UserNum) * dayPrice * day
- }
- order.Amount = &total
- } else {
- return false, NewError("请求错误")
- }
- if apictx.User.Parent != "" {
- pid, err := primitive.ObjectIDFromHex(apictx.User.Parent)
- if err != nil {
- return false, err
- }
- order.UserId = &pid
- } else {
- order.UserId = &pid
- }
- order.UserName = apictx.User.Name
- order.CreateTime = time.Now()
- order.UpdateTime = time.Now()
- return repo.RepoAddDoc(ctx, repo.CollectionOrder, &order)
- }
- func UpdateOrder(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var order model.Order
- err := c.ShouldBindJSON(&order)
- if err != nil {
- return false, nil
- }
- if order.Id == nil {
- return false, NewError("Id不能为空")
- }
- ctx := apictx.CreateRepoCtx()
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionOrder,
- Query: repo.Map{"_id": order.Id},
- Project: []string{"userId"},
- }
- found, _ := repo.RepoSeachDoc(ctx, option, &order)
- if !found {
- return false, NewError("不存在该订单")
- }
- if apictx.User.Key == SKU3DUSER {
- order.Status = &model.Applying
- }
- // 免费版加一年时间
- if *order.Status == model.ApplySuccess {
- // 获取免费版
- var member model.Member
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionMember,
- Query: repo.Map{"level": model.FREE},
- }
- found, _ := repo.RepoSeachDoc(ctx, option, &member)
- if !found {
- return false, err
- }
- // 更新增加时间
- data := repo.Map{"deadTime": time.Now().Add(365 * time.Hour * 24)}
- res, err := repo.RepoUpdateOneDoc(apictx.CreateRepoCtx(), repo.CollectionMemberUser, bson.M{"parentId": order.UserId, "memberId": member.Id}, data)
- fmt.Println(res, err)
- order.Status = &model.ApplySuccess
- }
- order.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, order.Id.Hex(), &order)
- }
- func DeleteOrder(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- idStr := c.Param("id")
- id, err := primitive.ObjectIDFromHex(idStr)
- if err != nil {
- return false, nil
- }
- // 1、查询是否存在
- query := repo.Map{"_id": id}
- if apictx.User.Key == SKU3DUSER {
- userId, err := primitive.ObjectIDFromHex(apictx.User.ID)
- if err != nil {
- return false, err
- }
- query["userId"] = userId
- }
- var order model.Order
- ctx := apictx.CreateRepoCtx()
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionOrder,
- Query: query,
- }
- found, _ := repo.RepoSeachDoc(ctx, option, &order)
- if !found {
- return false, NewError("不存在该订单")
- }
- // 2、关闭订单
- // om := &model.OrderMsg{Id: order.Id.Hex(), PayMode: order.PayMod, ModName: "SKU3D"}
- // res, err := bus.CloseOrder(om)
- // if err != nil {
- // return false, err
- // }
- // if res.Code != model.SUCCESS.Code {
- // return false, NewError(res.Message)
- // }
- return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, idStr)
- }
|