company.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_task/utils"
  5. "time"
  6. )
  7. type Company struct {
  8. CompanyId int `orm:"column(company_id);pk"`
  9. CompanyName string `description:"客户名称"`
  10. CreditCode string `description:"社会统一信用码"`
  11. CompanyCode string `description:"客户编码"`
  12. Sort int `description:"优先级"`
  13. IsFeeCustomer int `description:"是否付费用户"`
  14. Country string `description:"国家编码"`
  15. ProvinceId int `description:"省id"`
  16. CityId int `description:"市id"`
  17. Address string `description:"详细地址"`
  18. CompanyType int `orm:"column(type)" description:"客户标签,1:付费客户,2:试用客户,3:流失客户,4:潜在客户"`
  19. Enabled int `description:"用户状态"`
  20. CreatedTime time.Time `description:"创建时间"`
  21. LastUpdatedTime time.Time `description:"最后一次阅读时间"`
  22. Seller string `description:"销售员"`
  23. SellsId int `description:"销售员id"`
  24. CompanyBelong string `description:"客户所属,ficc:ficc客户,public_offering:公募客户,partner:合作伙伴"`
  25. StartDate string `description:"合同开始日期"`
  26. EndDate string `description:"合同结束日期"`
  27. LoseReason string `description:"流失原因"`
  28. LastType int `description:"原客户标签"`
  29. IsVip int `description:"0:普通用户,1:大客户"`
  30. LossTime time.Time `description:"流失时间"`
  31. FirstStartDate string `description:"首次设置为试用客户开始时间"`
  32. FirstEndDate string `description:"首次设置为试用客户结束时间"`
  33. DateType int `description:"设置流失类型,1:1个月,2:2个月,3:3个月"`
  34. InteractionNum int `description:"用户总的互动量"`
  35. }
  36. func GetCompanyByName(companyName string) (item *Company, err error) {
  37. o := orm.NewOrm()
  38. sql := `SELECT * FROM company WHERE company_name=? `
  39. err = o.Raw(sql, companyName).QueryRow(&item)
  40. return
  41. }
  42. func GetCompanyById(companyId int) (item *Company, err error) {
  43. o := orm.NewOrm()
  44. sql := `SELECT * FROM company WHERE company_id=? `
  45. err = o.Raw(sql, companyId).QueryRow(&item)
  46. return
  47. }
  48. func GetCompanyCountByCreditCode(CreditCode string) (count int, err error) {
  49. o := orm.NewOrm()
  50. sql := `SELECT COUNT(1) AS count FROM company WHERE credit_code=? `
  51. err = o.Raw(sql, CreditCode).QueryRow(&count)
  52. return
  53. }
  54. func AddCompany(item *Company) (newId int64, err error) {
  55. o := orm.NewOrm()
  56. newId, err = o.Insert(item)
  57. return
  58. }
  59. func GetCompany() (items []*Company, err error) {
  60. sql := `SELECT * FROM company WHERE type =1 `
  61. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  62. return
  63. }
  64. // 待冻结客户
  65. type CompanyNeedFreeze struct {
  66. CompanyId int
  67. CompanyProductId int
  68. ProductId int
  69. CompanyName string
  70. ProductName string
  71. Status string
  72. }
  73. func GetCompanyNeedFreeze(endDate string) (items []*CompanyNeedFreeze, err error) {
  74. o := orm.NewOrm()
  75. sql := `SELECT a.company_id,b.company_product_id,b.product_id,a.company_name,b.product_name,b.status
  76. FROM company AS a
  77. INNER JOIN company_product AS b ON a.company_id=b.company_id
  78. WHERE b.status='试用'
  79. AND b.end_date<=? `
  80. _, err = o.Raw(sql, endDate).QueryRows(&items)
  81. return
  82. }
  83. func GetCompanyNeedLoss(endDate string) (items []*CompanyNeedFreeze, err error) {
  84. o := orm.NewOrm()
  85. sql := `SELECT a.company_id,b.company_product_id,b.product_id,a.company_name,b.product_name,b.status
  86. FROM company AS a
  87. INNER JOIN company_product AS b ON a.company_id=b.company_id
  88. WHERE b.status='冻结'
  89. AND b.freeze_end_date<=? AND b.init_status != '永续' ` // 权益的永续客户冻结不转流失
  90. _, err = o.Raw(sql, endDate).QueryRows(&items)
  91. return
  92. }
  93. // 正式
  94. func GetCompanyNeedTryOut(endDate string) (items []*CompanyNeedFreeze, err error) {
  95. o := orm.NewOrm()
  96. sql := `SELECT a.company_id,b.company_product_id,b.product_id,a.company_name,b.product_name,b.status
  97. FROM company AS a
  98. INNER JOIN company_product AS b ON a.company_id=b.company_id
  99. WHERE b.status='正式'
  100. AND b.end_date<=? AND b.init_status != '永续' ` //权益的永续客户正式不转试用
  101. _, err = o.Raw(sql, endDate).QueryRows(&items)
  102. return
  103. }
  104. type CompanyIndustry struct {
  105. IndustryId int `description:"行业id"`
  106. IndustryName string `description:"行业名称"`
  107. ParentId int `description:"父级id"`
  108. Classify string `description:"分类"`
  109. }
  110. func GetCompanyIndustryByName(industryName string) (items *CompanyIndustry, err error) {
  111. o := orm.NewOrm()
  112. sql := `SELECT * FROM company_industry WHERE industry_name=? `
  113. err = o.Raw(sql, industryName).QueryRow(&items)
  114. return
  115. }
  116. func ModifyCompanyCreditCode(companyId int, creditCode string) (err error) {
  117. o := orm.NewOrm()
  118. sql := `UPDATE company SET credit_code=? WHERE company_id=? AND credit_code='' `
  119. _, err = o.Raw(sql, creditCode, companyId).Exec()
  120. return
  121. }
  122. func ModifyCompanyIndustry(industryId, companyId int, industryName string) (err error) {
  123. o := orm.NewOrm()
  124. sql := `UPDATE company_product SET industry_id=?,industry_name=? WHERE company_id=? AND product_id=1 AND industry_id=0 `
  125. _, err = o.Raw(sql, industryId, industryName, companyId).Exec()
  126. return
  127. }
  128. // 权益 正式->永续
  129. func GetCompanyNeedTryOutXClassRai(endDate string) (items []*CompanyNeedFreeze, err error) {
  130. o := orm.NewOrm()
  131. sql := `SELECT a.company_id,b.company_product_id,b.product_id,a.company_name,b.product_name,b.status
  132. FROM company AS a
  133. INNER JOIN company_product AS b ON a.company_id=b.company_id
  134. WHERE b.status='正式'
  135. AND b.end_date<=? AND b.init_status = '永续' ` //权益的永续客户正式不转试用
  136. _, err = o.Raw(sql, endDate).QueryRows(&items)
  137. return
  138. }
  139. // 权益 永续->冻结
  140. func GetCompanyNeedFreezeXClassRai(endDate string) (items []*CompanyNeedFreeze, err error) {
  141. o := orm.NewOrm()
  142. sql := `SELECT a.company_id,b.company_product_id,b.product_id,a.company_name,b.product_name,b.status
  143. FROM company AS a
  144. INNER JOIN company_product AS b ON a.company_id=b.company_id
  145. WHERE b.status='永续'
  146. AND b.end_date<=? AND b.init_status = '永续' `
  147. _, err = o.Raw(sql, endDate).QueryRows(&items)
  148. return
  149. }
  150. // GetCompanyListByCompanyId 根据公司ID获取公司信息
  151. func GetCompanyListByCompanyId(companyId []int) (items []*Company, err error) {
  152. lenArr := len(companyId)
  153. if lenArr == 0 {
  154. return
  155. }
  156. o := orm.NewOrm()
  157. sql := `SELECT interaction_num , company_id FROM company AS a WHERE a.company_id IN (` + utils.GetOrmInReplace(lenArr) + `) `
  158. _, err = o.Raw(sql, companyId).QueryRows(&items)
  159. return
  160. }