123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package model
- import (
- "fmt"
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/util"
- "go.uber.org/zap"
- "gorm.io/gorm"
- "time"
- )
- type THouseApprovedGarden struct {
- ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
- Uid int64 `gorm:"column:uid" json:"uid"`
- ApprovedAt time.Time `gorm:"column:approved_at" json:"approved_at"`
- CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
- UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
- UserType int32 `gorm:"column:user_type" json:"user_type"`
- HouseId int64 `gorm:"column:house_id" json:"house_id"`
- BuildingNumber string `gorm:"column:building_number" json:"building_number"`
- UnitNumber int64 `gorm:"column:unit_number" json:"unit_number"`
- HouseNumber string `gorm:"column:house_number" json:"house_number"`
- Appendix string `gorm:"column:appendix" json:"appendix"`
- Feedback string `gorm:"column:feedback" json:"feedback"`
- BuildingId int64 `gorm:"column:building_id" json:"building_id"`
- UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
- Phone string `gorm:"column:phone" json:"phone"`
- IdType int32 `gorm:"column:id_type" json:"id_type"`
- IdNumber string `gorm:"column:id_number" json:"id_number"`
- Name string `gorm:"column:name" json:"name"`
- OpenId string `gorm:"column:open_id" json:"open_id"`
- PublicOpenId string `gorm:"column:public_open_id" json:"public_open_id"`
- table string
- }
- func (p *THouseApprovedGarden) TableName() string {
- return p.table
- }
- func NewHouseApprovedGarden(database string) *THouseApprovedGarden {
- return &THouseApprovedGarden{table: fmt.Sprintf("%s.%s", database, "t_house_approved")}
- }
- func (p *THouseApprovedGarden) SetTable(database string) {
- p.table = fmt.Sprintf("%s.%s", database, "t_house_approved")
- }
- func (p *THouseApprovedGarden) CreateTable(db *gorm.DB) error {
- sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
- "`id` bigint(11) NOT NULL AUTO_INCREMENT," +
- "`uid` bigint(11) NOT NULL COMMENT '账户id'," +
- "`approved_at` datetime NOT NULL," +
- " `created_at` datetime NOT NULL," +
- " `updated_at` datetime NOT NULL," +
- " `user_type` tinyint(1) NOT NULL COMMENT '1业主 2家人 3租客 '," +
- "`house_id` bigint(11) NOT NULL COMMENT '房间id'," +
- "`building_number` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '楼栋编号'," +
- "`unit_number` int(10) NOT NULL COMMENT '单元编号'," +
- "`house_number` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '门牌号'," +
- " `appendix` text COLLATE utf8mb4_bin NOT NULL COMMENT '附件'," +
- " `feedback` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '审批回复'," +
- "`building_id` int(10) NOT NULL," +
- " `unit_id` int(10) NOT NULL," +
- " `phone` varchar(16) COLLATE utf8mb4_bin NOT NULL," +
- " `id_type` tinyint(1) NOT NULL COMMENT '1 身份证 2 港澳台居住证 3 港澳台同胞来往通行证 4 护照 5 军官证'," +
- "`id_number` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '证件号'," +
- "`name` varchar(64) COLLATE utf8mb4_bin NOT NULL," +
- " `open_id` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '小程序openid'," +
- "`public_open_id` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '公众号openid'," +
- " PRIMARY KEY (`id`) USING BTREE," +
- " UNIQUE KEY `uid_index` (`uid`,`house_id`) USING BTREE," +
- " KEY `house_id` (`house_id`) USING BTREE" +
- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT;"
- err := db.Exec(sql).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", "create table "+p.TableName()),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *THouseApprovedGarden) Find(db *gorm.DB, where map[string]interface{}) error {
- err := db.Table(p.TableName()).Where(where).First(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select from "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *THouseApprovedGarden) Last(db *gorm.DB) error {
- err := db.Table(p.TableName()).Last(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", "select last from "+p.TableName()),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
- // Insert 插入一条记录
- func (p *THouseApprovedGarden) Insert(db *gorm.DB) error {
- err := db.Table(p.TableName()).Create(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "insert into "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- // Insert 插入多条记录
- func (p *THouseApprovedGarden) InsertMulti(db *gorm.DB, values interface{}) error {
- err := db.Table(p.TableName()).Create(values).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "insert into "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *THouseApprovedGarden) Delete(db *gorm.DB, filter map[string]interface{}) error {
- cond, val, err := whereBuild(filter)
- if err != nil {
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
- }
- func (p *THouseApprovedGarden) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- if err != nil {
- fields, _ := util.MarshalToString(values)
- logger.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
- }
- func (p *THouseApprovedGarden) UpdateByModel(db *gorm.DB) error {
- err := db.Table(p.TableName()).Model(p).Updates(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *THouseApprovedGarden) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return 0, err
- }
- ret := int64(0)
- err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select count "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return ret, err
- }
- func (p *THouseApprovedGarden) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseApprovedGarden, err error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return list, err
- }
- if pageSize < 0 {
- result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return list, result.Error
- }
- offset := (page - 1) * pageSize
- result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Limit(pageSize).Offset(offset).Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return list, result.Error
- }
- 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) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return list, err
- }
- if page < 0 {
- result := db.Table(p.TableName()).Where(cond, val...).Group(group).Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- return nil, result.Error
- }
- } else {
- offset := (page - 1) * pageSize
- result := db.Table(p.TableName()).Where(cond, val...).Group(group).Limit(pageSize).Offset(offset).Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- return nil, result.Error
- }
- }
- return list, nil
- }
- func (p *THouseApprovedGarden) CountGroup(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, group string) (int64, error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return 0, err
- }
- sql := fmt.Sprintf("select count(*) as count from (select count(*) from %s group by %s) t1", p.TableName(), group)
- if len(cond) > 0 {
- sql = fmt.Sprintf("select count(*) as count from (select count(*) from %s where %s group by %s) t1", p.TableName(), cond, group)
- }
- type Result struct {
- Count int64
- }
- array := []Result{}
- err = db.Raw(sql, val...).Scan(&array).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select count "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- return 0, err
- }
- if len(array) == 0 {
- return 0, nil
- }
- return array[0].Count, err
- }
- 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) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return err
- }
- if pageSize < 0 {
- result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Group(group).Find(ret)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return result.Error
- }
- offset := (page - 1) * pageSize
- result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Group(group).Limit(pageSize).Offset(offset).Find(ret)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return result.Error
- }
- 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) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return err
- }
- if pageSize < 0 {
- result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Find(ret)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return result.Error
- }
- offset := (page - 1) * pageSize
- result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Limit(pageSize).Offset(offset).Find(ret)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return result.Error
- }
|