Browse Source

修改申请试用权限判断

rdluck 4 years ago
parent
commit
2cacf8c4d4
7 changed files with 226 additions and 119 deletions
  1. 95 71
      controllers/article.go
  2. 3 2
      controllers/search.go
  3. 2 2
      models/article.go
  4. 5 4
      models/company.go
  5. 52 0
      models/cygx_apply_record.go
  6. 0 37
      models/user.go
  7. 69 3
      services/elasticsearch.go

+ 95 - 71
controllers/article.go

@@ -36,92 +36,116 @@ func (this *ArticleController) Detail() {
 		return
 	}
 	uid := user.UserId
-	articleId, _ := this.GetInt("ArticleId")
+	articleId, err := this.GetInt("ArticleId")
 	if articleId <= 0 {
 		br.Msg = "参数错误"
 		br.ErrMsg = "参数错误"
 		return
 	}
-	detail, err := models.GetArticleDetailById(articleId)
-	if err != nil {
-		br.Msg = "获取信息失败"
-		br.ErrMsg = "获取信息失败"
-		return
-	}
+	detail := new(models.ArticleDetail)
+	hasPermission := 0
+	hasFree := 0
 
-	collectCount, err := models.GetArticleCollectCount(uid, articleId)
+	//判断是否已经申请过
+	applyCount, err := models.GetApplyRecordCount(uid)
 	if err != nil && err.Error() != utils.ErrNoRow() {
 		br.Msg = "获取信息失败"
-		br.ErrMsg = "判断是否已收藏失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
+		br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
 		return
 	}
 
-	if collectCount > 0 {
-		detail.IsCollect = true
-	}
+	//`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,已提交过申请,4:无该行业权限,未提交过申请,5:潜在客户,未提交过申请,6:潜在客户,已提交过申请"`
+	if user.CompanyId > 1 {
+		companyPermission, err := models.GetCompanyPermission(user.CompanyId)
+		if err != nil {
+			br.Msg = "获取信息失败"
+			br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
+			return
+		}
+		if companyPermission == "" {
+			hasPermission = 2
+			hasFree = 2
+			goto Loop
+		} else {
+			hasFree = 1
+			articlePermission, err := models.GetArticlePermission(detail.SubCategoryName)
+			if err != nil {
+				br.Msg = "获取信息失败"
+				br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
+				return
+			}
+			if articlePermission == nil {
+				br.Msg = "获取信息失败"
+				br.ErrMsg = "报告权限不存在,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
+				return
+			}
+			if strings.Contains(companyPermission, articlePermission.PermissionName) {
+				hasPermission = 1
+				//新增浏览记录
+				record := new(models.CygxArticleViewRecord)
+				record.UserId = uid
+				record.ArticleId = articleId
+				record.CreateTime = time.Now()
+				record.Mobile = user.Mobile
+				record.Email = user.Email
+				record.CompanyId = user.CompanyId
+				record.CompanyName = user.CompanyName
+				go models.AddCygxArticleViewRecord(record)
+			} else { //无该行业权限
+				if applyCount > 0 {
+					hasPermission = 3
+				} else {
+					hasPermission = 4
+				}
+			}
+		}
+		detail, err = models.GetArticleDetailById(articleId)
+		if err != nil {
+			br.Msg = "获取信息失败"
+			br.ErrMsg = "获取信息失败,Err:" + err.Error()
+			return
+		}
 
-	interviewApplyItem, err := models.GetArticleInterviewApply(uid, articleId)
-	if err != nil && err.Error() != utils.ErrNoRow() {
-		br.Msg = "获取信息失败"
-		br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
-		return
-	}
+		collectCount, err := models.GetArticleCollectCount(uid, articleId)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			br.Msg = "获取信息失败"
+			br.ErrMsg = "判断是否已收藏失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
+			return
+		}
 
-	if interviewApplyItem != nil && interviewApplyItem.InterviewApplyId > 0 {
-		detail.IsInterviewApply = true
-		detail.InterviewApplyStatus = interviewApplyItem.Status
-	}
+		if collectCount > 0 {
+			detail.IsCollect = true
+		}
 
-	hasPermission := 2
-	hasFree := 2
-	articlePermission, err := models.GetArticlePermission(detail.SubCategoryName)
-	if err != nil {
-		br.Msg = "获取信息失败"
-		br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
-		return
-	}
-	if articlePermission == nil {
-		br.Msg = "获取信息失败"
-		br.ErrMsg = "报告权限不存在,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
-		return
-	}
-	//GetCompanyPermission
-	companyPermission, err := models.GetCompanyPermission(user.CompanyId)
-	if err != nil {
-		br.Msg = "获取信息失败"
-		br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
-		return
-	}
-	if companyPermission != "" {
-		hasFree = 1
-	}
-	if strings.Contains(companyPermission, articlePermission.PermissionName) {
-		hasPermission = 1
-	}
-	if hasPermission == 1 {
-		//新增浏览记录
-		record := new(models.CygxArticleViewRecord)
-		record.UserId = uid
-		record.ArticleId = articleId
-		record.CreateTime = time.Now()
-		record.Mobile = user.Mobile
-		record.Email = user.Email
-		record.CompanyId = user.CompanyId
-		record.CompanyName = user.CompanyName
-		go models.AddCygxArticleViewRecord(record)
-	} else {
-		detail.Body = ""
-	}
-	//获取销售手机号
-	sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
-	if err != nil {
-		br.Msg = "获取信息失败"
-		br.ErrMsg = "获取销售数据失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
-		return
-	}
-	if sellerItem != nil {
-		detail.SellerMobile = sellerItem.Mobile
+		interviewApplyItem, err := models.GetArticleInterviewApply(uid, articleId)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			br.Msg = "获取信息失败"
+			br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
+			return
+		}
+
+		if interviewApplyItem != nil && interviewApplyItem.InterviewApplyId > 0 {
+			detail.IsInterviewApply = true
+			detail.InterviewApplyStatus = interviewApplyItem.Status
+		}
+		//获取销售手机号
+		sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
+		if err != nil {
+			br.Msg = "获取信息失败"
+			br.ErrMsg = "获取销售数据失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
+			return
+		}
+		if sellerItem != nil {
+			detail.SellerMobile = sellerItem.Mobile
+		}
+	} else { //潜在客户
+		if applyCount > 0 {
+			hasPermission = 6
+		} else {
+			hasPermission = 5
+		}
 	}
+Loop:
 	resp := new(models.ArticleDetailResp)
 	resp.HasPermission = hasPermission
 	resp.HasFree = hasFree

+ 3 - 2
controllers/search.go

@@ -3,7 +3,6 @@ package controllers
 import (
 	"hongze/hongze_cygx/models"
 	"hongze/hongze_cygx/services"
-	"strings"
 )
 
 type SearchController struct {
@@ -27,6 +26,7 @@ func (this *SearchController) SearchList() {
 		br.ErrMsg = "请输入搜索词"
 		return
 	}
+	/*
 	user := this.User
 	if user == nil {
 		br.Msg = "请重新登录"
@@ -46,7 +46,8 @@ func (this *SearchController) SearchList() {
 		categoryNameArr = append(categoryNameArr, v.CategoryName)
 	}
 	categoryName := strings.Join(categoryNameArr, ",")
-	result, err := services.SearchByKeyWord(keyWord, categoryName)
+	 */
+	result, err := services.SearchByKeyWord(keyWord)
 	if err != nil {
 		br.Msg = "检索失败"
 		br.ErrMsg = "检索失败,Err:" + err.Error()

+ 2 - 2
models/article.go

@@ -90,8 +90,8 @@ func GetArticlePermission(categoryName string) (item *ChartPermission, err error
 
 type ArticleDetailResp struct {
 	Detail        *ArticleDetail
-	HasPermission int `description:"1:有权限,2:无权限"`
-	HasFree       int `description:"1:已付费,2:未付费"`
+	HasPermission int `description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,已提交过申请,4:无该行业权限,未提交过申请,5:潜在客户,未提交过申请,6:潜在客户,已提交过申请"`
+	HasFree       int `description:"1:已付费(至少包含一个品类的权限),2:未付费(没有任何品类权限)"`
 }
 
 func ModifyArticleExpert(articleId int, expertNumStr, expertContentStr, interviewDateStr string) (err error) {

+ 5 - 4
models/company.go

@@ -20,12 +20,13 @@ func GetCompanyDetailById(companyId int) (item *CompanyDetail, err error) {
 }
 
 func GetCompanyPermission(companyId int) (permission string, err error) {
-	sql := ` SELECT GROUP_CONCAT(b.chart_permission_name SEPARATOR ',') AS permission
+	sql := ` SELECT GROUP_CONCAT(DISTINCT b.chart_permission_name SEPARATOR ',') AS permission
 			FROM company_report_permission AS a
 			INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
-			WHERE 
-			a.company_id=?
-			AND a.product_id=2 `
+			INNER JOIN company_product AS c ON a.company_id=c.company_id
+			WHERE  a.company_id=?
+			AND a.product_id=2
+			AND c.status IN('正式','试用','永续') `
 	o := orm.NewOrm()
 	err = o.Raw(sql, companyId).QueryRow(&permission)
 	return

+ 52 - 0
models/cygx_apply_record.go

@@ -0,0 +1,52 @@
+package models
+
+import (
+	"rdluck_tools/orm"
+	"time"
+)
+
+type CygxApplyRecord struct {
+	ApplyRecordId   int       `orm:"column(apply_record_id);pk" description:"申请试用id"`
+	BusinessCardUrl string    `description:"名片地址"`
+	RealName        string    `description:"姓名"`
+	CompanyName     string    `description:"公司名称"`
+	Mobile          string    `description:"手机号"`
+	CreateTime      time.Time `description:"创建时间"`
+	ApplyMethod     int       `description:"1:已付费客户申请试用,2:非客户申请试用"`
+}
+
+func AddApplyRecord(item *ApplyTryReq, mobile, companyNamePay string, userId, companyIdPay int) (err error) {
+	o := orm.NewOrm()
+	o.Begin()
+	defer func() {
+		if err != nil {
+			o.Rollback()
+		} else {
+			o.Commit()
+		}
+	}()
+
+	sql := `INSERT INTO cygx_apply_record (user_id,business_card_url, real_name,company_name, mobile,create_time, apply_method,company_id_pay,company_name_pay)
+          VALUES(?,?,?,?,?,?,?,?,?) `
+	_, err = o.Raw(sql, userId, item.BusinessCardUrl, item.RealName, item.CompanyName, mobile, time.Now(), item.ApplyMethod, companyIdPay, companyNamePay).Exec()
+	if err != nil {
+		return
+	}
+	msql := `UPDATE  wx_user
+		SET
+		  note = ?,
+		  is_note = 1,
+		  apply_method = ?,
+          real_name=?,
+		  mobile=?
+		WHERE user_id = ? `
+	_, err = o.Raw(msql, item.CompanyName, item.ApplyMethod, item.RealName, mobile, userId).Exec()
+	return
+}
+
+func GetApplyRecordCount(userId int) (count int, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT COUNT(1) AS count FROM cygx_apply_record WHERE user_id=? `
+	err = o.Raw(sql, userId).QueryRow(&count)
+	return
+}

+ 0 - 37
models/user.go

@@ -203,41 +203,4 @@ type ApplyTryReq struct {
 	ApplyMethod     int    `description:"1:已付费客户申请试用,2:非客户申请试用"`
 }
 
-type CygxApplyRecord struct {
-	ApplyRecordId   int       `orm:"column(apply_record_id);pk" description:"申请试用id"`
-	BusinessCardUrl string    `description:"名片地址"`
-	RealName        string    `description:"姓名"`
-	CompanyName     string    `description:"公司名称"`
-	Mobile          string    `description:"手机号"`
-	CreateTime      time.Time `description:"创建时间"`
-	ApplyMethod     int       `description:"1:已付费客户申请试用,2:非客户申请试用"`
-}
-
-func AddApplyRecord(item *ApplyTryReq, mobile,companyNamePay string, userId,companyIdPay int) (err error) {
-	o := orm.NewOrm()
-	o.Begin()
-	defer func() {
-		if err != nil {
-			o.Rollback()
-		} else {
-			o.Commit()
-		}
-	}()
 
-	sql := `INSERT INTO cygx_apply_record (user_id,business_card_url, real_name,company_name, mobile,create_time, apply_method,company_id_pay,company_name_pay)
-          VALUES(?,?,?,?,?,?,?,?,?) `
-	_, err = o.Raw(sql, userId, item.BusinessCardUrl, item.RealName, item.CompanyName, mobile, time.Now(), item.ApplyMethod,companyIdPay,companyNamePay).Exec()
-	if err != nil {
-		return
-	}
-	msql := `UPDATE  wx_user
-		SET
-		  note = ?,
-		  is_note = 1,
-		  apply_method = ?,
-          real_name=?,
-		  mobile=?
-		WHERE user_id = ? `
-	_, err = o.Raw(msql, item.CompanyName, item.ApplyMethod, item.RealName, mobile, userId).Exec()
-	return
-}

+ 69 - 3
services/elasticsearch.go

@@ -147,7 +147,74 @@ func SaveData() {
 	fmt.Println("end")
 }
 
-func SearchByKeyWord(keyWord, categoryName string) (result []*models.SearchItem, err error) {
+
+func SearchByKeyWord(keyWord string) (result []*models.SearchItem, err error) {
+	pageSize := 20
+	keyWordArr, err := GetIndustryMapNameSlice(keyWord)
+	if err != nil {
+		go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "GetIndustryMapNameSlice:"+err.Error(), utils.EmailSendToUsers)
+	}
+	var sniff = false //<4>
+	cfg := &config.Config{
+		URL:      ES_URL,
+		Username: ES_USERNAME,
+		Password: ES_PASSWORD,
+	}
+
+	cfg.Sniff = &sniff
+	client, err := elastic.NewClientFromConfig(cfg)
+	if err != nil {
+		return
+	}
+	var esIndex = "cygx_article"
+
+	searchMap := make(map[int]int)
+	for _, v := range keyWordArr {
+		keyWord = v
+		boolquery := elastic.NewBoolQuery()
+		boolquery.Must(elastic.NewMatchQuery("Title", keyWord), elastic.NewMatchQuery("BodyText", keyWord))
+
+		highlight := elastic.NewHighlight()
+		highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
+		highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
+		searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(boolquery).Do(context.Background())
+		if err != nil {
+			return result, err
+		}
+		if searchByMatch.Hits != nil {
+			for _, v := range searchByMatch.Hits.Hits {
+				articleJson, err := v.Source.MarshalJSON()
+				if err != nil {
+					return nil, err
+				}
+				article := new(models.CygxArticle)
+				err = json.Unmarshal(articleJson, &article)
+				if err != nil {
+					return nil, err
+				}
+				if _, ok := searchMap[article.ArticleId]; !ok {
+					searchItem := new(models.SearchItem)
+					searchItem.ArticleId, _ = strconv.Atoi(v.Id)
+					searchItem.Body = v.Highlight["BodyText"]
+					var title string
+					if len(v.Highlight["Title"]) > 0 {
+						title = v.Highlight["Title"][0]
+					} else {
+						title = article.Title
+					}
+					searchItem.Title = title
+					searchItem.PublishDate = article.PublishDate
+					result = append(result, searchItem)
+					searchMap[article.ArticleId] = article.ArticleId
+				}
+			}
+		}
+	}
+	return
+}
+
+
+func SearchByKeyWordBack(keyWord string) (result []*models.SearchItem, err error) {
 	pageSize := 20
 	keyWordArr, err := GetIndustryMapNameSlice(keyWord)
 	if err != nil {
@@ -167,7 +234,6 @@ func SearchByKeyWord(keyWord, categoryName string) (result []*models.SearchItem,
 	}
 	var esIndex = "cygx_article"
 
-	termsQuery := elastic.NewTermsQuery("category_name", categoryName)
 	searchMap := make(map[int]int)
 	for _, v := range keyWordArr {
 		keyWord = v
@@ -176,7 +242,7 @@ func SearchByKeyWord(keyWord, categoryName string) (result []*models.SearchItem,
 		highlight := elastic.NewHighlight()
 		highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
 		highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
-		searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(termsQuery).Query(boolquery).Do(context.Background())
+		searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(boolquery).Do(context.Background())
 		if err != nil {
 			return result, err
 		}