household_approved_garden.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 THouseApprovedGarden struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. Uid int64 `gorm:"column:uid" json:"uid"`
  15. ApprovedAt time.Time `gorm:"column:approved_at" json:"approved_at"`
  16. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  17. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  18. UserType int32 `gorm:"column:user_type" json:"user_type"`
  19. HouseId int64 `gorm:"column:house_id" json:"house_id"`
  20. BuildingNumber string `gorm:"column:building_number" json:"building_number"`
  21. UnitNumber int64 `gorm:"column:unit_number" json:"unit_number"`
  22. HouseNumber string `gorm:"column:house_number" json:"house_number"`
  23. Appendix string `gorm:"column:appendix" json:"appendix"`
  24. Feedback string `gorm:"column:feedback" json:"feedback"`
  25. BuildingId int64 `gorm:"column:building_id" json:"building_id"`
  26. UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
  27. Phone string `gorm:"column:phone" json:"phone"`
  28. IdType int32 `gorm:"column:id_type" json:"id_type"`
  29. IdNumber string `gorm:"column:id_number" json:"id_number"`
  30. Name string `gorm:"column:name" json:"name"`
  31. OpenId string `gorm:"column:open_id" json:"open_id"`
  32. PublicOpenId string `gorm:"column:public_open_id" json:"public_open_id"`
  33. table string
  34. }
  35. func (p *THouseApprovedGarden) TableName() string {
  36. return p.table
  37. }
  38. func NewHouseApprovedGarden(database string) *THouseApprovedGarden {
  39. return &THouseApprovedGarden{table: fmt.Sprintf("%s.%s", database, "t_house_approved")}
  40. }
  41. func (p *THouseApprovedGarden) SetTable(database string) {
  42. p.table = fmt.Sprintf("%s.%s", database, "t_house_approved")
  43. }
  44. func (p *THouseApprovedGarden) CreateTable(db *gorm.DB) error {
  45. sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
  46. "`id` bigint(11) NOT NULL AUTO_INCREMENT," +
  47. "`uid` bigint(11) NOT NULL COMMENT '账户id'," +
  48. "`approved_at` datetime NOT NULL," +
  49. " `created_at` datetime NOT NULL," +
  50. " `updated_at` datetime NOT NULL," +
  51. " `user_type` tinyint(1) NOT NULL COMMENT '1业主 2家人 3租客 '," +
  52. "`house_id` bigint(11) NOT NULL COMMENT '房间id'," +
  53. "`building_number` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '楼栋编号'," +
  54. "`unit_number` int(10) NOT NULL COMMENT '单元编号'," +
  55. "`house_number` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '门牌号'," +
  56. " `appendix` text COLLATE utf8mb4_bin NOT NULL COMMENT '附件'," +
  57. " `feedback` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '审批回复'," +
  58. "`building_id` int(10) NOT NULL," +
  59. " `unit_id` int(10) NOT NULL," +
  60. " `phone` varchar(16) COLLATE utf8mb4_bin NOT NULL," +
  61. " `id_type` tinyint(1) NOT NULL COMMENT '1 身份证 2 港澳台居住证 3 港澳台同胞来往通行证 4 护照 5 军官证'," +
  62. "`id_number` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '证件号'," +
  63. "`name` varchar(64) COLLATE utf8mb4_bin NOT NULL," +
  64. " `open_id` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '小程序openid'," +
  65. "`public_open_id` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '公众号openid'," +
  66. " PRIMARY KEY (`id`) USING BTREE," +
  67. " UNIQUE KEY `uid_index` (`uid`,`house_id`) USING BTREE," +
  68. " KEY `house_id` (`house_id`) USING BTREE" +
  69. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT;"
  70. err := db.Exec(sql).Error
  71. if err != nil {
  72. logger.Error("mysql",
  73. zap.String("sql", "create table "+p.TableName()),
  74. zap.String("fields", ""),
  75. zap.String("error", err.Error()))
  76. }
  77. return err
  78. }
  79. func (p *THouseApprovedGarden) Find(db *gorm.DB, where map[string]interface{}) error {
  80. err := db.Table(p.TableName()).Where(where).First(p).Error
  81. if err != nil {
  82. fields, _ := util.MarshalToString(where)
  83. logger.Error("mysql",
  84. zap.String("sql", "select from "+p.TableName()),
  85. zap.String("fields", fields),
  86. zap.String("error", err.Error()))
  87. }
  88. return err
  89. }
  90. func (p *THouseApprovedGarden) Last(db *gorm.DB) error {
  91. err := db.Table(p.TableName()).Last(p).Error
  92. if err != nil {
  93. logger.Error("mysql",
  94. zap.String("sql", "select last from "+p.TableName()),
  95. zap.String("fields", ""),
  96. zap.String("error", err.Error()))
  97. }
  98. return err
  99. }
  100. // Insert 插入一条记录
  101. func (p *THouseApprovedGarden) Insert(db *gorm.DB) error {
  102. err := db.Table(p.TableName()).Create(p).Error
  103. if err != nil {
  104. fields, _ := util.MarshalToString(*p)
  105. logger.Error("mysql",
  106. zap.String("sql", "insert into "+p.TableName()),
  107. zap.String("fields", fields),
  108. zap.String("error", err.Error()))
  109. }
  110. return err
  111. }
  112. // Insert 插入多条记录
  113. func (p *THouseApprovedGarden) InsertMulti(db *gorm.DB, values interface{}) error {
  114. err := db.Table(p.TableName()).Create(values).Error
  115. if err != nil {
  116. fields, _ := util.MarshalToString(*p)
  117. logger.Error("mysql",
  118. zap.String("sql", "insert into "+p.TableName()),
  119. zap.String("fields", fields),
  120. zap.String("error", err.Error()))
  121. }
  122. return err
  123. }
  124. func (p *THouseApprovedGarden) Delete(db *gorm.DB, filter map[string]interface{}) error {
  125. cond, val, err := whereBuild(filter)
  126. if err != nil {
  127. return err
  128. }
  129. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  130. }
  131. func (p *THouseApprovedGarden) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  132. cond, val, err := whereBuild(where)
  133. if err != nil {
  134. if err != nil {
  135. fields, _ := util.MarshalToString(values)
  136. logger.Error("mysql",
  137. zap.String("sql", "update "+p.TableName()),
  138. zap.String("fields", fields),
  139. zap.String("error", err.Error()))
  140. }
  141. return err
  142. }
  143. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  144. }
  145. func (p *THouseApprovedGarden) UpdateByModel(db *gorm.DB) error {
  146. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  147. if err != nil {
  148. fields, _ := util.MarshalToString(*p)
  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. func (p *THouseApprovedGarden) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  157. cond, val, err := whereBuildAndOr(where, or)
  158. if err != nil {
  159. return 0, err
  160. }
  161. ret := int64(0)
  162. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  163. if err != nil {
  164. fields, _ := util.MarshalToString(where)
  165. logger.Error("mysql",
  166. zap.String("sql", "select count "+p.TableName()),
  167. zap.String("fields", fields),
  168. zap.String("error", err.Error()))
  169. }
  170. return ret, err
  171. }
  172. func (p *THouseApprovedGarden) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseApprovedGarden, err error) {
  173. cond, val, err := whereBuildAndOr(where, or)
  174. if err != nil {
  175. return list, err
  176. }
  177. if pageSize < 0 {
  178. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  179. if result.Error != nil {
  180. wherefields, _ := util.MarshalToString(where)
  181. logger.Error("mysql",
  182. zap.String("sql", "select * from "+p.TableName()),
  183. zap.String("where", wherefields),
  184. zap.String("error", result.Error.Error()))
  185. }
  186. return list, result.Error
  187. }
  188. offset := (page - 1) * pageSize
  189. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Limit(pageSize).Offset(offset).Find(&list)
  190. if result.Error != nil {
  191. wherefields, _ := util.MarshalToString(where)
  192. logger.Error("mysql",
  193. zap.String("sql", "select * from "+p.TableName()),
  194. zap.String("where", wherefields),
  195. zap.String("error", result.Error.Error()))
  196. }
  197. return list, result.Error
  198. }
  199. func (p *THouseApprovedGarden) ListGroup(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, group string, page int, pageSize int) (list []THouseApprovedGarden, err error) {
  200. cond, val, err := whereBuildAndOr(where, or)
  201. if err != nil {
  202. return list, err
  203. }
  204. if page < 0 {
  205. result := db.Table(p.TableName()).Where(cond, val...).Group(group).Find(&list)
  206. if result.Error != nil {
  207. wherefields, _ := util.MarshalToString(where)
  208. logger.Error("mysql",
  209. zap.String("sql", "select * from "+p.TableName()),
  210. zap.String("where", wherefields),
  211. zap.String("error", result.Error.Error()))
  212. return nil, result.Error
  213. }
  214. } else {
  215. offset := (page - 1) * pageSize
  216. result := db.Table(p.TableName()).Where(cond, val...).Group(group).Limit(pageSize).Offset(offset).Find(&list)
  217. if result.Error != nil {
  218. wherefields, _ := util.MarshalToString(where)
  219. logger.Error("mysql",
  220. zap.String("sql", "select * from "+p.TableName()),
  221. zap.String("where", wherefields),
  222. zap.String("error", result.Error.Error()))
  223. return nil, result.Error
  224. }
  225. }
  226. return list, nil
  227. }
  228. func (p *THouseApprovedGarden) CountGroup(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, group string) (int64, error) {
  229. cond, val, err := whereBuildAndOr(where, or)
  230. if err != nil {
  231. return 0, err
  232. }
  233. sql := fmt.Sprintf("select count(*) as count from (select count(*) from %s group by %s) t1", p.TableName(), group)
  234. if len(cond) > 0 {
  235. sql = fmt.Sprintf("select count(*) as count from (select count(*) from %s where %s group by %s) t1", p.TableName(), cond, group)
  236. }
  237. type Result struct {
  238. Count int64
  239. }
  240. array := []Result{}
  241. err = db.Raw(sql, val...).Scan(&array).Error
  242. if err != nil {
  243. fields, _ := util.MarshalToString(where)
  244. logger.Error("mysql",
  245. zap.String("sql", "select count "+p.TableName()),
  246. zap.String("fields", fields),
  247. zap.String("error", err.Error()))
  248. return 0, err
  249. }
  250. if len(array) == 0 {
  251. return 0, nil
  252. }
  253. return array[0].Count, err
  254. }
  255. func (p *THouseApprovedGarden) SelectGroup(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, selectStr string, group string, page int, pageSize int, ret interface{}) (err error) {
  256. cond, val, err := whereBuildAndOr(where, or)
  257. if err != nil {
  258. return err
  259. }
  260. if pageSize < 0 {
  261. result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Group(group).Find(ret)
  262. if result.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", result.Error.Error()))
  268. }
  269. return result.Error
  270. }
  271. offset := (page - 1) * pageSize
  272. result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Group(group).Limit(pageSize).Offset(offset).Find(ret)
  273. if result.Error != nil {
  274. wherefields, _ := util.MarshalToString(where)
  275. logger.Error("mysql",
  276. zap.String("sql", "select * from "+p.TableName()),
  277. zap.String("where", wherefields),
  278. zap.String("error", result.Error.Error()))
  279. }
  280. return result.Error
  281. }
  282. func (p *THouseApprovedGarden) SelectList(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, selectStr string, page int, pageSize int, ret interface{}) (err error) {
  283. cond, val, err := whereBuildAndOr(where, or)
  284. if err != nil {
  285. return err
  286. }
  287. if pageSize < 0 {
  288. result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Find(ret)
  289. if result.Error != nil {
  290. wherefields, _ := util.MarshalToString(where)
  291. logger.Error("mysql",
  292. zap.String("sql", "select * from "+p.TableName()),
  293. zap.String("where", wherefields),
  294. zap.String("error", result.Error.Error()))
  295. }
  296. return result.Error
  297. }
  298. offset := (page - 1) * pageSize
  299. result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Limit(pageSize).Offset(offset).Find(ret)
  300. if result.Error != nil {
  301. wherefields, _ := util.MarshalToString(where)
  302. logger.Error("mysql",
  303. zap.String("sql", "select * from "+p.TableName()),
  304. zap.String("where", wherefields),
  305. zap.String("error", result.Error.Error()))
  306. }
  307. return result.Error
  308. }