company_product.go 8.9 KB

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