assessment_researcher.go 7.1 KB

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