// 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 TRepairOrder struct { ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"` ClassId int64 `gorm:"column:class_id" json:"class_id"` ApplyPeople string `gorm:"column:apply_people" json:"apply_people"` ApplyPeoplePhone string `gorm:"column:apply_people_phone" json:"apply_people_phone"` Appointment time.Time `gorm:"column:appointment" json:"appointment"` ApplyContent string `gorm:"column:apply_content" json:"apply_content"` ApplyPic string `gorm:"column:apply_pic" json:"apply_pic"` BuildingId int64 `gorm:"column:building_id" json:"building_id"` UnitId int64 `gorm:"column:unit_id" json:"unit_id"` HouseId int64 `gorm:"column:house_id" json:"house_id"` HouseName string `gorm:"column:house_name" json:"house_name"` LastUid int64 `gorm:"column:last_uid" json:"last_uid"` RepairPic string `gorm:"column:repair_pic" json:"repair_pic"` ReturnVisitContent string `gorm:"column:return_visit_content" json:"return_visit_content"` ReturnVisitLevel int32 `gorm:"column:return_visit_level" json:"return_visit_level"` Feedback string `gorm:"column:feedback" json:"feedback"` Status int32 `gorm:"column:status" json:"status"` Pipeline string `gorm:"column:pipeline" json:"pipeline"` CurrentUid int64 `gorm:"column:current_uid" json:"current_uid"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` RepairTime time.Time `gorm:"column:repair_time" json:"repair_time"` LastUidIsCompany bool `gorm:"column:last_uid_is_company" json:"last_uid_is_company"` HouseholdUid int64 `form:"household_uid" json:"household_uid"` //Area int32 `form:"area" json:"area"` table string } func (p *TRepairOrder) TableName() string { return p.table } func NewRepairOrder(database string) *TRepairOrder { return &TRepairOrder{table: fmt.Sprintf("%s.%s", database, "t_repair_order")} } func (p *TRepairOrder) SetTable(database string) { p.table = fmt.Sprintf("%s.%s", database, "t_repair_order") } func (p *TRepairOrder) CreateTable(db *gorm.DB) error { sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+ " `id` int(10) NOT NULL AUTO_INCREMENT,"+ " `class_id` int(10) NOT NULL COMMENT '分类id',"+ "`apply_people` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '报修人',"+ "`apply_people_phone` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '报修人电话',"+ "`appointment` datetime NOT NULL COMMENT '预约时间',"+ "`apply_content` text COLLATE utf8mb4_bin NOT NULL COMMENT '报修内容',"+ "`apply_pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '报修图片',"+ "`building_id` int(11) NOT NULL COMMENT '楼栋id',"+ "`unit_id` int(11) NOT NULL COMMENT '单元id',"+ "`house_id` int(11) NOT NULL COMMENT '房屋id',"+ "`house_name` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '房屋全称',"+ "`last_uid` int(10) NOT NULL COMMENT '上级处理人uid',"+ "`repair_pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '维修图片',"+ "`return_visit_content` text COLLATE utf8mb4_bin NOT NULL COMMENT '回访内容',"+ "`return_visit_level` tinyint(1) NOT NULL COMMENT '回访满意度',"+ "`feedback` text COLLATE utf8mb4_bin NOT NULL COMMENT '处理意见',"+ "`status` tinyint(1) NOT NULL COMMENT '1 未派单 2 已派单 3 已完结 4 退单',"+ "`pipeline` text COLLATE utf8mb4_bin NOT NULL COMMENT '工单流水线详情',"+ "`household_uid` bigint(11) NOT NULL COMMENT '业主id,业主小程序添加任务时不为空',"+ "`current_uid` int(10) NOT NULL COMMENT '当前处理人',"+ "`last_uid_is_company` tinyint(1) NOT NULL COMMENT '上级处理人是否是公司账户',"+ "`repair_time` datetime NOT NULL COMMENT '维修时间',"+ "`created_at` datetime NOT NULL,"+ " `updated_at` datetime NOT NULL,"+ // " `area` tinyint(1) NOT NULL COMMENT '1 公共区域 2 户内',"+ " PRIMARY KEY (`id`),"+ "KEY `class_id` (`class_id`) USING BTREE,"+ "KEY `apply_people` (`apply_people`) USING BTREE,"+ " KEY `household_uid` (`household_uid`) USING BTREE,"+ "KEY `apply_people_phone` (`apply_people_phone`) USING BTREE"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;" 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 *TRepairOrder) Find(db *gorm.DB, where map[string]interface{}) error { err := db.Table(p.TableName()).Where(where).Find(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 *TRepairOrder) 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 *TRepairOrder) 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 *TRepairOrder) 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 *TRepairOrder) 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 *TRepairOrder) 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 *TRepairOrder) 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 *TRepairOrder) 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 *TRepairOrder) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TRepairOrder, 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...).Order("created_at desc").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...).Limit(pageSize).Offset(offset).Order("created_at desc").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 }