company_user.go 19 KB

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