company.go 6.2 KB

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