company_product.go 8.1 KB

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