company_user.go 19 KB

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