order.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 WxNativeApiResp struct {
  9. CodeUrl string `comment:"微信支付二维码链接"`
  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. OutTradeCode string `comment:"外部交易号"`
  36. PaymentType int `comment:"支付类型。取值范围:1微信支付,2支付宝支付。"`
  37. GoodsName string `comment:"商品名称"`
  38. GoodsId int `comment:"商品ID"`
  39. BuyerInvoice string `comment:"买家发票信息"`
  40. GoodsMoney float64 `comment:"商品总价"`
  41. OrderMoney float64 `comment:"订单总价"`
  42. Point int `comment:"订单消耗积分"`
  43. PointMoney float64 `comment:"订单消耗积分抵多少钱"`
  44. PayMoney float64 `comment:"订单实付金额"`
  45. RefundTime time.Time `comment:"订单退款时间"`
  46. RefundMoney float64 `comment:"订单退款金额"`
  47. OrderStatus int `comment:"订单状态,0:已取消、1:待支付、2:已支付、3:已退款"`
  48. PayTime time.Time `comment:"订单付款时间"`
  49. SourceId int `comment:"来源ID"`
  50. Source string `comment:"来源\n报告 :article\n活动 :activity"`
  51. SourceTitle string `comment:"来源名称,活动或者报告标题"`
  52. UserId int `comment:"用户ID"`
  53. Mobile string `comment:"手机号"`
  54. Email string `comment:"邮箱"`
  55. CompanyId int `comment:"公司ID"`
  56. CompanyName string `comment:"公司名称"`
  57. RealName string `comment:"用户实际名称"`
  58. SellerName string `comment:"所属销售"`
  59. SellerId int `comment:"所属销售Id"`
  60. CreateTime time.Time `comment:"创建时间"`
  61. ModifyTime time.Time `comment:"修改时间"`
  62. RegisterPlatform int `comment:"来源 1小程序,2:网页"`
  63. OrderType int `comment:"订单类型,1:畅读卡订单,2:单场付费订单"`
  64. ShareId int `comment:"分享人ID"`
  65. ShareName string `comment:"分享人姓名"`
  66. StartDate time.Time `comment:"开始时间"`
  67. EndDate time.Time `comment:"结束时间"`
  68. }
  69. type CygxOrderResp struct {
  70. OrderId int `orm:"column(order_id);pk";comment:"订单id"`
  71. OrderCode string `comment:"订单编号"`
  72. OutTradeCode string `comment:"外部交易号"`
  73. PaymentType int `comment:"支付类型。取值范围:1微信支付,2支付宝支付。"`
  74. GoodsName string `comment:"商品名称"`
  75. GoodsId int `comment:"商品ID"`
  76. BuyerInvoice string `comment:"买家发票信息"`
  77. GoodsMoney float64 `comment:"商品总价"`
  78. OrderMoney float64 `comment:"订单总价"`
  79. Point int `comment:"订单消耗积分"`
  80. PointMoney float64 `comment:"订单消耗积分抵多少钱"`
  81. PayMoney float64 `comment:"订单实付金额"`
  82. RefundMoney float64 `comment:"订单退款金额"`
  83. OrderStatus int `comment:"订单状态,0:已取消、1:待支付、2:已支付、3:已退款"`
  84. PayTime time.Time `comment:"订单付款时间"`
  85. SourceId int `comment:"来源ID"`
  86. Source string `comment:"来源\n报告 :article\n活动 :activity"`
  87. SourceTitle string `comment:"来源名称,活动或者报告标题"`
  88. UserId int `comment:"用户ID"`
  89. Mobile string `comment:"手机号"`
  90. Email string `comment:"邮箱"`
  91. CompanyId int `comment:"公司ID"`
  92. CompanyName string `comment:"公司名称"`
  93. RealName string `comment:"用户实际名称"`
  94. SellerName string `comment:"所属销售"`
  95. CreateTime time.Time `comment:"创建时间"`
  96. ModifyTime time.Time `comment:"修改时间"`
  97. RegisterPlatform int `comment:"来源 1小程序,2:网页"`
  98. StartDate time.Time `comment:"开始时间"`
  99. EndDate time.Time `comment:"结束时间"`
  100. }
  101. type CygxOrderAction struct {
  102. ActionId int64 `orm:"column(action_id);pk"` // 动作id
  103. Action string // 动作内容
  104. OrderStatus int // 订单状态,0:已取消、1:待支付、2:已支付、3:已退款
  105. OrderStatusText string // 订单状态名称
  106. OrderCode string // 订单编号
  107. UserId int // 用户ID
  108. Mobile string // 手机号
  109. Email string // 邮箱
  110. CompanyId int // 公司ID
  111. CompanyName string // 公司名称
  112. RealName string // 用户实际名称
  113. SellerName string // 所属销售
  114. CreateTime time.Time // 创建时间
  115. ModifyTime time.Time // 修改时间
  116. RegisterPlatform int // 来源 1小程序,2:网页
  117. AdminId int // 管理员ID
  118. AdminName string // 管理员姓名
  119. }
  120. type OrderListResp struct {
  121. OrderCode string `comment:"订单编号"`
  122. OrderMoney float64 `comment:"订单总价"`
  123. CreateTime string `comment:"创建时间"`
  124. SourceId int `comment:"来源ID"`
  125. Source string `comment:"来源\n报告 :article\n活动 :activity"`
  126. SourceTitle string `comment:"来源名称,活动或者报告标题"`
  127. OrderStatus int `comment:"订单状态码"`
  128. OrderStatusText string `comment:"订单状态描述"`
  129. LabelKeywordImgLink string `comment:"标签关键词ico"`
  130. StartDate string `comment:"开始日期"`
  131. EndDate string `comment:"结束日期"`
  132. }
  133. type UserOrderListResp struct {
  134. Paging *paging.PagingItem `description:"分页数据"`
  135. List []*OrderListResp
  136. }
  137. type PayEdOrderDetailResp struct {
  138. OrderCode string `comment:"订单编号"`
  139. OrderType int `comment:"订单类型,1:畅读卡订单,2:单场付费订单"`
  140. EndDate string `comment:"结束日期"`
  141. OrderStatus int `comment:"订单状态,0:已取消、1:待支付、2:已支付、3:已退款"`
  142. SourceId int `comment:"来源ID"`
  143. Source string `comment:"来源\n报告 :article\n活动 :activity"`
  144. }
  145. // 添加
  146. func AddCygxOrder(item *CygxOrder) (err error) {
  147. o, err := orm.NewOrm().Begin()
  148. if err != nil {
  149. return
  150. }
  151. defer func() {
  152. fmt.Println(err)
  153. if err == nil {
  154. o.Commit()
  155. } else {
  156. o.Rollback()
  157. }
  158. }()
  159. item.OrderStatus = 1 // 初始状态待支付
  160. itemOrderAction := new(CygxOrderAction)
  161. itemOrderAction.Action = "创建订单"
  162. itemOrderAction.OrderStatus = 1
  163. itemOrderAction.OrderStatusText = "待支付"
  164. itemOrderAction.OrderCode = item.OrderCode
  165. itemOrderAction.UserId = item.UserId
  166. itemOrderAction.Mobile = item.Mobile
  167. itemOrderAction.Email = item.Email
  168. itemOrderAction.CompanyId = item.CompanyId
  169. itemOrderAction.CompanyName = item.CompanyName
  170. itemOrderAction.RealName = item.RealName
  171. itemOrderAction.SellerName = item.SellerName
  172. itemOrderAction.CreateTime = time.Now()
  173. itemOrderAction.ModifyTime = time.Now()
  174. itemOrderAction.RegisterPlatform = item.RegisterPlatform
  175. _, err = o.Insert(item) //写入订单信息
  176. if err != nil {
  177. return
  178. }
  179. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  180. if err != nil {
  181. return
  182. }
  183. return
  184. }
  185. // 添加
  186. func CancelCygxOrder(item *CygxOrder) (err error) {
  187. o, err := orm.NewOrm().Begin()
  188. if err != nil {
  189. return
  190. }
  191. defer func() {
  192. fmt.Println(err)
  193. if err == nil {
  194. o.Commit()
  195. } else {
  196. o.Rollback()
  197. }
  198. }()
  199. itemOrderAction := new(CygxOrderAction)
  200. itemOrderAction.Action = "取消订单"
  201. itemOrderAction.OrderStatus = 0
  202. itemOrderAction.OrderStatusText = "已取消"
  203. itemOrderAction.OrderCode = item.OrderCode
  204. itemOrderAction.UserId = item.UserId
  205. itemOrderAction.Mobile = item.Mobile
  206. itemOrderAction.Email = item.Email
  207. itemOrderAction.CompanyId = item.CompanyId
  208. itemOrderAction.CompanyName = item.CompanyName
  209. itemOrderAction.RealName = item.RealName
  210. itemOrderAction.SellerName = item.SellerName
  211. itemOrderAction.CreateTime = time.Now()
  212. itemOrderAction.ModifyTime = time.Now()
  213. itemOrderAction.RegisterPlatform = item.RegisterPlatform
  214. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  215. if err != nil {
  216. return
  217. }
  218. updateParams := make(map[string]interface{})
  219. updateParams["OrderStatus"] = 0
  220. updateParams["ModifyTime"] = item.ModifyTime
  221. ptrStructOrTableName := "cygx_order"
  222. whereParam := map[string]interface{}{"order_code": item.OrderCode}
  223. qs := o.QueryTable(ptrStructOrTableName)
  224. for expr, exprV := range whereParam {
  225. qs = qs.Filter(expr, exprV)
  226. }
  227. _, err = qs.Update(updateParams)
  228. if err != nil {
  229. return
  230. }
  231. return
  232. }
  233. // 根据订单编号修改
  234. func UpdateCygxOrder(item *CygxOrder, oldOrderCode string) (err error) {
  235. o := orm.NewOrm()
  236. updateParams := make(map[string]interface{})
  237. updateParams["SourceId"] = item.SourceId
  238. updateParams["Source"] = item.Source
  239. updateParams["SourceTitle"] = item.SourceTitle
  240. updateParams["ModifyTime"] = item.ModifyTime
  241. ptrStructOrTableName := "cygx_order"
  242. whereParam := map[string]interface{}{"order_code": oldOrderCode}
  243. qs := o.QueryTable(ptrStructOrTableName)
  244. for expr, exprV := range whereParam {
  245. qs = qs.Filter(expr, exprV)
  246. }
  247. _, err = qs.Update(updateParams)
  248. if err != nil {
  249. return
  250. }
  251. return
  252. }
  253. func GetCygxOrderList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxOrderResp, err error) {
  254. o := orm.NewOrm()
  255. sql := `SELECT *
  256. FROM
  257. cygx_order
  258. WHERE 1 = 1 ` + condition
  259. sql += ` LIMIT ?,? `
  260. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  261. return
  262. }
  263. // 根据订单编号获取订单详情
  264. func GetCygxOrderDetailByOrderCode(orderCode string) (item *CygxOrder, err error) {
  265. o := orm.NewOrm()
  266. sql := `SELECT * FROM cygx_order WHERE order_code = ? `
  267. err = o.Raw(sql, orderCode).QueryRow(&item)
  268. return
  269. }
  270. // 获取数量
  271. func GetCygxOrderCount(condition string, pars []interface{}) (count int, err error) {
  272. o := orm.NewOrm()
  273. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_order WHERE 1= 1 ` + condition
  274. err = o.Raw(sqlCount, pars).QueryRow(&count)
  275. return
  276. }