// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package model import ( "github.com/jinzhu/gorm" "time" "fmt" ) type TProjectJob struct { Id int64 `gorm:"column:id" json:"id" form:"id"` Type int64 `gorm:"column:type" json:"type" form:"type"` Origin string `gorm:"column:origin" json:"origin" form:"origin"` Content string `gorm:"column:content" json:"content" form:"content"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"` Status int64 `gorm:"column:status" json:"status" form:"status"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"` ProjectId int64 `gorm:"column:project_id" json:"project_id" form:"project_id"` Feedback string `json:"feedback"` Reason string `json:"reason"` ProviderId int64 `json:"provider_id"` DeviceId int64 `json:"device_id"` } func (TProjectJob) TableName() string { return "t_project_job" } func (p *TProjectJob) Insert(db *gorm.DB) error { return db.Create(p).Error } func (p *TProjectJob) Del(db *gorm.DB, where map[string]interface{}) error { cond, val, err := whereBuild(where) if err != nil { return err } return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error } func (p *TProjectJob) Find(db *gorm.DB, where map[string]interface{}) error { cond, val, err := whereBuild(where) if err != nil { return err } return db.Table(p.TableName()).Where(cond, val...).First(p).Error } func (p *TProjectJob) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error { cond, val, err := whereBuild(where) if err != nil { return err } return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error } func (p *TProjectJob) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error { cond, val, err := whereBuild(where) if err != nil { return err } ps := []TProjectJob{} err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error if err != nil { return err } if len(ps) > 0 { *p = ps[0] } return nil } func (p *TProjectJob) Save(db *gorm.DB) error { return db.Save(p).Error } func (p *TProjectJob) Count(db *gorm.DB, where map[string]interface{}) (int64, error) { if len(where) > 0 { cond, val, err := whereBuild(where) if err != nil { return 0, err } ret := int64(0) err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error return ret, err } ret := int64(0) err := db.Table(p.TableName()).Count(&ret).Error return ret, err } func (p *TProjectJob) List(db *gorm.DB, where map[string]interface{}, page int32) (list []TProjectJob, err error) { offset := (page - 1)*PageSize if len(where) > 0 { cond, val, err := whereBuild(where) if err != nil { return list, err } result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(offset).Find(&list) return list, result.Error } result := db.Table(p.TableName()).Limit(10).Offset(offset).Find(&list) return list, result.Error } func (p *TProjectJob) All(db *gorm.DB) (list []TProjectJob, err error) { result := db.Table(p.TableName()).Find(&list) return list, result.Error } type ProjectChangeItem struct { Origin string Content string Status int32 ApplyTime time.Time ApproveTime time.Time ProjectName string SafetyRecordNo string Reason string Feedback string SocialCode string Addr string Id int64 CompanyName string } func projectChangeListSql(page int32, filter string, filterStatus []int32)(string, string, []interface{}) { offset := (page - 1) *PageSize args := []interface{}{} sql := "select t1.id, t1.origin, t1.content, t1.status, t1.created_at as apply_time, t1.updated_at as approve_time, t1.reason, t1.feedback, t2.name as project_name, t3.social_code, t3.name as company_name, t2.safety_record_no, t2.location as addr "+ " from t_project_job as t1 left join t_project as t2 on t1.project_id=t2.id and t1.type = 2 left join t_company as t3 on t2.cid = t3.id " countSql := "select count(1) as count "+ " from t_project_job as t1 left join t_project as t2 on t1.project_id=t2.id and t1.type = 2 left join t_company as t3 on t2.cid = t3.id " whereArray := []string{} whereArray = append(whereArray, fmt.Sprintf("t1.type=2")) if filter != "" { whereArray = append(whereArray, fmt.Sprintf("(t2.name like '%%%s%%' or t3.social_code like '%%%s%%' or t2.safety_record_no like '%%%s%%')", filter, filter,filter)) } if len(filterStatus) > 0 { args = append(args, filterStatus) whereArray = append(whereArray, fmt.Sprintf("t1.status in(?)")) } where := "" for _, v := range whereArray { if where == "" { where = fmt.Sprintf("where %s", v) continue } where = fmt.Sprintf("%s and %s", where, v) } sql = fmt.Sprintf("%s %s limit %d offset %d", sql, where, PageSize, offset) countSql = fmt.Sprintf("%s %s", countSql, where) return sql, countSql, args } func (p *TProjectJob)ProjectChangeList(db *gorm.DB, page int32, filter string, filterStatus []int32)([]ProjectChangeItem, int64, error) { type ResultCount struct { Count int64 } sql, countSql, args := projectChangeListSql(page, filter, filterStatus) array := []ResultCount{} err := db.Raw(countSql, args...).Scan(&array).Error if err != nil { return nil, 0, err } if len(array) == 0 { return nil, 0, nil } if array[0].Count == 0 { return nil, 0, nil } ret := []ProjectChangeItem{} err = db.Raw(sql, args...).Scan(&ret).Error if err != nil { return nil, array[0].Count, err } return ret, array[0].Count, nil }