project.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "fmt"
  6. "time"
  7. "github.com/jinzhu/gorm"
  8. )
  9. type TProject struct {
  10. Id int64 `gorm:"column:id" json:"id" form:"id"`
  11. Cid int64 `gorm:"column:cid" json:"cid" form:"cid"`
  12. SafetyRecordNo string `gorm:"column:safety_record_no" json:"safety_record_no" form:"safety_record_no"`
  13. ProjectNo string `gorm:"column:project_no" json:"project_no" form:"project_no"`
  14. Name string `gorm:"column:name" json:"name" form:"name"`
  15. ShortName string `gorm:"column:short_name" json:"short_name" form:"short_name"`
  16. ProjectFunction string `gorm:"column:project_function" json:"project_function" form:"project_function"`
  17. ProjectType string `gorm:"column:project_type" json:"project_type" form:"project_type"`
  18. Price float64 `gorm:"column:price" json:"price" form:"price"`
  19. Desc string `gorm:"column:desc" json:"desc" form:"desc"`
  20. ProjectLeader string `gorm:"column:project_leader" json:"project_leader" form:"project_leader"`
  21. CoveredArea float64 `gorm:"column:covered_area" json:"covered_area" form:"covered_area"`
  22. Phone string `gorm:"column:phone" json:"phone" form:"phone"`
  23. TotalLevel int64 `gorm:"column:total_level" json:"total_level" form:"total_level"`
  24. BuildingStorey string `gorm:"column:building_storey" json:"building_storey" form:"building_storey"`
  25. Construction string `gorm:"column:construction" json:"construction" form:"construction"`
  26. Development string `gorm:"column:development" json:"development" form:"development"`
  27. Oversee string `gorm:"column:oversee" json:"oversee" form:"oversee"`
  28. ProjectPlan string
  29. Design string `gorm:"column:design" json:"design" form:"design"`
  30. Exploration string `gorm:"column:exploration" json:"exploration" form:"exploration"`
  31. Location string `gorm:"column:location" json:"location" form:"location"`
  32. Lon float64 `gorm:"column:lon" json:"lon" form:"lon"`
  33. Lat float64 `gorm:"column:lat" json:"lat" form:"lat"`
  34. IsFinish int64 `gorm:"column:is_finish" json:"is_finish" form:"is_finish"`
  35. CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
  36. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  37. StartDay string `gorm:"column:start_day" json:"start_day"`
  38. EndDay string `gorm:"column:end_day" json:"end_day"`
  39. }
  40. func (TProject) TableName() string {
  41. return "t_project"
  42. }
  43. func (p *TProject) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int32) (list []TProject, err error) {
  44. offset := (page - 1) * int32(PageSize)
  45. if len(where) > 0 || len(or) > 0 {
  46. cond, val, err := whereBuildAndOr(where, or)
  47. if err != nil {
  48. return list, err
  49. }
  50. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(offset).Find(&list)
  51. return list, result.Error
  52. }
  53. result := db.Table(p.TableName()).Find(&list)
  54. return list, result.Error
  55. }
  56. func (p *TProject) Insert(db *gorm.DB) error {
  57. return db.Create(p).Error
  58. }
  59. func (p *TProject) Del(db *gorm.DB, where map[string]interface{}) error {
  60. cond, val, err := whereBuild(where)
  61. if err != nil {
  62. return err
  63. }
  64. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  65. }
  66. func (p *TProject) Find(db *gorm.DB, where map[string]interface{}) error {
  67. cond, val, err := whereBuild(where)
  68. if err != nil {
  69. return err
  70. }
  71. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  72. }
  73. func (p *TProject) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  74. cond, val, err := whereBuild(where)
  75. if err != nil {
  76. return err
  77. }
  78. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  79. }
  80. func (p *TProject) Save(db *gorm.DB) error {
  81. return db.Save(p).Error
  82. }
  83. func (p *TProject) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  84. if len(where) > 0 || len(or) > 0 {
  85. cond, val, err := whereBuildAndOr(where, or)
  86. if err != nil {
  87. return 0, err
  88. }
  89. ret := int64(0)
  90. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  91. return ret, err
  92. }
  93. ret := int64(0)
  94. err := db.Table(p.TableName()).Count(&ret).Error
  95. return ret, err
  96. }
  97. type ProjectItem struct {
  98. CompanyName string
  99. TProject
  100. }
  101. func projectListWithComanySql(filter string, page int32, pageSize int32) string {
  102. 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")
  103. whereArray := []string{}
  104. if filter != "" {
  105. sub := fmt.Sprintf("t1.name like '%%%s%%' or t1.safety_record_no like '%%%s%%' or t1.location like '%%%s%%'", filter, filter, filter)
  106. whereArray = append(whereArray, sub)
  107. }
  108. where := ""
  109. for _, v := range whereArray {
  110. if where == "" {
  111. where = fmt.Sprintf(" where %s", v)
  112. continue
  113. }
  114. where = fmt.Sprintf("%s and %s", where, v)
  115. }
  116. if pageSize < 0 {
  117. sql = fmt.Sprintf("%s %s order by t1.created_at desc", sql, where)
  118. } else {
  119. offset := int64(page - 1) * int64(pageSize)
  120. sql = fmt.Sprintf("%s %s order by t1.created_at desc limit %d offset %d", sql, where, PageSize, offset)
  121. }
  122. return sql
  123. }
  124. func (p *TProject) ListWithCompay(db *gorm.DB, filter string, page int32, pageSize int32) (list []ProjectItem, err error) {
  125. ret := []ProjectItem{}
  126. sql := projectListWithComanySql(filter, page, pageSize)
  127. err = db.Raw(sql).Scan(&ret).Error
  128. if err != nil {
  129. return nil, err
  130. }
  131. return ret, nil
  132. }
  133. type ProjectStatistcData struct {
  134. Total int64
  135. Increase int64
  136. }
  137. func ProjectStatistic(db *gorm.DB) (ProjectStatistcData, error) {
  138. ret := ProjectStatistcData{}
  139. start := GetIncreaseStart(true)
  140. sql := fmt.Sprintf("select count(1) as total, sum(created_at >= '%s') as increase from t_project", start)
  141. array := []ProjectStatistcData{}
  142. err := db.Raw(sql).Scan(&array).Error
  143. if err != nil {
  144. return ret, err
  145. }
  146. if len(array) > 0 {
  147. return array[0], nil
  148. }
  149. return ret, nil
  150. }
  151. type ProjectTypeStatisticData struct {
  152. ProjectType string
  153. Count int64
  154. }
  155. func ProjectTypeStatistic(db *gorm.DB) ([]ProjectTypeStatisticData, error) {
  156. sql := fmt.Sprintf("select count(1) as count, project_type from t_project group by project_type")
  157. array := []ProjectTypeStatisticData{}
  158. err := db.Raw(sql).Scan(&array).Error
  159. if err != nil {
  160. return nil, err
  161. }
  162. return array, nil
  163. }