company_product.go 31 KB

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