charge_bill.go 13 KB

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