company_product.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. 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=? `
  40. _, err = o.Raw(sql, freezeStartDate, freezeEndDate, freezeStartDate, freezeEndDate, companyId, productId).Exec()
  41. if err != nil {
  42. return
  43. }
  44. //客户产品权限 状态 变更
  45. //获取需要变更的 客户产品权限
  46. oldPermissionEndDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  47. sql = `SELECT *
  48. FROM company_report_permission
  49. WHERE status='试用' AND end_date<=? AND company_id=? AND product_id=? `
  50. total, err := o.Raw(sql, oldPermissionEndDate, companyId, productId).QueryRows(&companyReportPermissionList)
  51. if err != nil {
  52. return
  53. }
  54. if total > 0 {
  55. sql = `UPDATE company_report_permission SET status='关闭',modify_time=NOW()
  56. WHERE status='试用' AND end_date<=? AND company_id=? AND product_id=? `
  57. _, err = o.Raw(sql, oldPermissionEndDate, companyId, productId).Exec()
  58. if err != nil {
  59. return
  60. }
  61. }
  62. //客户状态变更
  63. sql = `UPDATE company SET type=3,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
  64. _, err = o.Raw(sql, freezeStartDate, freezeEndDate, companyId).Exec()
  65. return
  66. }
  67. func CompanyLoss(companyId, productId int) (err error) {
  68. o := orm.NewOrm()
  69. //客户产品状态变更
  70. 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=? `
  71. _, err = o.Raw(sql, companyId, productId).Exec()
  72. if err != nil {
  73. return
  74. }
  75. // 将历史的任务给标记删除掉
  76. sql = `UPDATE company_todo SET is_delete=1,modify_time=NOW() WHERE company_id=? AND product_id=? AND is_delete = 0 `
  77. _, err = o.Raw(sql, companyId, productId).Exec()
  78. if err != nil {
  79. return
  80. }
  81. //客户状态变更
  82. sql = `UPDATE company SET type=3,last_updated_time=NOW() WHERE company_id=? `
  83. _, err = o.Raw(sql, companyId).Exec()
  84. if err != nil {
  85. return
  86. }
  87. return
  88. }
  89. //正式转试用
  90. func CompanyTryOut(companyId, productId int) (companyReportPermissionList []*CompanyReportPermission, err error) {
  91. o := orm.NewOrm()
  92. startDate := time.Now().Format(utils.FormatDate)
  93. endDate := time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  94. //客户产品 状态 变更
  95. 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=? `
  96. _, err = o.Raw(sql, startDate, endDate, companyId, productId).Exec()
  97. if err != nil {
  98. return
  99. }
  100. //客户产品权限 状态 变更
  101. //获取需要变更的 客户产品权限
  102. oldPermissionEndDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  103. sql = `SELECT *
  104. FROM company_report_permission
  105. WHERE status='正式' AND end_date<=? AND company_id=? AND product_id=? `
  106. total, err := o.Raw(sql, oldPermissionEndDate, companyId, productId).QueryRows(&companyReportPermissionList)
  107. if err != nil {
  108. return
  109. }
  110. if total > 0 {
  111. sql = `UPDATE company_report_permission SET status='试用',start_date=?,end_date=?,modify_time=NOW()
  112. WHERE status='正式' AND end_date<=? AND company_id=? AND product_id=? `
  113. _, err = o.Raw(sql, startDate, endDate, oldPermissionEndDate, companyId, productId).Exec()
  114. if err != nil {
  115. return
  116. }
  117. }
  118. //客户状态变更
  119. sql = `UPDATE company SET type=2,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
  120. _, err = o.Raw(sql, startDate, endDate, companyId).Exec()
  121. return
  122. }
  123. func GetCompanyOldDataSync() (items []*Company, err error) {
  124. sql := `SELECT * FROM company WHERE company_id NOT IN(
  125. SELECT company_id FROM company_product
  126. )
  127. AND company_id<>1 `
  128. o := orm.NewOrm()
  129. _, err = o.Raw(sql).QueryRows(&items)
  130. return
  131. }
  132. type CompanyProduct struct {
  133. CompanyProductId int `orm:"column(company_product_id);pk" description:"客户产品id"`
  134. CompanyId int `description:"客户id"`
  135. ProductId int `description:"产品id"`
  136. ProductName string `description:"产品名称"`
  137. CompanyName string `description:"客户名称"`
  138. Source string `description:"来源"`
  139. Reasons string `description:"新增理由"`
  140. Status string `description:"客户状态"`
  141. IndustryId int `description:"行业id"`
  142. IndustryName string `description:"行业名称"`
  143. SellerId int `description:"销售id"`
  144. SellerName string `description:"销售名称"`
  145. GroupId int `description:"销售分组id"`
  146. DepartmentId int `description:"销售部门id"`
  147. IsSuspend int `description:"1:暂停,0:启用"`
  148. SuspendTime time.Time `description:"暂停启用时间"`
  149. ApproveStatus string `description:"审批状态:'审批中','通过','驳回'"`
  150. FreezeTime time.Time `description:"冻结时间"`
  151. Remark string `description:"备注信息"`
  152. CreateTime time.Time `description:"创建时间"`
  153. ModifyTime time.Time `description:"修改时间"`
  154. StartDate string `description:"开始日期"`
  155. EndDate string `description:"结束日期"`
  156. ContractEndDate string `description:"合同结束日期"`
  157. LoseReason string `description:"流失原因"`
  158. LossTime time.Time `description:"流失时间"`
  159. CompanyType string `description:"客户类型"`
  160. OpenCode string `description:"开放给第三方的编码,不让第三方定位我们的客户信息"`
  161. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  162. IsFormal int `description:"是否已经转正式,0是没有转正式,1是已经转过正式"`
  163. }
  164. //获取产品详情
  165. func GetCompanyProduct(companyId int, productId int) (companyProduct *CompanyProduct, err error) {
  166. o := orm.NewOrm()
  167. sql := `SELECT *
  168. FROM company_product WHERE company_id= ? AND product_id = ? `
  169. err = o.Raw(sql, companyId, productId).QueryRow(&companyProduct)
  170. return
  171. }
  172. //新增客户产品
  173. func AddCompanyProduct(item *CompanyProduct) (newId int64, err error) {
  174. o := orm.NewOrm()
  175. newId, err = o.Insert(item)
  176. return
  177. }
  178. type Sellers struct {
  179. AdminId int `description:"销售id"`
  180. RealName string `description:"销售姓名"`
  181. Email string `description:"销售邮箱"`
  182. OpenId string `description:"销售openid"`
  183. Mobile string `description:"销售电话"`
  184. RoleTypeCode string `description:"角色编码"`
  185. }
  186. func GetSellers() (items []*Sellers, err error) {
  187. o := orm.NewOrm()
  188. sql := `SELECT a.real_name,a.email,c.open_id,a.mobile,b.mobile,a.role_type_code,a.admin_id
  189. FROM admin AS a
  190. LEFT JOIN wx_user AS b ON a.mobile=b.mobile
  191. LEFT JOIN user_record AS c ON b.user_id=c.user_id
  192. WHERE role_type_code IN('ficc_seller','ficc_group','ficc_team','rai_seller','rai_group')
  193. and (
  194. (a.email != "") or ( c.open_id<>'' and c.create_platform=1)
  195. )`
  196. _, err = o.Raw(sql).QueryRows(&items)
  197. return
  198. }
  199. func GetRemindCompany(sellerId int, endDate string) (items []*CompanyProduct, err error) {
  200. o := orm.NewOrm()
  201. sql := ` SELECT b.end_date,b.contract_end_date,a.company_name,b.status,b.seller_id,b.seller_name FROM
  202. company a
  203. INNER JOIN company_product AS b ON a.company_id = b.company_id
  204. WHERE
  205. b.seller_id = ?
  206. AND (
  207. ( b.contract_end_date = ? AND b.status = "正式" ) OR (b.end_date = ? AND b.status = "试用")
  208. ) ORDER BY b.status DESC `
  209. _, err = o.Raw(sql, sellerId, endDate, endDate).QueryRows(&items)
  210. return
  211. }
  212. // GetAllCompanyProduct 获取所有客户产品列表(永续、正式、试用、冻结)
  213. func GetAllCompanyProduct() (items []*CompanyProduct, err error) {
  214. o := orm.NewOrm()
  215. sql := ` SELECT * from company_product WHERE status in ("永续","正式","试用","冻结") `
  216. _, err = o.Raw(sql).QueryRows(&items)
  217. return
  218. }