|
@@ -0,0 +1,253 @@
|
|
|
+package model
|
|
|
+
|
|
|
+import (
|
|
|
+ "app.yhyue.com/moapp/jybase/common"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
|
+ IC "jyBXSubscribe/rpc/init"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+// GetStaffSubscribeList 员工订阅总览列表
|
|
|
+// query 检索名字/手机号
|
|
|
+// eStatus 是否有企业订阅 -1 无企业订阅 1 有企业订阅
|
|
|
+// pStatus 是否有个人订阅 -1 无个人订阅 1 有个人订阅
|
|
|
+func GetStaffSubscribeList(entId, entUserId int, query string, eStatus, pStatus, pageNum, pageSize int) (total int64, list []interface{}) {
|
|
|
+ userEnt := EntInfo(entId, entUserId)
|
|
|
+ // 非管理员无权限查询
|
|
|
+ if !(userEnt.Role_admin_system || userEnt.Role_admin_department) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 当检索是否有个人订阅时,因为无法关联查询,需要提前加载员工是否有个人订阅
|
|
|
+ pSubscribeList := getEntPersonOrderList(entId)
|
|
|
+
|
|
|
+ var sql string = ` FROM entniche_user e `
|
|
|
+ queryArr, valueArr := []string{" 1=1 "}, []interface{}{}
|
|
|
+ if userEnt.Role_admin_department { //部门管理员
|
|
|
+ sql += `INNER JOIN entniche_department_user a ON (e.id = a.user_id AND e.ent_id = ? ) INNER JOIN entniche_department_parent p ON (a.dept_id = p.id AND p.id = ?) LEFT JOIN entniche_user_rule r ON e.id =r.user_id WHERE `
|
|
|
+ valueArr = append(valueArr, entId, userEnt.Dept.Id)
|
|
|
+ } else { //企业管理员
|
|
|
+ sql += ` LEFT JOIN entniche_user_rule r ON (e.id = r.user_id ) WHERE e.ent_id = ? AND`
|
|
|
+ valueArr = append(valueArr, entId)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询条件过滤
|
|
|
+ if query = strings.TrimSpace(query); query != "" {
|
|
|
+ queryArr = append(queryArr, ` ( name LIKE ? or phone like ? ) `)
|
|
|
+ valueArr = append(valueArr, "%"+query+"%", "%"+query+"%")
|
|
|
+ }
|
|
|
+ // 企业订阅过滤
|
|
|
+ if eStatus == 1 { //有企业订阅
|
|
|
+ queryArr = append(queryArr, ` r.rule_id is not null `)
|
|
|
+ } else if eStatus == -1 { // 无企业订阅
|
|
|
+ queryArr = append(queryArr, ` r.rule_id is null `)
|
|
|
+ }
|
|
|
+ // 个人订阅过滤
|
|
|
+ if pStatus != 0 && len(pSubscribeList) > 0 {
|
|
|
+ tStr := ``
|
|
|
+ for i, v := range pSubscribeList {
|
|
|
+ if i != 0 {
|
|
|
+ tStr += `,`
|
|
|
+ }
|
|
|
+ tStr += fmt.Sprintf("%d", v)
|
|
|
+ }
|
|
|
+
|
|
|
+ if pStatus == 1 { //有个人订阅
|
|
|
+ queryArr = append(queryArr, fmt.Sprintf(` e.id in ( %s ) `, tStr))
|
|
|
+ } else if pStatus == -1 { //无个人订阅
|
|
|
+ queryArr = append(queryArr, fmt.Sprintf(` e.id not in ( %s ) `, tStr))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ countSql := fmt.Sprintf("SELECT count(e.id) %s %s", sql, strings.Join(queryArr, " AND "))
|
|
|
+ total = IC.MainMysql.CountBySql(countSql, valueArr...)
|
|
|
+ if total > 0 {
|
|
|
+ finalSql := fmt.Sprintf("SELECT e.id,e.name,e.phone,r.rule_id %s %s ORDER BY e.createtime DESC LIMIT %d,%d", sql, strings.Join(queryArr, " AND "), common.If(pageNum >= 0, pageNum, 0).(int)*pageSize, pageSize)
|
|
|
+ finalRes := IC.MainMysql.SelectBySql(finalSql, valueArr...)
|
|
|
+ if finalRes != nil && len(*finalRes) > 0 {
|
|
|
+ for _, m := range *finalRes {
|
|
|
+ eUser := common.Int64All(m["id"])
|
|
|
+ hasPersonSubscribe := false
|
|
|
+ for _, pid := range pSubscribeList {
|
|
|
+ if pid == eUser {
|
|
|
+ hasPersonSubscribe = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ list = append(list, map[string]interface{}{
|
|
|
+ "id": m["id"],
|
|
|
+ "name": common.ObjToString(m["name"]),
|
|
|
+ "phone": common.ObjToString(m["phone"]),
|
|
|
+ "eStatus": common.If(common.ObjToString(m["rule_id"]) == "", -1, 1),
|
|
|
+ "pStatus": common.If(hasPersonSubscribe, 1, -1),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// PersonSubscribe 个人订阅结构体
|
|
|
+type PersonSubscribe struct {
|
|
|
+ AInfotype []string `json:"a_infotype"`
|
|
|
+ ABuyerclass []string `json:"a_buyerclass"`
|
|
|
+ IProjectmatch int `json:"i_projectmatch"`
|
|
|
+ OArea map[string][]string `json:"o_area"`
|
|
|
+ AItems []struct {
|
|
|
+ SItem string `json:"s_item"`
|
|
|
+ AKey []struct {
|
|
|
+ Key []string `json:"key"`
|
|
|
+ Notkey []string `json:"notkey"`
|
|
|
+ Matchway float64 `json:"matchway"`
|
|
|
+ } `json:"a_key"`
|
|
|
+ } `json:"a_items"`
|
|
|
+ IEntFastimport int `json:"i_ent_fastimport"`
|
|
|
+ IApppush int `json:"i_apppush"`
|
|
|
+ IMailpush int `json:"i_mailpush"`
|
|
|
+ IMatchway int `json:"i_matchway"`
|
|
|
+ IRatemode int `json:"i_ratemode"`
|
|
|
+}
|
|
|
+
|
|
|
+// getEntPersonOrderList 查询企业订阅所有设置个人订阅的列表
|
|
|
+func getEntPersonOrderList(entId int) (uIds []int64) {
|
|
|
+ res, _ := IC.Mgo.Find("entniche_rule", map[string]interface{}{"i_entid": entId, "i_userid": map[string]interface{}{"$exists": 1}}, nil, `{"o_entniche":1,"i_userid":1}`, false, -1, -1)
|
|
|
+ if res == nil || len(*res) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, mData := range *res {
|
|
|
+ uId := common.Int64All(mData["i_userid"])
|
|
|
+ if uId <= 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ thisSub := &PersonSubscribe{}
|
|
|
+ if gconv.Struct(mData["o_entniche"], thisSub) == nil {
|
|
|
+ if len(thisSub.AItems) > 0 || len(thisSub.AInfotype) > 0 || len(thisSub.ABuyerclass) > 0 || len(thisSub.OArea) > 0 {
|
|
|
+ uIds = append(uIds, uId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetStaffSubscribeDetail 获取企业个人订阅
|
|
|
+func GetStaffSubscribeDetail(entId, uid int64) (rData map[string]interface{}) {
|
|
|
+ rData = map[string]interface{}{}
|
|
|
+ //个人订阅
|
|
|
+ if data := getPersonSubscribe(map[string]interface{}{"i_entid": entId, "i_userid": uid, "i_type": 2}); data != nil && len(data) > 0 {
|
|
|
+ rData["个人订阅"] = data
|
|
|
+ }
|
|
|
+ //个人订阅商机管理
|
|
|
+ if data := getPersonSubscribe(map[string]interface{}{"i_entid": entId, "i_userid": uid, "i_type": map[string]interface{}{"$exists": 0}}); data != nil && len(data) > 0 {
|
|
|
+ rData["个人订阅<br>商机管理"] = data
|
|
|
+ }
|
|
|
+ //大会员或超级订阅企业版
|
|
|
+ if data := getPersonSubscribe(map[string]interface{}{"i_entid": entId, "i_userid": uid, "i_type": map[string]interface{}{"$exists": 0}}); data != nil && len(data) > 0 {
|
|
|
+ key := "企业版"
|
|
|
+ //查询企业版是大会员还是超级订阅
|
|
|
+ if vipRes := IC.MainMysql.FindOne("entniche_wait_empower", map[string]interface{}{"ent_id": entId}, "product_type", "id desc"); vipRes != nil && len(*vipRes) > 0 {
|
|
|
+ typeFiled := gconv.String((*vipRes)["product_type"])
|
|
|
+ if strings.HasPrefix(typeFiled, "vip") {
|
|
|
+ key = "大会员企业版"
|
|
|
+ } else if strings.HasPrefix(typeFiled, "vip") {
|
|
|
+ key = "超级订阅企业版"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rData[fmt.Sprintf("个人订阅<br>%s", key)] = data
|
|
|
+ }
|
|
|
+ // 企业自动分发
|
|
|
+ if data := getEntDistribute(entId, uid); data != nil && len(data) > 0 {
|
|
|
+ rData["企业自动分发"] = data
|
|
|
+ }
|
|
|
+ return rData
|
|
|
+}
|
|
|
+
|
|
|
+// getPersonSubscribe 个人订阅
|
|
|
+func getPersonSubscribe(query map[string]interface{}) (rData map[string]interface{}) {
|
|
|
+ res, _ := IC.Mgo.FindOneByField("entniche_rule", query, `{"o_entniche":1,"i_userid":1}`)
|
|
|
+ if res == nil || len(*res) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ subDetail, rData := &PersonSubscribe{}, map[string]interface{}{}
|
|
|
+ if err := gconv.Struct((*res)["o_entniche"], subDetail); err == nil {
|
|
|
+ wordsList := []map[string]interface{}{}
|
|
|
+ for _, set := range subDetail.AItems {
|
|
|
+ for _, t := range set.AKey {
|
|
|
+ wordsList = append(wordsList, map[string]interface{}{
|
|
|
+ "key": t.Key,
|
|
|
+ "match": t.Matchway,
|
|
|
+ "notkey": t.Notkey,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ rData["area"] = subDetail.OArea
|
|
|
+ rData["infotype"] = subDetail.AInfotype
|
|
|
+ rData["projectmatch"] = subDetail.IProjectmatch
|
|
|
+ rData["matchway"] = subDetail.IMatchway
|
|
|
+ rData["wordsList"] = wordsList
|
|
|
+ rData["buyerClass"] = subDetail.ABuyerclass
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// getEntDistribute 获取企业分发订阅
|
|
|
+func getEntDistribute(entId, uid int64) (rData map[string]interface{}) {
|
|
|
+ //查询是否有分发
|
|
|
+ res := IC.MainMysql.FindOne("entniche_user_rule", map[string]interface{}{"user_id": uid}, "rule_id", "rule_id desc")
|
|
|
+ if res == nil || len(*res) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ruleId := common.ObjToString((*res)["rule_id"])
|
|
|
+ if ruleId == "" {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //查询分发内容
|
|
|
+ ruleRes, _ := IC.Mgo.FindById("entniche_distribute", ruleId, `{"i_deptid":1,"o_area":1,"a_buyerclass":1,"a_items":1}`)
|
|
|
+ if ruleRes == nil || len(*ruleRes) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ rData = map[string]interface{}{}
|
|
|
+ deptId := common.IntAll((*ruleRes)["i_deptid"]) //部门id
|
|
|
+ itemArr := gconv.SliceStr((*ruleRes)["a_items"]) //关键词分类名称
|
|
|
+
|
|
|
+ var area map[string][]string
|
|
|
+ _ = gconv.Struct((*ruleRes)["o_area"], &area) //订阅地区
|
|
|
+
|
|
|
+ //查询分发订阅关键词及信息类型
|
|
|
+ var wordsRes *map[string]interface{}
|
|
|
+ if deptId != 0 {
|
|
|
+ wordsRes, _ = IC.Mgo.FindOne("entniche_rule", map[string]interface{}{"i_entid": entId, "i_deptid": deptId, "i_userid": map[string]interface{}{"$exists": 0}})
|
|
|
+ } else {
|
|
|
+ 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}})
|
|
|
+ }
|
|
|
+ wordsList := []map[string]interface{}{}
|
|
|
+ infotype, i_projectmatch, i_matchway := []string{}, 0, 0
|
|
|
+ if wordsRes != nil && len(*wordsRes) > 0 {
|
|
|
+ thisSub := &PersonSubscribe{}
|
|
|
+ if gconv.Struct((*wordsRes)["o_entniche"], thisSub) == nil {
|
|
|
+ for _, set := range thisSub.AItems {
|
|
|
+ for _, name := range itemArr {
|
|
|
+ if set.SItem == name {
|
|
|
+ for _, t := range set.AKey {
|
|
|
+ wordsList = append(wordsList, map[string]interface{}{
|
|
|
+ "key": t.Key,
|
|
|
+ "match": t.Matchway,
|
|
|
+ "notkey": t.Notkey,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ i_matchway = thisSub.IMatchway
|
|
|
+ i_projectmatch = thisSub.IProjectmatch
|
|
|
+ infotype = thisSub.AInfotype
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rData["buyerClass"] = (*ruleRes)["a_buyerclass"] //采购单位类型
|
|
|
+ rData["area"] = area
|
|
|
+ rData["infotype"] = infotype
|
|
|
+ rData["projectmatch"] = i_projectmatch
|
|
|
+ rData["matchway"] = i_matchway
|
|
|
+ rData["wordsList"] = wordsList
|
|
|
+ return
|
|
|
+}
|