charge_bind.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. "database/sql"
  6. "fmt"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "git.getensh.com/common/gopkgs/util"
  9. "go.uber.org/zap"
  10. "gorm.io/gorm"
  11. "time"
  12. )
  13. type TChargeBind struct {
  14. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  15. ObjId int64 `gorm:"column:obj_id" json:"obj_id"`
  16. ObjName string `gorm:"column:obj_name" json:"obj_name"`
  17. HouseId int64 `gorm:"column:house_id" json:"house_id"`
  18. ChargeId int64 `gorm:"column:charge_id" json:"charge_id"`
  19. Start int64 `gorm:"column:start" json:"start"`
  20. End int64 `gorm:"column:end" json:"end"`
  21. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  22. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  23. ChargeType int32 `gorm:"column:charge_type" json:"charge_type"`
  24. UniqFlag sql.NullInt32 `gorm:"column:uniq_flag" json:"uniq_flag"`
  25. ObjType int32 `gorm:"column:obj_type" json:"obj_type"`
  26. CustomFee int64 `gorm:"column:custom_fee" json:"custom_fee"`
  27. BillLastTime int64 `gorm:"column:bill_last_time" json:"bill_last_time"`
  28. ObjArea float64 `gorm:"column:obj_area" json:"obj_area"`
  29. //ChargeBasis int32 `gorm:"column:charge_basis" json:"charge_basis"`
  30. table string
  31. }
  32. func (p *TChargeBind) TableName() string {
  33. return p.table
  34. }
  35. func NewChargeBind(database string) *TChargeBind {
  36. return &TChargeBind{table: fmt.Sprintf("%s.%s", database, "t_charge_bind")}
  37. }
  38. func (p *TChargeBind) SetTable(database string) {
  39. p.table = fmt.Sprintf("%s.%s", database, "t_charge_bind")
  40. }
  41. func (p *TChargeBind) CreateTable(db *gorm.DB) error {
  42. sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
  43. " `id` bigint(11) NOT NULL AUTO_INCREMENT,"+
  44. " `obj_id` bigint(11) NOT NULL COMMENT '房间id/车位id/车辆id',"+
  45. "`obj_name` varchar(16) COLLATE utf8mb4_bin NOT NULL COMMENT '房屋号/车位/车牌',"+
  46. "`house_id` bigint(11) NOT NULL COMMENT '房屋id',"+
  47. "`charge_id` int(10) NOT NULL COMMENT '费用项目id',"+
  48. "`start` bigint(11) NOT NULL COMMENT '计费开始时间戳',"+
  49. "`end` bigint(11) NOT NULL COMMENT '结束时间 车辆收费会用到',"+
  50. "`created_at` datetime NOT NULL,"+
  51. " `updated_at` datetime NOT NULL,"+
  52. " `charge_type` int(10) NOT NULL COMMENT '费用类型1 物业费 2 水费 3 电费 4 气费 99其他',"+
  53. "`uniq_flag` tinyint(1) DEFAULT NULL COMMENT '和其他字段做联合唯一索引1 不能重复 null 可以重复',"+
  54. "`obj_type` tinyint(1) NOT NULL COMMENT '1 房屋 2 车位 3 车辆',"+
  55. "`custom_fee` int(10) NOT NULL COMMENT '自定义费用',"+
  56. "`bill_last_time` bigint(11) NOT NULL COMMENT '账单上次生成时间',"+
  57. "`obj_area` decimal(18,2) NOT NULL COMMENT '房屋或车位面积',"+
  58. //"`charge_basis` tinyint(1) NOT NULL COMMENT '1 按房屋面积 2 按使用面积 3 按车位面积 4 按使用量 5 固定费用 6 自定义',"+
  59. "PRIMARY KEY (`id`) USING BTREE,"+
  60. "UNIQUE KEY `house_charge_id` (`obj_id`,`charge_id`) USING BTREE COMMENT '一个房屋和一个项目不能重复绑定',"+
  61. "UNIQUE KEY `house_charge_type` (`obj_id`,`charge_type`,`uniq_flag`) USING BTREE COMMENT ' 水电气,物业费,一个房屋和缴费类型不能重复绑定',"+
  62. "KEY `charge_id` (`charge_id`) USING BTREE"+
  63. ") ENGINE=InnoDB AUTO_INCREMENT=1 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 *TChargeBind) Find(db *gorm.DB, where map[string]interface{}) error {
  74. cond, val, err := whereBuild(where)
  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. return err
  82. }
  83. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  84. if err != nil {
  85. fields, _ := util.MarshalToString(where)
  86. logger.Error("mysql",
  87. zap.String("sql", "select from "+p.TableName()),
  88. zap.String("fields", fields),
  89. zap.String("error", err.Error()))
  90. }
  91. return err
  92. }
  93. func (p *TChargeBind) AllNot(db *gorm.DB, not map[string]interface{}) (list []TChargeBind,err error) {
  94. err = db.Table(p.TableName()).Not(not).Find(&list).Error
  95. if err != nil {
  96. fields, _ := util.MarshalToString(not)
  97. logger.Error("mysql",
  98. zap.String("sql", "select from where not"+p.TableName()),
  99. zap.String("fields", fields),
  100. zap.String("error", err.Error()))
  101. }
  102. return list ,err
  103. }
  104. func (p *TChargeBind) Last(db *gorm.DB) error {
  105. err := db.Table(p.TableName()).Last(p).Error
  106. if err != nil {
  107. logger.Error("mysql",
  108. zap.String("sql", "select last from "+p.TableName()),
  109. zap.String("fields", ""),
  110. zap.String("error", err.Error()))
  111. }
  112. return err
  113. }
  114. // Insert 插入一条记录
  115. func (p *TChargeBind) Insert(db *gorm.DB) error {
  116. err := db.Table(p.TableName()).Create(p).Error
  117. if err != nil {
  118. fields, _ := util.MarshalToString(*p)
  119. logger.Error("mysql",
  120. zap.String("sql", "insert into "+p.TableName()),
  121. zap.String("fields", fields),
  122. zap.String("error", err.Error()))
  123. }
  124. return err
  125. }
  126. func (p *TChargeBind) InsertMulti(db *gorm.DB, values interface{}) error {
  127. err := db.Table(p.TableName()).Create(values).Error
  128. if err != nil {
  129. fields, _ := util.MarshalToString(*p)
  130. logger.Error("mysql",
  131. zap.String("sql", "insert into "+p.TableName()),
  132. zap.String("fields", fields),
  133. zap.String("error", err.Error()))
  134. }
  135. return err
  136. }
  137. func (p *TChargeBind) Delete(db *gorm.DB, filter map[string]interface{}) error {
  138. cond, val, err := whereBuild(filter)
  139. if err != nil {
  140. return err
  141. }
  142. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  143. }
  144. func (p *TChargeBind) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  145. cond, val, err := whereBuild(where)
  146. if err != nil {
  147. if err != nil {
  148. fields, _ := util.MarshalToString(values)
  149. logger.Error("mysql",
  150. zap.String("sql", "update "+p.TableName()),
  151. zap.String("fields", fields),
  152. zap.String("error", err.Error()))
  153. }
  154. return err
  155. }
  156. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  157. }
  158. func (p *TChargeBind) UpdateByModel(db *gorm.DB) error {
  159. err := db.Model(p).Updates(p).Error
  160. if err != nil {
  161. fields, _ := util.MarshalToString(*p)
  162. logger.Error("mysql",
  163. zap.String("sql", "update "+p.TableName()),
  164. zap.String("fields", fields),
  165. zap.String("error", err.Error()))
  166. }
  167. return err
  168. }
  169. func (p *TChargeBind) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  170. cond, val, err := whereBuildAndOr(where, or)
  171. if err != nil {
  172. return 0, err
  173. }
  174. ret := int64(0)
  175. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  176. if err != nil {
  177. fields, _ := util.MarshalToString(where)
  178. logger.Error("mysql",
  179. zap.String("sql", "select count "+p.TableName()),
  180. zap.String("fields", fields),
  181. zap.String("error", err.Error()))
  182. }
  183. return ret, err
  184. }
  185. type IdResult struct {
  186. Id int64
  187. }
  188. func (p *TChargeBind) GetIds(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) ([]IdResult, error) {
  189. cond, val, err := whereBuildAndOr(where, or)
  190. if err != nil {
  191. return nil, err
  192. }
  193. array := []IdResult{}
  194. err = db.Table(p.TableName()).Select("id").Where(cond, val...).Find(&array).Error
  195. if err != nil {
  196. fields, _ := util.MarshalToString(where)
  197. logger.Error("mysql",
  198. zap.String("sql", "select id "+p.TableName()),
  199. zap.String("fields", fields),
  200. zap.String("error", err.Error()))
  201. }
  202. return array, nil
  203. }
  204. func (p *TChargeBind) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargeBind, err error) {
  205. if page >= 0 {
  206. return p.ListByJoin(db, where, or, page, pageSize)
  207. }
  208. cond, val, err := whereBuildAndOr(where, or)
  209. if err != nil {
  210. return list, err
  211. }
  212. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  213. if result.Error != nil {
  214. wherefields, _ := util.MarshalToString(where)
  215. logger.Error("mysql",
  216. zap.String("sql", "select * from "+p.TableName()),
  217. zap.String("where", wherefields),
  218. zap.String("error", result.Error.Error()))
  219. }
  220. return list, result.Error
  221. }
  222. func (p *TChargeBind) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargeBind, err error) {
  223. cond, val, err := whereBuildAndOr(where, or)
  224. if err != nil {
  225. return list, err
  226. }
  227. offset := (page - 1) * pageSize
  228. 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)
  229. if cond != "" {
  230. 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)
  231. }
  232. result := db.Raw(sql, val...).Scan(&list)
  233. if result.Error != nil {
  234. wherefields, _ := util.MarshalToString(where)
  235. logger.Error("mysql",
  236. zap.String("sql", "select * from "+p.TableName()),
  237. zap.String("where", wherefields),
  238. zap.String("error", result.Error.Error()))
  239. }
  240. return list, result.Error
  241. }
  242. func (p *TChargeBind)GetObjIds(db *gorm.DB, where map[string]interface{})([]int64, error) {
  243. cond, val, err := whereBuildAndOr(where, nil)
  244. if err != nil {
  245. return nil, err
  246. }
  247. type Result struct {
  248. ObjId int64
  249. }
  250. array := []Result{}
  251. err = db.Table(p.TableName()).Select("obj_id").Where(cond, val...).Find(&array).Error
  252. if err != nil {
  253. wherefields, _ := util.MarshalToString(where)
  254. logger.Error("mysql",
  255. zap.String("sql", "select house_id from "+p.TableName()),
  256. zap.String("where", wherefields),
  257. zap.String("error", err.Error()))
  258. }
  259. ret := make([]int64, len(array))
  260. for i, v := range array {
  261. ret[i] = v.ObjId
  262. }
  263. return ret, err
  264. }