浏览代码

wip:企业关注分组提交

wkyuer 7 月之前
父节点
当前提交
29606793d9

+ 209 - 33
src/jfw/modules/bigmember/src/entity/followEnterprise.go

@@ -2,8 +2,10 @@ package entity
 
 import (
 	"app.yhyue.com/moapp/jybase/go-xweb/httpsession"
+	"database/sql"
 	"errors"
 	"fmt"
+	"github.com/gogf/gf/v2/util/gconv"
 	"jy/src/jfw/modules/bigmember/src/config"
 	"jy/src/jfw/modules/bigmember/src/db"
 	"jy/src/jfw/modules/bigmember/src/util"
@@ -15,7 +17,6 @@ import (
 	qutil "app.yhyue.com/moapp/jybase/common"
 	. "app.yhyue.com/moapp/jybase/date"
 	elastic "app.yhyue.com/moapp/jybase/es"
-	"app.yhyue.com/moapp/jybase/mongodb"
 	"app.yhyue.com/moapp/jybase/redis"
 	"app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
 )
@@ -120,8 +121,12 @@ func (this *EntFollow) EntFollowedShow(entId string) (followed, showFollow bool,
 // GetEntFollowList 我关注的企业列表
 // param A默认分组 B竞争对手 C合作伙伴 逗号分隔
 // db  0||nil 默认分组、1 竞争对手、2 合作伙伴、3 竞争对手和合作伙伴
-func (this *EntFollow) GetEntFollowList(pNum, pSize int, match, group string) (followData []map[string]interface{}, hasNext bool, count, total int, err error) {
-	flistSql := fmt.Sprintf(`SELECT id,f_capital,i_apppushunread,l_establishdate,l_lastpushtime,s_area,s_city,s_employeeno,s_entId,s_entname,i_group,s_phone FROM follow_ent_monitor WHERE  s_userid="%s" ORDER BY l_lastpushtime DESC,l_createtime DESC LIMIT %d,%d`, this.UserId, 0, this.MaxNum)
+func (this *EntFollow) GetEntFollowList(pNum, pSize int, match, groups string) (followData []map[string]interface{}, hasNext bool, count, total int, err error) {
+	groupIds, groupErr := checkGroup(this.UserId, groups)
+	if groupErr != nil {
+		return nil, false, -1, -1, fmt.Errorf("分组不存在")
+	}
+	flistSql := fmt.Sprintf(`SELECT  m.*,GROUP_CONCAT(g.id SEPARATOR ',') as groups  from follow_ent_monitor m inner join follow_ent_monitor_group g on(m.s_userid =g.s_userid and g.s_userid ='%s') INNER JOIN follow_ent_monitor_relationship r  on ( g.id=r.s_groupId and m.s_entId=r.s_entId ) GROUP BY s_entId  ORDER BY m.l_lastpushtime DESC,m.l_createtime DESC LIMIT %d,%d`, this.UserId, 0, this.MaxNum)
 	log.Print("我关注的企业列表开始查询:", flistSql)
 	res := db.Base.SelectBySql(flistSql)
 	if res == nil || len(*res) == 0 {
@@ -139,18 +144,22 @@ func (this *EntFollow) GetEntFollowList(pNum, pSize int, match, group string) (f
 				continue
 			}
 		}
-		if group != "" {
-			var iGroup bool
-			if strings.Contains(group, "A") && (qutil.IntAll(item["i_group"]) == 0) {
-				iGroup = true
-			}
-			if !iGroup && strings.Contains(group, "B") && (qutil.IntAll(item["i_group"]) == 1 || qutil.IntAll(item["i_group"]) == 3) {
-				iGroup = true
-			}
-			if !iGroup && strings.Contains(group, "C") && (qutil.IntAll(item["i_group"]) == 2 || qutil.IntAll(item["i_group"]) == 3) {
-				iGroup = true
+		var enCodeGroups []string
+
+		if len(groupIds) > 0 {
+			var (
+				groups = strings.Split(gconv.String(item["i_group"]), ",")
+				has    = false
+			)
+			for _, group := range groups {
+				if group != "" {
+					enCodeGroups = append(enCodeGroups, util.EncodeId(group))
+					if !has && groupIds[gconv.Int(group)] {
+						has = true
+					}
+				}
 			}
-			if !iGroup {
+			if !has {
 				continue
 			}
 		}
@@ -172,7 +181,7 @@ func (this *EntFollow) GetEntFollowList(pNum, pSize int, match, group string) (f
 			"s_phone":         item["s_phone"],
 			"i_apppushunread": item["i_apppushunread"],
 			"l_lastpushtime":  item["l_lastpushtime"],
-			"s_group":         parseGroupStr(qutil.IntAll(item["i_group"])),
+			"s_grousp":        strings.Join(enCodeGroups, ","),
 		}
 		followDataAll = append(followDataAll, rowMap)
 	}
@@ -272,8 +281,11 @@ func (this *EntFollow) updateFollowed(follow map[string]interface{}) string {
 }
 
 // AddFollowEnt 添加关注企业
-func (this *EntFollow) AddFollowEnt(entId, group string, getOldData ...bool) error {
-	groupFlag := getGroup(group)
+func (this *EntFollow) AddFollowEnt(entId, groups string, getOldData ...bool) error {
+	groupIds, groupErr := checkGroup(this.UserId, groups)
+	if groupErr != nil {
+		return fmt.Errorf("分组不存在")
+	}
 	//if this.HasPower {
 	if db.Base.Count(this.SaveTable, map[string]interface{}{"s_userid": this.UserId, "s_entId": entId}) > 0 {
 		return errors.New("已关注此企业")
@@ -286,7 +298,7 @@ func (this *EntFollow) AddFollowEnt(entId, group string, getOldData ...bool) err
 		return errors.New("查找企业异常")
 	}
 	followData := map[string]interface{}{
-		"s_entId":        mongodb.BsonIdToSId((*res)["_id"]), //dev3.6.2 企业画像根据entId查询
+		"s_entId":        entId, //dev3.6.2 企业画像根据entId查询
 		"s_entname":      (*res)["company_name"],
 		"s_area":         qutil.If((*res)["company_area"] == nil, "", (*res)["company_area"]),
 		"s_city":         qutil.If((*res)["company_city"] == nil, "", (*res)["company_city"]),
@@ -313,14 +325,22 @@ func (this *EntFollow) AddFollowEnt(entId, group string, getOldData ...bool) err
 			followData["l_establishdate"] = t.Unix()
 		}
 	}
-
-	//设置分组
-	if groupFlag > -1 {
-		followData["i_group"] = groupFlag
-	}
-	followId := db.Base.Insert(this.SaveTable, followData)
-	if followId == -1 {
-		return errors.New("保存关注企业异常")
+	if !db.Base.ExecTx("企业关注", func(tx *sql.Tx) bool {
+		followId := db.Base.InsertByTx(tx, this.SaveTable, followData)
+		if followId == -1 {
+			return false
+		}
+		for groupId, _ := range groupIds {
+			if db.Base.InsertByTx(tx, "follow_ent_monitor_relationship", map[string]interface{}{
+				"s_entId":   entId,
+				"s_groupId": groupId,
+			}) <= 0 {
+				return false
+			}
+		}
+		return true
+	}) {
+		return fmt.Errorf("企业关注存储异常")
 	}
 	go func(userId string) {
 		//首页潜在竞争对手缓存
@@ -357,6 +377,33 @@ func getGroup(groupStr string) (s int) {
 	return
 }
 
+// checkGroup 校验分组是否存在
+func checkGroup(userId, groupStr string) (map[int]bool, error) {
+	rData := db.Base.Query("SELECT id FROM follow_ent_monitor_group WHERE s_userid=?", userId)
+	if rData == nil || len(*rData) == 0 {
+		return nil, fmt.Errorf("未查询到标签")
+	}
+	var (
+		hasGroupId map[int]bool
+		rMap       map[int]bool
+	)
+	for _, m := range *rData {
+		hasGroupId[gconv.Int(m["id"])] = true
+	}
+	for _, gId := range strings.Split(groupStr, ",") {
+		tId := gconv.Int(util.DecodeId(gId))
+		if tId <= 0 {
+			continue
+		}
+		if hasGroupId[tId] {
+			rMap[tId] = true
+		} else {
+			return nil, fmt.Errorf("未知标签")
+		}
+	}
+	return rMap, nil
+}
+
 // 格式化分组字段
 func parseGroupStr(gFlag int) string {
 	switch gFlag {
@@ -372,12 +419,59 @@ func parseGroupStr(gFlag int) string {
 }
 
 // ChangeFollowGroup 更换关注企业分组
-func (this *EntFollow) ChangeFollowGroup(fid, group string) error {
-	groupValue := getGroup(group)
-	if groupValue == -1 {
-		groupValue = 0
-	}
-	if !db.Base.Update(this.SaveTable, map[string]interface{}{"id": fid}, map[string]interface{}{"i_group": groupValue}) {
+func (this *EntFollow) ChangeFollowGroup(entId, groupStr string) error {
+	groupMap, groupErr := checkGroup(this.UserId, groupStr)
+	if groupErr != nil {
+		return fmt.Errorf("分组不存在")
+	}
+	if db.Base.ExecTx("更换关注分组", func(tx *sql.Tx) bool {
+		//查询关联的标签
+		rData := db.Base.Query("SELECT r.id,r.s_groupId FROM follow_ent_monitor_group g inner join follow_ent_monitor_relationship r on (g.id = r.s_groupId ) WHERE g.s_userid=? AND r.s_entId=?", this.UserId, entId)
+		if rData == nil || len(*rData) == 0 {
+			return false
+		}
+		var (
+			remove []int
+		)
+		for _, m := range *rData {
+			var (
+				releaseId = gconv.Int(m["id"])
+				groupId   = gconv.Int(m["s_groupId"])
+				has       = false
+			)
+
+			for newGroupId, _ := range groupMap {
+				if groupId == newGroupId {
+					has = true
+					groupMap[groupId] = false
+					break
+				}
+			}
+
+			if !has {
+				remove = append(remove, releaseId)
+			}
+		}
+		for _, id := range remove {
+			if !db.Base.DeleteByTx(tx, "follow_ent_monitor_relationship", map[string]interface{}{
+				"id": id,
+			}) {
+				return false
+			}
+		}
+
+		for groupId, ok := range groupMap {
+			if ok {
+				if db.Base.Insert("follow_ent_monitor_relationship", map[string]interface{}{
+					"s_entId":   entId,
+					"s_groupId": groupId,
+				}) <= 0 {
+					return false
+				}
+			}
+		}
+		return true
+	}) {
 		return fmt.Errorf("设置分组失败")
 	}
 	return nil
@@ -398,7 +492,23 @@ func (this *EntFollow) DelFollowEnt(entId, fid string) error {
 		query["id"] = fid
 	}
 	data := db.Base.FindOne(this.SaveTable, query, "id,s_entId,s_entname,s_area,s_city,s_phone,f_capital,s_employeeno,l_createtime,s_userid,l_lastpushtime", "")
-	if data != nil && db.Base.Delete(this.SaveTable, query) {
+	if data != nil {
+		db.Base.ExecTx("取消关注", func(tx *sql.Tx) bool {
+			if !db.Base.DeleteByTx(tx, this.SaveTable, query) {
+				return false
+			}
+			//查询关联的标签
+			rData := db.Base.Query("SELECT r.id FROM follow_ent_monitor_group g inner join follow_ent_monitor_relationship r on (g.id = r.s_groupId ) WHERE g.s_userid=? AND r.s_entId=?", this.UserId, entId)
+			if rData == nil || len(*rData) == 0 {
+				return false
+			}
+			for _, m := range *rData {
+				if !db.Base.DeleteByTx(tx, "follow_ent_monitor_relationship", map[string]interface{}{"id": gconv.String(m["id"])}) {
+					return false
+				}
+			}
+			return true
+		})
 		(*data)["s_followid"] = (*data)["id"]
 		delete(*data, "id")
 		db.Mgo.Save("jylab_followent_back", data)
@@ -604,3 +714,69 @@ func (this *EntFollow) PutLabelGroup(name string, groupId string) error {
 	}, map[string]interface{}{"s_name": name})
 	return nil
 }
+
+// DelLabelGroup删除分组 -删除分组:如果删除完分组对应企业没分组了,放到默认分组里,如果企业还有其他分组,就还在其他分组里
+func (this *EntFollow) DelLabelGroup(groupId string) error {
+	var err error
+	if !db.Base.ExecTx("删除分组", func(tx *sql.Tx) bool {
+		var (
+			needMoveDefault []string
+		)
+		//查询当前分组内所有数据
+		rData := db.Base.Query("SELECT r.s_entId,GROUP_CONCAT(g.id SEPARATOR ',') as gs  FROM  follow_ent_monitor_relationship r inner join  follow_ent_monitor_group g  on ( r.s_groupId = g.id ) WHERE g.s_userid ='?' GROUP BY r.s_entId  HAVING FIND_IN_SET('?', gs) > 0;", this.UserId, groupId)
+		if rData != nil && len(*rData) > 0 {
+			for _, m := range *rData {
+				var (
+					s_entId       = gconv.String(m["s_entId"])
+					groups        = gconv.String(m["gs"])
+					hasOtherGroup = false //是否存在其他标签
+				)
+				for _, s := range strings.Split(groups, ",") {
+					if s == "" {
+						continue
+					}
+					if s != groupId {
+						hasOtherGroup = true
+						break
+					}
+				}
+				if !hasOtherGroup {
+					needMoveDefault = append(needMoveDefault, s_entId)
+				}
+			}
+		}
+		if len(needMoveDefault) > 0 {
+			//查询默认分组id
+			rMap := db.Base.FindOne("follow_ent_monitor_group", map[string]interface{}{
+				"s_name":   "默认分组",
+				"s_userid": this.UserId,
+			}, "", "")
+			if rMap == nil || len(*rMap) == 0 {
+				err = fmt.Errorf("未知默认分组")
+				return false
+			}
+			var defaultGroupId = gconv.Int((*rMap)["id"])
+			for _, entId := range needMoveDefault {
+				if db.Base.InsertByTx(tx, "follow_ent_monitor_relationship", map[string]interface{}{
+					"s_groupId": defaultGroupId,
+					"s_entId":   entId,
+				}) <= 0 {
+					err = fmt.Errorf("企业数据迁移异常")
+					return false
+				}
+			}
+		}
+		//删除当前标签
+		if db.Base.DeleteByTx(tx, "follow_ent_monitor_group", map[string]interface{}{
+			"s_userid": this.UserId,
+			"id":       groupId,
+		}) {
+			err = fmt.Errorf("未知分组")
+			return false
+		}
+		return true
+	}) && err == nil {
+		err = fmt.Errorf("删除分组异常")
+	}
+	return err
+}

+ 6 - 7
src/jfw/modules/bigmember/src/service/follow/enterprise.go

@@ -23,11 +23,10 @@ type FollowEnt struct {
 	list               xweb.Mapper `xweb:"/follow/ent/list"`               //关注企业列表
 	addFollow          xweb.Mapper `xweb:"/follow/ent/addFollow"`          //添加关注标企业
 	delFollow          xweb.Mapper `xweb:"/follow/ent/delFollow"`          //取消关注标企业
+	changeGroup        xweb.Mapper `xweb:"/follow/ent/changeGroup"`        //关注企业分组改变
 	entChangeList      xweb.Mapper `xweb:"/follow/ent/entChangeList"`      //企业变更记录
 	pcSimpleFollowList xweb.Mapper `xweb:"/follow/ent/pcSimpleFollowList"` //pc关注企业列表(带变更)
-	changeGroup        xweb.Mapper `xweb:"/follow/ent/changeGroup"`        //关注企业分组改变
-
-	labelGroup xweb.Mapper `xweb:"/follow/ent/labelGroup"` //关注标签分组
+	labelGroup         xweb.Mapper `xweb:"/follow/ent/labelGroup"`         //关注标签分组
 }
 
 // Association 中标企业联想
@@ -161,7 +160,7 @@ func (this *FollowEnt) AddFollow() {
 				"limit_count": followEntManager.MaxNum,
 			}, errors.New(fmt.Sprintf("您关注的企业已达%d个,无法关注更多", followEntManager.MaxNum))
 		}
-		err = followEntManager.AddFollowEnt(util.DecodeId(entId), this.GetString("group"))
+		err = followEntManager.AddFollowEnt(util.DecodeId(entId), this.GetString("groups"))
 		if err != nil {
 			return nil, err
 		}
@@ -249,9 +248,9 @@ func (this *FollowEnt) ChangeGroup() {
 		if err != nil {
 			return nil, err
 		}
-		group := this.GetString("group")
+		groups := this.GetString("groups")
 		followId := util.DecodeId(this.GetString("fid"))
-		err = cepm.ChangeFollowGroup(followId, group)
+		err = cepm.ChangeFollowGroup(followId, groups)
 		if err != nil {
 			return nil, err
 		}
@@ -283,7 +282,7 @@ func (this *FollowEnt) LabelGroup() {
 			err = followEntManager.PutLabelGroup(this.GetString("name"), util.DecodeId(this.GetString("id")))
 			return nil, err
 		case "del":
-			//err = followEntManager.DelLabelGroup(util.DecodeId(this.GetString("name")))
+			err = followEntManager.DelLabelGroup(util.DecodeId(this.GetString("groupId")))
 		default:
 			return nil, errors.New("错误参数")
 		}