maycur_company_profile.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strings"
  6. "time"
  7. )
  8. // MaycurCompanyProfile 每刻报销-客户档案表
  9. type MaycurCompanyProfile struct {
  10. MaycurCompanyProfileId int `orm:"column(maycur_company_profile_id);pk"`
  11. BizCode string `description:"选项编码"`
  12. Name string `description:"公司名称"`
  13. NameDisplay string `description:"公司名称-JSON"`
  14. Enabled int `description:"状态:0-禁用; 1-启用"`
  15. ParentCode string `description:"父级选项编码"`
  16. Authzs string `description:"客户可见性-JSON"`
  17. CreateTime time.Time `description:"创建时间"`
  18. ModifyTime time.Time `description:"更新时间"`
  19. }
  20. func (m *MaycurCompanyProfile) TableName() string {
  21. return "maycur_company_profile"
  22. }
  23. func (m *MaycurCompanyProfile) Create() (err error) {
  24. o := orm.NewOrm()
  25. id, err := o.Insert(m)
  26. if err != nil {
  27. return
  28. }
  29. m.MaycurCompanyProfileId = int(id)
  30. return
  31. }
  32. func (m *MaycurCompanyProfile) CreateMulti(items []*MaycurCompanyProfile) (err error) {
  33. if len(items) == 0 {
  34. return
  35. }
  36. o := orm.NewOrm()
  37. _, err = o.InsertMulti(len(items), items)
  38. return
  39. }
  40. func (m *MaycurCompanyProfile) Update(cols []string) (err error) {
  41. o := orm.NewOrm()
  42. _, err = o.Update(m, cols...)
  43. return
  44. }
  45. func (m *MaycurCompanyProfile) Del() (err error) {
  46. o := orm.NewOrm()
  47. sql := `DELETE FROM maycur_company_profile WHERE maycur_company_profile_id = ? LIMIT 1`
  48. _, err = o.Raw(sql, m.MaycurCompanyProfileId).Exec()
  49. return
  50. }
  51. func (m *MaycurCompanyProfile) GetItemById(id int) (err error) {
  52. o := orm.NewOrm()
  53. sql := `SELECT * FROM maycur_company_profile WHERE maycur_company_profile_id = ? LIMIT 1`
  54. err = o.Raw(sql, id).QueryRow(&m)
  55. return
  56. }
  57. func (m *MaycurCompanyProfile) GetItemByCondition(condition string, pars []interface{}) (err error) {
  58. o := orm.NewOrm()
  59. sql := `SELECT * FROM maycur_company_profile WHERE 1=1 `
  60. sql += condition
  61. sql += ` LIMIT 1`
  62. err = o.Raw(sql, pars).QueryRow(&m)
  63. return
  64. }
  65. func (m *MaycurCompanyProfile) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  66. o := orm.NewOrm()
  67. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  68. err = o.Raw(sql, pars).QueryRow(&count)
  69. return
  70. }
  71. func (m *MaycurCompanyProfile) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*MaycurCompanyProfile, err error) {
  72. o := orm.NewOrm()
  73. fields := strings.Join(fieldArr, ",")
  74. if len(fieldArr) == 0 {
  75. fields = `*`
  76. }
  77. order := `ORDER BY create_time DESC`
  78. if orderRule != "" {
  79. order = ` ORDER BY ` + orderRule
  80. }
  81. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  82. _, err = o.Raw(sql, pars).QueryRows(&items)
  83. return
  84. }
  85. func (m *MaycurCompanyProfile) Clear() (err error) {
  86. o := orm.NewOrm()
  87. sql := `TRUNCATE TABLE maycur_company_profile`
  88. _, err = o.Raw(sql).Exec()
  89. return
  90. }
  91. // MultiCreateAndUpdate 批量新增及更新
  92. func (m *MaycurCompanyProfile) MultiCreateAndUpdate(newProfiles, updateProfiles []*MaycurCompanyProfile, updateCols []string) (err error) {
  93. o := orm.NewOrm()
  94. tx, err := o.Begin()
  95. if err != nil {
  96. return
  97. }
  98. defer func() {
  99. if err != nil {
  100. _ = tx.Rollback()
  101. } else {
  102. _ = tx.Commit()
  103. }
  104. }()
  105. if len(newProfiles) > 0 {
  106. _, err = tx.InsertMulti(len(newProfiles), newProfiles)
  107. if err != nil {
  108. return
  109. }
  110. }
  111. if len(updateProfiles) > 0 {
  112. for _, p := range updateProfiles {
  113. _, err = o.Update(p, updateCols...)
  114. if err != nil {
  115. return
  116. }
  117. }
  118. }
  119. return
  120. }
  121. // MaycurSyncCompanyListWithSeller 需要同步的中英文客户及销售
  122. type MaycurSyncCompanyListWithSeller struct {
  123. CompanyId int `description:"客户ID"`
  124. CompanyName string `description:"客户姓名"`
  125. SellerIds string `description:"销售IDs"`
  126. IsShare int `description:"是否为共享客户"`
  127. ShareSellerId int `description:"共享销售ID"`
  128. }
  129. // GetMaycurSyncCompanyListWithSeller 获取需要同步的中英文客户及销售
  130. func GetMaycurSyncCompanyListWithSeller() (items []*MaycurSyncCompanyListWithSeller, err error) {
  131. o := orm.NewOrm()
  132. sql := `SELECT
  133. a.company_id,
  134. a.company_name,
  135. GROUP_CONCAT(DISTINCT b.seller_id) AS seller_ids
  136. FROM
  137. company AS a
  138. JOIN company_product AS b ON a.company_id = b.company_id
  139. JOIN admin AS c ON b.seller_id = c.admin_id AND c.enabled = 1
  140. WHERE
  141. b.status IN ("试用", "正式", "永续", "冻结")
  142. GROUP BY
  143. a.company_id`
  144. _, err = o.Raw(sql).QueryRows(&items)
  145. return
  146. }