Browse Source

用户绑定对应关联销售

xingzai 3 years ago
parent
commit
92b3949198
5 changed files with 71 additions and 6 deletions
  1. 8 0
      models/company.go
  2. 38 0
      models/crm_company.go
  3. 1 0
      models/db.go
  4. 16 0
      models/wx_user.go
  5. 8 6
      services/htgj.go

+ 8 - 0
models/company.go

@@ -179,3 +179,11 @@ func GetCompanyByName(companyName string) (item *Company, err error) {
 	err = o.Raw(sql, companyName).QueryRow(&item)
 	return
 }
+
+//获取公司详情详情
+func GetCompanyByThirdName(tripartiteCompanyCode string) (item *Company, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT *  FROM company WHERE tripartite_company_code = ? LIMIT 1`
+	err = o.Raw(sql, tripartiteCompanyCode).QueryRow(&item)
+	return
+}

+ 38 - 0
models/crm_company.go

@@ -189,6 +189,13 @@ func GetCompanyCountByName(companyName string) (count int, err error) {
 	return
 }
 
+//判断三方公司公司名称是否存在
+func GetCompanyCountByThirdName(tripartiteCompanyCode string) (count int, err error) {
+	sql := `SELECT  COUNT(1) AS count FROM company WHERE tripartite_company_code = ?`
+	err = orm.NewOrm().Raw(sql, tripartiteCompanyCode).QueryRow(&count)
+	return
+}
+
 //获取来源详情
 func GetCompanySourceByName(companyName string) (item *CompanySource, err error) {
 	o := orm.NewOrm()
@@ -280,3 +287,34 @@ func AddCompanyLog(item *CompanyLog) (err error) {
 	_, err = o.Insert(item)
 	return
 }
+
+type UserSellerRelation struct {
+	RelationId int64     `orm:"column(relation_id);pk"`
+	UserId     int       `description:"用户id"`
+	CompanyId  int       `description:"企业用户id"`
+	SellerId   int       `description:"销售id"`
+	Seller     string    `description:"销售员名称"`
+	ProductId  int       `description:"产品id"`
+	Mobile     string    `description:"手机号"`
+	Email      string    `description:"邮箱"`
+	ModifyTime time.Time `description:"修改时间"`
+	CreateTime time.Time `description:"创建时间"`
+}
+
+//添加销售员与员工的关系
+func AddUserSellerRelation(userId int64, companyId, sellerId, productId int, seller, mobile, email string) (lastId int64, err error) {
+	o := orm.NewOrm()
+	relation := UserSellerRelation{
+		UserId:     int(userId),
+		SellerId:   sellerId,
+		CompanyId:  companyId,
+		Seller:     seller,
+		ProductId:  productId,
+		Mobile:     mobile,
+		Email:      email,
+		CreateTime: time.Now(),
+		ModifyTime: time.Now(),
+	}
+	lastId, err = o.Insert(&relation)
+	return
+}

+ 1 - 0
models/db.go

@@ -92,5 +92,6 @@ func init() {
 		new(CompanyPermissionLog),
 		new(CompanyProduct),
 		new(CompanyProductLog),
+		new(UserSellerRelation),
 	)
 }

+ 16 - 0
models/wx_user.go

@@ -48,6 +48,7 @@ type WxUser struct {
 	CountryCode         string    `description:"区号"`
 	OutboundMobile      string    `description:"外呼手机号"`
 	OutboundCountryCode string    `description:"外呼手机号区号"`
+	TripartiteCode      string    `description:"第三方给过来的用户编码,判断用户是否存在"`
 }
 
 //添加用户信息
@@ -258,6 +259,13 @@ func GetUserCountByName(companyId int, name string) (count int, err error) {
 	return
 }
 
+//判断公司下用户名称是否存在
+func GetUserCountByThirdName(companyId int, name string) (count int, err error) {
+	sql := `SELECT  COUNT(1) AS count FROM wx_user WHERE company_id = ? AND tripartite_code = ?`
+	err = orm.NewOrm().Raw(sql, companyId, name).QueryRow(&count)
+	return
+}
+
 func UpdateUserMobile(uid int, mobile string) (err error) {
 	sql := ` UPDATE wx_user SET mobile=?
 			WHERE user_id=? `
@@ -272,3 +280,11 @@ func GetUserByName(companyId int, name string) (item *WxUser, err error) {
 	err = o.Raw(sql, companyId, name).QueryRow(&item)
 	return
 }
+
+//获取公司下用户详情详情详情
+func GetUserByThirdName(companyId int, name string) (item *WxUser, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT *  FROM wx_user WHERE company_id = ? AND tripartite_code = ? `
+	err = o.Raw(sql, companyId, name).QueryRow(&item)
+	return
+}

+ 8 - 6
services/htgj.go

@@ -30,9 +30,8 @@ func CheckHtgj(companyCodeHt, companyNameHt, email, sign string) (errMsg string,
 		errHt = err
 		return
 	}
-
 	wxuUserName = email
-	countCompay, err := models.GetCompanyCountByName(companyName)
+	countCompay, err := models.GetCompanyCountByThirdName(tripartiteCompanyCode)
 	if err != nil {
 		errHt = err
 		return
@@ -97,7 +96,7 @@ func CheckHtgj(companyCodeHt, companyNameHt, email, sign string) (errMsg string,
 		companyProduct.CreateTime = time.Now()
 		companyProduct.ModifyTime = time.Now()
 		companyProduct.CompanyType = "权益"
-		companyProduct.OpenCode = GenerateOpenCompanyProductCode(int(companyId), 2)
+		companyProduct.OpenCode = GenerateOpenCompanyProductCode(int(companyId), productId)
 		companyProductId, err := models.AddCompanyProduct(companyProduct)
 		if err != nil {
 			errHt = err
@@ -194,18 +193,20 @@ func CheckHtgj(companyCodeHt, companyNameHt, email, sign string) (errMsg string,
 		}
 
 	} else {
-		companyInfo, err := models.GetCompanyByName(companyName)
+		companyInfo, err := models.GetCompanyByThirdName(tripartiteCompanyCode)
 		if err != nil {
 			errHt = err
 			return
 		}
 		companyId = int64(companyInfo.CompanyId)
 	}
-	countUser, err := models.GetUserCountByName(int(companyId), wxuUserName)
+	countUser, err := models.GetUserCountByThirdName(int(companyId), wxuUserName)
 	if countUser == 0 {
 		wxUser.CompanyId = int(companyId)
 		wxUser.RealName = wxuUserName
 		wxUser.Email = wxuUserName
+		wxUser.TripartiteCode = wxuUserName
+		wxUser.Source = 7
 		wxUser.CreatedTime = time.Now()
 		newUserId, err := models.AddWxUser(wxUser)
 		if err != nil {
@@ -220,8 +221,9 @@ func CheckHtgj(companyCodeHt, companyNameHt, email, sign string) (errMsg string,
 			errHt = err
 			return
 		}
+		models.AddUserSellerRelation(newUserId, int(companyId), sellerInfo.AdminId, productId, sellerInfo.RealName, wxUser.Mobile, wxuUserName)
 	} else {
-		wxUser, err = models.GetUserByName(int(companyId), wxuUserName)
+		wxUser, err = models.GetUserByThirdName(int(companyId), wxuUserName)
 		if err != nil {
 			errHt = err
 			return