// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package model import ( "fmt" "time" "github.com/jinzhu/gorm" ) type TProject struct { Id int64 `gorm:"column:id" json:"id" form:"id"` Cid int64 `gorm:"column:cid" json:"cid" form:"cid"` SafetyRecordNo string `gorm:"column:safety_record_no" json:"safety_record_no" form:"safety_record_no"` ProjectNo string `gorm:"column:project_no" json:"project_no" form:"project_no"` Name string `gorm:"column:name" json:"name" form:"name"` ShortName string `gorm:"column:short_name" json:"short_name" form:"short_name"` ProjectFunction string `gorm:"column:project_function" json:"project_function" form:"project_function"` ProjectType string `gorm:"column:project_type" json:"project_type" form:"project_type"` Price float64 `gorm:"column:price" json:"price" form:"price"` Desc string `gorm:"column:desc" json:"desc" form:"desc"` ProjectLeader string `gorm:"column:project_leader" json:"project_leader" form:"project_leader"` CoveredArea float64 `gorm:"column:covered_area" json:"covered_area" form:"covered_area"` Phone string `gorm:"column:phone" json:"phone" form:"phone"` TotalLevel int64 `gorm:"column:total_level" json:"total_level" form:"total_level"` BuildingStorey string `gorm:"column:building_storey" json:"building_storey" form:"building_storey"` Construction string `gorm:"column:construction" json:"construction" form:"construction"` Development string `gorm:"column:development" json:"development" form:"development"` Oversee string `gorm:"column:oversee" json:"oversee" form:"oversee"` ProjectPlan string Design string `gorm:"column:design" json:"design" form:"design"` Exploration string `gorm:"column:exploration" json:"exploration" form:"exploration"` Location string `gorm:"column:location" json:"location" form:"location"` Lon float64 `gorm:"column:lon" json:"lon" form:"lon"` Lat float64 `gorm:"column:lat" json:"lat" form:"lat"` IsFinish int64 `gorm:"column:is_finish" json:"is_finish" form:"is_finish"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"` StartDay string `gorm:"column:start_day" json:"start_day"` EndDay string `gorm:"column:end_day" json:"end_day"` } func (TProject) TableName() string { return "t_project" } func (p *TProject) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int32) (list []TProject, err error) { offset := (page - 1) * int32(PageSize) if len(where) > 0 || len(or) > 0 { cond, val, err := whereBuildAndOr(where, or) 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()).Find(&list) return list, result.Error } func (p *TProject) Insert(db *gorm.DB) error { return db.Create(p).Error } func (p *TProject) 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 *TProject) 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 *TProject) 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 *TProject) Save(db *gorm.DB) error { return db.Save(p).Error } func (p *TProject) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) { if len(where) > 0 || len(or) > 0 { 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 return ret, err } ret := int64(0) err := db.Table(p.TableName()).Count(&ret).Error return ret, err } type ProjectItem struct { CompanyName string TProject } func projectListWithComanySql(filter string, page int32, pageSize int32) string { sql := fmt.Sprintf("select t1.*, t2.name as company_name from t_project as t1 left join t_company as t2 on t1.cid = t2.id") whereArray := []string{} if filter != "" { sub := fmt.Sprintf("t1.name like '%%%s%%' or t1.safety_record_no like '%%%s%%' or t1.location like '%%%s%%'", filter, filter, filter) whereArray = append(whereArray, sub) } where := "" for _, v := range whereArray { if where == "" { where = fmt.Sprintf(" where %s", v) continue } where = fmt.Sprintf("%s and %s", where, v) } if pageSize < 0 { sql = fmt.Sprintf("%s %s order by t1.created_at desc", sql, where) } else { offset := int64(page - 1) * int64(pageSize) sql = fmt.Sprintf("%s %s order by t1.created_at desc limit %d offset %d", sql, where, PageSize, offset) } return sql } func (p *TProject) ListWithCompay(db *gorm.DB, filter string, page int32, pageSize int32) (list []ProjectItem, err error) { ret := []ProjectItem{} sql := projectListWithComanySql(filter, page, pageSize) err = db.Raw(sql).Scan(&ret).Error if err != nil { return nil, err } return ret, nil } type ProjectStatistcData struct { Total int64 Increase int64 } func ProjectStatistic(db *gorm.DB) (ProjectStatistcData, error) { ret := ProjectStatistcData{} start := GetIncreaseStart(true) sql := fmt.Sprintf("select count(1) as total, sum(created_at >= '%s') as increase from t_project", start) array := []ProjectStatistcData{} err := db.Raw(sql).Scan(&array).Error if err != nil { return ret, err } if len(array) > 0 { return array[0], nil } return ret, nil } type ProjectTypeStatisticData struct { ProjectType string Count int64 } func ProjectTypeStatistic(db *gorm.DB) ([]ProjectTypeStatisticData, error) { sql := fmt.Sprintf("select count(1) as count, project_type from t_project group by project_type") array := []ProjectTypeStatisticData{} err := db.Raw(sql).Scan(&array).Error if err != nil { return nil, err } return array, nil }