english_company.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package models
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "time"
  8. )
  9. const (
  10. EnglishCompanyDisabled = iota
  11. EnglishCompanyEnabled
  12. EnglishCompanyHalfEnabled
  13. )
  14. // EnglishCompany 英文客户
  15. type EnglishCompany struct {
  16. CompanyId int `gorm:"column:company_id;primaryKey;autoIncrement" description:"英文客户ID"`
  17. CompanyName string `description:"客户名称"`
  18. CountryCode string `description:"国家Code"`
  19. Country string `description:"国家"`
  20. SellerId int `description:"销售ID"`
  21. SellerName string `description:"销售姓名"`
  22. ViewTotal int `description:"累计点击量/阅读量"`
  23. IsDeleted int `description:"删除状态:0-正常;1-已删除"`
  24. CreateTime time.Time `description:"创建时间"`
  25. ModifyTime time.Time `description:"更新时间"`
  26. Enabled int `description:"0-禁用; 1-启用; 2-部分禁用"`
  27. Status int `description:"1:正式,2:临时,3:终止"`
  28. }
  29. type EnglishCompanyListItem struct {
  30. EnglishCompany
  31. TodoStatusStr string
  32. TodoEndTime string
  33. TodoSellerId int
  34. }
  35. func (item *EnglishCompany) TableName() string {
  36. return "english_company"
  37. }
  38. func (item *EnglishCompany) Create() (err error) {
  39. err = global.DbMap[utils.DbNameReport].Create(item).Error
  40. return
  41. }
  42. // EnglishCompanySaveReq 英文客户-保存请求体
  43. type EnglishCompanySaveReq struct {
  44. CompanyId int `description:"客户ID"`
  45. CompanyName string `description:"客户名称"`
  46. CountryCode string `description:"国家代码"`
  47. Country string `description:"国家"`
  48. SellerId int `description:"销售ID"`
  49. EnPermissions []int `description:"英文权限IDs"`
  50. }
  51. func (item *EnglishCompany) Update(cols []string) (err error) {
  52. err = global.DbMap[utils.DbNameReport].Select(cols).Updates(item).Error
  53. return
  54. }
  55. // GetEnglishCompanyById 主键获取客户
  56. func GetEnglishCompanyById(id int) (item *EnglishCompany, err error) {
  57. sql := `SELECT * FROM english_company WHERE is_deleted = 0 AND company_id = ? LIMIT 1`
  58. err = global.DbMap[utils.DbNameReport].Raw(sql, id).First(&item).Error
  59. return
  60. }
  61. // GetEnglishCompanyByName 名称获取客户
  62. func GetEnglishCompanyByName(companyName string) (item *EnglishCompany, err error) {
  63. sql := `SELECT * FROM english_company WHERE is_deleted = 0 AND company_name = ? LIMIT 1`
  64. err = global.DbMap[utils.DbNameReport].Raw(sql, companyName).First(&item).Error
  65. return
  66. }
  67. // EnglishCompanyDelReq 英文客户-删除请求体
  68. type EnglishCompanyDelReq struct {
  69. CompanyId int `description:"客户ID"`
  70. }
  71. // DeleteEnglishCompanyAndEmails 删除英文客户及联系人
  72. func DeleteEnglishCompanyAndEmails(companyId int) (err error) {
  73. tx := global.DbMap[utils.DbNameReport].Begin()
  74. defer func() {
  75. if err != nil {
  76. _ = tx.Rollback()
  77. } else {
  78. _ = tx.Commit()
  79. }
  80. }()
  81. // 删除客户
  82. sql := `UPDATE english_company SET is_deleted = 1,modify_time = NOW() WHERE company_id = ? LIMIT 1`
  83. err = tx.Exec(sql, companyId).Error
  84. if err != nil {
  85. return
  86. }
  87. // 删除联系人
  88. sql = `UPDATE english_report_email SET is_deleted = 1,modify_time = NOW() WHERE company_id = ?`
  89. err = tx.Exec(sql, companyId).Error
  90. return
  91. }
  92. // EnglishCompanyPageListResp 英文客户-分页列表响应体
  93. type EnglishCompanyPageListResp struct {
  94. List []*EnglishCompanyResp
  95. Paging *paging.PagingItem `description:"分页数据"`
  96. }
  97. // EnglishCompanyResp 英文客户-列表响应体
  98. type EnglishCompanyResp struct {
  99. CompanyId int `description:"客户ID"`
  100. CompanyName string `description:"客户名称"`
  101. CountryCode string `description:"国家代码"`
  102. Country string `description:"国家"`
  103. SellerId int `description:"销售ID"`
  104. SellerName string `description:"销售姓名"`
  105. ViewTotal int `description:"累计点击量"`
  106. CreateTime string `description:"创建时间"`
  107. Enabled int `description:"0-禁用; 1-启用; 2-部分禁用"`
  108. TodoInfo *EnglishCompanyListTodo `description:"TODO任务信息"`
  109. EnPermissions []int `description:"英文权限"`
  110. }
  111. // EnglishCompanyListTodo 英文客户列表-TODO任务信息
  112. type EnglishCompanyListTodo struct {
  113. Deadline string `description:"未完成的todo任务的截止日期,截止目前还剩余的天数"`
  114. TodoEndTimeStr string `description:"未完成的todo任务的截止日期拼接格式"`
  115. //TodoEndTime time.Time `description:"未完成的todo任务的截止日期"`
  116. TodoStatus bool `description:"是否存在进行中任务"`
  117. CanConfirm bool `description:"是否允许完成任务"`
  118. HiddenConfirm bool `description:"是否隐藏完成任务按钮"`
  119. HiddenCreate bool `description:"是否隐藏新增/编辑按钮"`
  120. TodoButtonColor string `description:"任务按钮颜色: red; green; gray"`
  121. }
  122. // GetEnglishCompanyPageList 获取客户列表-分页
  123. func GetEnglishCompanyPageList(condition string, pars []interface{}, order string, startSize, pageSize int) (total int, list []*EnglishCompanyListItem, err error) {
  124. sql := `SELECT
  125. c.*,
  126. IF
  127. ( ct.status IS NULL, '无任务', ct.status ) AS todo_status_str,
  128. ct.seller_id as todo_seller_id,
  129. IF
  130. ( ct.end_time IS NULL or ct.status != '进行中', '9999-01-01', ct.end_time) AS todo_end_time
  131. FROM
  132. english_company AS c
  133. LEFT JOIN (
  134. SELECT
  135. b.*
  136. FROM
  137. (
  138. SELECT
  139. company_id,
  140. MAX( create_time ) AS ct
  141. FROM
  142. english_company_todo
  143. WHERE
  144. is_delete = 0
  145. AND STATUS != '已作废'
  146. GROUP BY
  147. company_id
  148. ) AS a
  149. LEFT JOIN english_company_todo AS b ON b.company_id = a.company_id
  150. AND b.create_time = a.ct
  151. ) AS ct ON c.company_id = ct.company_id
  152. WHERE
  153. c.is_deleted = 0 AND c.status = 1`
  154. sql += condition
  155. if order != "" {
  156. sql += order
  157. } else {
  158. sql += ` ORDER BY c.create_time DESC, c.company_id DESC`
  159. }
  160. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  161. var totalNull sql2.NullInt64
  162. err = global.DbMap[utils.DbNameReport].Raw(totalSql, pars...).Scan(&totalNull).Error
  163. if err != nil {
  164. return
  165. }
  166. if totalNull.Valid {
  167. total = int(totalNull.Int64)
  168. }
  169. sql += ` LIMIT ?,?`
  170. pars = append(pars, startSize, pageSize)
  171. err = global.DbMap[utils.DbNameReport].Raw(sql, pars...).Find(&list).Error
  172. return
  173. }
  174. // GetEnglishCompanyViewPageListResp 英文客户-点击量分页列表响应体
  175. type GetEnglishCompanyViewPageListResp struct {
  176. List []*EnglishCompanyViewResp
  177. Paging *paging.PagingItem `description:"分页数据"`
  178. }
  179. // EnglishCompanyViewResp 英文客户-点击量响应体
  180. type EnglishCompanyViewResp struct {
  181. EmailId int `description:"联系人ID"`
  182. UserName string `description:"联系人姓名"`
  183. Email string `description:"邮箱地址"`
  184. ViewTotal int `description:"累计点击量"`
  185. LastViewTime string `description:"创建时间"`
  186. }
  187. // GetEnglishCompanyList 获取英文客户列表
  188. func GetEnglishCompanyList(condition string, pars []interface{}, order string) (list []*EnglishCompany, err error) {
  189. sql := `SELECT * FROM english_company WHERE is_deleted = 0 `
  190. sql += condition
  191. if order != "" {
  192. sql += order
  193. } else {
  194. sql += ` ORDER BY create_time DESC`
  195. }
  196. err = global.DbMap[utils.DbNameReport].Raw(sql, pars...).Find(&list).Error
  197. return
  198. }
  199. // EnglishCompanyEditEnabledReq 禁启用请求体
  200. type EnglishCompanyEditEnabledReq struct {
  201. CompanyId int `description:"公司ID"`
  202. Enabled int `description:"1:有效,0:禁用"`
  203. }