staffSubscribe.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. package model
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jybase/encrypt"
  6. "app.yhyue.com/moapp/jybase/mongodb"
  7. "fmt"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/init"
  10. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/type/bxsubscribe"
  11. "strings"
  12. "time"
  13. )
  14. // PersonSubscribe 个人订阅结构体
  15. type PersonSubscribe struct {
  16. AInfotype []string `json:"a_infotype"`
  17. ABuyerclass []string `json:"a_buyerclass"`
  18. IProjectmatch int `json:"i_projectmatch"`
  19. OArea map[string][]string `json:"o_area"`
  20. Odistrict map[string][]string `json:"o_district"`
  21. AItems []struct {
  22. SItem string `json:"s_item"`
  23. AKey []struct {
  24. Key []string `json:"key"`
  25. Notkey []string `json:"notkey"`
  26. Matchway float64 `json:"matchway"`
  27. } `json:"a_key"`
  28. } `json:"a_items"`
  29. IEntFastimport int `json:"i_ent_fastimport"`
  30. IApppush int `json:"i_apppush"`
  31. IMailpush int `json:"i_mailpush"`
  32. IMatchway int `json:"i_matchmode"`
  33. IMatchmode []string `json:"i_matchmode"`
  34. IRatemode int `json:"i_ratemode"`
  35. Amount string `json:"amount"`
  36. ISwitch int `json:"i_switch"`
  37. }
  38. // IsEmpty 判断订阅内容是否为空
  39. func (p *PersonSubscribe) IsEmpty() bool {
  40. if len(p.AInfotype) > 0 || len(p.ABuyerclass) > 0 || len(p.OArea) > 0 {
  41. return false
  42. }
  43. // 遍历订阅词
  44. for _, item := range p.AItems {
  45. for _, key := range item.AKey {
  46. if len(key.Key) > 0 || len(key.Notkey) > 0 {
  47. return false
  48. }
  49. }
  50. }
  51. return true
  52. }
  53. // GetStaffSubscribeList 员工订阅总览列表
  54. // query 检索名字/手机号
  55. // eStatus 是否有企业订阅 -1 无企业订阅 1 有企业订阅
  56. // pStatus 是否有个人订阅 -1 无个人订阅 1 有个人订阅
  57. func GetStaffSubscribeList(entId, entUserId int, query string, eStatus, pStatus, pageNum, pageSize int64) (total int64, list []*bxsubscribe.StaffSubscribe) {
  58. userEnt := EntInfo(entId, entUserId)
  59. // 非管理员无权限查询
  60. if !(userEnt.Role_admin_system || userEnt.Role_admin_department) {
  61. return
  62. }
  63. // 当检索是否有个人订阅时,因为无法关联查询,需要提前加载员工是否有个人订阅
  64. pSubscribeList := getEntPersonOrderList(entId)
  65. // 加载当前企业所有企业分发数据
  66. ruleIds := getEntSubscirbeList(entId, userEnt.Dept.Id, (userEnt.Role_admin_system || userEnt.Role_admin_department))
  67. allRule := ""
  68. if len(ruleIds) > 0 {
  69. allRule = " AND b.rule_id in ('" + strings.Join(ruleIds, "','") + "')"
  70. } else {
  71. allRule = " AND b.rule_id is NULL "
  72. }
  73. var sql string = ` FROM (
  74. SELECT
  75. a.id,
  76. a.name,
  77. a.phone,
  78. b.rule_id,
  79. a.createtime,
  80. GROUP_CONCAT( a.product_type ) AS product_type
  81. FROM
  82. (
  83. SELECT
  84. u.id,
  85. u.NAME,
  86. u.phone,
  87. 'e' AS product_type,
  88. u.createtime
  89. FROM
  90. entniche_info a
  91. INNER JOIN entniche_user u ON ( a.id = ? AND a.STATUS = 1 AND u.power = 1 AND a.id = u.ent_id )
  92. %s
  93. UNION
  94. SELECT
  95. u.id,
  96. u.NAME,
  97. u.phone,
  98. IF ( instr( a.product_type, 'v' )> 0, 'v', 'm' ) AS product_type ,
  99. u.createtime
  100. FROM
  101. entniche_wait_empower a
  102. INNER JOIN entniche_power b ON ( a.ent_id = ? AND a.end_time > ? AND b.STATUS = 1 AND a.id = b.wait_empower_id )
  103. INNER JOIN entniche_user u ON ( b.ent_user_id = u.id )
  104. %s
  105. ) a
  106. LEFT JOIN entniche_user_rule b ON ( a.id = b.user_id %s)
  107. GROUP BY
  108. a.id,
  109. a.NAME,
  110. a.phone,
  111. b.rule_id,
  112. a.createtime
  113. ) as AllData
  114. WHERE`
  115. queryArr, valueArr := []string{" 1=1 "}, []interface{}{}
  116. nowStr := time.Now().Format(date.Date_Full_Layout)
  117. if userEnt.Role_admin_system { //企业管理员
  118. sql = fmt.Sprintf(sql, "", "", allRule)
  119. valueArr = append(valueArr, entId, entId, nowStr)
  120. } else { //部门管理员
  121. limit := fmt.Sprintf(" INNER JOIN entniche_department_user d ON ( d.user_id = u.id) WHERE d.dept_id =? ")
  122. sql = fmt.Sprintf(sql, limit, limit, allRule)
  123. valueArr = append(valueArr, entId, userEnt.Dept.Id, entId, nowStr, userEnt.Dept.Id)
  124. }
  125. // 查询条件过滤
  126. if query = strings.TrimSpace(query); query != "" {
  127. queryArr = append(queryArr, ` ( name LIKE ? or phone like ? ) `)
  128. valueArr = append(valueArr, "%"+query+"%", "%"+query+"%")
  129. }
  130. // 企业分发过滤
  131. if eStatus == 1 && len(ruleIds) == 0 {
  132. return 0, nil
  133. } else if eStatus != 0 && len(ruleIds) > 0 {
  134. if eStatus == 1 { //有个人订阅
  135. tStr := ``
  136. for i, v := range ruleIds {
  137. if i != 0 {
  138. tStr += `,`
  139. }
  140. tStr += fmt.Sprintf(`"%s"`, v)
  141. }
  142. queryArr = append(queryArr, fmt.Sprintf(` rule_id in ( %s ) `, tStr))
  143. } else if eStatus == -1 { //无个人订阅
  144. queryArr = append(queryArr, ` rule_id is NULL `)
  145. }
  146. }
  147. // 个人订阅过滤
  148. if pStatus == 1 && len(pSubscribeList) == 0 {
  149. return 0, nil
  150. } else if pStatus != 0 && len(pSubscribeList) > 0 {
  151. tStr := ``
  152. for i, v := range pSubscribeList {
  153. if i != 0 {
  154. tStr += `,`
  155. }
  156. tStr += fmt.Sprintf("%d", v)
  157. }
  158. if pStatus == 1 { //有个人订阅
  159. queryArr = append(queryArr, fmt.Sprintf(` id in ( %s ) `, tStr))
  160. } else if pStatus == -1 { //无个人订阅
  161. queryArr = append(queryArr, fmt.Sprintf(` id not in ( %s ) `, tStr))
  162. }
  163. }
  164. countSql := fmt.Sprintf("SELECT count(id) %s %s", sql, strings.Join(queryArr, " AND "))
  165. total = IC.MainMysql.CountBySql(countSql, valueArr...)
  166. if total > 0 {
  167. finalSql := fmt.Sprintf("SELECT * %s %s ORDER BY createtime DESC LIMIT %d,%d", sql, strings.Join(queryArr, " AND "), common.If(pageNum >= 0, pageNum, 0).(int64)*pageSize, pageSize)
  168. finalRes := IC.MainMysql.SelectBySql(finalSql, valueArr...)
  169. if finalRes != nil && len(*finalRes) > 0 {
  170. for _, m := range *finalRes {
  171. eUser := common.Int64All(m["id"])
  172. hasPersonSubscribe := false
  173. for _, pid := range pSubscribeList {
  174. if pid == eUser {
  175. hasPersonSubscribe = true
  176. break
  177. }
  178. }
  179. list = append(list, &bxsubscribe.StaffSubscribe{
  180. Token: encodeSubscribeMsg(gconv.Int64(entId), gconv.Int64(entUserId), gconv.Int64(m["id"]), common.ObjToString(m["rule_id"]), gconv.String(m["product_type"])),
  181. Name: common.ObjToString(m["name"]),
  182. Phone: common.ObjToString(m["phone"]),
  183. EStatus: gconv.Int64(common.If(common.ObjToString(m["rule_id"]) == "", -1, 1)),
  184. PStatus: gconv.Int64(common.If(hasPersonSubscribe, 1, -1)),
  185. })
  186. }
  187. }
  188. }
  189. return
  190. }
  191. // getEntPersonOrderList 查询企业订阅所有设置个人订阅的列表
  192. func getEntPersonOrderList(entId int) (uIds []int64) {
  193. res, _ := IC.Mgo.Find("entniche_rule", map[string]interface{}{"i_entid": entId, "i_userid": map[string]interface{}{"$exists": 1}, "i_type": map[string]interface{}{"$exists": 1}}, nil, `{"o_entniche":1,"i_userid":1}`, false, -1, -1)
  194. if res == nil || len(*res) == 0 {
  195. return
  196. }
  197. for _, mData := range *res {
  198. uId := common.Int64All(mData["i_userid"])
  199. if uId <= 0 {
  200. continue
  201. }
  202. thisSub := &PersonSubscribe{}
  203. if gconv.Struct(mData["o_entniche"], thisSub) == nil {
  204. if !thisSub.IsEmpty() {
  205. uIds = append(uIds, uId)
  206. }
  207. }
  208. }
  209. return
  210. }
  211. // getEntSubscirbeList 查询企业分发列表
  212. func getEntSubscirbeList(entId, depId int, isAdmin bool) (rId []string) {
  213. //查询是否有企业分发
  214. dept_subscribe := IC.MainMysql.Count("entniche_info", map[string]interface{}{"dept_subscribe": 1, "id": entId}) > 0
  215. queryMap := map[string]interface{}{
  216. "1": "1",
  217. }
  218. if dept_subscribe && !isAdmin {
  219. queryMap = map[string]interface{}{
  220. "i_entid": entId,
  221. "i_status": 0, //0未删除 1已删除
  222. "i_deptid": depId,
  223. }
  224. } else if isAdmin {
  225. queryMap = map[string]interface{}{
  226. "i_entid": entId,
  227. "i_status": 0, //0未删除 1已删除
  228. "i_deptid": map[string]interface{}{
  229. "$exists": false,
  230. },
  231. }
  232. }
  233. res, _ := IC.Mgo.Find("entniche_distribute", queryMap, nil, `{"_id":1,"o_area":1,"a_buyerclass":1,"a_items":1}`, false, -1, -1)
  234. if len(*res) > 0 {
  235. for _, v := range *res {
  236. rId = append(rId, mongodb.BsonIdToSId(v["_id"]))
  237. }
  238. }
  239. return
  240. }
  241. // GetStaffSubscribeDetail 获取企业个人订阅
  242. func GetStaffSubscribeDetail(entId, uid int64, token string) (rData map[string]interface{}, err error) {
  243. entIdCheck, uidCheck, staffId, ruleId, power, err := decodeSubscribeMsg(token)
  244. if err != nil {
  245. return
  246. }
  247. if entId != entIdCheck || uid != uidCheck {
  248. err = fmt.Errorf("权限异常")
  249. return
  250. }
  251. if power == "" {
  252. err = fmt.Errorf("无订阅内容")
  253. return
  254. }
  255. rData = map[string]interface{}{}
  256. nameEntNiche, nameVipMember := "", "" //当只存在大会员或商机管理时 展示个人订阅;当都存在时展示产品信息
  257. valueEntNiche, valueVipMember := map[string]interface{}{}, map[string]interface{}{}
  258. if strings.Index(power, "e") > -1 {
  259. //个人订阅商机管理
  260. if data := getPersonSubscribe(map[string]interface{}{"i_entid": entId, "i_userid": staffId, "i_type": 0}); data != nil && len(data) > 0 {
  261. nameEntNiche = "个人订阅<br>商机管理"
  262. valueEntNiche = data
  263. }
  264. }
  265. if strings.Index(power, "m") > -1 || strings.Index(power, "v") > -1 {
  266. //大会员或超级订阅企业版
  267. if data := getPersonSubscribe(map[string]interface{}{"i_entid": entId, "i_userid": staffId, "i_type": 1}); data != nil && len(data) > 0 {
  268. nameVipMember = fmt.Sprintf("个人订阅<br>%s", common.If(strings.Index(power, "v") > -1, "超级订阅", "大会员"))
  269. valueVipMember = data
  270. }
  271. }
  272. if len(valueEntNiche) > 0 && len(valueVipMember) > 0 {
  273. rData[nameEntNiche] = valueEntNiche
  274. rData[nameVipMember] = valueVipMember
  275. } else if len(valueEntNiche) > 0 {
  276. rData["个人订阅"] = valueEntNiche
  277. } else if len(valueVipMember) > 0 {
  278. rData["个人订阅"] = valueVipMember
  279. }
  280. // 企业自动分发
  281. if ruleId != "" {
  282. if data := getEntDistribute(ruleId, entId, staffId); data != nil && len(data) > 0 {
  283. rData["企业自动分发"] = data
  284. }
  285. }
  286. return
  287. }
  288. // getPersonSubscribe 个人订阅
  289. func getPersonSubscribe(query map[string]interface{}) (rData map[string]interface{}) {
  290. res, _ := IC.Mgo.FindOneByField("entniche_rule", query, `{"o_entniche":1,"i_userid":1}`)
  291. if res == nil || len(*res) == 0 {
  292. return
  293. }
  294. subDetail, rData := &PersonSubscribe{}, map[string]interface{}{}
  295. if err := gconv.Struct((*res)["o_entniche"], subDetail); err == nil {
  296. if subDetail.IsEmpty() {
  297. return
  298. }
  299. wordsList := []map[string]interface{}{}
  300. for _, set := range subDetail.AItems {
  301. for _, t := range set.AKey {
  302. wordsList = append(wordsList, map[string]interface{}{
  303. "key": t.Key,
  304. "match": t.Matchway,
  305. "notkey": t.Notkey,
  306. })
  307. }
  308. }
  309. rData["area"] = subDetail.OArea
  310. rData["district"] = subDetail.Odistrict
  311. rData["infotype"] = subDetail.AInfotype
  312. rData["projectmatch"] = subDetail.IProjectmatch
  313. rData["matchway"] = common.If(subDetail.IMatchway == 0, 1, subDetail.IMatchway) //匹配方式 默认标题匹配 1
  314. rData["matchmode"] = common.If(len(subDetail.IMatchmode) > 0, subDetail.IMatchmode, []string{"title"}) //匹配方式 默认标题匹配 1
  315. rData["wordsList"] = wordsList
  316. rData["buyerClass"] = subDetail.ABuyerclass
  317. rData["switch"] = subDetail.ISwitch
  318. rData["amount"] = subDetail.Amount
  319. regionMap := map[string]map[string][]string{}
  320. if subDetail.OArea != nil {
  321. district := map[string]interface{}{}
  322. if subDetail.Odistrict != nil {
  323. district = gconv.Map(subDetail.Odistrict)
  324. }
  325. o_area := subDetail.OArea
  326. for k, v := range gconv.Map(o_area) {
  327. areaMap := map[string][]string{}
  328. if k == "北京" || k == "天津" || k == "上海" || k == "重庆" {
  329. city := fmt.Sprintf("%s市", k)
  330. if district[city] != nil {
  331. v = city
  332. }
  333. }
  334. for _, city := range gconv.Strings(v) {
  335. if city == "" {
  336. continue
  337. }
  338. if district[city] != nil {
  339. areaMap[city] = gconv.Strings(district[city])
  340. } else {
  341. areaMap[city] = []string{}
  342. }
  343. }
  344. regionMap[k] = areaMap
  345. }
  346. }
  347. rData["regionMap"] = regionMap
  348. }
  349. return
  350. }
  351. // getEntDistribute 获取企业分发订阅
  352. func getEntDistribute(ruleId string, entId, uid int64) (rData map[string]interface{}) {
  353. //查询分发内容
  354. ruleRes, _ := IC.Mgo.FindById("entniche_distribute", ruleId, `{"o_district":1,i_deptid":1,"o_area":1,"a_buyerclass":1,"a_items":1}`)
  355. if ruleRes == nil || len(*ruleRes) == 0 {
  356. return
  357. }
  358. rData = map[string]interface{}{}
  359. deptId := common.IntAll((*ruleRes)["i_deptid"]) //部门id
  360. itemArr := gconv.SliceStr((*ruleRes)["a_items"]) //关键词分类名称
  361. var area map[string][]string
  362. _ = gconv.Struct((*ruleRes)["o_area"], &area) //订阅地区
  363. var district map[string][]string
  364. _ = gconv.Struct((*ruleRes)["o_district"], &district) //区县
  365. //查询分发订阅关键词及信息类型
  366. var wordsRes *map[string]interface{}
  367. if deptId != 0 {
  368. wordsRes, _ = IC.Mgo.FindOne("entniche_rule", map[string]interface{}{"i_entid": entId, "i_deptid": deptId, "i_userid": map[string]interface{}{"$exists": 0}})
  369. } else {
  370. wordsRes, _ = IC.Mgo.FindOne("entniche_rule", map[string]interface{}{"i_entid": entId, "i_deptid": map[string]interface{}{"$exists": 0}, "i_userid": map[string]interface{}{"$exists": 0}})
  371. }
  372. wordsList := []map[string]interface{}{}
  373. infotype, i_projectmatch, i_matchway, amount, iSwitch, i_matchmode := []string{}, 0, 1, "", 0, []string{"title"}
  374. if wordsRes != nil && len(*wordsRes) > 0 {
  375. thisSub := &PersonSubscribe{}
  376. if gconv.Struct((*wordsRes)["o_entniche"], thisSub) == nil {
  377. for _, set := range thisSub.AItems {
  378. for _, name := range itemArr {
  379. if set.SItem == name {
  380. for _, t := range set.AKey {
  381. wordsList = append(wordsList, map[string]interface{}{
  382. "key": t.Key,
  383. "match": t.Matchway,
  384. "notkey": t.Notkey,
  385. })
  386. }
  387. }
  388. }
  389. }
  390. i_matchway = thisSub.IMatchway
  391. i_matchmode = thisSub.IMatchmode
  392. i_projectmatch = thisSub.IProjectmatch
  393. infotype = thisSub.AInfotype
  394. amount = thisSub.Amount
  395. iSwitch = thisSub.ISwitch
  396. }
  397. }
  398. rData["buyerClass"] = (*ruleRes)["a_buyerclass"] //采购单位类型
  399. rData["area"] = area
  400. rData["district"] = district
  401. regionMap := map[string]map[string][]string{}
  402. if (*ruleRes)["o_area"] != nil {
  403. o_area := (*ruleRes)["o_area"]
  404. for k, v := range gconv.Map(o_area) {
  405. areaMap := map[string][]string{}
  406. if k == "北京" || k == "天津" || k == "上海" || k == "重庆" {
  407. city := fmt.Sprintf("%s市", k)
  408. if district[city] != nil {
  409. v = city
  410. }
  411. }
  412. for _, city := range gconv.Strings(v) {
  413. if city == "" {
  414. continue
  415. }
  416. if district[city] != nil {
  417. areaMap[city] = gconv.Strings(district[city])
  418. } else {
  419. areaMap[city] = []string{}
  420. }
  421. }
  422. regionMap[k] = areaMap
  423. }
  424. }
  425. rData["regionMap"] = regionMap
  426. rData["infotype"] = infotype
  427. rData["projectmatch"] = i_projectmatch
  428. rData["switch"] = iSwitch
  429. rData["amount"] = amount
  430. rData["matchmode"] = i_matchmode
  431. rData["matchway"] = i_matchway
  432. rData["wordsList"] = wordsList
  433. return
  434. }
  435. // decodeSubscribeMsg token解密
  436. func decodeSubscribeMsg(token string) (entId, uId, staffId int64, ruleId, power string, err error) {
  437. values := encrypt.DecodeArticleId2ByCheck(token)
  438. if len(values) != 5 {
  439. err = fmt.Errorf("解析异常")
  440. return
  441. }
  442. entId = gconv.Int64(values[0])
  443. uId = gconv.Int64(values[1])
  444. staffId = gconv.Int64(values[2])
  445. ruleId = gconv.String(values[3])
  446. power = gconv.String(values[4])
  447. if entId == 0 || uId == 0 || staffId == 0 {
  448. err = fmt.Errorf("参数异常")
  449. return
  450. }
  451. return
  452. }
  453. // encodeSubscribeMsg token加密
  454. func encodeSubscribeMsg(entId, uId, staffId int64, ruleId, power string) string {
  455. return encrypt.EncodeArticleId2ByCheck(fmt.Sprintf("%d,%d,%d,%s,%s", entId, uId, staffId, ruleId, strings.Replace(power, ",", "_", -1)))
  456. }