project.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. "github.com/jinzhu/gorm"
  7. "smart-enterprise-management/consts"
  8. "time"
  9. )
  10. type ProjectInfo struct {
  11. ID int64 `gorm:"column:ID;PRIMARY_KEY" json:"ID" form:"id"`
  12. Code string `gorm:"column:Code" json:"Code" form:"code"`
  13. Name string `gorm:"column:Name" json:"Name" form:"name"`
  14. Safetyno string `gorm:"column:SafetyNo" json:"SafetyNo" form:"safetyno"`
  15. Prjcode string `gorm:"column:PrjCode" json:"PrjCode" form:"prjcode"`
  16. Description string `gorm:"column:Description" json:"Description" form:"description"`
  17. Category uint32 `gorm:"column:Category" json:"Category" form:"category"`
  18. Constructtype uint32 `gorm:"column:ConstructType" json:"ConstructType" form:"constructtype"`
  19. InvestType int32 `gorm:"column:InvestType" json:"InvestType" form:"investtype"`
  20. Areacode string `gorm:"column:AreaCode" json:"AreaCode" form:"areacode"`
  21. Address string `gorm:"column:Address" json:"Address" form:"address"`
  22. Buildingarea float64 `gorm:"column:BuildingArea" json:"BuildingArea" form:"buildingarea"`
  23. Buildinglength float64 `gorm:"column:BuildingLength" json:"BuildingLength" form:"buildinglength"`
  24. Invest float64 `gorm:"column:Invest" json:"Invest" form:"invest"`
  25. Scale string `gorm:"column:Scale" json:"Scale" form:"scale"`
  26. Startdate time.Time `gorm:"column:StartDate" json:"StartDate" form:"startdate"`
  27. Enddate time.Time `gorm:"column:EndDate" json:"EndDate" form:"enddate"`
  28. Lng float64 `gorm:"column:Lng" json:"Lng" form:"lng"`
  29. Lat float64 `gorm:"column:Lat" json:"Lat" form:"lat"`
  30. Thirdpartyprojectcode string `gorm:"column:ThirdpartyProjectCode" json:"ThirdpartyProjectCode" form:"thirdpartyprojectcode"`
  31. Prjstatus uint32 `gorm:"column:PrjStatus" json:"PrjStatus" form:"prjstatus"`
  32. Effectpic string `gorm:"column:EffectPic" json:"EffectPic" form:"effectpic"`
  33. Planpic string `gorm:"column:PlanPic" json:"PlanPic" form:"planpic"`
  34. Createdat time.Time `gorm:"column:CreatedAt" json:"CreatedAt" form:"createdat"`
  35. Updatedat time.Time `gorm:"column:UpdatedAt" json:"UpdatedAt" form:"updatedat"`
  36. Cid int64 `gorm:"column:Cid" json:"cid"`
  37. ProvincialVerifyStatus int32 `gorm:"column:ProvincialVerifyStatus" json:"provincial_verify_status"`
  38. ProvincialVerifyCode int32 `gorm:"column:ProvincialVerifyCode" json:"provincial_verify_code"`
  39. ProvincialVerifyInfo string `gorm:"column:ProvincialVerifyInfo" json:"provincial_verify_info"`
  40. ProvincialVerifyID string `gorm:"column:ProvincialVerifyID" json:"provincial_verify_id"`
  41. }
  42. func (ProjectInfo) TableName() string {
  43. return "ProjectInfo"
  44. }
  45. func (p *ProjectInfo) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int32, pageSize int32) (list []ProjectInfo, err error) {
  46. if len(where) > 0 || len(or) > 0 {
  47. cond, val, err := whereBuildAndOr(where, or)
  48. if err != nil {
  49. return list, err
  50. }
  51. if pageSize < 0 {
  52. result := db.Table(p.TableName()).Where(cond, val...).Order("CreatedAt desc").Find(&list)
  53. return list, result.Error
  54. }
  55. offset := (page - 1) * pageSize
  56. result := db.Table(p.TableName()).Where(cond, val...).Order("CreatedAt desc").Limit(pageSize).Offset(offset).Find(&list)
  57. return list, result.Error
  58. }
  59. if pageSize < 0 {
  60. result := db.Table(p.TableName()).Order("CreatedAt desc").Find(&list)
  61. return list, result.Error
  62. }
  63. offset := (page - 1) * pageSize
  64. result := db.Table(p.TableName()).Order("CreatedAt desc").Limit(pageSize).Offset(offset).Find(&list)
  65. return list, result.Error
  66. }
  67. type ProjectListItem struct {
  68. Manager string
  69. ManagerPhone string
  70. ProjectInfo
  71. }
  72. func projectListWithPmSql(filter string, cid int64, page int32, pageSize int32)(string, string) {
  73. 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)
  74. 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)
  75. whereArray := []string{}
  76. if filter != "" {
  77. whereArray = append(whereArray, fmt.Sprintf("(t1.Name like '%%%s%%' or t1.Address like '%%%s%%' or t1.SafetyNo like '%%%s%%')", filter, filter, filter))
  78. }
  79. if cid > 0 {
  80. whereArray = append(whereArray, fmt.Sprintf("t1.Cid=%d", cid))
  81. }
  82. where := ""
  83. for _, v := range whereArray {
  84. if where == "" {
  85. where = fmt.Sprintf("where %s", v)
  86. continue
  87. }
  88. where = fmt.Sprintf("%s and %s", where, v)
  89. }
  90. if pageSize > 0 {
  91. offset := int64(page - 1) * int64(pageSize)
  92. sql = fmt.Sprintf("%s %s group by t1.ID order by t1.CreatedAt desc limit %d offset %d", sql, where, pageSize, offset)
  93. countSql = fmt.Sprintf("select count(1) as count from (%s %s group by t1.ID) a", countSql, where)
  94. } else {
  95. sql = fmt.Sprintf("%s %s group by t1.ID order by t1.CreatedAt desc", sql, where)
  96. countSql = fmt.Sprintf("select count(1) as count from (%s %s group by t1.ID) a", countSql, where)
  97. }
  98. return sql, countSql
  99. }
  100. func (p *ProjectInfo) ListWithPm(db *gorm.DB, filter string, cid int64, page int32, pageSize int32) (list []ProjectListItem, total int64, err error) {
  101. sql, countSql := projectListWithPmSql(filter, cid, page, pageSize)
  102. type ResultCount struct {
  103. Count int64
  104. }
  105. array := []ResultCount{}
  106. ret := []ProjectListItem{}
  107. err = db.Raw(countSql).Scan(&array).Error
  108. if err != nil {
  109. return nil, 0, err
  110. }
  111. if len(array) == 0 {
  112. return nil, 0, nil
  113. }
  114. if array[0].Count == 0 {
  115. return nil, 0, nil
  116. }
  117. err = db.Raw(sql).Scan(&ret).Error
  118. if err != nil {
  119. return nil, array[0].Count, err
  120. }
  121. return ret, array[0].Count, nil
  122. }
  123. func (p *ProjectInfo) Insert(db *gorm.DB) error {
  124. return db.Table(p.TableName()).Create(p).Error
  125. }
  126. func (p *ProjectInfo) Del(db *gorm.DB, where map[string]interface{}) error {
  127. cond, val, err := whereBuild(where)
  128. if err != nil {
  129. return err
  130. }
  131. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  132. }
  133. func (p *ProjectInfo) Find(db *gorm.DB, where map[string]interface{}) error {
  134. cond, val, err := whereBuild(where)
  135. if err != nil {
  136. return err
  137. }
  138. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  139. }
  140. func (p *ProjectInfo) FindOr(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) error {
  141. cond, val, err := whereBuildAndOr(where, or)
  142. if err != nil {
  143. return err
  144. }
  145. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  146. }
  147. func (p *ProjectInfo) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  148. cond, val, err := whereBuild(where)
  149. if err != nil {
  150. return err
  151. }
  152. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  153. }
  154. func (p *ProjectInfo) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  155. if len(where) > 0 || len(or) > 0{
  156. cond, val, err := whereBuildAndOr(where, or)
  157. if err != nil {
  158. return 0, err
  159. }
  160. ret := int64(0)
  161. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  162. return ret, err
  163. }
  164. ret := int64(0)
  165. err := db.Table(p.TableName()).Count(&ret).Error
  166. return ret, err
  167. }
  168. func GetNextProjectCode(db *gorm.DB, prefix string) (string, error) {
  169. sql := fmt.Sprintf("select max(right(Code, 2)+0) as max from ProjectInfo where Code like '%s%%'", prefix)
  170. type Result struct {
  171. Max int64
  172. }
  173. array := []Result{}
  174. err := db.Raw(sql).Scan(&array).Error
  175. if err != nil {
  176. return "", err
  177. }
  178. if len(array) == 0 {
  179. return prefix+"01", nil
  180. }
  181. return fmt.Sprintf("%s%02d",prefix,array[0].Max ), nil
  182. }