完善购买和套餐功能
This commit is contained in:
@@ -403,8 +403,8 @@ func (c *PlatformProductController) ActivateProductOrder() {
|
||||
c.jsonErr(404, 404, "订单不存在")
|
||||
return
|
||||
}
|
||||
if order.Status == models.ProductOrderStatusActivated {
|
||||
c.jsonErr(400, 400, "订单已开通")
|
||||
if order.Status == models.ProductOrderStatusActivated && !services.ProductOrderExpired(&order, time.Now()) {
|
||||
c.jsonErr(400, 400, "订单已开通且在有效期内")
|
||||
return
|
||||
}
|
||||
if err := activateProductOrder(&order, "平台手动开通"); err != nil {
|
||||
@@ -414,18 +414,73 @@ func (c *PlatformProductController) ActivateProductOrder() {
|
||||
c.jsonOK(nil, "开通成功")
|
||||
}
|
||||
|
||||
// activateProductOrder 订单开通:绑定套餐(产品配置了套餐时)并置为已开通
|
||||
// CloseProductOrder POST /platform/product/order/close/:id
|
||||
// 关闭已开通的单品功能:订单置为已关闭,租户端对应模块权益即时回收(菜单/卡片恢复锁定)。
|
||||
// 仅允许关闭单品订单(package_id=0);误操作可在购买订单列表重新「手动开通」。
|
||||
func (c *PlatformProductController) CloseProductOrder() {
|
||||
id := parseUint64Param(c.Ctx.Input.Param(":id"))
|
||||
if id == 0 {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
var order models.PlatformProductOrder
|
||||
if err := models.Orm.QueryTable(new(models.PlatformProductOrder)).
|
||||
Filter("id", id).
|
||||
Filter("delete_time__isnull", true).
|
||||
One(&order); err != nil {
|
||||
c.jsonErr(404, 404, "订单不存在")
|
||||
return
|
||||
}
|
||||
if order.PackageID > 0 {
|
||||
c.jsonErr(400, 400, "套餐订单请通过「套餐与用户数」调整套餐")
|
||||
return
|
||||
}
|
||||
if order.Status != models.ProductOrderStatusActivated {
|
||||
c.jsonErr(400, 400, "仅已开通的订单可关闭功能")
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
order.Status = models.ProductOrderStatusClosed
|
||||
order.UpdateTime = &now
|
||||
order.Remark = strings.TrimSpace(order.Remark + " 平台关闭功能")
|
||||
if _, err := models.Orm.Update(&order, "Status", "Remark", "UpdateTime"); err != nil {
|
||||
c.jsonErr(500, 500, "关闭失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
c.jsonOK(nil, "功能已关闭")
|
||||
}
|
||||
|
||||
// activateProductOrder 订单开通:绑定套餐(产品配置了套餐时)并置为已开通。
|
||||
// 单品功能默认 1 年有效期(ProductOrderDurationDays),到期后租户端权益自动失效,需续费。
|
||||
func activateProductOrder(order *models.PlatformProductOrder, operator string) error {
|
||||
now := time.Now()
|
||||
if order.PackageID > 0 && order.Tid > 0 {
|
||||
// 套餐订单:绑定/续费套餐,有效期由套餐自身到期时间管理
|
||||
if _, err := services.ApplyPackageToTenant(order.Tid, order.PackageID, true); err != nil {
|
||||
return fmt.Errorf("开通失败:%v", err)
|
||||
}
|
||||
} else if order.ProductID > 0 {
|
||||
// 单品功能:有效期精确到日(自然日 0 点),到期日当天 0 点起即失效;
|
||||
// 未到期续费从原到期日顺延 1 年,已过期重新开通从开通当日 0 点起算 1 年
|
||||
base := services.DayStart(now)
|
||||
if cur := services.ProductOrderExpireAt(order); cur != nil && cur.After(base) {
|
||||
base = *cur
|
||||
}
|
||||
expire := base.AddDate(0, 0, models.ProductOrderDurationDays)
|
||||
order.ExpireTime = &expire
|
||||
}
|
||||
now := time.Now()
|
||||
order.Status = models.ProductOrderStatusActivated
|
||||
order.ActivateTime = &now
|
||||
if order.ActivateTime == nil {
|
||||
// 开通时间精确到日(自然日 0 点),与有效期的计算粒度保持一致
|
||||
day := services.DayStart(now)
|
||||
order.ActivateTime = &day
|
||||
}
|
||||
order.UpdateTime = &now
|
||||
order.Remark = strings.TrimSpace(order.Remark + " " + operator)
|
||||
_, err := models.Orm.Update(order, "Status", "ActivateTime", "Remark", "UpdateTime")
|
||||
fields := []string{"Status", "ActivateTime", "Remark", "UpdateTime"}
|
||||
if order.ExpireTime != nil {
|
||||
fields = append(fields, "ExpireTime")
|
||||
}
|
||||
_, err := models.Orm.Update(order, fields...)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user