charge_power.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 TChargePower struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. HouseId int64 `gorm:"column:house_id" json:"house_id"`
  15. LastAmount float64 `gorm:"column:last_amount" json:"last_amount"`
  16. CurrentAmount float64 `gorm:"column:current_amount" json:"current_amount"`
  17. Start int64 `gorm:"column:start" json:"start"`
  18. End int64 `gorm:"column:end" json:"end"`
  19. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  20. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  21. Comment string `gorm:"column:comment" json:"comment"`
  22. ChargeType int32 `gorm:"column:charge_type" json:"charge_type"`
  23. BillId int64 `gorm:"column:bill_id" json:"bill_id"`
  24. table string
  25. }
  26. func (p *TChargePower) TableName() string {
  27. return p.table
  28. }
  29. func NewChargePower(database string) *TChargePower {
  30. return &TChargePower{table: fmt.Sprintf("%s.%s", database, "t_charge_power")}
  31. }
  32. func (p *TChargePower) SetTable(database string) {
  33. p.table = fmt.Sprintf("%s.%s", database, "t_charge_power")
  34. }
  35. func (p *TChargePower) CreateTable(db *gorm.DB) error {
  36. sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
  37. " `id` bigint(11) NOT NULL AUTO_INCREMENT," +
  38. " `house_id` bigint(11) NOT NULL COMMENT '房屋id'," +
  39. "`last_amount` decimal(18,6) NOT NULL COMMENT '上期使用量'," +
  40. "`current_amount` decimal(18,6) NOT NULL COMMENT '本期使用量'," +
  41. "`start` bigint(11) NOT NULL COMMENT '开始时间'," +
  42. "`end` bigint(11) NOT NULL COMMENT '结束时间'," +
  43. "`created_at` datetime NOT NULL," +
  44. " `updated_at` datetime NOT NULL," +
  45. " `comment` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '备注'," +
  46. "`charge_type` tinyint(1) NOT NULL COMMENT ' 2 水费 3 电费 4 气费'," +
  47. "`bill_id` bigint(11) NOT NULL COMMENT '账单id'," +
  48. "PRIMARY KEY (`id`) USING BTREE," +
  49. " UNIQUE KEY `house_id` (`house_id`,`charge_type`,`start`,`end`) USING BTREE," +
  50. " KEY `bill_id` (`bill_id`) USING BTREE" +
  51. ") ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT COMMENT='水电气抄表记录';"
  52. err := db.Exec(sql).Error
  53. if err != nil {
  54. logger.Error("mysql",
  55. zap.String("sql", "create table "+p.TableName()),
  56. zap.String("fields", ""),
  57. zap.String("error", err.Error()))
  58. }
  59. return err
  60. }
  61. func (p *TChargePower) Find(db *gorm.DB, where map[string]interface{}) error {
  62. err := db.Table(p.TableName()).Where(where).Find(p).Error
  63. if err != nil {
  64. fields, _ := util.MarshalToString(where)
  65. logger.Error("mysql",
  66. zap.String("sql", "select from "+p.TableName()),
  67. zap.String("fields", fields),
  68. zap.String("error", err.Error()))
  69. }
  70. return err
  71. }
  72. func (p *TChargePower) Last(db *gorm.DB) error {
  73. err := db.Table(p.TableName()).Last(p).Error
  74. if err != nil {
  75. logger.Error("mysql",
  76. zap.String("sql", "select last from "+p.TableName()),
  77. zap.String("fields", ""),
  78. zap.String("error", err.Error()))
  79. }
  80. return err
  81. }
  82. // Insert 插入一条记录
  83. func (p *TChargePower) Insert(db *gorm.DB) error {
  84. err := db.Table(p.TableName()).Create(p).Error
  85. if err != nil {
  86. fields, _ := util.MarshalToString(*p)
  87. logger.Error("mysql",
  88. zap.String("sql", "insert into "+p.TableName()),
  89. zap.String("fields", fields),
  90. zap.String("error", err.Error()))
  91. }
  92. return err
  93. }
  94. // Insert 插入多条记录
  95. func (p *TChargePower) InsertMulti(db *gorm.DB, values interface{}) error {
  96. err := db.Table(p.TableName()).Create(values).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 *TChargePower) Delete(db *gorm.DB, filter map[string]interface{}) error {
  107. cond, val, err := whereBuild(filter)
  108. if err != nil {
  109. return err
  110. }
  111. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  112. }
  113. func (p *TChargePower) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  114. cond, val, err := whereBuild(where)
  115. if err != nil {
  116. if err != nil {
  117. fields, _ := util.MarshalToString(values)
  118. logger.Error("mysql",
  119. zap.String("sql", "update "+p.TableName()),
  120. zap.String("fields", fields),
  121. zap.String("error", err.Error()))
  122. }
  123. return err
  124. }
  125. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  126. }
  127. func (p *TChargePower) UpdateByModel(db *gorm.DB) error {
  128. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  129. if err != nil {
  130. fields, _ := util.MarshalToString(*p)
  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. func (p *TChargePower) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  139. cond, val, err := whereBuildAndOr(where, or)
  140. if err != nil {
  141. return 0, err
  142. }
  143. ret := int64(0)
  144. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  145. if err != nil {
  146. fields, _ := util.MarshalToString(where)
  147. logger.Error("mysql",
  148. zap.String("sql", "select count "+p.TableName()),
  149. zap.String("fields", fields),
  150. zap.String("error", err.Error()))
  151. }
  152. return ret, err
  153. }
  154. func (p *TChargePower) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargePower, err error) {
  155. cond, val, err := whereBuildAndOr(where, or)
  156. if err != nil {
  157. return list, err
  158. }
  159. if pageSize < 0 {
  160. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  161. if result.Error != nil {
  162. wherefields, _ := util.MarshalToString(where)
  163. logger.Error("mysql",
  164. zap.String("sql", "select * from "+p.TableName()),
  165. zap.String("where", wherefields),
  166. zap.String("error", result.Error.Error()))
  167. }
  168. return list, result.Error
  169. }
  170. offset := (page - 1) * pageSize
  171. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Limit(pageSize).Offset(offset).Find(&list)
  172. if result.Error != nil {
  173. wherefields, _ := util.MarshalToString(where)
  174. logger.Error("mysql",
  175. zap.String("sql", "select * from "+p.TableName()),
  176. zap.String("where", wherefields),
  177. zap.String("error", result.Error.Error()))
  178. }
  179. return list, result.Error
  180. }