company_product.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_task/utils"
  5. "time"
  6. )
  7. type CompanyOperationRecord struct {
  8. Id int `orm:"column(id);pk"`
  9. CompanyId int `description:"客户id"`
  10. CompanyName string `description:"客户名称"`
  11. SysUserId int `description:"操作者id"`
  12. SysRealName string `description:"操作者名称"`
  13. Remark string `description:"备注"`
  14. Operation string `description:"操作"`
  15. CreateTime time.Time `description:"操作时间"`
  16. ProductId int `description:"产品id"`
  17. ProductName string `description:"产品名称"`
  18. ApproveUserId int `description:"审批人id"`
  19. ApproveRealName string `description:"审批人姓名"`
  20. ApproveContent string `description:"审批人内容"`
  21. ApproveRemark string `description:"审批人内容"`
  22. Status string `description:"状态"`
  23. }
  24. //新增操作记录
  25. func AddCompanyOperationRecord(item *CompanyOperationRecord) (lastId int64, err error) {
  26. o := orm.NewOrm()
  27. lastId, err = o.Insert(item)
  28. return
  29. }
  30. func CompanyFreeze(companyId, productId int) (companyReportPermissionList []*CompanyReportPermission, err error) {
  31. o := orm.NewOrm()
  32. freezeStartDate := time.Now().Format(utils.FormatDate)
  33. freezeEndDate := time.Now().AddDate(0, 3, 0).Format(utils.FormatDate)
  34. //FICC客户冻结期由三个月改为两个月
  35. if productId == 1 {
  36. freezeEndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  37. }
  38. // 权益客户 冻结期 改为两个月
  39. if productId == 2 {
  40. freezeEndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  41. }
  42. //客户产品 状态 变更
  43. sql := `UPDATE company_product SET status='冻结',is_formal=0,is_suspend=0,freeze_time=NOW(),modify_time=NOW(),start_date=?,end_date=?,freeze_start_date=?,freeze_end_date=?,try_stage=1 WHERE company_id=? AND product_id=? `
  44. _, err = o.Raw(sql, freezeStartDate, freezeEndDate, freezeStartDate, freezeEndDate, companyId, productId).Exec()
  45. if err != nil {
  46. return
  47. }
  48. //客户产品权限 状态 变更
  49. //获取需要变更的 客户产品权限
  50. oldPermissionEndDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  51. sql = `SELECT *
  52. FROM company_report_permission
  53. WHERE status='试用' AND end_date<=? AND company_id=? AND product_id=? `
  54. total, err := o.Raw(sql, oldPermissionEndDate, companyId, productId).QueryRows(&companyReportPermissionList)
  55. if err != nil {
  56. return
  57. }
  58. if total > 0 {
  59. sql = `UPDATE company_report_permission SET status='关闭',modify_time=NOW()
  60. WHERE status='试用' AND end_date<=? AND company_id=? AND product_id=? `
  61. _, err = o.Raw(sql, oldPermissionEndDate, companyId, productId).Exec()
  62. if err != nil {
  63. return
  64. }
  65. }
  66. //客户状态变更
  67. sql = `UPDATE company SET type=3,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
  68. _, err = o.Raw(sql, freezeStartDate, freezeEndDate, companyId).Exec()
  69. return
  70. }
  71. func CompanyLoss(companyId, productId int) (err error) {
  72. o := orm.NewOrm()
  73. //客户产品状态变更
  74. sql := `UPDATE company_product SET status='流失',is_formal=0,loss_time=NOW(),modify_time=NOW(),lose_reason='冻结到期系统自动流失',try_stage=1,todo_status="无任务",todo_create_time=null,todo_approve_time=null,todo_modify_time=null,todo_end_time=null WHERE company_id=? AND product_id=? `
  75. _, err = o.Raw(sql, companyId, productId).Exec()
  76. if err != nil {
  77. return
  78. }
  79. // 将历史的任务给标记删除掉
  80. sql = `UPDATE company_todo SET is_delete=1,modify_time=NOW() WHERE company_id=? AND product_id=? AND is_delete = 0 `
  81. _, err = o.Raw(sql, companyId, productId).Exec()
  82. if err != nil {
  83. return
  84. }
  85. //客户状态变更
  86. sql = `UPDATE company SET type=3,last_updated_time=NOW() WHERE company_id=? `
  87. _, err = o.Raw(sql, companyId).Exec()
  88. if err != nil {
  89. return
  90. }
  91. return
  92. }
  93. //正式转试用
  94. func CompanyTryOut(companyId, productId int) (companyReportPermissionList []*CompanyReportPermission, err error) {
  95. o := orm.NewOrm()
  96. startDate := time.Now().Format(utils.FormatDate)
  97. endDate := time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  98. //客户产品 状态 变更
  99. sql := `UPDATE company_product SET status='试用',start_date=?,end_date=?,modify_time=NOW(),try_out_time=NOW(),renewal_reason="",package_type=0,try_stage=1 WHERE company_id=? AND product_id=? `
  100. _, err = o.Raw(sql, startDate, endDate, companyId, productId).Exec()
  101. if err != nil {
  102. return
  103. }
  104. //客户产品权限 状态 变更
  105. //获取需要变更的 客户产品权限
  106. oldPermissionEndDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  107. sql = `SELECT *
  108. FROM company_report_permission
  109. WHERE status='正式' AND end_date<=? AND company_id=? AND product_id=? `
  110. total, err := o.Raw(sql, oldPermissionEndDate, companyId, productId).QueryRows(&companyReportPermissionList)
  111. if err != nil {
  112. return
  113. }
  114. if total > 0 {
  115. sql = `UPDATE company_report_permission SET status='试用',start_date=?,end_date=?,modify_time=NOW()
  116. WHERE status='正式' AND end_date<=? AND company_id=? AND product_id=? `
  117. _, err = o.Raw(sql, startDate, endDate, oldPermissionEndDate, companyId, productId).Exec()
  118. if err != nil {
  119. return
  120. }
  121. }
  122. //客户状态变更
  123. sql = `UPDATE company SET type=2,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
  124. _, err = o.Raw(sql, startDate, endDate, companyId).Exec()
  125. return
  126. }
  127. func GetCompanyOldDataSync() (items []*Company, err error) {
  128. sql := `SELECT * FROM company WHERE company_id NOT IN(
  129. SELECT company_id FROM company_product
  130. )
  131. AND company_id<>1 `
  132. o := orm.NewOrm()
  133. _, err = o.Raw(sql).QueryRows(&items)
  134. return
  135. }
  136. type CompanyProduct struct {
  137. CompanyProductId int `orm:"column(company_product_id);pk" description:"客户产品id"`
  138. CompanyId int `description:"客户id"`
  139. ProductId int `description:"产品id"`
  140. ProductName string `description:"产品名称"`
  141. CompanyName string `description:"客户名称"`
  142. Source string `description:"来源"`
  143. Reasons string `description:"新增理由"`
  144. Status string `description:"客户状态"`
  145. IndustryId int `description:"行业id"`
  146. IndustryName string `description:"行业名称"`
  147. SellerId int `description:"销售id"`
  148. SellerName string `description:"销售名称"`
  149. GroupId int `description:"销售分组id"`
  150. DepartmentId int `description:"销售部门id"`
  151. IsSuspend int `description:"1:暂停,0:启用"`
  152. SuspendTime time.Time `description:"暂停启用时间"`
  153. ApproveStatus string `description:"审批状态:'审批中','通过','驳回'"`
  154. FreezeTime time.Time `description:"冻结时间"`
  155. Remark string `description:"备注信息"`
  156. CreateTime time.Time `description:"创建时间"`
  157. ModifyTime time.Time `description:"修改时间"`
  158. StartDate string `description:"开始日期"`
  159. EndDate string `description:"结束日期"`
  160. ContractEndDate string `description:"合同结束日期"`
  161. LoseReason string `description:"流失原因"`
  162. LossTime time.Time `description:"流失时间"`
  163. CompanyType string `description:"客户类型"`
  164. OpenCode string `description:"开放给第三方的编码,不让第三方定位我们的客户信息"`
  165. Scale string `description:"管理规模,空不填,1::50亿以下,2:50~100亿,3:100亿以上。"`
  166. ViewTotal int `description:"总阅读次数"`
  167. RoadShowTotal int `description:"累计路演次数"`
  168. LastViewTime time.Time `description:"最后一次阅读时间"`
  169. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  170. IsFormal int `description:"是否已经转正式,0是没有转正式,1是已经转过正式"`
  171. TodoStatus string `description:"任务处理状态;枚举值:'无任务','未完成','已完成'"`
  172. TodoCreateTime time.Time `description:"任务创建时间"`
  173. TodoApproveTime time.Time `description:"任务审批时间"`
  174. TryStage int `description:"试用客户子标签:1未分类、2 推进、3 跟踪、4 预备"`
  175. }
  176. //获取产品详情
  177. func GetCompanyProduct(companyId int, productId int) (companyProduct *CompanyProduct, err error) {
  178. o := orm.NewOrm()
  179. sql := `SELECT *
  180. FROM company_product WHERE company_id= ? AND product_id = ? `
  181. err = o.Raw(sql, companyId, productId).QueryRow(&companyProduct)
  182. return
  183. }
  184. //新增客户产品
  185. func AddCompanyProduct(item *CompanyProduct) (newId int64, err error) {
  186. o := orm.NewOrm()
  187. newId, err = o.Insert(item)
  188. return
  189. }
  190. type Sellers struct {
  191. AdminId int `description:"销售id"`
  192. RealName string `description:"销售姓名"`
  193. Email string `description:"销售邮箱"`
  194. OpenId string `description:"销售openid"`
  195. Mobile string `description:"销售电话"`
  196. RoleTypeCode string `description:"角色编码"`
  197. }
  198. func GetSellers() (items []*Sellers, err error) {
  199. o := orm.NewOrm()
  200. sql := `SELECT a.real_name,a.email,c.open_id,a.mobile,b.mobile,a.role_type_code,a.admin_id
  201. FROM admin AS a
  202. LEFT JOIN wx_user AS b ON a.mobile=b.mobile
  203. LEFT JOIN user_record AS c ON b.user_id=c.user_id
  204. WHERE role_type_code IN('ficc_seller','ficc_group','ficc_team','rai_seller','rai_group')
  205. and (
  206. (a.email != "") or ( c.open_id<>'' and c.create_platform=1)
  207. )`
  208. _, err = o.Raw(sql).QueryRows(&items)
  209. return
  210. }
  211. func GetRemindCompany(sellerId int, endDate string) (items []*CompanyProduct, err error) {
  212. o := orm.NewOrm()
  213. sql := ` SELECT b.end_date,b.contract_end_date,a.company_name,b.status,b.seller_id,b.seller_name FROM
  214. company a
  215. INNER JOIN company_product AS b ON a.company_id = b.company_id
  216. WHERE
  217. b.seller_id = ?
  218. AND (
  219. ( b.contract_end_date = ? AND b.status = "正式" ) OR (b.end_date = ? AND b.status = "试用")
  220. ) ORDER BY b.status DESC `
  221. _, err = o.Raw(sql, sellerId, endDate, endDate).QueryRows(&items)
  222. return
  223. }
  224. // GetAllCompanyProduct 获取所有客户产品列表(永续、正式、试用、冻结)
  225. func GetAllCompanyProduct() (items []*CompanyProduct, err error) {
  226. o := orm.NewOrm()
  227. sql := ` SELECT * from company_product WHERE status in ("永续","正式","试用","冻结") `
  228. _, err = o.Raw(sql).QueryRows(&items)
  229. return
  230. }