staffSubscribe.go 13 KB

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