charge_order.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "git.getensh.com/common/gopkgs/util"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. "time"
  11. )
  12. type TChargeOrder struct {
  13. OrderId string `gorm:"column:order_id;PRIMARY_KEY" json:"order_id"`
  14. Comment string `gorm:"column:comment" json:"comment"`
  15. Amount int64 `gorm:"column:amount" json:"amount"`
  16. PayAmount int64 `gorm:"column:pay_amount" json:"pay_amount"`
  17. PayType int32 `gorm:"column:pay_type" json:"pay_type"`
  18. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  19. Uid int64 `gorm:"column:uid" json:"uid"`
  20. PackageId int64 `gorm:"column:package_id" json:"package_id"`
  21. PrepayId string `gorm:"column:prepay_id" json:"prepay_id"`
  22. TransactionId string `gorm:"column:transaction_id" json:"transaction_id"`
  23. Status int64 `gorm:"column:status" json:"status"`
  24. PayingDesc string `gorm:"column:paying_desc" json:"paying_desc"`
  25. PayTime int64 `gorm:"column:pay_time" json:"pay_time"`
  26. BillIds string `gorm:"column:bill_ids" json:"bill_ids"`
  27. ObjName string `gorm:"column:obj_name" json:"obj_name"`
  28. OnlinePay int32 `gorm:"column:online_pay" json:"online_pay"`
  29. OnlinePayBill string `gorm:"column:online_pay_bill" json:"online_pay_bill"`
  30. Deleted int32 `gorm:"column:deleted" json:"deleted"`
  31. table string
  32. }
  33. func (p *TChargeOrder) TableName() string {
  34. return p.table
  35. }
  36. func NewChargeOrder(database string) *TChargeOrder {
  37. return &TChargeOrder{table: fmt.Sprintf("%s.%s", database, "t_charge_order")}
  38. }
  39. func (p *TChargeOrder) SetTable(database string) {
  40. p.table = fmt.Sprintf("%s.%s", database, "t_charge_order")
  41. }
  42. func (p *TChargeOrder) CreateTable(db *gorm.DB) error {
  43. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  44. " `order_id` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '订单号',"+
  45. "`comment` varchar(512) COLLATE utf8mb4_bin NOT NULL COMMENT '备注',"+
  46. "`amount` int(10) NOT NULL COMMENT '总应收金额',"+
  47. "`pay_amount` int(10) NOT NULL COMMENT '实收金额',"+
  48. "`pay_type` tinyint(1) NOT NULL COMMENT '1 现金 2 微信 3 支付宝 4 pos 5 转账 6 小程序微信支付 7 小程序支付报支付 8 app 微信支付 9 app 支付宝支付',"+
  49. "`created_at` datetime NOT NULL,"+
  50. " `uid` bigint(11) NOT NULL COMMENT 'app或小程序用户id,线下收款该字段0',"+
  51. "`package_id` int(11) NOT NULL COMMENT '套餐id, app或小程序购买套餐时该字段不为0',"+
  52. "`prepay_id` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '线上支付三方预支付(如微信)标识',"+
  53. "`transaction_id` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '线上支付三方交易标识(如微信)',"+
  54. "`status` tinyint(1) NOT NULL COMMENT '1 待支付 2 已支付',"+
  55. "`paying_desc` text COLLATE utf8mb4_bin NOT NULL COMMENT '订单描述',"+
  56. "`pay_time` bigint(11) NOT NULL COMMENT '支付时间',"+
  57. "`bill_ids` text COLLATE utf8mb4_bin NOT NULL COMMENT '账单列表',"+
  58. "`online_pay` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0 非线上预存 1 线上预存',"+
  59. "`online_pay_bill` text COLLATE utf8mb4_bin NOT NULL COMMENT '线上预存费用信息',"+
  60. "`obj_name` varchar(16) COLLATE utf8mb4_bin NOT NULL COMMENT '房屋号/车位/车牌',"+
  61. "`deleted` tinyint(4) NOT NULL COMMENT '1 已删除',"+
  62. "PRIMARY KEY (`order_id`) USING BTREE,"+
  63. " KEY `uid` (`uid`) USING BTREE"+
  64. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT;"
  65. err := db.Exec(sql).Error
  66. if err != nil {
  67. logger.Error("mysql",
  68. zap.String("sql", "create table "+p.TableName()),
  69. zap.String("fields", ""),
  70. zap.String("error", err.Error()))
  71. }
  72. return err
  73. }
  74. func (p *TChargeOrder) Find(db *gorm.DB, where map[string]interface{}) error {
  75. err := db.Table(p.TableName()).Where(where).Find(p).Error
  76. if err != nil {
  77. fields, _ := util.MarshalToString(where)
  78. logger.Error("mysql",
  79. zap.String("sql", "select from "+p.TableName()),
  80. zap.String("fields", fields),
  81. zap.String("error", err.Error()))
  82. }
  83. return err
  84. }
  85. func (p *TChargeOrder) Last(db *gorm.DB) error {
  86. err := db.Table(p.TableName()).Last(p).Error
  87. if err != nil {
  88. logger.Error("mysql",
  89. zap.String("sql", "select last from "+p.TableName()),
  90. zap.String("fields", ""),
  91. zap.String("error", err.Error()))
  92. }
  93. return err
  94. }
  95. // Insert 插入一条记录
  96. func (p *TChargeOrder) Insert(db *gorm.DB) error {
  97. err := db.Table(p.TableName()).Create(p).Error
  98. if err != nil {
  99. fields, _ := util.MarshalToString(*p)
  100. logger.Error("mysql",
  101. zap.String("sql", "insert into "+p.TableName()),
  102. zap.String("fields", fields),
  103. zap.String("error", err.Error()))
  104. }
  105. return err
  106. }
  107. // Insert 插入多条记录
  108. func (p *TChargeOrder) InsertMulti(db *gorm.DB, values interface{}) error {
  109. err := db.Table(p.TableName()).Create(values).Error
  110. if err != nil {
  111. fields, _ := util.MarshalToString(*p)
  112. logger.Error("mysql",
  113. zap.String("sql", "insert into "+p.TableName()),
  114. zap.String("fields", fields),
  115. zap.String("error", err.Error()))
  116. }
  117. return err
  118. }
  119. func (p *TChargeOrder) Delete(db *gorm.DB, filter map[string]interface{}) error {
  120. cond, val, err := whereBuild(filter)
  121. if err != nil {
  122. return err
  123. }
  124. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  125. }
  126. func (p *TChargeOrder) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  127. cond, val, err := whereBuild(where)
  128. if err != nil {
  129. if err != nil {
  130. fields, _ := util.MarshalToString(values)
  131. logger.Error("mysql",
  132. zap.String("sql", "update "+p.TableName()),
  133. zap.String("fields", fields),
  134. zap.String("error", err.Error()))
  135. }
  136. return err
  137. }
  138. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  139. }
  140. func (p *TChargeOrder) UpdateByModel(db *gorm.DB) error {
  141. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  142. if err != nil {
  143. fields, _ := util.MarshalToString(*p)
  144. logger.Error("mysql",
  145. zap.String("sql", "update "+p.TableName()),
  146. zap.String("fields", fields),
  147. zap.String("error", err.Error()))
  148. }
  149. return err
  150. }
  151. func (p *TChargeOrder) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  152. cond, val, err := whereBuildAndOr(where, or)
  153. if err != nil {
  154. return 0, err
  155. }
  156. ret := int64(0)
  157. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  158. if err != nil {
  159. fields, _ := util.MarshalToString(where)
  160. logger.Error("mysql",
  161. zap.String("sql", "select count "+p.TableName()),
  162. zap.String("fields", fields),
  163. zap.String("error", err.Error()))
  164. }
  165. return ret, err
  166. }
  167. func (p *TChargeOrder) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargeOrder, err error) {
  168. cond, val, err := whereBuildAndOr(where, or)
  169. if err != nil {
  170. return list, err
  171. }
  172. if pageSize < 0 {
  173. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  174. if result.Error != nil {
  175. wherefields, _ := util.MarshalToString(where)
  176. logger.Error("mysql",
  177. zap.String("sql", "select * from "+p.TableName()),
  178. zap.String("where", wherefields),
  179. zap.String("error", result.Error.Error()))
  180. }
  181. return list, result.Error
  182. }
  183. offset := (page - 1) * pageSize
  184. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  185. if result.Error != nil {
  186. wherefields, _ := util.MarshalToString(where)
  187. logger.Error("mysql",
  188. zap.String("sql", "select * from "+p.TableName()),
  189. zap.String("where", wherefields),
  190. zap.String("error", result.Error.Error()))
  191. }
  192. return list, result.Error
  193. }