company_product.go 32 KB

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