order.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package order
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "time"
  7. )
  8. type WxJsapiApiResp struct {
  9. Detail PrepayWithRequestPaymentResponse
  10. OrderCode string `comment:"订单编号"`
  11. PayTimeCountdown int `description:"支付时间倒计时"`
  12. }
  13. // PrepayWithRequestPaymentResponse 预下单ID,并包含了调起支付的请求参数
  14. type PrepayWithRequestPaymentResponse struct {
  15. PrepayId string // 预支付交易会话标识
  16. Appid string // 应用ID
  17. TimeStamp string // 时间戳
  18. NonceStr string // 随机字符串
  19. Package string // 订单详情扩展字符串
  20. SignType string // 签名方式
  21. PaySign string // 签名
  22. }
  23. type CygxOrderAddReq struct {
  24. GoodsId int `description:"商品ID"`
  25. Source string `description:"资源(文章、活动)"`
  26. SourceId int `description:"资源ID"`
  27. InviteShareCode string `description:"销售账号邀请码"`
  28. }
  29. type CygxOrderCancelReq struct {
  30. OrderCode string `comment:"订单编号"`
  31. }
  32. type CygxOrder struct {
  33. OrderId int `orm:"column(order_id);pk";comment:"订单id"`
  34. OrderCode string `comment:"订单编号"`
  35. OutTradeNo string `comment:"【商户订单号】 商户系统内部订单号"`
  36. OutTradeCode string `comment:"外部交易号"`
  37. PaymentType int `comment:"支付类型。取值范围:1微信支付,2支付宝支付。"`
  38. TradeType string `comment:"交易类型,枚举值:JSAPI:公众号支付 、 NATIVE:扫码支付 、 App:App支付 、 MICROPAY:付款码支付 、 MWEB:H5支付 、 FACEPAY:刷脸支付"`
  39. GoodsName string `comment:"商品名称"`
  40. GoodsId int `comment:"商品ID"`
  41. BuyerInvoice string `comment:"买家发票信息"`
  42. GoodsMoney float64 `comment:"商品总价"`
  43. OrderMoney float64 `comment:"订单总价"`
  44. Point int `comment:"订单消耗积分"`
  45. PointMoney float64 `comment:"订单消耗积分抵多少钱"`
  46. PayMoney float64 `comment:"订单实付金额"`
  47. RefundTime time.Time `comment:"订单退款时间"`
  48. RefundMoney float64 `comment:"订单退款金额"`
  49. OrderStatus int `comment:"订单状态,0:已取消、1:待支付、2:已支付、3:已退款"`
  50. PayTime time.Time `comment:"订单付款时间"`
  51. SourceId int `comment:"来源ID"`
  52. Source string `comment:"来源\n报告 :article\n活动 :activity"`
  53. SourceTitle string `comment:"来源名称,活动或者报告标题"`
  54. UserId int `comment:"用户ID"`
  55. Mobile string `comment:"手机号"`
  56. Email string `comment:"邮箱"`
  57. CompanyId int `comment:"公司ID"`
  58. CompanyName string `comment:"公司名称"`
  59. RealName string `comment:"用户实际名称"`
  60. SellerName string `comment:"所属销售"`
  61. SellerId int `comment:"所属销售Id"`
  62. CreateTime time.Time `comment:"创建时间"`
  63. ModifyTime time.Time `comment:"修改时间"`
  64. RegisterPlatform int `comment:"来源 1小程序,2:网页"`
  65. OrderType int `comment:"订单类型,1:畅读卡订单,2:单场付费订单"`
  66. ShareId int `comment:"分享人ID"`
  67. ShareName string `comment:"分享人姓名"`
  68. StartDate time.Time `comment:"开始时间"`
  69. EndDate time.Time `comment:"结束时间"`
  70. }
  71. type CygxOrderResp struct {
  72. OrderId int `orm:"column(order_id);pk";comment:"订单id"`
  73. OrderCode string `comment:"订单编号"`
  74. OutTradeCode string `comment:"外部交易号"`
  75. PaymentType int `comment:"支付类型。取值范围:1微信支付,2支付宝支付。"`
  76. GoodsName string `comment:"商品名称"`
  77. GoodsId int `comment:"商品ID"`
  78. BuyerInvoice string `comment:"买家发票信息"`
  79. GoodsMoney float64 `comment:"商品总价"`
  80. OrderMoney float64 `comment:"订单总价"`
  81. Point int `comment:"订单消耗积分"`
  82. PointMoney float64 `comment:"订单消耗积分抵多少钱"`
  83. PayMoney float64 `comment:"订单实付金额"`
  84. RefundMoney float64 `comment:"订单退款金额"`
  85. OrderStatus int `comment:"订单状态,0:已取消、1:待支付、2:已支付、3:已退款"`
  86. PayTime time.Time `comment:"订单付款时间"`
  87. SourceId int `comment:"来源ID"`
  88. Source string `comment:"来源\n报告 :article\n活动 :activity"`
  89. SourceTitle string `comment:"来源名称,活动或者报告标题"`
  90. UserId int `comment:"用户ID"`
  91. Mobile string `comment:"手机号"`
  92. Email string `comment:"邮箱"`
  93. CompanyId int `comment:"公司ID"`
  94. CompanyName string `comment:"公司名称"`
  95. RealName string `comment:"用户实际名称"`
  96. SellerName string `comment:"所属销售"`
  97. CreateTime time.Time `comment:"创建时间"`
  98. ModifyTime time.Time `comment:"修改时间"`
  99. RegisterPlatform int `comment:"来源 1小程序,2:网页"`
  100. PlaySeconds string `description:"音视频时长"`
  101. OrderType int `comment:"订单类型,1:畅读卡订单,2:单场付费订单"`
  102. StartDate time.Time `comment:"开始时间"`
  103. EndDate time.Time `comment:"结束时间"`
  104. }
  105. type CygxOrderAction struct {
  106. ActionId int64 `orm:"column(action_id);pk"` // 动作id
  107. Action string // 动作内容
  108. OrderStatus int // 订单状态,0:已取消、1:待支付、2:已支付、3:已退款
  109. OrderStatusText string // 订单状态名称
  110. OrderCode string // 订单编号
  111. UserId int // 用户ID
  112. Mobile string // 手机号
  113. Email string // 邮箱
  114. CompanyId int // 公司ID
  115. CompanyName string // 公司名称
  116. RealName string // 用户实际名称
  117. SellerName string // 所属销售
  118. CreateTime time.Time // 创建时间
  119. ModifyTime time.Time // 修改时间
  120. RegisterPlatform int // 来源 1小程序,2:网页
  121. AdminId int // 管理员ID
  122. AdminName string // 管理员姓名
  123. }
  124. type OrderListResp struct {
  125. OrderCode string `comment:"订单编号"`
  126. OrderMoney float64 `comment:"订单总价"`
  127. CreateTime string `comment:"创建时间"`
  128. SourceId int `comment:"来源ID"`
  129. Source string `comment:"来源\n报告 :article\n活动 :activity"`
  130. SourceTitle string `comment:"来源名称,活动或者报告标题"`
  131. OrderStatus int `comment:"订单状态码"`
  132. OrderStatusText string `comment:"订单状态描述"`
  133. LabelKeywordImgLink string `comment:"标签关键词ico"`
  134. PlaySeconds string `description:"音视频时长"`
  135. StartDate string `comment:"开始日期"`
  136. EndDate string `comment:"结束日期"`
  137. }
  138. type UserOrderListResp struct {
  139. Paging *paging.PagingItem `description:"分页数据"`
  140. List []*OrderListResp
  141. }
  142. type PayEdOrderDetailResp struct {
  143. OrderCode string `comment:"订单编号"`
  144. OrderType int `comment:"订单类型,1:畅读卡订单,2:单场付费订单"`
  145. EndDate string `comment:"结束日期"`
  146. OrderStatus int `comment:"订单状态,0:已取消、1:待支付、2:已支付、3:已退款"`
  147. SourceId int `comment:"来源ID"`
  148. Source string `comment:"来源\n报告 :article\n活动 :activity"`
  149. IsPublicActivitie bool `comment:"是否是公开活动"`
  150. IsSignUp bool `comment:"是否自动报名"`
  151. }
  152. // 添加
  153. func AddCygxOrder(item *CygxOrder) (err error) {
  154. o, err := orm.NewOrm().Begin()
  155. if err != nil {
  156. return
  157. }
  158. defer func() {
  159. fmt.Println(err)
  160. if err == nil {
  161. o.Commit()
  162. } else {
  163. o.Rollback()
  164. }
  165. }()
  166. item.OrderStatus = 1 // 初始状态待支付
  167. itemOrderAction := new(CygxOrderAction)
  168. itemOrderAction.Action = "创建订单"
  169. itemOrderAction.OrderStatus = 1
  170. itemOrderAction.OrderStatusText = "待支付"
  171. itemOrderAction.OrderCode = item.OrderCode
  172. itemOrderAction.UserId = item.UserId
  173. itemOrderAction.Mobile = item.Mobile
  174. itemOrderAction.Email = item.Email
  175. itemOrderAction.CompanyId = item.CompanyId
  176. itemOrderAction.CompanyName = item.CompanyName
  177. itemOrderAction.RealName = item.RealName
  178. itemOrderAction.SellerName = item.SellerName
  179. itemOrderAction.CreateTime = time.Now()
  180. itemOrderAction.ModifyTime = time.Now()
  181. itemOrderAction.RegisterPlatform = item.RegisterPlatform
  182. _, err = o.Insert(item) //写入订单信息
  183. if err != nil {
  184. return
  185. }
  186. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  187. if err != nil {
  188. return
  189. }
  190. return
  191. }
  192. // 添加
  193. func CancelCygxOrder(item *CygxOrder) (err error) {
  194. o, err := orm.NewOrm().Begin()
  195. if err != nil {
  196. return
  197. }
  198. defer func() {
  199. fmt.Println(err)
  200. if err == nil {
  201. o.Commit()
  202. } else {
  203. o.Rollback()
  204. }
  205. }()
  206. itemOrderAction := new(CygxOrderAction)
  207. itemOrderAction.Action = "取消订单"
  208. itemOrderAction.OrderStatus = 0
  209. itemOrderAction.OrderStatusText = "已取消"
  210. itemOrderAction.OrderCode = item.OrderCode
  211. itemOrderAction.UserId = item.UserId
  212. itemOrderAction.Mobile = item.Mobile
  213. itemOrderAction.Email = item.Email
  214. itemOrderAction.CompanyId = item.CompanyId
  215. itemOrderAction.CompanyName = item.CompanyName
  216. itemOrderAction.RealName = item.RealName
  217. itemOrderAction.SellerName = item.SellerName
  218. itemOrderAction.CreateTime = time.Now()
  219. itemOrderAction.ModifyTime = time.Now()
  220. itemOrderAction.RegisterPlatform = item.RegisterPlatform
  221. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  222. if err != nil {
  223. return
  224. }
  225. updateParams := make(map[string]interface{})
  226. updateParams["OrderStatus"] = 0
  227. updateParams["ModifyTime"] = item.ModifyTime
  228. ptrStructOrTableName := "cygx_order"
  229. whereParam := map[string]interface{}{"order_code": item.OrderCode}
  230. qs := o.QueryTable(ptrStructOrTableName)
  231. for expr, exprV := range whereParam {
  232. qs = qs.Filter(expr, exprV)
  233. }
  234. _, err = qs.Update(updateParams)
  235. if err != nil {
  236. return
  237. }
  238. return
  239. }
  240. // 根据订单编号修改
  241. func UpdateCygxOrder(item *CygxOrder, oldOrderCode string) (err error) {
  242. o := orm.NewOrm()
  243. updateParams := make(map[string]interface{})
  244. updateParams["SourceId"] = item.SourceId
  245. updateParams["Source"] = item.Source
  246. updateParams["SourceTitle"] = item.SourceTitle
  247. updateParams["OutTradeNo"] = item.OutTradeNo
  248. updateParams["GoodsMoney"] = item.GoodsMoney
  249. updateParams["OrderMoney"] = item.OrderMoney
  250. updateParams["ShareId"] = item.ShareId
  251. updateParams["ShareName"] = item.ShareName
  252. updateParams["ModifyTime"] = item.ModifyTime
  253. ptrStructOrTableName := "cygx_order"
  254. whereParam := map[string]interface{}{"order_code": oldOrderCode}
  255. qs := o.QueryTable(ptrStructOrTableName)
  256. for expr, exprV := range whereParam {
  257. qs = qs.Filter(expr, exprV)
  258. }
  259. _, err = qs.Update(updateParams)
  260. if err != nil {
  261. return
  262. }
  263. return
  264. }
  265. func GetCygxOrderList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxOrderResp, err error) {
  266. o := orm.NewOrm()
  267. sql := `SELECT *
  268. FROM
  269. cygx_order
  270. WHERE 1 = 1 ` + condition
  271. sql += ` LIMIT ?,? `
  272. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  273. return
  274. }
  275. func GetCygxOrderDetailList(condition string, pars []interface{}) (items []*CygxOrder, err error) {
  276. o := orm.NewOrm()
  277. sql := `SELECT *
  278. FROM
  279. cygx_order
  280. WHERE 1 = 1 ` + condition
  281. _, err = o.Raw(sql, pars).QueryRows(&items)
  282. return
  283. }
  284. // 根据订单编号获取订单详情
  285. func GetCygxOrderDetailByOrderCode(orderCode string) (item *CygxOrder, err error) {
  286. o := orm.NewOrm()
  287. sql := `SELECT * FROM cygx_order WHERE order_code = ? `
  288. err = o.Raw(sql, orderCode).QueryRow(&item)
  289. return
  290. }
  291. // 根据订单编号获取订单详情
  292. func GetCygxOrderDetailByOutTradeNo(outTradeNo string) (item *CygxOrder, err error) {
  293. o := orm.NewOrm()
  294. sql := `SELECT * FROM cygx_order WHERE out_trade_no = ? `
  295. err = o.Raw(sql, outTradeNo).QueryRow(&item)
  296. return
  297. }
  298. // 获取数量
  299. func GetCygxOrderCount(condition string, pars []interface{}) (count int, err error) {
  300. o := orm.NewOrm()
  301. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_order WHERE 1= 1 ` + condition
  302. err = o.Raw(sqlCount, pars).QueryRow(&count)
  303. return
  304. }