company_product.go 28 KB

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