company_product.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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, 6, 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 CompanyFreezeXClassRai(companyId, productId int) (companyReportPermissionList []*CompanyReportPermission, err error) {
  72. o := orm.NewOrm()
  73. freezeStartDate := time.Now().Format(utils.FormatDate)
  74. freezeEndDate := time.Now().AddDate(0, 3, 0).Format(utils.FormatDate)
  75. //FICC客户冻结期由三个月改为两个月
  76. //if productId == 1 {
  77. // freezeEndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  78. //}
  79. // 权益客户 冻结期 改为两个月
  80. if productId == 2 {
  81. freezeEndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  82. }
  83. //客户产品 状态 变更
  84. 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=? `
  85. _, err = o.Raw(sql, freezeStartDate, freezeEndDate, freezeStartDate, freezeEndDate, companyId, productId).Exec()
  86. if err != nil {
  87. return
  88. }
  89. //客户产品权限 状态 变更
  90. //获取需要变更的 客户产品权限
  91. oldPermissionEndDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  92. sql = `SELECT *
  93. FROM company_report_permission
  94. WHERE status='永续' AND end_date<=? AND company_id=? AND product_id=? `
  95. total, err := o.Raw(sql, oldPermissionEndDate, companyId, productId).QueryRows(&companyReportPermissionList)
  96. if err != nil {
  97. return
  98. }
  99. if total > 0 {
  100. sql = `UPDATE company_report_permission SET status='关闭',modify_time=NOW()
  101. WHERE status='永续' AND end_date<=? AND company_id=? AND product_id=? `
  102. _, err = o.Raw(sql, oldPermissionEndDate, companyId, productId).Exec()
  103. if err != nil {
  104. return
  105. }
  106. }
  107. //客户状态变更
  108. sql = `UPDATE company SET type=3,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
  109. _, err = o.Raw(sql, freezeStartDate, freezeEndDate, companyId).Exec()
  110. return
  111. }
  112. func CompanyLoss(companyId, productId int) (err error) {
  113. o := orm.NewOrm()
  114. //客户产品状态变更
  115. 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=? `
  116. _, err = o.Raw(sql, companyId, productId).Exec()
  117. if err != nil {
  118. return
  119. }
  120. // 将历史的任务给标记删除掉
  121. sql = `UPDATE company_todo SET is_delete=1,modify_time=NOW() WHERE company_id=? AND product_id=? AND is_delete = 0 `
  122. _, err = o.Raw(sql, companyId, productId).Exec()
  123. if err != nil {
  124. return
  125. }
  126. //客户状态变更
  127. sql = `UPDATE company SET type=3,last_updated_time=NOW() WHERE company_id=? `
  128. _, err = o.Raw(sql, companyId).Exec()
  129. if err != nil {
  130. return
  131. }
  132. return
  133. }
  134. // 正式转试用
  135. func CompanyTryOut(companyId, productId int) (companyReportPermissionList []*CompanyReportPermission, err error) {
  136. o := orm.NewOrm()
  137. startDate := time.Now().Format(utils.FormatDate)
  138. endDate := time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  139. //客户产品 状态 变更
  140. 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=? `
  141. _, err = o.Raw(sql, startDate, endDate, companyId, productId).Exec()
  142. if err != nil {
  143. return
  144. }
  145. //客户产品权限 状态 变更
  146. //获取需要变更的 客户产品权限
  147. oldPermissionEndDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  148. sql = `SELECT *
  149. FROM company_report_permission
  150. WHERE status='正式' AND end_date<=? AND company_id=? AND product_id=? `
  151. total, err := o.Raw(sql, oldPermissionEndDate, companyId, productId).QueryRows(&companyReportPermissionList)
  152. if err != nil {
  153. return
  154. }
  155. if total > 0 {
  156. sql = `UPDATE company_report_permission SET status='试用',start_date=?,end_date=?,modify_time=NOW()
  157. WHERE status='正式' AND end_date<=? AND company_id=? AND product_id=? `
  158. _, err = o.Raw(sql, startDate, endDate, oldPermissionEndDate, companyId, productId).Exec()
  159. if err != nil {
  160. return
  161. }
  162. }
  163. //客户状态变更
  164. sql = `UPDATE company SET type=2,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
  165. _, err = o.Raw(sql, startDate, endDate, companyId).Exec()
  166. return
  167. }
  168. // 权益正式转X类试用(永续)
  169. func CompanyTryOutXClassRai(companyId, productId int) (companyReportPermissionList []*CompanyReportPermission, err error) {
  170. o := orm.NewOrm()
  171. startDate := time.Now().Format(utils.FormatDate)
  172. endDate := time.Now().AddDate(0, 3, 0).Format(utils.FormatDate)
  173. //客户产品 状态 变更
  174. 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=? `
  175. _, err = o.Raw(sql, startDate, endDate, companyId, productId).Exec()
  176. if err != nil {
  177. return
  178. }
  179. //客户产品权限 状态 变更
  180. //获取需要变更的 客户产品权限
  181. oldPermissionEndDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  182. sql = `SELECT *
  183. FROM company_report_permission
  184. WHERE status='正式' AND end_date<=? AND company_id=? AND product_id=? `
  185. total, err := o.Raw(sql, oldPermissionEndDate, companyId, productId).QueryRows(&companyReportPermissionList)
  186. if err != nil {
  187. return
  188. }
  189. if total > 0 {
  190. sql = `UPDATE company_report_permission SET status='永续',start_date=?,end_date=?,modify_time=NOW()
  191. WHERE status='正式' AND end_date<=? AND company_id=? AND product_id=? `
  192. _, err = o.Raw(sql, startDate, endDate, oldPermissionEndDate, companyId, productId).Exec()
  193. if err != nil {
  194. return
  195. }
  196. }
  197. //客户状态变更
  198. sql = `UPDATE company SET type=5,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
  199. _, err = o.Raw(sql, startDate, endDate, companyId).Exec()
  200. return
  201. }
  202. func GetCompanyOldDataSync() (items []*Company, err error) {
  203. sql := `SELECT * FROM company WHERE company_id NOT IN(
  204. SELECT company_id FROM company_product
  205. )
  206. AND company_id<>1 `
  207. o := orm.NewOrm()
  208. _, err = o.Raw(sql).QueryRows(&items)
  209. return
  210. }
  211. type CompanyProduct struct {
  212. CompanyProductId int `orm:"column(company_product_id);pk" description:"客户产品id"`
  213. CompanyId int `description:"客户id"`
  214. ProductId int `description:"产品id"`
  215. ProductName string `description:"产品名称"`
  216. CompanyName string `description:"客户名称"`
  217. Source string `description:"来源"`
  218. Reasons string `description:"新增理由"`
  219. Status string `description:"客户状态"`
  220. IndustryId int `description:"行业id"`
  221. IndustryName string `description:"行业名称"`
  222. SellerId int `description:"销售id"`
  223. SellerName string `description:"销售名称"`
  224. GroupId int `description:"销售分组id"`
  225. DepartmentId int `description:"销售部门id"`
  226. IsSuspend int `description:"1:暂停,0:启用"`
  227. SuspendTime time.Time `description:"暂停启用时间"`
  228. ApproveStatus string `description:"审批状态:'审批中','通过','驳回'"`
  229. FreezeTime time.Time `description:"冻结时间"`
  230. Remark string `description:"备注信息"`
  231. CreateTime time.Time `description:"创建时间"`
  232. ModifyTime time.Time `description:"修改时间"`
  233. StartDate string `description:"开始日期"`
  234. EndDate string `description:"结束日期"`
  235. ContractEndDate string `description:"合同结束日期"`
  236. LoseReason string `description:"流失原因"`
  237. LossTime time.Time `description:"流失时间"`
  238. CompanyType string `description:"客户类型"`
  239. OpenCode string `description:"开放给第三方的编码,不让第三方定位我们的客户信息"`
  240. Scale string `description:"管理规模,空不填,1::50亿以下,2:50~100亿,3:100亿以上。"`
  241. ViewTotal int `description:"总阅读次数"`
  242. RoadShowTotal int `description:"累计路演次数"`
  243. LastViewTime time.Time `description:"最后一次阅读时间"`
  244. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  245. IsFormal int `description:"是否已经转正式,0是没有转正式,1是已经转过正式"`
  246. TodoStatus string `description:"任务处理状态;枚举值:'无任务','未完成','已完成'"`
  247. TodoCreateTime time.Time `description:"任务创建时间"`
  248. TodoApproveTime time.Time `description:"任务审批时间"`
  249. TryStage int `description:"试用客户子标签:1未分类、2 推进、3 跟踪、4 预备"`
  250. }
  251. // GetCompanyProduct 获取产品详情
  252. func GetCompanyProduct(companyId int, productId int) (companyProduct *CompanyProduct, err error) {
  253. o := orm.NewOrm()
  254. sql := `SELECT *
  255. FROM company_product WHERE company_id= ? AND product_id = ? `
  256. err = o.Raw(sql, companyId, productId).QueryRow(&companyProduct)
  257. return
  258. }
  259. // 新增客户产品
  260. func AddCompanyProduct(item *CompanyProduct) (newId int64, err error) {
  261. o := orm.NewOrm()
  262. newId, err = o.Insert(item)
  263. return
  264. }
  265. type Sellers struct {
  266. AdminId int `description:"销售id"`
  267. RealName string `description:"销售姓名"`
  268. Email string `description:"销售邮箱"`
  269. OpenId string `description:"销售openid"`
  270. Mobile string `description:"销售电话"`
  271. RoleTypeCode string `description:"角色编码"`
  272. }
  273. func GetSellers() (items []*Sellers, err error) {
  274. o := orm.NewOrm()
  275. sql := `SELECT a.real_name,a.email,c.open_id,a.mobile,b.mobile,a.role_type_code,a.admin_id
  276. FROM admin AS a
  277. LEFT JOIN wx_user AS b ON a.mobile=b.mobile
  278. LEFT JOIN user_record AS c ON b.user_id=c.user_id
  279. WHERE role_type_code IN('ficc_seller','ficc_group','ficc_team','rai_seller','rai_group')
  280. and (
  281. (a.email != "") or ( c.open_id<>'' and c.create_platform=1)
  282. )`
  283. _, err = o.Raw(sql).QueryRows(&items)
  284. return
  285. }
  286. func GetSellersOpenId() (items []*Sellers, err error) {
  287. o := orm.NewOrm()
  288. sql := `SELECT a.real_name,a.email,a.open_id,a.mobile,a.role_type_code,a.admin_id
  289. FROM admin AS a
  290. WHERE role_type_code IN('ficc_seller','ficc_group','ficc_team','rai_seller','rai_group')
  291. and (
  292. (a.email != "") or ( a.open_id<>'')
  293. )`
  294. _, err = o.Raw(sql).QueryRows(&items)
  295. return
  296. }
  297. func GetRemindCompany(sellerId int, endDate string) (items []*CompanyProduct, err error) {
  298. o := orm.NewOrm()
  299. sql := ` SELECT b.end_date,b.contract_end_date,a.company_id,a.company_name,b.status,b.seller_id,b.seller_name FROM
  300. company a
  301. INNER JOIN company_product AS b ON a.company_id = b.company_id
  302. WHERE
  303. (b.seller_id = ? OR b.share_seller_id =?)
  304. AND (
  305. ( b.contract_end_date = ? AND b.status = "正式" ) OR (b.end_date = ? AND b.status = "试用")
  306. ) ORDER BY b.status DESC `
  307. _, err = o.Raw(sql, sellerId, sellerId, endDate, endDate).QueryRows(&items)
  308. return
  309. }
  310. // GetAllCompanyProduct 获取所有客户产品列表(永续、正式、试用、冻结)
  311. func GetAllCompanyProduct() (items []*CompanyProduct, err error) {
  312. o := orm.NewOrm()
  313. sql := ` SELECT * from company_product WHERE status in ("永续","正式","试用","冻结") `
  314. _, err = o.Raw(sql).QueryRows(&items)
  315. return
  316. }
  317. // AddCompanyProductTryOutDayTotal 将客户产品的试用天数+1
  318. func AddCompanyProductTryOutDayTotal(companyId, productId int) (err error) {
  319. o := orm.NewOrm()
  320. sql := `UPDATE company_product
  321. SET
  322. try_out_day_total = try_out_day_total+1
  323. WHERE company_id = ? AND product_id = ?`
  324. _, err = o.Raw(sql, companyId, productId).Exec()
  325. return
  326. }
  327. type CompanyProductItem struct {
  328. CompanyProductId int `orm:"column(company_product_id);pk" description:"客户产品id"`
  329. CompanyId int `description:"客户id"`
  330. ProductId int `description:"产品id"`
  331. ProductName string `description:"产品名称"`
  332. CompanyName string `description:"客户名称"`
  333. Source string `description:"来源"`
  334. Reasons string `description:"新增理由"`
  335. Status string `description:"客户状态"`
  336. IndustryId int `description:"行业id"`
  337. IndustryName string `description:"行业名称"`
  338. SellerId int `description:"销售id"`
  339. SellerName string `description:"销售名称"`
  340. GroupId int `description:"销售分组id"`
  341. DepartmentId int `description:"销售部门id"`
  342. IsSuspend int `description:"1:暂停,0:启用"`
  343. SuspendTime time.Time `description:"暂停启用时间"`
  344. ApproveStatus string `description:"审批状态:'审批中','通过','驳回'"`
  345. FreezeTime time.Time `description:"冻结时间"`
  346. Remark string `description:"备注信息"`
  347. CreateTime time.Time `description:"创建时间"`
  348. ModifyTime time.Time `description:"修改时间"`
  349. StartDate string `description:"开始日期"`
  350. EndDate string `description:"结束日期"`
  351. ContractEndDate string `description:"合同结束日期"`
  352. LoseReason string `description:"流失原因"`
  353. LossTime time.Time `description:"流失时间"`
  354. CompanyType string `description:"客户类型"`
  355. OpenCode string `description:"开放给第三方的编码,不让第三方定位我们的客户信息"`
  356. Scale string `description:"管理规模,空不填,1::50亿以下,2:50~100亿,3:100亿以上。"`
  357. ViewTotal int `description:"总阅读次数"`
  358. RoadShowTotal int `description:"累计路演次数"`
  359. LastViewTime time.Time `description:"最后一次阅读时间"`
  360. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  361. IsFormal int `description:"是否已经转正式,0是没有转正式,1是已经转过正式"`
  362. TodoStatus string `description:"任务处理状态;枚举值:'无任务','未完成','已完成'"`
  363. TodoCreateTime time.Time `description:"任务创建时间"`
  364. TodoApproveTime time.Time `description:"任务审批时间"`
  365. TryStage int `description:"试用客户子标签:1未分类、2 推进、3 跟踪、4 预备"`
  366. IsShare int `description:"0:非共享用户,1:共享客户"`
  367. ShareSeller string `description:"共享销售员"`
  368. ShareSellerId int `description:"共享销售员id"`
  369. }
  370. // GetCompanyProductItemListByCompanyIdList
  371. // @Description: 根据客户ID列表获取客户产品列表
  372. // @author: Roc
  373. // @datetime 2023-12-07 11:06:58
  374. // @param companyIdList []int
  375. // @param productId int
  376. // @return items []*CompanyProductItem
  377. // @return err error
  378. func GetCompanyProductItemListByCompanyIdList(companyIdList []int, productId int) (items []*CompanyProductItem, err error) {
  379. num := len(companyIdList)
  380. if num <= 0 {
  381. return
  382. }
  383. o := orm.NewOrm()
  384. sql := `SELECT a.* FROM company_product as a
  385. JOIN company b on a.company_id=b.company_id
  386. WHERE a.company_id in (` + utils.GetOrmInReplace(num) + `) AND a.product_id = ? `
  387. _, err = o.Raw(sql, companyIdList, productId).QueryRows(&items)
  388. return
  389. }
  390. // GetCompanyProductItemByCompanyId
  391. // @Description: 根据客户ID获取客户产品列表
  392. // @author: Roc
  393. // @datetime 2023-12-07 11:06:58
  394. // @param companyIdList []int
  395. // @param productId int
  396. // @return items []*CompanyProductItem
  397. // @return err error
  398. func GetCompanyProductItemByCompanyId(companyId int, productId int) (item *CompanyProductItem, err error) {
  399. o := orm.NewOrm()
  400. sql := `SELECT a.* FROM company_product as a
  401. JOIN company b on a.company_id=b.company_id
  402. WHERE a.company_id = ? AND a.product_id = ? `
  403. err = o.Raw(sql, companyId, productId).QueryRow(&item)
  404. return
  405. }