charge_bill_del.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 TChargeBillDel struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. ObjType int32 `gorm:"column:obj_type" json:"obj_type"`
  15. ObjId int64 `gorm:"column:obj_id" json:"obj_id"`
  16. ChargeId int64 `gorm:"column:charge_id" json:"charge_id"`
  17. ChargeBindId int64 `gorm:"column:charge_bind_id" json:"charge_bind_id"`
  18. ChargeTimeType int32 `gorm:"column:charge_time_type" json:"charge_time_type"`
  19. ChargeDesc string `gorm:"column:charge_desc" json:"charge_desc"`
  20. Comment string `gorm:"column:comment" json:"comment"`
  21. Amount int64 `gorm:"column:amount" json:"amount"`
  22. ChargePowerId int64 `gorm:"column:charge_power_id" json:"charge_power_id"`
  23. ChargeType int32 `gorm:"column:charge_type" json:"charge_type"`
  24. ChargeName string `gorm:"column:charge_name" json:"charge_name"`
  25. Uid int64 `gorm:"column:uid" json:"uid"`
  26. PayMode int64 `gorm:"column:pay_mode" json:"pay_mode"`
  27. PackageId int64 `gorm:"column:package_id" json:"package_id"`
  28. ChargeEnd int64 `gorm:"column:charge_end" json:"charge_end"`
  29. ChargeStart int64 `gorm:"column:charge_start" json:"charge_start"`
  30. OrderId string `gorm:"column:order_id" json:"order_id"`
  31. PayTime int64 `gorm:"column:pay_time" json:"pay_time"`
  32. HouseId int64 `gorm:"column:house_id" json:"house_id"`
  33. ObjName string `gorm:"column:obj_name" json:"obj_name"`
  34. LateFee int64 `gorm:"column:late_fee" json:"late_fee"`
  35. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  36. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  37. PayType int32 `gorm:"column:pay_type" json:"pay_type"`
  38. Status int32 `gorm:"column:status" json:"status"`
  39. DelAt time.Time `gorm:"column:del_at" json:"del_at"`
  40. DelReason string `gorm:"column:del_reason" json:"del_reason"`
  41. table string
  42. }
  43. func (p *TChargeBillDel) TableName() string {
  44. return p.table
  45. }
  46. func NewChargeBillDel(database string) *TChargeBillDel {
  47. return &TChargeBillDel{table: fmt.Sprintf("%s.%s", database, "t_charge_bill_del")}
  48. }
  49. func (p *TChargeBillDel) SetTable(database string) {
  50. p.table = fmt.Sprintf("%s.%s", database, "t_charge_bill_del")
  51. }
  52. func (p *TChargeBillDel) CreateTable(db *gorm.DB) error {
  53. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  54. "`id` bigint(11) NOT NULL AUTO_INCREMENT,"+
  55. "`obj_type` tinyint(1) NOT NULL COMMENT '1 房屋 2 车位 3 车辆',"+
  56. "`obj_id` bigint(11) NOT NULL COMMENT '房屋或车位或车辆',"+
  57. "`charge_id` int(10) NOT NULL COMMENT '费用项目id',"+
  58. "`charge_bind_id` bigint(11) NOT NULL COMMENT '费用绑定关系id',"+
  59. "`charge_time_type` tinyint(1) NOT NULL COMMENT '1 周期 2 一次性',"+
  60. "`charge_desc` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '费用描述',"+
  61. "`comment` varchar(512) COLLATE utf8mb4_bin NOT NULL COMMENT '备注',"+
  62. "`amount` int(10) NOT NULL COMMENT '应收金额(不包含滞纳金)',"+
  63. "`charge_power_id` bigint(11) NOT NULL COMMENT '抄表记录id',"+
  64. "`charge_type` int(10) NOT NULL COMMENT '费用类型',"+
  65. "`pay_type` tinyint(1) NOT NULL COMMENT '1 现金 2 微信 3 支付宝 4 pos 5 转账 6 小程序微信支付 7 小程序支付报支付 8 app 微信支付 9 app 支付宝支付',"+
  66. "`charge_name` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '费用名',"+
  67. "`uid` bigint(11) NOT NULL COMMENT 'app或小程序用户id,线下收款该字段0',"+
  68. "`pay_mode` tinyint(1) NOT NULL COMMENT '1 欠费缴费 2 预存',"+
  69. "`package_id` int(11) NOT NULL COMMENT '套餐id app或小程序购买套餐时该字段不为0',"+
  70. "`charge_end` bigint(11) NOT NULL COMMENT '账单结束时间戳',"+
  71. "`charge_start` bigint(11) NOT NULL COMMENT '账单开始时间戳',"+
  72. "`order_id` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '订单号',"+
  73. "`pay_time` bigint(11) NOT NULL COMMENT '支付时间',"+
  74. "`house_id` bigint(11) NOT NULL COMMENT '房屋',"+
  75. "`obj_name` varchar(16) COLLATE utf8mb4_bin NOT NULL COMMENT '房屋号/车位/车牌',"+
  76. "`late_fee` int(10) NOT NULL COMMENT '滞纳金',"+
  77. "`created_at` datetime NOT NULL,"+
  78. " `updated_at` datetime NOT NULL,"+
  79. "`status` tinyint(1) NOT NULL COMMENT '1 待支付 2 已支付',"+
  80. "`del_at` datetime NOT NULL COMMENT '作废时间',"+
  81. "`del_reason` text COLLATE utf8mb4_bin NOT NULL COMMENT '作废原因',"+
  82. "PRIMARY KEY (`id`) USING BTREE,"+
  83. " UNIQUE KEY `charge_bind_id` (`charge_bind_id`,`charge_end`) USING BTREE,"+
  84. " KEY `house_id` (`obj_id`) USING BTREE,"+
  85. " KEY `charge_id` (`charge_id`) USING BTREE,"+
  86. " KEY `uid` (`uid`) USING BTREE,"+
  87. " KEY `order` (`order_id`) USING BTREE,"+
  88. "KEY `charge_name` (`charge_name`) USING BTREE,"+
  89. "KEY `obj_name` (`obj_name`) USING BTREE"+
  90. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT COMMENT='账单表';"
  91. err := db.Exec(sql).Error
  92. if err != nil {
  93. logger.Error("mysql",
  94. zap.String("sql", "create table "+p.TableName()),
  95. zap.String("fields", ""),
  96. zap.String("error", err.Error()))
  97. }
  98. return err
  99. }
  100. func (p *TChargeBillDel) Find(db *gorm.DB, where map[string]interface{}) error {
  101. err := db.Table(p.TableName()).Where(where).Find(p).Error
  102. if err != nil {
  103. fields, _ := util.MarshalToString(where)
  104. logger.Error("mysql",
  105. zap.String("sql", "select from "+p.TableName()),
  106. zap.String("fields", fields),
  107. zap.String("error", err.Error()))
  108. }
  109. return err
  110. }
  111. func (p *TChargeBillDel) Last(db *gorm.DB) error {
  112. err := db.Table(p.TableName()).Last(p).Error
  113. if err != nil {
  114. logger.Error("mysql",
  115. zap.String("sql", "select last from "+p.TableName()),
  116. zap.String("fields", ""),
  117. zap.String("error", err.Error()))
  118. }
  119. return err
  120. }
  121. // Insert 插入一条记录
  122. func (p *TChargeBillDel) Insert(db *gorm.DB) error {
  123. err := db.Table(p.TableName()).Create(p).Error
  124. if err != nil {
  125. fields, _ := util.MarshalToString(*p)
  126. logger.Error("mysql",
  127. zap.String("sql", "insert into "+p.TableName()),
  128. zap.String("fields", fields),
  129. zap.String("error", err.Error()))
  130. }
  131. return err
  132. }
  133. // Insert 插入多条记录
  134. func (p *TChargeBillDel) InsertMulti(db *gorm.DB, values interface{}) error {
  135. err := db.Table(p.TableName()).Create(values).Error
  136. if err != nil {
  137. fields, _ := util.MarshalToString(*p)
  138. logger.Error("mysql",
  139. zap.String("sql", "insert into "+p.TableName()),
  140. zap.String("fields", fields),
  141. zap.String("error", err.Error()))
  142. }
  143. return err
  144. }
  145. func (p *TChargeBillDel) Delete(db *gorm.DB, filter map[string]interface{}) error {
  146. cond, val, err := whereBuild(filter)
  147. if err != nil {
  148. return err
  149. }
  150. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  151. }
  152. func (p *TChargeBillDel) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  153. cond, val, err := whereBuild(where)
  154. if err != nil {
  155. if err != nil {
  156. fields, _ := util.MarshalToString(values)
  157. logger.Error("mysql",
  158. zap.String("sql", "update "+p.TableName()),
  159. zap.String("fields", fields),
  160. zap.String("error", err.Error()))
  161. }
  162. return err
  163. }
  164. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  165. }
  166. func (p *TChargeBillDel) UpdateByModel(db *gorm.DB) error {
  167. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  168. if err != nil {
  169. fields, _ := util.MarshalToString(*p)
  170. logger.Error("mysql",
  171. zap.String("sql", "update "+p.TableName()),
  172. zap.String("fields", fields),
  173. zap.String("error", err.Error()))
  174. }
  175. return err
  176. }
  177. func (p *TChargeBillDel) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  178. cond, val, err := whereBuildAndOr(where, or)
  179. if err != nil {
  180. return 0, err
  181. }
  182. ret := int64(0)
  183. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  184. if err != nil {
  185. fields, _ := util.MarshalToString(where)
  186. logger.Error("mysql",
  187. zap.String("sql", "select count "+p.TableName()),
  188. zap.String("fields", fields),
  189. zap.String("error", err.Error()))
  190. }
  191. return ret, err
  192. }
  193. func (p *TChargeBillDel) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargeBillDel, err error) {
  194. cond, val, err := whereBuildAndOr(where, or)
  195. if err != nil {
  196. return list, err
  197. }
  198. if pageSize < 0 {
  199. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  200. if result.Error != nil {
  201. wherefields, _ := util.MarshalToString(where)
  202. logger.Error("mysql",
  203. zap.String("sql", "select * from "+p.TableName()),
  204. zap.String("where", wherefields),
  205. zap.String("error", result.Error.Error()))
  206. }
  207. return list, result.Error
  208. }
  209. offset := (page - 1) * pageSize
  210. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  211. if result.Error != nil {
  212. wherefields, _ := util.MarshalToString(where)
  213. logger.Error("mysql",
  214. zap.String("sql", "select * from "+p.TableName()),
  215. zap.String("where", wherefields),
  216. zap.String("error", result.Error.Error()))
  217. }
  218. return list, result.Error
  219. }
  220. func (p *TChargeBillDel) SelectGroup(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, selectStr string, group string, page int, pageSize int, result interface{}) (err error) {
  221. cond, val, err := whereBuildAndOr(where, or)
  222. if err != nil {
  223. return err
  224. }
  225. if pageSize < 0 {
  226. result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Group(group).Find(result)
  227. if result.Error != nil {
  228. wherefields, _ := util.MarshalToString(where)
  229. logger.Error("mysql",
  230. zap.String("sql", "select "+selectStr+" from "+p.TableName()),
  231. zap.String("where", wherefields),
  232. zap.String("error", result.Error.Error()))
  233. }
  234. return result.Error
  235. }
  236. offset := (page - 1) * pageSize
  237. r := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Group(group).Limit(pageSize).Offset(offset).Find(result)
  238. if r.Error != nil {
  239. wherefields, _ := util.MarshalToString(where)
  240. logger.Error("mysql",
  241. zap.String("sql", "select * from "+p.TableName()),
  242. zap.String("where", wherefields),
  243. zap.String("error", r.Error.Error()))
  244. }
  245. return r.Error
  246. }
  247. func (p *TChargeBillDel) CountGroup(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, group string) (int64, error) {
  248. cond, val, err := whereBuildAndOr(where, or)
  249. if err != nil {
  250. return 0, err
  251. }
  252. sql := fmt.Sprintf("select count(*) as count from (select count(*) from %s group by %s) t1", p.TableName(), group)
  253. if len(cond) > 0 {
  254. sql = fmt.Sprintf("select count(*) as count from (select count(*) from %s where %s group by %s) t1", p.TableName(), cond, group)
  255. }
  256. type Result struct {
  257. Count int64
  258. }
  259. array := []Result{}
  260. err = db.Raw(sql, val...).Scan(&array).Error
  261. if err != nil {
  262. fields, _ := util.MarshalToString(where)
  263. logger.Error("mysql",
  264. zap.String("sql", "select count "+p.TableName()),
  265. zap.String("fields", fields),
  266. zap.String("error", err.Error()))
  267. return 0, err
  268. }
  269. if len(array) == 0 {
  270. return 0, nil
  271. }
  272. return array[0].Count, err
  273. }
  274. func (p *TChargeBillDel) Select(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, selectStr string, page int, pageSize int, result interface{}) (err error) {
  275. cond, val, err := whereBuildAndOr(where, or)
  276. if err != nil {
  277. return err
  278. }
  279. if pageSize < 0 {
  280. result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Find(result)
  281. if result.Error != nil {
  282. wherefields, _ := util.MarshalToString(where)
  283. logger.Error("mysql",
  284. zap.String("sql", "select "+selectStr+" from "+p.TableName()),
  285. zap.String("where", wherefields),
  286. zap.String("error", result.Error.Error()))
  287. }
  288. return result.Error
  289. }
  290. offset := (page - 1) * pageSize
  291. r := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Limit(pageSize).Offset(offset).Find(result)
  292. if r.Error != nil {
  293. wherefields, _ := util.MarshalToString(where)
  294. logger.Error("mysql",
  295. zap.String("sql", "select * from "+p.TableName()),
  296. zap.String("where", wherefields),
  297. zap.String("error", r.Error.Error()))
  298. }
  299. return r.Error
  300. }