package controllers import ( "encoding/json" "github.com/rdlucklib/rdluck_tools/paging" "hongze/hongze_mfyx/models" "hongze/hongze_mfyx/models/order" "hongze/hongze_mfyx/services" "hongze/hongze_mfyx/utils" "strings" "time" ) // 订单 type OrderController struct { BaseAuthController } // @Title 通过文章创建订单 // @Description 通过文章创建订单接口 // @Param request body models.ActivitySingnupRep true "type json string" // @Success Ret=200 {object} models.SignupStatus // @router /addByArticle [post] func (this *OrderController) AddByArticle() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req order.CygxOrderAddReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } uid := user.UserId goodsId := req.GoodsId sourceId := req.SourceId if goodsId == 0 { br.Msg = "支付失败" br.ErrMsg = "支付失败,goodsId 信息错误Err:" + err.Error() return } goodsDetail, err := order.GetCygxGoodsDetailByGoodsId(goodsId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } var title string oldOrderCode := services.GetHaverEquallyOrderByUser10Min(uid, goodsId) //获取用户十分钟之内是否有相同的订单信息 articleDetail, err := models.GetArticleDetailById(sourceId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } title = articleDetail.Title item := new(order.CygxOrder) item.OrderCode = utils.GetOrdernum() item.OutTradeNo = "TRADE" + item.OrderCode item.GoodsName = goodsDetail.GoodsName item.GoodsId = goodsId item.GoodsMoney = goodsDetail.Price item.OrderMoney = goodsDetail.CurrentPrice item.SourceId = sourceId item.Source = utils.CYGX_OBJ_ARTICLE item.SourceTitle = title item.UserId = user.UserId item.Mobile = user.Mobile item.Email = user.Email item.CompanyId = user.CompanyId item.CompanyName = user.CompanyName item.RealName = user.RealName item.SellerName, item.SellerId = services.GetSellerName(user) // 销售姓名 item.ShareName, item.ShareId = services.GetAdminByInviteShareCode(req.InviteShareCode) // 分享人信息 item.CreateTime = time.Now() item.ModifyTime = time.Now() item.RegisterPlatform = utils.REGISTER_PLATFORM item.OrderType = 1 //如果十分钟之内有相同的订单就修改,没有就新增 if oldOrderCode == "" { err = order.AddCygxOrder(item) } else { item.OrderCode = oldOrderCode err = order.UpdateCygxOrder(item, oldOrderCode) } if err != nil { br.Msg = "创建订单信息失败" br.ErrMsg = "创建订单信息失败,Err:" + err.Error() return } JsapiApiResp, err := services.ExampleJsapiApiServicePrepay(item, user.UnionId) if err != nil { br.Msg = "创建订单信息失败" br.ErrMsg = "创建订单信息失败,ExampleJsapiApiServicePrepayErr:" + err.Error() return } resp := new(order.WxJsapiApiResp) resp.Detail = JsapiApiResp resp.OrderCode = item.OrderCode br.Data = resp br.Ret = 200 br.Success = true br.Msg = "操作成功" } // @Title 通过活动创建订单 // @Description 通过活动创建订单接口 // @Param request body models.ActivitySingnupRep true "type json string" // @Success Ret=200 {object} models.SignupStatus // @router /addByActivity [post] func (this *OrderController) AddByActivity() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req order.CygxOrderAddReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } uid := user.UserId goodsId := req.GoodsId sourceId := req.SourceId source := req.Source if goodsId == 0 { br.Msg = "支付失败" br.ErrMsg = "支付失败,goodsId 信息错误Err:" + err.Error() return } goodsDetail, err := order.GetCygxGoodsDetailByGoodsId(goodsId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } activityDetail, err := models.GetAddActivityInfoById(sourceId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } var title string var oldOrderCode string var payTimeCountdown int //var _ int item := new(order.CygxOrder) if source == "ActivityVivo" { oldOrderCode, payTimeCountdown = services.GetHaverEquallyOrderByUser10MinByActivtyVivo(uid, sourceId) item.OrderType = 3 } else { if (!activityDetail.IsResearchPoints && activityDetail.IsLimitPeople == 0) || activityDetail.YidongActivityId != "" { //易董的活动 或者(不扣点且不限制人数)走月卡日卡逻辑 oldOrderCode = services.GetHaverEquallyOrderByUser10Min(uid, goodsId) //获取用户十分钟之内是否有相同的订单信息 item.OrderType = 1 } else { oldOrderCode, payTimeCountdown = services.GetHaverEquallyOrderByUser10MinByActivty(uid, sourceId) item.OrderType = 2 } } title = activityDetail.ActivityName item.OrderCode = utils.GetOrdernum() item.OutTradeNo = "TRADE" + item.OrderCode if activityDetail.ActivityTypeName == "买方线下交流" { item.GoodsName = "买方交流" } if activityDetail.ActivityTypeName == "专家电话会" || activityDetail.ActivityTypeName == "专家线下沙龙" { item.GoodsName = "专家调研" } if activityDetail.ActivityTypeName == "公司调研电话会" { item.GoodsName = "上市公司小范围" } switch goodsId { case 1: item.GoodsName = "日卡" case 2: item.GoodsName = "月卡" case 9: item.GoodsName = "活动回放" } //如果活动有自定义的价格就优先按照自定义的价格来 if activityDetail.ActivityPrice > 0 { goodsDetail.CurrentPrice = activityDetail.ActivityPrice } //return item.GoodsId = goodsId item.GoodsMoney = goodsDetail.Price item.OrderMoney = goodsDetail.CurrentPrice item.SourceId = sourceId item.Source = utils.CYGX_OBJ_ACTIVITY item.SourceTitle = title item.UserId = user.UserId item.Mobile = user.Mobile item.Email = user.Email item.CompanyId = user.CompanyId item.CompanyName = user.CompanyName item.RealName = user.RealName item.SellerName, item.SellerId = services.GetSellerName(user) // 销售姓名 item.ShareName, item.ShareId = services.GetAdminByInviteShareCode(req.InviteShareCode) // 分享人信息 item.CreateTime = time.Now() item.ModifyTime = time.Now() item.RegisterPlatform = utils.REGISTER_PLATFORM //如果是音视频的订单,数据做二次处理 if source == "ActivityVivo" { vivoPointsSetDetail, err := models.GetCygxActivityVivoPointsSetByActivityId(sourceId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } item.GoodsMoney = vivoPointsSetDetail.GoodsMoney item.OrderMoney = vivoPointsSetDetail.GoodsMoney item.Source = vivoPointsSetDetail.Source if vivoPointsSetDetail.Source == utils.CYGX_OBJ_ACTIVITYVIDEO { activityVideoInfo, err := models.GetCygxActivityVideoByActivityId(sourceId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } title = activityVideoInfo.VideoName } else if vivoPointsSetDetail.Source == utils.CYGX_OBJ_ACTIVITYVOICE { activityVoiceInfo, err := models.GetCygxActivityVoiceByActivityId(sourceId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } title = activityVoiceInfo.VoiceName } else { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,音视频数据类型错误" return } item.SourceTitle = title } //如果十分钟之内有相同的订单就修改,没有就新增 if oldOrderCode == "" { err = order.AddCygxOrder(item) } else { item.OrderCode = oldOrderCode err = order.UpdateCygxOrder(item, oldOrderCode) } JsapiApiResp, err := services.ExampleJsapiApiServicePrepay(item, user.UnionId) if err != nil { br.Msg = "创建订单信息失败" br.ErrMsg = "创建订单信息失败,ExampleJsapiApiServicePrepayErr:" + err.Error() return } resp := new(order.WxJsapiApiResp) resp.Detail = JsapiApiResp resp.OrderCode = item.OrderCode resp.PayTimeCountdown = payTimeCountdown //截止支付时间倒计时 if err != nil { br.Msg = "创建订单信息失败" br.ErrMsg = "创建订单信息失败,Err:" + err.Error() return } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "操作成功" } // @Title 通过研选专栏创建订单 // @Description 通过文章创建订单接口 // @Param request body models.ActivitySingnupRep true "type json string" // @Success Ret=200 {object} models.SignupStatus // @router /addByYanxuanspecial [post] func (this *OrderController) AddByYanxuanspecial() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req order.CygxOrderAddReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } uid := user.UserId goodsId := req.GoodsId sourceId := req.SourceId if goodsId == 0 { br.Msg = "支付失败" br.ErrMsg = "支付失败,goodsId 信息错误Err:" + err.Error() return } goodsDetail, err := order.GetCygxGoodsDetailByGoodsId(goodsId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } var title string oldOrderCode := services.GetHaverEquallyOrderByUser10Min(uid, goodsId) //获取用户十分钟之内是否有相同的订单信息 // 研选专栏 yanxuanSpecialBySpeciaDetail, err := models.GetYanxuanSpecialBySpecialId(sourceId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } title = yanxuanSpecialBySpeciaDetail.Title item := new(order.CygxOrder) item.OrderCode = utils.GetOrdernum() item.OutTradeNo = "TRADE" + item.OrderCode item.GoodsName = goodsDetail.GoodsName item.GoodsId = goodsId item.GoodsMoney = goodsDetail.Price item.OrderMoney = goodsDetail.CurrentPrice item.SourceId = sourceId item.Source = utils.CYGX_OBJ_YANXUANSPECIAL item.SourceTitle = title item.UserId = user.UserId item.Mobile = user.Mobile item.Email = user.Email item.CompanyId = user.CompanyId item.CompanyName = user.CompanyName item.RealName = user.RealName item.SellerName, item.SellerId = services.GetSellerName(user) // 销售姓名 item.ShareName, item.ShareId = services.GetAdminByInviteShareCode(req.InviteShareCode) // 分享人信息 item.CreateTime = time.Now() item.ModifyTime = time.Now() item.RegisterPlatform = utils.REGISTER_PLATFORM item.OrderType = 1 //如果十分钟之内有相同的订单就修改,没有就新增 if oldOrderCode == "" { err = order.AddCygxOrder(item) } else { item.OrderCode = oldOrderCode err = order.UpdateCygxOrder(item, oldOrderCode) } if err != nil { br.Msg = "创建订单信息失败" br.ErrMsg = "创建订单信息失败,Err:" + err.Error() return } JsapiApiResp, err := services.ExampleJsapiApiServicePrepay(item, user.UnionId) if err != nil { br.Msg = "创建订单信息失败" br.ErrMsg = "创建订单信息失败,ExampleJsapiApiServicePrepayErr:" + err.Error() return } resp := new(order.WxJsapiApiResp) resp.Detail = JsapiApiResp resp.OrderCode = item.OrderCode br.Data = resp br.Data = resp br.Ret = 200 br.Success = true br.Msg = "操作成功" } // @Title 用户订单列表 // @Description 用户订单列表接口 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param OrderType query int false "订单类型 1:畅读卡订单,2:单场付费订单" // @Success 200 {object} models.SpecialListResp // @router /user/orderList [get] func (this *OrderController) UserOrderList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") orderType, _ := this.GetInt("OrderType", 1) var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = utils.StartIndex(currentIndex, pageSize) resp := new(order.UserOrderListResp) var condition string var pars []interface{} userId := user.UserId if orderType > 0 { condition += ` AND order_type = ? ` pars = append(pars, orderType) } condition += ` AND user_id = ? ` pars = append(pars, userId) total, err := order.GetCygxOrderCount(condition, pars) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } condition += ` ORDER BY order_id DESC ` list, err := order.GetCygxOrderList(condition, pars, startSize, pageSize) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } //音视频时长 var activityVivoIds []int for _, v := range list { if v.OrderType == 3 { activityVivoIds = append(activityVivoIds, v.SourceId) } } mapActivityVivo := services.GetActivityVivoByActivityIds(activityVivoIds) for _, v := range list { item := new(order.OrderListResp) item.OrderCode = v.OrderCode item.OrderMoney = v.OrderMoney item.SourceId = v.SourceId item.Source = v.Source item.SourceTitle = v.SourceTitle item.OrderStatus = v.OrderStatus switch item.OrderStatus { case 0: item.OrderStatusText = "已取消" case 1: item.OrderStatusText = "待支付" case 2: item.OrderStatusText = "支付成功" case 3: item.OrderStatusText = "转入退款" case 4: item.OrderStatusText = "退款处理中" case 5: item.OrderStatusText = "退款异常" case 6: item.OrderStatusText = "退款关闭" } if orderType == 1 { if v.GoodsId == 1 { item.LabelKeywordImgLink = utils.LABEL_ICO_7 //日卡图标 item.StartDate = v.StartDate.Format(utils.FormatDateTime) item.EndDate = v.EndDate.Format(utils.FormatDateTime) } else if v.GoodsId == 2 { item.LabelKeywordImgLink = utils.LABEL_ICO_8 // 月卡图标 item.StartDate = v.StartDate.Format(utils.FormatDate) item.EndDate = v.EndDate.Format(utils.FormatDate) } } else if orderType == 2 { item.LabelKeywordImgLink = utils.LABEL_ICO_3 } else if orderType == 3 { item.LabelKeywordImgLink = utils.LABEL_ICO_10 } if v.OrderType == 3 { item.PlaySeconds = mapActivityVivo[v.SourceId] } item.CreateTime = v.CreateTime.Format(utils.FormatDateTime) resp.List = append(resp.List, item) } page := paging.GetPaging(currentIndex, pageSize, total) resp.Paging = page br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @Title 订单支付详情 // @Description 订单支付详情接口 // @Param OrderCode query string false "订单号" // @Success 200 {object} models.SpecialListResp // @router /orderPay/detail [get] func (this *OrderController) OrderDetail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } resp := new(order.PayEdOrderDetailResp) orderCode := this.GetString("OrderCode") orderDetail, err := order.GetCygxOrderDetailByOrderCode(orderCode) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } if orderDetail.OrderType == 1 && orderDetail.OrderStatus == 2 { cardDetail, err := order.GetCygxOrderUserCardDetailByOrderCode(orderCode) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } resp.EndDate = strings.Replace(cardDetail.EndDate.Format(utils.FormatDateTime), "00:00:00", "", -1) } resp.OrderCode = orderDetail.OrderCode resp.OrderType = orderDetail.OrderType resp.OrderStatus = orderDetail.OrderStatus resp.Source = orderDetail.Source resp.SourceId = orderDetail.SourceId if orderDetail.Source == utils.CYGX_OBJ_ACTIVITY { resp.IsPublicActivitie, resp.IsSignUp = services.GetActivityWechatPayMsg(orderDetail.SourceId) //如果是活动就要判断是否是公开活动,是否需要自动报名 } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @Title 取消订单 // @Description 取消订单接口 // @Param request body models.CygxOrderCancelReq true "type json string" // @router /cancel [post] func (this *OrderController) Cancel() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req order.CygxOrderCancelReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } orderCode := req.OrderCode orderDetail, err := order.GetCygxOrderDetailByOrderCode(orderCode) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } err = order.CancelCygxOrder(orderDetail) if err != nil { br.Msg = "取消订单失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" }