assessment_researcher.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package models
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_hub/global"
  5. "eta/eta_hub/utils"
  6. "fmt"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "gorm.io/gorm"
  9. "strings"
  10. "time"
  11. )
  12. const (
  13. AssessmentResearcherDisabled = 0
  14. AssessmentResearcherEnabled = 1
  15. )
  16. // AssessmentResearcher 考核研究员表
  17. type AssessmentResearcher struct {
  18. AssessmentResearcherId int `gorm:"column:assessment_researcher_id;primaryKey;autoIncrement"`
  19. AdminId int `description:"系统用户ID"`
  20. RealName string `description:"用户姓名"`
  21. Enabled int `description:"状态:0-禁用;1-正常"`
  22. CreateTime time.Time `description:"创建时间"`
  23. ModifyTime time.Time `description:"修改时间"`
  24. }
  25. func (m *AssessmentResearcher) TableName() string {
  26. return "assessment_researcher"
  27. }
  28. type AssessmentResearcherCols struct {
  29. PrimaryId string
  30. AdminId string
  31. RealName string
  32. Enabled string
  33. CreateTime string
  34. ModifyTime string
  35. }
  36. func (m *AssessmentResearcher) Cols() AssessmentResearcherCols {
  37. return AssessmentResearcherCols{
  38. PrimaryId: "assessment_researcher_id",
  39. AdminId: "admin_id",
  40. RealName: "real_name",
  41. Enabled: "enabled",
  42. CreateTime: "create_time",
  43. ModifyTime: "modify_time",
  44. }
  45. }
  46. func (m *AssessmentResearcher) Create() (err error) {
  47. err = global.DEFAULT_DB.Create(m).Error
  48. return
  49. }
  50. func (m *AssessmentResearcher) Update(cols []string) (err error) {
  51. err = global.DEFAULT_DB.Select(cols).Updates(m).Error
  52. return
  53. }
  54. func (m *AssessmentResearcher) Remove() (err error) {
  55. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  56. err = global.DEFAULT_DB.Exec(sql, m.AssessmentResearcherId).Error
  57. return
  58. }
  59. func (m *AssessmentResearcher) MultiRemove(ids []int) (err error) {
  60. if len(ids) == 0 {
  61. return
  62. }
  63. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  64. err = global.DEFAULT_DB.Exec(sql, ids).Error
  65. return
  66. }
  67. func (m *AssessmentResearcher) RemoveByCondition(condition string, pars []interface{}) (err error) {
  68. if condition == "" {
  69. return
  70. }
  71. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  72. err = global.DEFAULT_DB.Exec(sql, pars...).Error
  73. return
  74. }
  75. func (m *AssessmentResearcher) GetItemById(primaryId int) (item *AssessmentResearcher, err error) {
  76. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  77. err = global.DEFAULT_DB.Raw(sql, primaryId).First(&item).Error
  78. return
  79. }
  80. func (m *AssessmentResearcher) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *AssessmentResearcher, err error) {
  81. order := ``
  82. if orderRule != "" {
  83. order = ` ORDER BY ` + orderRule
  84. }
  85. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  86. err = global.DEFAULT_DB.Raw(sql, pars...).First(&item).Error
  87. return
  88. }
  89. func (m *AssessmentResearcher) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  90. var totalNull sql2.NullInt64
  91. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  92. err = global.DEFAULT_DB.Raw(sql, pars...).Scan(&totalNull).Error
  93. if err != nil {
  94. return
  95. }
  96. count = int(totalNull.Int64)
  97. return
  98. }
  99. func (m *AssessmentResearcher) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*AssessmentResearcher, err error) {
  100. fields := strings.Join(fieldArr, ",")
  101. if len(fieldArr) == 0 {
  102. fields = `*`
  103. }
  104. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  105. if orderRule != "" {
  106. order = ` ORDER BY ` + orderRule
  107. }
  108. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  109. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  110. return
  111. }
  112. func (m *AssessmentResearcher) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*AssessmentResearcher, err error) {
  113. fields := strings.Join(fieldArr, ",")
  114. if len(fieldArr) == 0 {
  115. fields = `*`
  116. }
  117. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  118. if orderRule != "" {
  119. order = ` ORDER BY ` + orderRule
  120. }
  121. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  122. pars = append(pars, startSize, pageSize)
  123. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  124. return
  125. }
  126. // AfterUpdate 同步更新其他表冗余
  127. func (m *AssessmentResearcher) AfterUpdate(tx *gorm.DB) (err error) {
  128. if tx.Statement.Changed(m.Cols().RealName) {
  129. fmt.Println(111)
  130. formOb := new(AssessmentForm)
  131. err = tx.Model(formOb).Where(fmt.Sprintf("%s = ?", formOb.Cols().ResearcherId), m.AssessmentResearcherId).Update(formOb.Cols().ResearcherName, m.RealName).Error
  132. fmt.Println("AfterUpdate", err)
  133. }
  134. return
  135. }
  136. // AssessmentResearcherAddReq 新增研究员请求
  137. type AssessmentResearcherAddReq struct {
  138. AdminId int `description:"用户ID"`
  139. ViewAdminIds []int `description:"查看权限用户IDs"`
  140. AssessmentAdminIds []int `description:"统计权限用户IDs"`
  141. }
  142. // AssessmentResearcherEditReq 编辑研究员请求
  143. type AssessmentResearcherEditReq struct {
  144. AssessmentResearcherId int `description:"研究员ID"`
  145. AssessmentResearcherAddReq
  146. }
  147. // AssessmentResearcherRemoveReq 删除研究员请求
  148. type AssessmentResearcherRemoveReq struct {
  149. AssessmentResearcherId int `description:"研究员ID"`
  150. }
  151. // AssessmentResearcherPageListReq 研究员列表
  152. type AssessmentResearcherPageListReq struct {
  153. PageSize int `form:"PageSize"`
  154. CurrentIndex int `form:"CurrentIndex"`
  155. RealName string `form:"RealName" description:"用户姓名"`
  156. AssessmentResearcherIds string `form:"AssessmentResearcherIds" description:"研究员IDs"`
  157. OnlyEnabled bool `form:"OnlyEnabled" description:"是否仅显示启用:true-是"`
  158. ResearcherAuthType int `form:"ResearcherAuthType" description:"权限类型:0-全部;1-当前用户有查看权限的;2-当前用户有统计权限的"`
  159. }
  160. // AssessmentResearcherPageListResp 研究员列表响应
  161. type AssessmentResearcherPageListResp struct {
  162. List []*AssessmentResearcherDetail
  163. Paging *paging.PagingItem `description:"分页数据"`
  164. }
  165. type AssessmentResearcherDetail struct {
  166. AssessmentResearcherId int `description:"研究员ID"`
  167. AdminId int `description:"用户ID"`
  168. RealName string `description:"用户姓名"`
  169. Enabled int `description:"状态:0-禁用;1-正常"`
  170. ViewAdmins []AssessmentResearcherDetail `description:"查看权限用户"`
  171. AssessmentAdmins []AssessmentResearcherDetail `description:"统计权限用户"`
  172. }
  173. func (m *AssessmentResearcher) Format2Detail() (item *AssessmentResearcherDetail) {
  174. item = new(AssessmentResearcherDetail)
  175. item.AssessmentResearcherId = m.AssessmentResearcherId
  176. item.AdminId = m.AdminId
  177. item.RealName = m.RealName
  178. item.Enabled = m.Enabled
  179. return
  180. }