company_user.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. package company
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hz_crm_api/utils"
  7. "time"
  8. )
  9. // 新增客户请求参数
  10. type AddUserReq struct {
  11. RealName string `description:"姓名"`
  12. Sex int `description:"用户性别,1为男性,2为女性"`
  13. Source string `description:"来源,新增客户时添加:add_custom;领取客户时添加:pick_custom;普通领取联系人时添加:add_user"`
  14. RegionType string `description:"区域,枚举值:国内、海外"`
  15. CountryCode string `description:"区号,86、852、886等"`
  16. MobileOne string `description:"手机号1"`
  17. MobileTwo string `description:"手机号2"`
  18. Email string `description:"邮箱"`
  19. Position string `description:"职位"`
  20. IsMaker int `description:"是否决策人,1:是,0:否"`
  21. BusinessCardUrl string `description:"名片地址"`
  22. CompanyId int `description:"客户id"`
  23. DepartmentName string `description:"联系人部门"`
  24. CompanyName string `description:"客户名称(公司名称)"`
  25. }
  26. type CompanyUser struct {
  27. UserId int64 `orm:"column(user_id);pk"`
  28. CountryCode string `description:"区号,86、852、886等"`
  29. Mobile string `description:"手机号"`
  30. Email string `description:"邮箱"`
  31. CompanyId int `description:"公司id"`
  32. CompanyName string `description:"公司名称"`
  33. RealName string `description:"姓名"`
  34. CreatedTime string `description:"创建时间"`
  35. MobileTwo string `description:"备用手机号"`
  36. BusinessCardUrl string `description:"名片地址"`
  37. IsMaker int `description:"是否决策人,1:是,0:否"`
  38. Position string `description:"职位"`
  39. Sex int `description:"普通用户性别,1为男性,2为女性"`
  40. IsRegister bool `description:"是否注册,true:已注册,false:未注册"`
  41. OpenId string `json:"-"`
  42. DepartmentName string `description:"联系人部门"`
  43. SellerRealName string `description:"销售"`
  44. Status string `description:"客户状态"`
  45. HasMove bool `description:"true:可移动,false:显示知道了"`
  46. ViewTotal int `description:"累计阅读次数"`
  47. LastViewTime time.Time `json:"-" description:"最后一次阅读时间"`
  48. LastViewTimeStr string `description:"最后一次阅读时间"`
  49. ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"`
  50. RegisterTime string `description:"注册时间"`
  51. RegisterPlatform int `description:"注册平台,1:微信端,2:网页端"`
  52. SellerId int `description:"销售id"`
  53. SellerName string `description:"销售名称"`
  54. GroupId int `description:"所属销售分组id"`
  55. DepartmentId int `description:"所属销售分组id"`
  56. IsShared bool `description:"是否共享联系人"`
  57. ProductIds string `description:"商品权限集合,用英文,隔开"`
  58. IsChartPermissionSetting int `description:"是否设置过图表权限, 0-未设置,1-已设置,2-无需设置"`
  59. YbProductViewTotal int `description:"研报分产品阅读统计"`
  60. IsSubscribe int `description:"是否关注了微信公众号: 0-未关注; 1-已关注"`
  61. IsSubscribeHzyj int `description:"是否关注了弘则研究微信公众号: 0-未关注; 1-已关注"`
  62. IsSubscribeCygx int `description:"是否关注了查研观向微信公众号: 0-未关注; 1-已关注"`
  63. FiccViewTotal int `description:"ficc报告的阅读次数" json:"-"`
  64. FiccLastViewTime time.Time `description:"ficc报告最近一次阅读时间" json:"-"`
  65. RaiViewTotal int `description:"权益报告的阅读次数" json:"-"`
  66. RaiLastViewTime time.Time `description:"权益报告的最近一次阅读时间" json:"-"`
  67. }
  68. type CompanyUserListResp struct {
  69. Paging *paging.PagingItem
  70. List []*CompanyUser
  71. }
  72. // 联系人数量总数返回数据结构体
  73. type CompanyUserTotal struct {
  74. Total int `description:"联系人总数"`
  75. }
  76. func GetCompanyUserListCount(condition string, pars []interface{}, companyId int) (count int, err error) {
  77. o := orm.NewOrm()
  78. sql := ` SELECT COUNT(1) AS count FROM wx_user AS a WHERE company_id=? `
  79. if condition != "" {
  80. sql += condition
  81. }
  82. err = o.Raw(sql, companyId, pars).QueryRow(&count)
  83. return
  84. }
  85. func GetCompanyUserList(condition string, pars []interface{}, companyId, startSize, pageSize int) (items []*CompanyUser, err error) {
  86. o := orm.NewOrm()
  87. sql := ` SELECT a.*,b.company_name,
  88. (SELECT count(1) FROM user_view_history AS uvh WHERE uvh.user_id=a.user_id GROUP BY a.user_id) AS view_total,
  89. (SELECT max(uvh.created_time) FROM user_view_history AS uvh WHERE uvh.user_id=a.user_id GROUP BY a.user_id) AS last_view_time
  90. FROM wx_user AS a
  91. INNER JOIN company AS b ON a.company_id=b.company_id
  92. WHERE a.company_id=? `
  93. if condition != "" {
  94. sql += condition
  95. }
  96. sql += `ORDER BY a.last_updated_time DESC LIMIT ?,? `
  97. _, err = o.Raw(sql, companyId, pars, startSize, pageSize).QueryRows(&items)
  98. return
  99. }
  100. // 获取该用户下联系人数量(跟销售绑定,2021年03月09日16:35:11)
  101. func GetCompanyUserListCountV2(condition string, pars []interface{}, companyId int) (count int, err error) {
  102. o := orm.NewOrm()
  103. tmpSql := `SELECT
  104. a.*, c.subscribe AS is_subscribe
  105. FROM
  106. wx_user AS a
  107. LEFT JOIN user_seller_relation AS b ON a.user_id = b.user_id
  108. LEFT JOIN (SELECT * FROM user_record WHERE create_platform = 1 GROUP BY user_id) AS c ON a.user_id = c.user_id
  109. WHERE
  110. a.company_id = ? `
  111. if condition != "" {
  112. tmpSql += condition
  113. }
  114. sql := `SELECT COUNT(1) AS count FROM (` + tmpSql + ` group by a.user_id) AS c `
  115. err = o.Raw(sql, companyId, pars).QueryRow(&count)
  116. return
  117. }
  118. // 获取该用户下联系人切片列表(跟销售绑定,2021年03月09日16:35:11)
  119. func GetCompanyUserListV2(condition string, pars []interface{}, companyId, startSize, pageSize int) (items []*CompanyUser, err error) {
  120. o := orm.NewOrm()
  121. sql := `SELECT
  122. a.*, a.cygx_subscribe as is_subscribe_cygx, c.subscribe AS is_subscribe_hzyj
  123. FROM
  124. wx_user AS a
  125. LEFT JOIN user_seller_relation AS b ON a.user_id = b.user_id
  126. LEFT JOIN (SELECT * FROM user_record WHERE create_platform = 1 GROUP BY user_id) AS c ON a.user_id = c.user_id
  127. WHERE
  128. a.company_id = ? `
  129. if condition != "" {
  130. sql += condition
  131. }
  132. sql += ` group by a.user_id ORDER BY a.is_register desc,a.report_last_view_time desc,a.last_updated_time DESC LIMIT ?,? `
  133. _, err = o.Raw(sql, companyId, pars, startSize, pageSize).QueryRows(&items)
  134. return
  135. }
  136. // GetCompanyUserListById 获取该用户下联系人切片列表
  137. func GetCompanyUserListById(companyId int) (items []*CompanyUser, err error) {
  138. o := orm.NewOrm()
  139. sql := ` SELECT a.* FROM wx_user AS a WHERE a.company_id=? `
  140. _, err = o.Raw(sql, companyId).QueryRows(&items)
  141. return
  142. }
  143. // 删除客户请求参数
  144. type DeleteUserReq struct {
  145. UserId int `orm:"column(user_id);pk"`
  146. }
  147. func DeleteCompanyUser(userId int) (err error) {
  148. o := orm.NewOrm()
  149. sql := ` DELETE FROM wx_user WHERE user_id=? `
  150. _, err = o.Raw(sql, userId).Exec()
  151. return
  152. }
  153. // 新增客户请求参数
  154. type EditUserReq struct {
  155. UserId int `orm:"column(user_id);pk"`
  156. RealName string `description:"姓名"`
  157. Sex int `description:"用户性别,1为男性,2为女性"`
  158. CountryCode string `description:"区号,86、852、886等"`
  159. MobileOne string `description:"手机号1"`
  160. MobileTwo string `description:"手机号2"`
  161. Email string `description:"邮箱"`
  162. Position string `description:"职位"`
  163. IsMaker int `description:"是否决策人,1:是,0:否"`
  164. BusinessCardUrl string `description:"名片地址"`
  165. CompanyId int `description:"客户id"`
  166. DepartmentName string `description:"联系人部门"`
  167. }
  168. func EditCompanyUser(item *EditUserReq) (err error) {
  169. o := orm.NewOrm()
  170. sql := ` UPDATE wx_user
  171. SET
  172. real_name=?,
  173. mobile= ?,
  174. email = ?,
  175. sex = ?,
  176. last_updated_time = NOW(),
  177. position = ?,
  178. is_maker = ?,
  179. business_card_url = ?,
  180. mobile_two= ?,
  181. department_name=?,
  182. company_id=?,country_code=?
  183. WHERE user_id = ? `
  184. _, err = o.Raw(sql, item.RealName, item.MobileOne, item.Email, item.Sex, item.Position, item.IsMaker, item.BusinessCardUrl, item.MobileTwo, item.DepartmentName, item.CompanyId, item.CountryCode, item.UserId).Exec()
  185. return
  186. }
  187. // 修改联系人的联系方式
  188. func EditCompanyUserContact(userId int64, mobileOne, mobileTwo, email, businessCardUrl string) (err error) {
  189. o := orm.NewOrm()
  190. sql := ` UPDATE wx_user
  191. SET
  192. mobile= ?,
  193. mobile_two= ?,
  194. email = ?,
  195. business_card_url = ?,
  196. last_updated_time = NOW()
  197. WHERE user_id = ? `
  198. _, err = o.Raw(sql, mobileOne, mobileTwo, email, businessCardUrl, userId).Exec()
  199. return
  200. }
  201. func GetCompanyIdByKeyWord(keyWord string) (company_id string, err error) {
  202. o := orm.NewOrm()
  203. sql := ` SELECT GROUP_CONCAT(DISTINCT company_id) AS company_id FROM wx_user WHERE mobile LIKE '%` + keyWord + `%' OR email LIKE '%` + keyWord + `%'`
  204. err = o.Raw(sql).QueryRow(&company_id)
  205. return
  206. }
  207. func GetCompanyIdByKeyWordAll(keyWord string) (company_id string, err error) {
  208. o := orm.NewOrm()
  209. sql := ` SELECT GROUP_CONCAT(DISTINCT company_id) AS company_id FROM wx_user WHERE mobile LIKE '%` + keyWord + `%' OR email LIKE '%` + keyWord + `%' `
  210. err = o.Raw(sql).QueryRow(&company_id)
  211. return
  212. }
  213. func GetCompanyUserCount(companyId int) (count int, err error) {
  214. o := orm.NewOrm()
  215. sql := ` SELECT COUNT(1) AS count FROM wx_user WHERE company_id =? `
  216. err = o.Raw(sql, companyId).QueryRow(&count)
  217. return
  218. }
  219. func CheckCompanyUserCountByMobileTwo(mobileTwo string) (item *CompanyUser, err error) {
  220. o := orm.NewOrm()
  221. sql := ` SELECT a.*,b.company_name,GROUP_CONCAT(DISTINCT d.real_name SEPARATOR '/') AS seller_real_name,GROUP_CONCAT(DISTINCT c.status SEPARATOR '/') AS status
  222. FROM wx_user AS a
  223. LEFT JOIN company AS b ON a.company_id=b.company_id
  224. LEFT JOIN company_product AS c ON b.company_id=c.company_id
  225. LEFT JOIN admin AS d ON c.seller_id=d.admin_id
  226. WHERE a.mobile_two =? GROUP BY a.company_id `
  227. err = o.Raw(sql, mobileTwo).QueryRow(&item)
  228. return
  229. }
  230. func CheckCompanyUserCountByEmail(email string) (item *CompanyUser, err error) {
  231. o := orm.NewOrm()
  232. sql := ` SELECT a.*,b.company_name,GROUP_CONCAT(DISTINCT d.real_name SEPARATOR '/') AS seller_real_name,GROUP_CONCAT(DISTINCT c.status SEPARATOR '/') AS status
  233. FROM wx_user AS a
  234. LEFT JOIN company AS b ON a.company_id=b.company_id
  235. LEFT JOIN company_product AS c ON b.company_id=c.company_id
  236. LEFT JOIN admin AS d ON c.seller_id=d.admin_id
  237. WHERE a.email =? GROUP BY a.company_id `
  238. err = o.Raw(sql, email).QueryRow(&item)
  239. return
  240. }
  241. func GetUserCountByMobile(mobile string) (item *CompanyUser, err error) {
  242. o := orm.NewOrm()
  243. sql := ` SELECT a.*,b.company_name,GROUP_CONCAT(DISTINCT d.real_name SEPARATOR '/') AS seller_real_name,GROUP_CONCAT(DISTINCT c.status SEPARATOR '/') AS status
  244. FROM wx_user AS a
  245. LEFT JOIN company AS b ON a.company_id=b.company_id
  246. LEFT JOIN company_product AS c ON b.company_id=c.company_id
  247. LEFT JOIN admin AS d ON c.seller_id=d.admin_id
  248. WHERE a.mobile=? GROUP BY a.company_id `
  249. err = o.Raw(sql, mobile).QueryRow(&item)
  250. return
  251. }
  252. //<template slot-scope="scope">{{scope.row.ReportType=='day'?'晨报':scope.row.ReportType=='week'?'周报':scope.row.ReportType=='twoweek'?'双周报':scope.row.ReportType=='month'?'月报':scope.row.ReportType=='rddp'?'日度点评':scope.row.ReportType=='cygx'?'查研观向':scope.row.ReportType=='advisory'?'每日商品聚焦':''}}</template>
  253. type ViewReportList struct {
  254. ResearchReportName string `description:"报告标题"`
  255. ReportType string `description:"报告类型 'day 晨报'、'week 周报'、'twoweek 双周报'、'month 月报'、'rddp 日度点评'、'cygx 查研观向'、'advisory 每日商品聚焦'"`
  256. CreatedTime string `description:"创建时间"`
  257. TxtType string `description:"类型 ficc:ficc 、 rights:权益"`
  258. MatchTypeName string `description:"匹配类型"`
  259. StopTime string `description:"停留时间"`
  260. }
  261. type ViewReportListResp struct {
  262. Total int `description:"数量"`
  263. List []*ViewReportList
  264. }
  265. func ModifyCompanyUserCompanyId(userId, companyId int) (err error) {
  266. o := orm.NewOrm()
  267. sql := ` UPDATE wx_user SET company_id=? WHERE user_id=? `
  268. _, err = o.Raw(sql, companyId, userId).Exec()
  269. return
  270. }
  271. func GetCompanyUserBusinessCardCount(companyId int) (count int, err error) {
  272. o := orm.NewOrm()
  273. sql := ` SELECT COUNT(1) AS count FROM wx_user WHERE company_id =? AND business_card_url<>'' `
  274. err = o.Raw(sql, companyId).QueryRow(&count)
  275. return
  276. }
  277. // 标记用户
  278. func DealCompanyUser(userId int) (err error) {
  279. o := orm.NewOrm()
  280. sql := ` UPDATE wx_user SET is_deal=1 WHERE user_id=? `
  281. _, err = o.Raw(sql, userId).Exec()
  282. return
  283. }
  284. type CompanyActivityUser struct {
  285. UserId int64
  286. Mobile string `description:"手机号"`
  287. Email string `description:"邮箱"`
  288. CompanyId int `description:"公司id"`
  289. CompanyName string `description:"公司名称"`
  290. RealName string `description:"姓名"`
  291. }
  292. // GetNoPotentialCompanyUserList 获取不是潜在客户的联系人列表
  293. func GetNoPotentialCompanyUserList(condition string, pars []interface{}) (items []*CompanyActivityUser, err error) {
  294. o := orm.NewOrm()
  295. sql := ` SELECT a.user_id,a.mobile,a.email,a.company_id,a.real_name,b.company_name FROM wx_user AS a
  296. INNER JOIN company AS b ON a.company_id=b.company_id WHERE a.company_id>1 `
  297. if condition != "" {
  298. sql += condition
  299. }
  300. sql += `ORDER BY a.last_updated_time DESC `
  301. _, err = o.Raw(sql, pars).QueryRows(&items)
  302. return
  303. }
  304. // GetFiccCompanyUserByUserIds 批量查询用户的购买ficc的产品详情
  305. func GetFiccCompanyUserByUserIds(userIds string) (list []*CompanyUser, err error) {
  306. o := orm.NewOrm()
  307. sql := ` SELECT a.user_id, a.real_name, if(b.company_id=1,"",b.company_name) as company_name, c.status, b.company_id, c.seller_name
  308. FROM wx_user AS a
  309. LEFT JOIN company AS b ON a.company_id=b.company_id
  310. LEFT JOIN company_product AS c ON a.company_id=c.company_id and c.product_id=1
  311. WHERE a.user_id in (` + userIds + `)`
  312. _, err = o.Raw(sql).QueryRows(&list)
  313. return
  314. }
  315. // UserImportMatchResp 联系人名单导入匹配响应体
  316. type UserImportMatchResp struct {
  317. List []*UserImportMatch `description:"名单列表"`
  318. Code string `description:"下载名单的code"`
  319. }
  320. // UserImportMatch 联系人名单导入匹配
  321. type UserImportMatch struct {
  322. UserName string `description:"姓名"`
  323. CountryCode string `description:"国际(区号)"`
  324. Mobile string `description:"手机号"`
  325. CompanyName string `description:"导入的客户名称"`
  326. SysCompanyName string `description:"系统匹配的客户名称"`
  327. FiccStatus string `descripiton:"FICC状态"`
  328. FiccSeller string `description:"FICC销售"`
  329. RaiStatus string `description:"权益状态"`
  330. RaiSeller string `description:"权益销售"`
  331. }
  332. // GetCompanyUsersByCondition 获取联系人列表
  333. func GetCompanyUsersByCondition(condition string, pars []interface{}) (list []*CompanyUser, err error) {
  334. sql := `SELECT * FROM wx_user WHERE 1 = 1 `
  335. if condition != `` {
  336. sql += condition
  337. }
  338. _, err = orm.NewOrm().Raw(sql, pars).QueryRows(&list)
  339. return
  340. }
  341. type ViewReportListV2 struct {
  342. Id int `description:"阅读记录id"`
  343. UserId int `description:"用户id"`
  344. Mobile string `description:"手机号"`
  345. Email string `description:"邮箱"`
  346. RealName string `description:"真实姓名"`
  347. CompanyName string `description:"客户名称"`
  348. ResearchReportName string `description:"报告标题"`
  349. ReportType int8 `description:"来源:1:rddp的报告;2:weekly_report的PHP报告;3:weekly_report商品的报告(应该是作废了);4:察研观向的报告""`
  350. CreatedTime string `description:"创建时间"`
  351. TxtType string `description:"类型 ficc:ficc 、 rights:权益"`
  352. MatchTypeName string `description:"匹配类型"`
  353. StopTime int `description:"停留时间"`
  354. ReportId int `description:"报告id"`
  355. ReportChapterId int `description:"报告章节id"`
  356. }
  357. func GetViewReportListByDate(startDate, endDate string) (items []*ViewReportListV2, err error) {
  358. dataName := ""
  359. sql := ``
  360. if utils.RunMode == "debug" {
  361. dataName = "test_v2_hongze_rddp"
  362. } else {
  363. dataName = "hongze_rddp"
  364. }
  365. ficcSql := `SELECT
  366. uvh.user_id,uvh.mobile,uvh.email,uvh.view_history_id AS id,uvh.real_name,uvh.company_name,
  367. uvh.research_report_id as report_id,
  368. uvh.research_report_type_id as report_chapter_id,
  369. rr.research_report_name,
  370. "2" AS report_type,
  371. 'ficc' AS txt_type,
  372. '--' AS match_type_name,
  373. '0' AS stop_time,
  374. uvh.created_time AS created_time
  375. FROM
  376. user_view_history uvh
  377. LEFT JOIN research_report rr ON rr.research_report_id = uvh.research_report_id
  378. WHERE
  379. uvh.created_time >= ? AND uvh.created_time< ?
  380. UNION ALL
  381. SELECT
  382. rvr.user_id,rvr.mobile,rvr.email,rvr.id,rvr.real_name,rvr.company_name,
  383. rvr.report_id,
  384. rvr.report_chapter_id,
  385. r.title AS research_report_name,
  386. '1' AS report_type,
  387. 'ficc' AS txt_type,
  388. r.classify_name_first AS match_type_name,
  389. '0' AS stop_time,
  390. rvr.create_time AS created_time
  391. FROM %s.report_view_record rvr
  392. LEFT JOIN %s.report r ON r.id = rvr.report_id
  393. WHERE
  394. rvr.create_time >= ? AND rvr.create_time< ?
  395. UNION ALL
  396. SELECT
  397. auc.user_id,auc.mobile,auc.email,auc.id,auc.real_name,auc.company_name,
  398. auc.chart_permission_id as report_id,
  399. '0' AS report_chapter_id,
  400. cha.permission_name AS research_report_name,
  401. '3' AS report_type,
  402. 'ficc' AS txt_type,
  403. cha.classify_name AS match_type_name,
  404. '0' AS stop_time,
  405. auc.create_time AS created_time
  406. FROM
  407. advisory_user_chart_article_record auc
  408. LEFT JOIN chart_permission cha ON cha.chart_permission_id = auc.chart_permission_id
  409. WHERE
  410. auc.create_time >= ? AND auc.create_time< ?`
  411. rightsSql := `SELECT
  412. h.user_id,h.mobile,h.email,h.id,h.company_name,'' AS real_name,
  413. h.article_id AS report_id,
  414. '0' AS report_chapter_id,
  415. art.title AS research_report_name,
  416. '4' AS report_type,
  417. 'rights' AS txt_type,
  418. art.match_type_name,
  419. h.stop_time,
  420. h.create_time AS created_time
  421. FROM
  422. cygx_article_history_record_newpv h
  423. INNER JOIN cygx_article art ON art.article_id = h.article_id
  424. WHERE
  425. h.create_time >= ? AND h.create_time< ? `
  426. sql = ` SELECT * FROM ( ` + ficcSql + " UNION ALL " + rightsSql + `
  427. )AS t ORDER BY t.created_time asc`
  428. o := orm.NewOrm()
  429. sql = fmt.Sprintf(sql, dataName, dataName)
  430. _, err = o.Raw(sql, startDate, endDate, startDate, endDate, startDate, endDate, startDate, endDate).QueryRows(&items)
  431. return
  432. }