// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package model import ( "fmt" "github.com/jinzhu/gorm" "smart-enterprise-management/consts" "time" ) type ProjectInfo struct { ID int64 `gorm:"column:ID;PRIMARY_KEY" json:"ID" form:"id"` Code string `gorm:"column:Code" json:"Code" form:"code"` Name string `gorm:"column:Name" json:"Name" form:"name"` Safetyno string `gorm:"column:SafetyNo" json:"SafetyNo" form:"safetyno"` Prjcode string `gorm:"column:PrjCode" json:"PrjCode" form:"prjcode"` Description string `gorm:"column:Description" json:"Description" form:"description"` Category uint32 `gorm:"column:Category" json:"Category" form:"category"` Constructtype uint32 `gorm:"column:ConstructType" json:"ConstructType" form:"constructtype"` InvestType int32 `gorm:"column:InvestType" json:"InvestType" form:"investtype"` Areacode string `gorm:"column:AreaCode" json:"AreaCode" form:"areacode"` Address string `gorm:"column:Address" json:"Address" form:"address"` Buildingarea float64 `gorm:"column:BuildingArea" json:"BuildingArea" form:"buildingarea"` Buildinglength float64 `gorm:"column:BuildingLength" json:"BuildingLength" form:"buildinglength"` Invest float64 `gorm:"column:Invest" json:"Invest" form:"invest"` Scale string `gorm:"column:Scale" json:"Scale" form:"scale"` Startdate time.Time `gorm:"column:StartDate" json:"StartDate" form:"startdate"` Enddate time.Time `gorm:"column:EndDate" json:"EndDate" form:"enddate"` Lng float64 `gorm:"column:Lng" json:"Lng" form:"lng"` Lat float64 `gorm:"column:Lat" json:"Lat" form:"lat"` Thirdpartyprojectcode string `gorm:"column:ThirdpartyProjectCode" json:"ThirdpartyProjectCode" form:"thirdpartyprojectcode"` Prjstatus uint32 `gorm:"column:PrjStatus" json:"PrjStatus" form:"prjstatus"` Effectpic string `gorm:"column:EffectPic" json:"EffectPic" form:"effectpic"` Planpic string `gorm:"column:PlanPic" json:"PlanPic" form:"planpic"` Createdat time.Time `gorm:"column:CreatedAt" json:"CreatedAt" form:"createdat"` Updatedat time.Time `gorm:"column:UpdatedAt" json:"UpdatedAt" form:"updatedat"` Cid int64 `gorm:"column:Cid" json:"cid"` ProvincialVerifyStatus int32 `gorm:"column:ProvincialVerifyStatus" json:"provincial_verify_status"` ProvincialVerifyCode int32 `gorm:"column:ProvincialVerifyCode" json:"provincial_verify_code"` ProvincialVerifyInfo string `gorm:"column:ProvincialVerifyInfo" json:"provincial_verify_info"` ProvincialVerifyID string `gorm:"column:ProvincialVerifyID" json:"provincial_verify_id"` } func (ProjectInfo) TableName() string { return "ProjectInfo" } func (p *ProjectInfo) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int32, pageSize int32) (list []ProjectInfo, err error) { if len(where) > 0 || len(or) > 0 { cond, val, err := whereBuildAndOr(where, or) if err != nil { return list, err } if pageSize < 0 { result := db.Table(p.TableName()).Where(cond, val...).Order("CreatedAt desc").Find(&list) return list, result.Error } offset := (page - 1) * pageSize result := db.Table(p.TableName()).Where(cond, val...).Order("CreatedAt desc").Limit(pageSize).Offset(offset).Find(&list) return list, result.Error } if pageSize < 0 { result := db.Table(p.TableName()).Order("CreatedAt desc").Find(&list) return list, result.Error } offset := (page - 1) * pageSize result := db.Table(p.TableName()).Order("CreatedAt desc").Limit(pageSize).Offset(offset).Find(&list) return list, result.Error } type ProjectListItem struct { Manager string ManagerPhone string ProjectInfo } func projectListWithPmSql(filter string, cid int64, page int32, pageSize int32)(string, string) { sql := fmt.Sprintf("select t1.*, t2.PMName as manager, t2.PMPhone as manager_phone from ProjectInfo as t1 left join db_smart_attendance.ProjectPMInfo as t2 on t1.ID=t2.ProjectId and t2.PType=%d ", consts.StaffTypeManager) countSql := fmt.Sprintf("select 1 from ProjectInfo as t1 left join db_smart_attendance.ProjectPMInfo as t2 on t1.ID=t2.ProjectId and t2.PType=%d", consts.StaffTypeManager) whereArray := []string{} if filter != "" { whereArray = append(whereArray, fmt.Sprintf("(t1.Name like '%%%s%%' or t1.Address like '%%%s%%' or t1.SafetyNo like '%%%s%%')", filter, filter, filter)) } if cid > 0 { whereArray = append(whereArray, fmt.Sprintf("t1.Cid=%d", cid)) } 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 { offset := int64(page - 1) * int64(pageSize) sql = fmt.Sprintf("%s %s group by t1.ID order by t1.CreatedAt desc limit %d offset %d", sql, where, pageSize, offset) countSql = fmt.Sprintf("select count(1) as count from (%s %s group by t1.ID) a", countSql, where) } else { sql = fmt.Sprintf("%s %s group by t1.ID order by t1.CreatedAt desc", sql, where) countSql = fmt.Sprintf("select count(1) as count from (%s %s group by t1.ID) a", countSql, where) } return sql, countSql } func (p *ProjectInfo) ListWithPm(db *gorm.DB, filter string, cid int64, page int32, pageSize int32) (list []ProjectListItem, total int64, err error) { sql, countSql := projectListWithPmSql(filter, cid, page, pageSize) type ResultCount struct { Count int64 } array := []ResultCount{} ret := []ProjectListItem{} err = db.Raw(countSql).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 } err = db.Raw(sql).Scan(&ret).Error if err != nil { return nil, array[0].Count, err } return ret, array[0].Count, nil } func (p *ProjectInfo) Insert(db *gorm.DB) error { return db.Table(p.TableName()).Create(p).Error } func (p *ProjectInfo) 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 *ProjectInfo) 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 *ProjectInfo) FindOr(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) error { cond, val, err := whereBuildAndOr(where, or) if err != nil { return err } return db.Table(p.TableName()).Where(cond, val...).First(p).Error } func (p *ProjectInfo) 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 *ProjectInfo) 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 } func GetNextProjectCode(db *gorm.DB, prefix string) (string, error) { sql := fmt.Sprintf("select max(right(Code, 2)+0) as max from ProjectInfo where Code like '%s%%'", prefix) type Result struct { Max int64 } array := []Result{} err := db.Raw(sql).Scan(&array).Error if err != nil { return "", err } if len(array) == 0 { return prefix+"01", nil } return fmt.Sprintf("%s%02d",prefix,array[0].Max ), nil }