staff_new.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package model
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/jaryhe/gopkgs/logger"
  6. "github.com/jinzhu/gorm"
  7. "go.uber.org/zap"
  8. )
  9. type ProjectWorkInfo struct {
  10. Id int64 `gorm:"column:ID;PRIMARY_KEY" json:"id"`
  11. Code string `gorm:"column:Code" json:"code"`
  12. CorpCode string `gorm:"column:CorpCode" json:"corp_code"`
  13. CorpName string `gorm:"column:CorpName" json:"corp_name"`
  14. TeamSysNo int `gorm:"column:TeamSysNo" json:"team_sys_no"`
  15. TeamName string `gorm:"column:TeamName" json:"team_name"`
  16. WorkName string `gorm:"column:WorkName" json:"work_name"`
  17. IsTeamLeader string `gorm:"column:IsTeamLeader" json:"is_team_leader"`
  18. IDCardType int `gorm:"column:IDCardType" json:"id_card_type"`
  19. IDCardNumber string `gorm:"column:IDCardNumber" json:"id_card_number"`
  20. Age int `gorm:"column:Age" json:"age"`
  21. Gender string `gorm:"column:Gender" json:"gender"`
  22. Nation string `gorm:"column:Nation" json:"nation"`
  23. Address string `gorm:"column:Address" json:"address"`
  24. HeadImage string `gorm:"column:HeadImage" json:"head_image"`
  25. PoliticsType int `gorm:"column:PoliticsType" json:"politics_type"`
  26. CultureLevelType int `gorm:"column:CultureLevelType" json:"colture_level_type"`
  27. GrantOrg string `gorm:"column:GrantOrg" json:"grant_org"`
  28. WorkType string `gorm:"column:WorkType" json:"work_type"`
  29. NativePlace string `gorm:"column:NativePlace" json:"native_place"`
  30. Mobile string `gorm:"column:Mobile" json:"mobile"`
  31. IssueCardPicUrl string `gorm:"column:IssueCardPicUrl" json:"issue_card_pic_url"`
  32. HasContract string `gorm:"column:HasContract" json:"has_contract"`
  33. HasBuyInsurance string `gorm:"column:HasBuyInsurance" json:"has_buy_insurance"`
  34. Remark string `gorm:"column:Remark" json:"remark"`
  35. ProjectId int64 `gorm:"column:ProjectId" json:"project_id"`
  36. WorkNo string `gorm:"column:WorkNo" json:"work_no"`
  37. CreatedAt time.Time `gorm:"column:CreatedAt" json:"created_at"`
  38. UpdatedAt time.Time `gorm:"column:UpdatedAt" json:"updated_at"`
  39. Sn string `gorm:"column:Sn" json:"sn"`
  40. ProvincialVerifyStatus int32 `gorm:"column:ProvincialVerifyStatus" json:"provincial_verify_status"`
  41. ProvincialVerifyCode int32 `gorm:"column:ProvincialVerifyCode" json:"provincial_verify_code"`
  42. ProvincialVerifyInfo string `gorm:"column:ProvincialVerifyInfo" json:"provincial_verify_info"`
  43. ProvincialVerifyID string `gorm:"column:ProvincialVerifyID" json:"provincial_verify_id"`
  44. }
  45. func (ProjectWorkInfo) TableName() string {
  46. return "db_smart_attendance.ProjectWorkInfo"
  47. }
  48. // Insert 插入一条记录
  49. func (p *ProjectWorkInfo) Insert(db *gorm.DB) error {
  50. err := db.Table(p.TableName()).Create(p).Error
  51. if err != nil {
  52. fields, _ := json.MarshalToString(*p)
  53. logger.Error("mysql",
  54. zap.String("sql", "insert into ProjectWorkInfo"),
  55. zap.String("fields", fields),
  56. zap.String("error", err.Error()))
  57. }
  58. return err
  59. }
  60. func (p *ProjectWorkInfo) Del(db *gorm.DB, where map[string]interface{}) error {
  61. cond, val, err := whereBuild(where)
  62. if err != nil {
  63. return err
  64. }
  65. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  66. }
  67. func (p *ProjectWorkInfo) Delete(db *gorm.DB, filter map[string]interface{}) error {
  68. err := db.Table(p.TableName()).Where(filter).Delete(p).Error
  69. if err != nil {
  70. fields, _ := json.MarshalToString(filter)
  71. logger.Error("mysql",
  72. zap.String("sql", "delete from ProjectWorkInfo"),
  73. zap.String("fields", fields),
  74. zap.String("error", err.Error()))
  75. }
  76. return err
  77. }
  78. func (p *ProjectWorkInfo) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  79. cond, val, err := whereBuild(where)
  80. if err != nil {
  81. whereStr, _ := json.MarshalToString(where)
  82. valuesStr, _ := json.MarshalToString(values)
  83. logger.Error("mysql",
  84. zap.String("sql", "update ProjectWorkInfo"),
  85. zap.String("where", whereStr),
  86. zap.String("values", valuesStr),
  87. zap.String("error", err.Error()))
  88. return err
  89. }
  90. err = db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  91. if err != nil {
  92. whereStr, _ := json.MarshalToString(where)
  93. valuesStr, _ := json.MarshalToString(values)
  94. logger.Error("mysql",
  95. zap.String("sql", "update ProjectWorkInfo"),
  96. zap.String("where", whereStr),
  97. zap.String("values", valuesStr),
  98. zap.String("error", err.Error()))
  99. }
  100. return err
  101. }
  102. func (p *ProjectWorkInfo) Find(db *gorm.DB, where map[string]interface{}) error {
  103. cond, val, err := whereBuild(where)
  104. if err != nil {
  105. whereStr, _ := json.MarshalToString(where)
  106. logger.Error("mysql",
  107. zap.String("sql", "select from ProjectWorkInfo"),
  108. zap.String("where", whereStr),
  109. zap.String("error", err.Error()))
  110. return err
  111. }
  112. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  113. if err != nil {
  114. whereStr, _ := json.MarshalToString(where)
  115. logger.Error("mysql",
  116. zap.String("sql", "select from ProjectWorkInfo"),
  117. zap.String("where", whereStr),
  118. zap.String("error", err.Error()))
  119. return err
  120. }
  121. return err
  122. }
  123. type StaffIDCard struct {
  124. IDCardNumber string `gorm:"column:IDCardNumber" json:"id_card_number"`
  125. WorkNo string `gorm:"column:WorkNo" json:"work_no"`
  126. }
  127. func GetIDCardNumberByWorkNo(db *gorm.DB, projectId int64, worknos []string) ([]StaffIDCard,error) {
  128. sql := fmt.Sprintf("select IDCardNumber, WorkNo from db_smart_attendance.ProjectWorkInfo where ProjectId=? and WorkNo in(?)")
  129. array := []StaffIDCard{}
  130. err := db.Raw(sql, projectId, worknos).Scan(&array).Error
  131. if err != nil {
  132. return nil, err
  133. }
  134. return array, nil
  135. }
  136. func (p *ProjectWorkInfo) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int32, pageSize int32) (list []ProjectWorkInfo, err error) {
  137. if len(where) > 0 || len(or) > 0 {
  138. cond, val, err := whereBuildAndOr(where, or)
  139. if err != nil {
  140. return list, err
  141. }
  142. if pageSize < 0 {
  143. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  144. return list, result.Error
  145. }
  146. offset := (page - 1) * pageSize
  147. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  148. return list, result.Error
  149. }
  150. if pageSize < 0 {
  151. result := db.Table(p.TableName()).Find(&list)
  152. return list, result.Error
  153. }
  154. offset := (page - 1) * pageSize
  155. result := db.Table(p.TableName()).Limit(pageSize).Offset(offset).Find(&list)
  156. return list, result.Error
  157. }