financial_analyst.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package financial_analyst
  2. import (
  3. "errors"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. "eta/eta_mini_ht_api/models"
  6. "gorm.io/gorm"
  7. "gorm.io/gorm/clause"
  8. "time"
  9. )
  10. type AnalystStatus string
  11. const (
  12. AnalystStatusEnabled AnalystStatus = "enabled"
  13. AnalystStatusDisabled AnalystStatus = "disabled"
  14. columns = "id,eta_id,name,head_img_url,head_origin_img_url,introduction,position,investment_certificate,professional_certificate"
  15. )
  16. type CrmFinancialAnalyst struct {
  17. Id int `gorm:"primaryKey;autoIncrement;column:id;comment:主键"`
  18. ETAId int `gorm:"column:eta_id"`
  19. HTId int `gorm:"column:ht_id"`
  20. Name string `gorm:"column:name"`
  21. HeadImgURL string `gorm:"column:head_img_url"`
  22. HeadOriginImgUrl string `gorm:"column:head_origin_img_url"`
  23. Position string `gorm:"column:position"`
  24. InvestmentCertificate string `gorm:"column:investment_certificate"`
  25. ProfessionalCertificate string `gorm:"column:professional_certificate"`
  26. Introduction string `gorm:"column:introduction"`
  27. Status AnalystStatus `gorm:"column:status"`
  28. Deleted bool `gorm:"column:deleted"`
  29. CreatedTime time.Time `gorm:"column:created_time;type:timestamps;comment:创建时间"`
  30. UpdatedTime time.Time `gorm:"column:updated_time;type:timestamps;comment:更新时间"`
  31. }
  32. func BatchInsertOrUpdate(list []CrmFinancialAnalyst) (err error) {
  33. db := models.Main()
  34. OnConflictFunc := clause.OnConflict{
  35. Columns: []clause.Column{{Name: "eta_id"}},
  36. DoUpdates: clause.AssignmentColumns([]string{"name", "status", "deleted"}),
  37. }
  38. // 执行批量插入或更新操作
  39. err = db.Clauses(OnConflictFunc).Create(&list).Error
  40. return
  41. }
  42. func (fa *CrmFinancialAnalyst) BeforeCreate(db *gorm.DB) (err error) {
  43. fa.CreatedTime = time.Now()
  44. return
  45. }
  46. func GetAnalystById(id int) (analyst CrmFinancialAnalyst, err error) {
  47. db := models.Main()
  48. err = db.Select(columns).Where("id = ?", id).First(&analyst).Error
  49. return
  50. }
  51. func GetAnalystByName(name string) (analyst CrmFinancialAnalyst, err error) {
  52. db := models.Main()
  53. err = db.Select(columns).Where("name = ?", name).First(&analyst).Error
  54. return
  55. }
  56. func GetCount() (total int64, latestId int64) {
  57. db := models.Main()
  58. err := db.Model(&CrmFinancialAnalyst{}).Select("count(*) count").Where("deleted = ? and investment_certificate !=''", false).Scan(&total).Error
  59. if err != nil {
  60. logger.Error("查询研究员列表失败,%v", err)
  61. return 0, 0
  62. }
  63. err = db.Model(&CrmFinancialAnalyst{}).Select("MAX(id) id").Where("deleted = ? and investment_certificate !=''", false).Scan(&latestId).Error
  64. if err != nil {
  65. logger.Error("查询研究员列表失败,%v", err)
  66. return 0, 0
  67. }
  68. return
  69. }
  70. func GetAnalystList(latestId int64, offset int, limit int) (analysts []CrmFinancialAnalyst, err error) {
  71. if latestId < 0 {
  72. err = errors.New("非法的id参数")
  73. logger.Error("非法的id参数:%d", latestId)
  74. return
  75. }
  76. if limit <= 0 {
  77. err = errors.New("非法的limit参数")
  78. logger.Error("非法的limit参数:%d", limit)
  79. }
  80. db := models.Main()
  81. err = db.Select(columns).Where(" id<= ? ", latestId).Where("deleted = ? and investment_certificate !=''", false).Order("created_time desc").Offset(offset).Limit(limit).Find(&analysts).Error
  82. return
  83. }