company_product.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. package company
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hz_crm_api/utils"
  5. "time"
  6. )
  7. type CompanyProduct struct {
  8. CompanyProductId int `orm:"column(company_product_id);pk" description:"客户产品id"`
  9. CompanyId int `description:"客户id"`
  10. ProductId int `description:"产品id"`
  11. ProductName string `description:"产品名称"`
  12. CompanyName string `description:"客户名称"`
  13. Source string `description:"来源"`
  14. Reasons string `description:"新增理由"`
  15. Status string `description:"客户状态"`
  16. InitStatus string `description:"客户初始化状态(目前用来处理权益的永续客户使用)"`
  17. IndustryId int `description:"行业id"`
  18. IndustryName string `description:"行业名称"`
  19. SellerId int `description:"销售id"`
  20. SellerName string `description:"销售名称"`
  21. GroupId int `description:"销售分组id"`
  22. DepartmentId int `description:"销售部门id"`
  23. IsSuspend int `description:"1:暂停,0:启用"`
  24. SuspendTime time.Time `description:"暂停启用时间"`
  25. TryOutTime time.Time `description:"正式转试用时间"`
  26. RenewalReason string `description:"正式转试用后的续约情况说明"`
  27. RenewalTodo string `description:"未续约说明中的待办事项说明"`
  28. LastDescriptionTime time.Time `description:"上次添加说明时间"`
  29. RenewalIntention int `description:"是否勾选无续约意向,1:确认,0:未确认"`
  30. ApproveStatus string `description:"审批状态:'审批中','通过','驳回'"`
  31. FreezeTime time.Time `description:"冻结时间"`
  32. FreezeReason time.Time `description:"冻结理由"`
  33. Remark string `description:"备注信息"`
  34. CreateTime time.Time `description:"创建时间"`
  35. ModifyTime time.Time `description:"修改时间"`
  36. StartDate string `description:"开始日期"`
  37. EndDate string `description:"结束日期"`
  38. ContractEndDate time.Time `description:"合同结束日期"`
  39. LoseReason string `description:"流失原因"`
  40. LossTime time.Time `description:"流失时间"`
  41. CompanyType string `description:"客户类型"`
  42. OpenCode string `description:"开放给第三方的编码,不让第三方定位我们的客户信息"`
  43. Scale string `description:"管理规模,空不填,1::50亿以下,2:50~100亿,3:100亿以上。"`
  44. ViewTotal int `description:"总阅读次数"`
  45. RoadShowTotal int `description:"累计路演次数"`
  46. LastViewTime time.Time `description:"最后一次阅读时间"`
  47. PackageType int `description:"套餐类型"`
  48. IsFormal int `description:"是否已经转正式,0是没有转正式,1是已经转过正式"`
  49. TodoStatus string `description:"任务处理状态;枚举值:'无任务','未完成','已完成'"`
  50. TodoCreateTime time.Time `description:"任务创建时间"`
  51. TodoApproveTime time.Time `description:"任务审批时间"`
  52. TryStage int `description:"试用客户子标签:1未分类、2 推进、3 跟踪、4 预备"`
  53. TryOutDayTotal int `description:"客户总试用天数"`
  54. CloseReason string `description:"关闭原因"`
  55. CloseTime time.Time `description:"关闭时间"`
  56. OverseasLabel int `description:"海外客户试用子标签:1未分类、2 推进、3 跟踪、4 预备、"`
  57. IsOverseas int `description:"是否显示在海外客户0:显示,1:不显示"`
  58. IsShare int `description:"0:非共享用户,1:共享客户"`
  59. ShareSeller string `description:"共享销售员"`
  60. ShareSellerId int `description:"共享销售员id"`
  61. ShareGroupId int `description:"共享销售员所属分组ID"`
  62. IsUserMaker int `description:"近四周之内是否包含决策人互动过 ,0否,1是"`
  63. }
  64. // 新增客户产品
  65. func AddCompanyProduct(item *CompanyProduct) (newId int64, err error) {
  66. o := orm.NewOrm()
  67. newId, err = o.Insert(item)
  68. return
  69. }
  70. type CompanyProductDetail struct {
  71. CompanyProductId int `orm:"column(company_product_id);pk" description:"客户产品id"`
  72. CompanyId int `description:"客户id"`
  73. ProductId int `description:"产品id"`
  74. ProductName string `description:"产品名称"`
  75. CompanyName string `description:"客户名称"`
  76. Source string `description:"来源"`
  77. Reasons string `description:"新增理由"`
  78. Status string `description:"客户状态"`
  79. IndustryId int `description:"行业id"`
  80. IndustryName string `description:"行业名称"`
  81. SellerId int `description:"销售id"`
  82. SellerName string `description:"销售名称"`
  83. GroupId int `description:"销售分组id"`
  84. DepartmentId int `description:"销售部门id"`
  85. IsSuspend int `description:"1:暂停,0:启用"`
  86. SuspendTime time.Time `description:"暂停启用时间"`
  87. ApproveStatus string `description:"审批状态:'审批中','通过','驳回'"`
  88. FreezeTime time.Time `description:"冻结时间"`
  89. Remark string `description:"备注信息"`
  90. CreateTime time.Time `description:"创建时间"`
  91. ModifyTime time.Time `description:"修改时间"`
  92. StartDate string `description:"开始日期"`
  93. EndDate string `description:"结束日期"`
  94. LoseReason string `description:"流失原因"`
  95. LossTime time.Time `description:"流失时间"`
  96. OpenCode string `description:"开放给第三方的编码,不让第三方定位我们的客户信息"`
  97. Scale string `description:"管理规模,空不填,1::50亿以下,2:50~100亿,3:100亿以上。"`
  98. SpecialSurplus string `description:"专项调研剩余次数"`
  99. Points string `description:"公司研选服务剩余点数"`
  100. IsShare int `description:"0:非共享用户,1:共享客户"`
  101. ShareSeller string `description:"共享销售员"`
  102. ShareSellerId int `description:"共享销售员id"`
  103. PermissionList []*PermissionLookList
  104. ViewTotal int `description:"总阅读次数"`
  105. }
  106. func GetCompanyProductsByCompanyId(companyId int) (items []*CompanyProductDetail, err error) {
  107. sql := `SELECT * FROM company_product WHERE company_id=? ORDER BY create_time ASC `
  108. o := orm.NewOrm()
  109. _, err = o.Raw(sql, companyId).QueryRows(&items)
  110. return
  111. }
  112. func GetCompanyProductById(companyProductId int) (item *CompanyProduct, err error) {
  113. o := orm.NewOrm()
  114. sql := `SELECT * FROM company_product WHERE company_product_id=? `
  115. err = o.Raw(sql, companyProductId).QueryRow(&item)
  116. return
  117. }
  118. func GetCompanyProductByCompanyIdAndProductId(companyId, productId int) (item *CompanyProduct, err error) {
  119. o := orm.NewOrm()
  120. sql := `SELECT b.* FROM company AS a
  121. INNER JOIN company_product AS b ON a.company_id=b.company_id
  122. WHERE a.company_id=? AND b.product_id=? LIMIT 1 `
  123. err = o.Raw(sql, companyId, productId).QueryRow(&item)
  124. return
  125. }
  126. // 修改客户产品
  127. func ModifyCompanyProduct(item *CompanyProduct) (err error) {
  128. o := orm.NewOrm()
  129. sql := `UPDATE company_product
  130. SET
  131. company_name = ?,
  132. source = ?,
  133. reasons = ?,
  134. status = ?,
  135. industry_id = ?,
  136. industry_name = ?,
  137. seller_id = ?,
  138. seller_name = ?,
  139. group_id = ?,
  140. department_id = ?,
  141. start_date = ?,
  142. end_date = ?,
  143. scale = ?,
  144. modify_time = NOW()
  145. WHERE company_product_id = ? `
  146. _, err = o.Raw(sql, item.CompanyName, item.Source, item.Reasons, item.Status, item.IndustryId, item.IndustryName, item.SellerId,
  147. item.SellerName, item.GroupId, item.DepartmentId, item.StartDate, item.EndDate, item.Scale, item.CompanyProductId).Exec()
  148. return
  149. }
  150. // 更新客户产品信息
  151. func (companyProduct *CompanyProduct) Update(cols []string) (err error) {
  152. o := orm.NewOrm()
  153. _, err = o.Update(companyProduct, cols...)
  154. return
  155. }
  156. func DeleteCompanyProductById(companyId, productId, companyProductId int) (companyProductList []*CompanyProduct, companyReportPermissionList []*CompanyReportPermission, err error) {
  157. o := orm.NewOrm()
  158. to, err := o.Begin()
  159. if err != nil {
  160. return
  161. }
  162. defer func() {
  163. if err != nil {
  164. _ = to.Rollback()
  165. } else {
  166. _ = to.Commit()
  167. }
  168. }()
  169. searchSql := `select * FROM company_product WHERE company_product_id=? `
  170. _, err = to.Raw(searchSql, companyProductId).QueryRows(&companyProductList)
  171. sql := `DELETE FROM company_product WHERE company_product_id=? `
  172. _, err = to.Raw(sql, companyProductId).Exec()
  173. if err != nil {
  174. return
  175. }
  176. searchSql = `select * FROM company_report_permission WHERE company_id=? AND product_id=? `
  177. _, err = to.Raw(searchSql, companyId, productId).QueryRows(&companyReportPermissionList)
  178. sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
  179. _, err = to.Raw(sql, companyId, productId).Exec()
  180. return
  181. }
  182. func SuspendCompanyProductById(companyId, productId, suspend int) (err error) {
  183. o := orm.NewOrm()
  184. sql := `UPDATE company_product SET is_suspend=?,suspend_time=NOW(),modify_time=NOW() WHERE company_id=? AND product_id=? `
  185. _, err = o.Raw(sql, suspend, companyId, productId).Exec()
  186. return
  187. }
  188. func GetAllCompanyProducts() (items []*CompanyProductDetail, err error) {
  189. sql := `SELECT * FROM company_product `
  190. o := orm.NewOrm()
  191. _, err = o.Raw(sql).QueryRows(&items)
  192. return
  193. }
  194. type CompanyProductTotalSlice struct {
  195. Total int `description:"总产品数"`
  196. CompanyId int `description:"用户id"`
  197. }
  198. // 根据用户id字符串获取企业用户的产品数
  199. func GetCountProductByCompanyIds(companyIds string) (items []*CompanyProductTotalSlice, err error) {
  200. //如果companyIds是空串,那么直接返回
  201. if companyIds == "" {
  202. return
  203. }
  204. o := orm.NewOrm()
  205. sql := `SELECT count(1) total,company_id FROM company_product WHERE company_id in (` + companyIds + `) group by company_id`
  206. _, err = o.Raw(sql).QueryRows(&items)
  207. return
  208. //return items2,err
  209. }
  210. // TryOutCompanyList 正式转试用的客户
  211. type TryOutCompanyList 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. CreditCode string `description:"社会统一信用码"`
  218. Status string `description:"客户状态"`
  219. TryOutTime time.Time `description:"正式转试用时间"`
  220. RenewalReason string `description:"正式转试用后的续约情况说明"`
  221. RenewalTodo string `description:"未续约说明中的待办事项说明"`
  222. LastDescriptionTime string `description:"上次添加说明时间"`
  223. RenewalIntention int `description:"是否勾选无续约意向,1:确认,0:未确认"`
  224. CreateTime time.Time `description:"创建时间"`
  225. ModifyTime time.Time `description:"修改时间"`
  226. StartDate string `description:"开始日期"`
  227. EndDate string `description:"结束日期"`
  228. ExpireDays int `description:"到期日期"`
  229. }
  230. // GetTryOutCompanyCount
  231. // 获取今日待办消息数量
  232. func GetTryOutCompanyCount(condition string, pars []interface{}) (total int, err error) {
  233. o := orm.NewOrm()
  234. sql := ` select count(*) count from company_product a
  235. join company b on a.company_id=b.company_id where 1=1 and a.status="试用" `
  236. sql += condition
  237. err = o.Raw(sql, pars).QueryRow(&total)
  238. return
  239. }
  240. // GetTryOutCompanyList
  241. // 获取今日待办消息
  242. func GetTryOutCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*TryOutCompanyList, err error) {
  243. o := orm.NewOrm()
  244. sql := ` select a.company_product_id,a.company_id,a.product_id,a.product_name,a.status,b.credit_code,b.company_name,a.try_out_time,a.renewal_reason,a.renewal_todo,a.last_description_time,a.create_time,a.modify_time,a.start_date,a.end_date from company_product a
  245. join company b on a.company_id=b.company_id where 1=1 and a.status="试用" `
  246. sql += condition
  247. sql += ` LIMIT ?,? `
  248. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  249. return
  250. }
  251. // EditTryOutCompanyReason 编辑续约情况说明
  252. func EditTryOutCompanyReason(companyProduct *CompanyProduct, reason string, renewalTodo string) (err error) {
  253. o := orm.NewOrm()
  254. updateCols := make([]string, 0)
  255. companyProduct.RenewalReason = reason
  256. companyProduct.RenewalTodo = renewalTodo
  257. companyProduct.ModifyTime = time.Now()
  258. companyProduct.LastDescriptionTime = time.Now()
  259. updateCols = append(updateCols, "RenewalReason", "RenewalTodo", "ModifyTime", "LastDescriptionTime", "RenewalIntention")
  260. _, err = o.Update(companyProduct, updateCols...)
  261. return
  262. }
  263. // CompanyRenewalReason 未续约说明
  264. type CompanyRenewalReason struct {
  265. CompanyRenewalReasonId int `orm:"column(company_renewal_reason_id);pk" description:"未续约说明id"`
  266. CompanyId int `description:"客户id"`
  267. ProductId int `description:"产品id"`
  268. ProductName string `description:"产品名称"`
  269. CompanyName string `description:"客户名称"`
  270. TryOutTime time.Time `description:"正式转试用时间"`
  271. RenewalReason string `description:"正式转试用后的续约情况说明"`
  272. RenewalTodo string `description:"未续约说明中的待办事项说明"`
  273. LastDescriptionTime time.Time `description:"上次添加说明时间"`
  274. EndDays time.Time `description:"到期时间"`
  275. ExpirationTimeExceeded int `description:"超出到期时间"`
  276. RenewalIntention int `description:"是否勾选无续约意向,1:确认,0:未确认"`
  277. CreateTime time.Time `description:"创建时间"`
  278. }
  279. // AddRenewalReason 新增续约情况说明
  280. func AddRenewalReason(renewalReason *CompanyRenewalReason) (newId int64, err error) {
  281. o := orm.NewOrm()
  282. newId, err = o.Insert(renewalReason)
  283. return
  284. }
  285. // GetLastDescriptionTime 查询上次添加未续约说明时间
  286. func GetLastDescriptionTime() (items []*TryOutCompanyList, err error) {
  287. o := orm.NewOrm()
  288. sql := "SELECT * FROM company_product ORDER BY last_description_time desc limit 1"
  289. err = o.Raw(sql).QueryRow(&items)
  290. return
  291. }
  292. // UpdateLastDescriptionTime 更新最后一次修改未续约说明时间
  293. func UpdateLastDescriptionTime(companyProduct *CompanyProduct) (err error) {
  294. o := orm.NewOrm()
  295. updateCols := make([]string, 0)
  296. companyProduct.LastDescriptionTime = time.Now()
  297. updateCols = append(updateCols, "LastDescriptionTime")
  298. _, err = o.Update(companyProduct, updateCols...)
  299. return
  300. }
  301. // GetTryOutTimeFromOperationRecord 查询转试用时间
  302. func GetTryOutTimeFromOperationRecord(companyId int) (item *string, err error) {
  303. o := orm.NewOrm()
  304. sql := "SELECT create_time FROM company_operation_record WHERE company_id=? AND operation='try_out' ORDER BY create_time DESC "
  305. err = o.Raw(sql, companyId).QueryRow(&item)
  306. return
  307. }
  308. // GetCompanyProductsByCompanyIds 根据客户id集合字符串获取所有客户产品列表
  309. func GetCompanyProductsByCompanyIds(companyIds, condition string, pars []interface{}) (items []*CompanyProduct, err error) {
  310. if companyIds == `` {
  311. return
  312. }
  313. sql := `SELECT * FROM company_product WHERE company_id in (` + companyIds + `) `
  314. sql += condition
  315. sql += ` ORDER BY create_time ASC `
  316. o := orm.NewOrm()
  317. _, err = o.Raw(sql, pars).QueryRows(&items)
  318. return
  319. }
  320. // 更新最后一次编辑时间
  321. func UpdateCompanyProductModifyTime(companyId, productId int) (err error) {
  322. o := orm.NewOrm()
  323. sql := `UPDATE company_product SET modify_time=NOW() WHERE company_id=? AND product_id=? `
  324. _, err = o.Raw(sql, companyId, productId).Exec()
  325. return
  326. }
  327. // CompanyProductRemark 备注
  328. type CompanyProductRemark struct {
  329. CompanyProductRemarkId int `orm:"column(company_product_remark_id);pk" description:"备注id"`
  330. CompanyId int `description:"客户id"`
  331. ProductId int `description:"产品id"`
  332. ProductName string `description:"产品名称"`
  333. Remark string `description:"备注"`
  334. CreateTime time.Time `description:"创建时间"`
  335. SysUserId int `description:"创建人ID"`
  336. SysRealName string `description:"创建人姓名"`
  337. }
  338. func AddCompanyRemark(remark *CompanyProductRemark) (newId int64, err error) {
  339. o := orm.NewOrm()
  340. newId, err = o.Insert(remark)
  341. return
  342. }
  343. // GetRemarkList 获取备注列表
  344. func GetRemarkList(CompanyId, ProductId string) (items []*CompanyProductRemark, err error) {
  345. o := orm.NewOrm()
  346. sql := "SELECT * FROM company_product_remark WHERE company_id=? AND product_id=? ORDER BY create_time DESC"
  347. _, err = o.Raw(sql, CompanyId, ProductId).QueryRows(&items)
  348. return
  349. }
  350. // GetRemarkList 获取备注列表15.9.1job
  351. func GetRemarkListInit() (items []*CompanyProductRemark, err error) {
  352. o := orm.NewOrm()
  353. sql := "SELECT * FROM company_product_remark WHERE product_id=2 ORDER BY create_time DESC"
  354. _, err = o.Raw(sql).QueryRows(&items)
  355. return
  356. }
  357. // GetRemarkList 获取备注列表
  358. func GetRemarkListByCompanyId(CompanyId string) (items []*CompanyProductRemark, err error) {
  359. o := orm.NewOrm()
  360. sql := "SELECT * FROM company_product_remark WHERE company_id=? ORDER BY create_time DESC"
  361. _, err = o.Raw(sql, CompanyId).QueryRows(&items)
  362. return
  363. }
  364. // 冻结客户
  365. //func FreezeCompany(productId, companyId, adminId int, remark, realName string) (msg, errMsg string){
  366. // time.Sleep(time.Second)
  367. // productName := ""
  368. // if productId == 1 {
  369. // productName = "ficc"
  370. // } else if productId == 2 {
  371. // productName = "权益"
  372. // } else {
  373. // productName = "佣金客户"
  374. // }
  375. //
  376. // cp, err := GetCompanyProductByCompanyIdAndProductId(companyId, productId)
  377. // if err != nil {
  378. // msg = "查询该客户产品异常!"
  379. // errMsg = "查询该客户产品异常,err:" + err.Error()
  380. // return
  381. // }
  382. // if cp.Status != utils.COMPANY_STATUS_TRY_OUT {
  383. // msg = "客户状态为:" + cp.Status + ";不可冻结"
  384. // errMsg = "客户状态为:" + cp.Status + ";不可冻结"
  385. // return
  386. // }
  387. // //查询客户的产品权限
  388. // companyReportPermissionList, err := GetCompanyReportPermission(companyId, productId)
  389. // if err != nil {
  390. // msg = "查询客户的产品权限异常"
  391. // errMsg = "查询客户的产品权限异常:" + err.Error()
  392. // return
  393. // }
  394. // err = CompanyFreeze(companyId, productId, remark)
  395. // if err != nil {
  396. // msg = "冻结失败!"
  397. // errMsg = "冻结失败,Err:" + err.Error()
  398. // return
  399. // }
  400. // //新增操作记录
  401. // {
  402. // approveContent := remark
  403. // remark := "冻结客户"
  404. // operation := "freeze"
  405. // services.AddCompanyOperationRecord(companyId, cp.SellerId, adminId, productId, adminId, cp.CompanyName,
  406. // productName, "超级管理员", remark, operation, approveContent, realName, "", "冻结")
  407. //
  408. // //新增产品权限冻结操作日志
  409. // for _, companyReportPermission := range companyReportPermissionList {
  410. // record := company_report_permission_log.CompanyReportPermissionLog{
  411. // CompanyReportPermissionId: companyReportPermission.CompanyReportPermissionId,
  412. // CompanyId: companyReportPermission.CompanyId,
  413. // ProductId: productId,
  414. // ProductName: companyReportPermission.ProductName,
  415. // SysUserId: adminId,
  416. // SysUserName: "超级管理员",
  417. // Remark: "冻结客户",
  418. // Operation: "freeze",
  419. // Status: companyReportPermission.Status,
  420. // CreateTime: time.Now(),
  421. // }
  422. // _, err = company_report_permission_log.AddCompanyReportPermissionLog(&record)
  423. // }
  424. // }
  425. // if err != nil {
  426. // msg = "编辑失败"
  427. // errMsg = "编辑失败,Err:" + err.Error()
  428. // }
  429. // return
  430. //}
  431. // SellerTryCompanyProductIds 销售对应的客户ID
  432. type SellerTryCompanyProductIds struct {
  433. CompanyIds string `description:"客户ids"`
  434. SellerId int `description:"销售ID"`
  435. }
  436. // GetTryCompanyIdsBySellerIds 获取销售对应的试用客户ID
  437. func GetTryCompanyIdsBySellerIds(productId int, sellerIds string) (list []*SellerTryCompanyProductIds, err error) {
  438. o := orm.NewOrm()
  439. sql := ` SELECT seller_id, GROUP_CONCAT(DISTINCT company_id SEPARATOR ',') AS company_ids
  440. FROM company_product
  441. WHERE product_id=? and status = "试用" and seller_id in ` + sellerIds + ` GROUP BY seller_id
  442. `
  443. _, err = o.Raw(sql, productId).QueryRows(&list)
  444. return
  445. }
  446. // GetCompanyProductsByCompanyIdsAndProductId 根据客户id集合字符串以及品种id获取所有客户产品列表
  447. func GetCompanyProductsByCompanyIdsAndProductId(companyIds []int, productId int) (items []*CompanyProduct, err error) {
  448. lenCompanyId := len(companyIds)
  449. if lenCompanyId == 0 {
  450. return
  451. }
  452. sql := `SELECT b.* FROM company a left join company_product b on a.company_id=b.company_id WHERE a.company_id in (` + utils.GetOrmInReplace(lenCompanyId) + `) AND b.product_id = ? `
  453. o := orm.NewOrm()
  454. _, err = o.Raw(sql, companyIds, productId).QueryRows(&items)
  455. return
  456. }
  457. // GetCompanyProductByCompanyIdAndSellerId 根据客户id和所属销售id获取客户产品信息
  458. func GetCompanyProductByCompanyIdAndSellerId(companyId, sellerId int) (item *CompanyProduct, err error) {
  459. o := orm.NewOrm()
  460. sql := `SELECT b.* FROM company AS a
  461. INNER JOIN company_product AS b ON a.company_id=b.company_id
  462. WHERE a.company_id=? AND b.seller_id=? LIMIT 1 `
  463. err = o.Raw(sql, companyId, sellerId).QueryRow(&item)
  464. return
  465. }
  466. // GetCompanyProductsByCompanyIds 根据客户id集合字符串获取所有客户产品列表
  467. func GetCompanyProductsBySellerId(sellerId string) (items []*CompanyProduct, err error) {
  468. sql := `SELECT * FROM company_product WHERE (seller_id in (` + sellerId + `) OR share_seller_id IN (` + sellerId + `) )`
  469. o := orm.NewOrm()
  470. _, err = o.Raw(sql).QueryRows(&items)
  471. return
  472. }
  473. // GetCompanyProductsByCondition 获取客户产品列表
  474. func GetCompanyProductsByCondition(condition string, pars []interface{}) (list []*CompanyProduct, err error) {
  475. sql := `SELECT
  476. b.*
  477. FROM
  478. company AS a
  479. JOIN company_product AS b ON a.company_id = b.company_id
  480. WHERE
  481. 1 = 1 `
  482. if condition != `` {
  483. sql += condition
  484. }
  485. _, err = orm.NewOrm().Raw(sql, pars).QueryRows(&list)
  486. return
  487. }
  488. // AddCompanyProductTryOutDayTotal 将客户产品的试用天数+1
  489. func AddCompanyProductTryOutDayTotal(companyId, productId int) (err error) {
  490. o := orm.NewOrm()
  491. sql := `UPDATE company_product
  492. SET
  493. try_out_day_total = try_out_day_total+1
  494. WHERE company_id = ? AND product_id = ?`
  495. _, err = o.Raw(sql, companyId, productId).Exec()
  496. return
  497. }
  498. // 获取详情
  499. func GetCompanyProductDetail(companyId, productId int) (item *CompanyProductDetail, err error) {
  500. sql := ` SELECT * FROM company_product WHERE company_id = ? AND product_id = ?; `
  501. o := orm.NewOrm()
  502. err = o.Raw(sql, companyId, productId).QueryRow(&item)
  503. return
  504. }
  505. // 根据状态获取客户列表
  506. func GetCompanyProductListByStatus(status string, productId int) (items []*CompanyProduct, err error) {
  507. sql := ` SELECT * FROM company_product WHERE status = ? AND product_id = ?; `
  508. o := orm.NewOrm()
  509. _, err = o.Raw(sql, status, productId).QueryRows(&items)
  510. return
  511. }
  512. // 根据规模取客户列表
  513. func GetCompanyProductListByScale(status string, productId int) (items []*CompanyProduct, err error) {
  514. sql := ` SELECT * FROM company_product WHERE scale IN (?) AND product_id = ?; `
  515. o := orm.NewOrm()
  516. _, err = o.Raw(sql, status, productId).QueryRows(&items)
  517. return
  518. }
  519. // GetCompanyRemarkById 获取客户备注
  520. func GetCompanyRemarkById(remarkId int) (item *CompanyProductRemark, err error) {
  521. o := orm.NewOrm()
  522. sql := `SELECT * FROM company_product_remark WHERE company_product_remark_id = ? LIMIT 1`
  523. err = o.Raw(sql, remarkId).QueryRow(&item)
  524. return
  525. }
  526. // DelCompanyRemark 删除客户备注
  527. func DelCompanyRemark(remarkId int) (err error) {
  528. o := orm.NewOrm()
  529. sql := `DELETE FROM company_product_remark WHERE company_product_remark_id = ? LIMIT 1`
  530. _, err = o.Raw(sql, remarkId).Exec()
  531. return
  532. }
  533. // 获取列表
  534. func GetCompanyProductList(condition string, pars []interface{}) (items []*CompanyProduct, err error) {
  535. o := orm.NewOrm()
  536. sql := `SELECT * FROM company_product WHERE 1= 1 `
  537. if condition != "" {
  538. sql += condition
  539. }
  540. _, err = o.Raw(sql, pars).QueryRows(&items)
  541. return
  542. }
  543. // 获取列表
  544. func GetCompanyProductListBycondition(condition string, pars []interface{}) (items []*CompanyProduct, err error) {
  545. o := orm.NewOrm()
  546. sql := ` SELECT a.company_id FROM company AS a INNER JOIN company_product AS b ON a.company_id=b.company_id WHERE 1=1 `
  547. if condition != "" {
  548. sql += condition
  549. }
  550. _, err = o.Raw(sql, pars).QueryRows(&items)
  551. return
  552. }
  553. // CompanyProductItem
  554. // @Description: 客户品种
  555. type CompanyProductItem struct {
  556. CompanyProductId int `orm:"column(company_product_id);pk" description:"客户产品id"`
  557. CompanyId int `description:"客户id"`
  558. ProductId int `description:"产品id"`
  559. ProductName string `description:"产品名称"`
  560. CompanyName string `description:"客户名称"`
  561. Source string `description:"来源"`
  562. Reasons string `description:"新增理由"`
  563. Status string `description:"客户状态"`
  564. IndustryId int `description:"行业id"`
  565. IndustryName string `description:"行业名称"`
  566. SellerId int `description:"销售id"`
  567. SellerName string `description:"销售名称"`
  568. GroupId int `description:"销售分组id"`
  569. DepartmentId int `description:"销售部门id"`
  570. IsSuspend int `description:"1:暂停,0:启用"`
  571. SuspendTime time.Time `description:"暂停启用时间"`
  572. ApproveStatus string `description:"审批状态:'审批中','通过','驳回'"`
  573. FreezeTime time.Time `description:"冻结时间"`
  574. Remark string `description:"备注信息"`
  575. CreateTime time.Time `description:"创建时间"`
  576. ModifyTime time.Time `description:"修改时间"`
  577. StartDate string `description:"开始日期"`
  578. EndDate string `description:"结束日期"`
  579. ContractEndDate string `description:"合同结束日期"`
  580. LoseReason string `description:"流失原因"`
  581. LossTime time.Time `description:"流失时间"`
  582. CompanyType string `description:"客户类型"`
  583. OpenCode string `description:"开放给第三方的编码,不让第三方定位我们的客户信息"`
  584. Scale string `description:"管理规模,空不填,1::50亿以下,2:50~100亿,3:100亿以上。"`
  585. ViewTotal int `description:"总阅读次数"`
  586. RoadShowTotal int `description:"累计路演次数"`
  587. LastViewTime time.Time `description:"最后一次阅读时间"`
  588. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  589. IsFormal int `description:"是否已经转正式,0是没有转正式,1是已经转过正式"`
  590. TodoStatus string `description:"任务处理状态;枚举值:'无任务','未完成','已完成'"`
  591. TodoCreateTime time.Time `description:"任务创建时间"`
  592. TodoApproveTime time.Time `description:"任务审批时间"`
  593. TryStage int `description:"试用客户子标签:1未分类、2 推进、3 跟踪、4 预备"`
  594. IsShare int `description:"0:非共享用户,1:共享客户"`
  595. ShareSeller string `description:"共享销售员"`
  596. ShareSellerId int `description:"共享销售员id"`
  597. }
  598. // GetCompanyProductItemByCompanyId
  599. // @Description: 根据客户ID获取客户产品列表
  600. // @author: Roc
  601. // @datetime 2023-12-07 11:06:58
  602. // @param companyIdList []int
  603. // @param productId int
  604. // @return items []*CompanyProductItem
  605. // @return err error
  606. func GetCompanyProductItemByCompanyId(companyId int, productId int) (item *CompanyProductItem, err error) {
  607. o := orm.NewOrm()
  608. sql := `SELECT a.*,b.is_share,b.share_seller,b.share_seller_id FROM company_product as a
  609. JOIN company b on a.company_id=b.company_id
  610. WHERE a.company_id = ? AND a.product_id = ? `
  611. err = o.Raw(sql, companyId, productId).QueryRow(&item)
  612. return
  613. }
  614. // GetCompanyProductListByCompanyIds 根据客户id集合以及类型获取所有客户产品列表
  615. func GetCompanyProductListByCompanyIds(companyIds []int, productId int) (items []*CompanyProduct, err error) {
  616. lenArr := len(companyIds)
  617. if lenArr == 0 {
  618. return
  619. }
  620. sql := `SELECT * FROM company_product WHERE 1= 1 AND company_id IN (` + utils.GetOrmInReplace(lenArr) + `)` + ` AND product_id = ?`
  621. o := orm.NewOrm()
  622. _, err = o.Raw(sql, companyIds, productId).QueryRows(&items)
  623. return
  624. }
  625. // 获取权益用户客户数量
  626. func GetCompanyProductRaiForeverCount(companyId int) (count int, err error) {
  627. sqlCount := ` SELECT COUNT(1) AS count FROM company_product WHERE 1= 1 AND product_id = 2 AND status = '永续' AND company_id = ? `
  628. o := orm.NewOrm()
  629. err = o.Raw(sqlCount, companyId).QueryRow(&count)
  630. return
  631. }
  632. // 根据共享销售id查被共享的公司销售id
  633. func GetCompanyProductSellerIdByShareSellerId(shareSellerId int) (sellerIds string, err error) {
  634. sqlCount := ` SELECT GROUP_CONCAT( DISTINCT seller_id SEPARATOR ',' ) AS seller_ids FROM company_product WHERE share_seller_id = ? AND product_id = 2 `
  635. o := orm.NewOrm()
  636. err = o.Raw(sqlCount, shareSellerId).QueryRow(&sellerIds)
  637. return
  638. }
  639. // 更新被分享销售的所属组ID
  640. func UpdateSharGroupid(sharGroupid, shareSellerId int) (err error) {
  641. o := orm.NewOrm()
  642. sql := `UPDATE company_product SET share_group_id = ? WHERE share_seller_id = ?`
  643. _, err = o.Raw(sql, sharGroupid, shareSellerId).Exec()
  644. return
  645. }
  646. // GetCompanyProductsByCompanyIdsAndProductId 根据客户id集合字符串以及品种id获取所有客户产品列表
  647. func GetCompanyProductsByCompanyIdsAndProductIdinit(companyIds []int) (items []*CompanyProduct, err error) {
  648. lenCompanyId := len(companyIds)
  649. if lenCompanyId == 0 {
  650. return
  651. }
  652. sql := `SELECT * FROM company_product WHERE company_id in (` + utils.GetOrmInReplace(lenCompanyId) + `) AND product_id = 2 `
  653. o := orm.NewOrm()
  654. _, err = o.Raw(sql, companyIds).QueryRows(&items)
  655. return
  656. }