company_product.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. LoseReason string `description:"流失原因"`
  147. LossTime time.Time `description:"流失时间"`
  148. CompanyType string `description:"客户类型"`
  149. }
  150. //获取产品详情
  151. func GetCompanyProduct(companyId int,productId int)(companyProduct *CompanyProduct,err error){
  152. o := orm.NewOrm()
  153. sql := `SELECT *
  154. FROM company_product WHERE company_id= ? AND product_id = ? `
  155. err = o.Raw(sql,companyId,productId).QueryRow(&companyProduct)
  156. return
  157. }
  158. //新增客户产品
  159. func AddCompanyProduct(item *CompanyProduct) (newId int64, err error) {
  160. o := orm.NewOrm()
  161. newId, err = o.Insert(item)
  162. return
  163. }
  164. type Sellers struct {
  165. AdminId int `description:"销售id"`
  166. RealName string `description:"销售姓名"`
  167. Email string `description:"销售邮箱"`
  168. OpenId string `description:"销售openid"`
  169. Mobile string `description:"销售电话"`
  170. RoleTypeCode string `description:"角色编码"`
  171. }
  172. func GetSellers() (items []*Sellers, err error) {
  173. o := orm.NewOrm()
  174. sql := `SELECT a.real_name,a.email,b.open_id,a.mobile,b.mobile,a.role_type_code,a.admin_id
  175. FROM admin AS a
  176. LEFT JOIN wx_user AS b ON a.mobile=b.mobile
  177. WHERE role_type_code IN('ficc_seller','ficc_admin','rai_seller','rai_admin')
  178. AND b.open_id<>'' `
  179. _, err = o.Raw(sql).QueryRows(&items)
  180. return
  181. }
  182. func GetRemindCompany(sellerId int, endDate string) (items []*CompanyProduct, err error) {
  183. o := orm.NewOrm()
  184. sql := ` SELECT b.end_date,a.company_name,b.status,b.seller_id,b.seller_name
  185. FROM company a
  186. INNER JOIN company_product AS b ON a.company_id=b.company_id
  187. WHERE b.seller_id=? AND b.end_date = ?
  188. AND b.status IN('正式','试用') ORDER BY b.status DESC `
  189. _,err=o.Raw(sql, sellerId, endDate).QueryRows(&items)
  190. return
  191. }