charge_conf.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 TChargeConf struct {
  13. ID int64 `gorm:"column:id" json:"id"`
  14. ChargeType int32 `gorm:"column:charge_type" json:"charge_type"`
  15. ChargeName string `gorm:"column:charge_name" json:"charge_name"`
  16. ChargeTimeType int32 `gorm:"column:charge_time_type" json:"charge_time_type"`
  17. ChargeBasis int32 `gorm:"column:charge_basis" json:"charge_basis"`
  18. FixAmount int64 `gorm:"column:fix_amount" json:"fix_amount"`
  19. FixAmountName string `gorm:"column:fix_amount_name" json:"fix_amount_name"`
  20. UnitPrice int64 `gorm:"column:unit_price" json:"unit_price"`
  21. BillPeriod int32 `gorm:"column:bill_period" json:"bill_period"`
  22. BillPeriodType int32 `gorm:"column:bill_period_type" json:"bill_period_type"`
  23. ChargeEffectiveTime int64 `gorm:"column:charge_effective_time" json:"charge_effective_time"`
  24. LateFeeDay int64 `gorm:"column:late_fee_day" json:"late_fee_day"`
  25. LateFeeMaxDays int64 `gorm:"column:late_fee_max_days" json:"late_fee_max_days"`
  26. LateFeePercent int64 `gorm:"column:late_fee_percent" json:"late_fee_percent"`
  27. LateFeeEnable int32 `gorm:"column:late_fee_enable" json:"late_fee_enable"`
  28. BillLastTime int64 `gorm:"column:bill_last_time" json:"bill_last_time"`
  29. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  30. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  31. table string
  32. }
  33. func (p *TChargeConf) TableName() string {
  34. return p.table
  35. }
  36. func NewChargeConf(database string) *TChargeConf {
  37. return &TChargeConf{table: fmt.Sprintf("%s.%s", database, "t_charge_conf")}
  38. }
  39. func (p *TChargeConf) SetTable(database string) {
  40. p.table = fmt.Sprintf("%s.%s", database, "t_charge_conf")
  41. }
  42. func (p *TChargeConf) CreateTable(db *gorm.DB) error {
  43. sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
  44. "`id` int(10) NOT NULL AUTO_INCREMENT," +
  45. "`charge_type` int(10) NOT NULL COMMENT '费用类型1 物业费 2 水费 3 电费 4 气费 5 车位管理费 6 月租停车费 99 其他'," +
  46. "`charge_name` varchar(128) COLLATE utf8mb4_bin NOT NULL," +
  47. " `charge_time_type` tinyint(1) NOT NULL COMMENT ' 1.周期收费 2 单次收费'," +
  48. "`charge_basis` tinyint(1) NOT NULL COMMENT '1 按房屋面积 2 按使用面积 3 按车位面积 4 按使用量 5 固定费用 6 自定义'," +
  49. "`fix_amount` int(10) NOT NULL COMMENT '固定费用 分'," +
  50. "`fix_amount_name` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '固定费用别名'," +
  51. "`unit_price` int(10) NOT NULL COMMENT '单价'," +
  52. "`bill_period` int(10) NOT NULL COMMENT '账单生成周期 1 月 2 季度 3 半年 4 一年'," +
  53. "`bill_period_type` tinyint(1) NOT NULL COMMENT '账单周期计算方式1 自然周期 2 按费用生效时间'," +
  54. "`charge_effective_time` bigint(11) NOT NULL COMMENT '费用生效时间'," +
  55. "`late_fee_day` int(10) NOT NULL COMMENT '账单生成后xx天收滞纳金'," +
  56. "`late_fee_max_days` int(10) NOT NULL COMMENT '滞纳金封顶天数'," +
  57. "`late_fee_percent` decimal(18,2) NOT NULL COMMENT '滞纳金比例 1 代表 1/1000'," +
  58. "`late_fee_enable` tinyint(1) NOT NULL COMMENT '1 滞纳金启用 2 滞纳金禁用'," +
  59. "`bill_last_time` bigint(11) NOT NULL COMMENT '账单上次生成时间'," +
  60. "`updated_at` datetime NOT NULL," +
  61. " `created_at` datetime NOT NULL," +
  62. " PRIMARY KEY (`id`) USING BTREE" +
  63. ") ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT COMMENT='费用项目设置';"
  64. err := db.Exec(sql).Error
  65. if err != nil {
  66. logger.Error("mysql",
  67. zap.String("sql", "create table "+p.TableName()),
  68. zap.String("fields", ""),
  69. zap.String("error", err.Error()))
  70. }
  71. return err
  72. }
  73. func (p *TChargeConf) Find(db *gorm.DB, where map[string]interface{}) error {
  74. err := db.Table(p.TableName()).Where(where).First(p).Error
  75. if err != nil {
  76. fields, _ := util.MarshalToString(where)
  77. logger.Error("mysql",
  78. zap.String("sql", "select from "+p.TableName()),
  79. zap.String("fields", fields),
  80. zap.String("error", err.Error()))
  81. }
  82. return err
  83. }
  84. func (p *TChargeConf) Last(db *gorm.DB) error {
  85. err := db.Table(p.TableName()).Last(p).Error
  86. if err != nil {
  87. logger.Error("mysql",
  88. zap.String("sql", "select last from "+p.TableName()),
  89. zap.String("fields", ""),
  90. zap.String("error", err.Error()))
  91. }
  92. return err
  93. }
  94. // Insert 插入一条记录
  95. func (p *TChargeConf) Insert(db *gorm.DB) error {
  96. err := db.Table(p.TableName()).Create(p).Error
  97. if err != nil {
  98. fields, _ := util.MarshalToString(*p)
  99. logger.Error("mysql",
  100. zap.String("sql", "insert into "+p.TableName()),
  101. zap.String("fields", fields),
  102. zap.String("error", err.Error()))
  103. }
  104. return err
  105. }
  106. func (p *TChargeConf) Delete(db *gorm.DB, filter map[string]interface{}) error {
  107. err := db.Table(p.TableName()).Where(filter).Delete(p).Error
  108. if err != nil {
  109. fields, _ := util.MarshalToString(filter)
  110. logger.Error("mysql",
  111. zap.String("sql", "delete from "+p.TableName()),
  112. zap.String("where", fields),
  113. zap.String("error", err.Error()))
  114. }
  115. return err
  116. }
  117. func (p *TChargeConf) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  118. cond, val, err := whereBuild(where)
  119. if err != nil {
  120. if err != nil {
  121. fields, _ := util.MarshalToString(values)
  122. logger.Error("mysql",
  123. zap.String("sql", "update "+p.TableName()),
  124. zap.String("fields", fields),
  125. zap.String("error", err.Error()))
  126. }
  127. return err
  128. }
  129. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  130. }
  131. func (p *TChargeConf) UpdateByModel(db *gorm.DB) error {
  132. err := db.Model(p).Updates(p).Error
  133. if err != nil {
  134. fields, _ := util.MarshalToString(*p)
  135. logger.Error("mysql",
  136. zap.String("sql", "update "+p.TableName()),
  137. zap.String("fields", fields),
  138. zap.String("error", err.Error()))
  139. }
  140. return err
  141. }
  142. func (p *TChargeConf) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  143. cond, val, err := whereBuildAndOr(where, or)
  144. if err != nil {
  145. return 0, err
  146. }
  147. ret := int64(0)
  148. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  149. if err != nil {
  150. fields, _ := util.MarshalToString(where)
  151. logger.Error("mysql",
  152. zap.String("sql", "select count "+p.TableName()),
  153. zap.String("fields", fields),
  154. zap.String("error", err.Error()))
  155. }
  156. return ret, err
  157. }
  158. func (p *TChargeConf) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargeConf, err error) {
  159. if page >= 0 {
  160. return p.ListByJoin(db, where, or, page, pageSize)
  161. }
  162. cond, val, err := whereBuildAndOr(where, or)
  163. if err != nil {
  164. return list, err
  165. }
  166. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  167. if result.Error != nil {
  168. wherefields, _ := util.MarshalToString(where)
  169. logger.Error("mysql",
  170. zap.String("sql", "select * from "+p.TableName()),
  171. zap.String("where", wherefields),
  172. zap.String("error", result.Error.Error()))
  173. }
  174. return list, result.Error
  175. }
  176. func (p *TChargeConf) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargeConf, err error) {
  177. cond, val, err := whereBuildAndOr(where, or)
  178. if err != nil {
  179. return list, err
  180. }
  181. offset := (page - 1) * pageSize
  182. sql := fmt.Sprintf("select * from %s t1 join (select id from %s limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), pageSize, offset)
  183. if cond != "" {
  184. sql = fmt.Sprintf("select * from %s t1 join (select id from %s where %s limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), cond, pageSize, offset)
  185. }
  186. result := db.Raw(sql, val...).Scan(&list)
  187. if result.Error != nil {
  188. wherefields, _ := util.MarshalToString(where)
  189. logger.Error("mysql",
  190. zap.String("sql", "select * from "+p.TableName()),
  191. zap.String("where", wherefields),
  192. zap.String("error", result.Error.Error()))
  193. }
  194. return list, result.Error
  195. }