123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- package services
- import (
- "encoding/json"
- "errors"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "hongze/hz_crm_api/models"
- "hongze/hz_crm_api/models/company"
- "hongze/hz_crm_api/models/company_user"
- "hongze/hz_crm_api/models/system"
- "hongze/hz_crm_api/models/yb"
- "hongze/hz_crm_api/services/alarm_msg"
- "hongze/hz_crm_api/utils"
- "strconv"
- "strings"
- "time"
- )
- func MovePotentialUser(userId, companyId int, realName, mobile, email, remark string, companyProductList []*company.CompanyProduct, wxUser *models.WxUser, updateWxCol []string) (err error) {
- wxUser.CompanyId = companyId
- wxUser.Remark = remark
- wxUser.LastUpdatedTime = time.Now()
- updateWxCol = append(updateWxCol, "CompanyId", "Remark", "LastUpdatedTime")
- err = wxUser.Update(updateWxCol)
- if err != nil {
- return
- }
-
- _ = models.DeleteUserSellerRelationByUserId(userId)
-
- if companyProductList != nil && len(companyProductList) > 0 {
- for _, companyProduct := range companyProductList {
- _, tmpErr := models.AddUserSellerRelation(int64(userId), companyProduct.CompanyId, companyProduct.SellerId, companyProduct.ProductId, companyProduct.SellerName, mobile, email)
- if tmpErr != nil {
- utils.FileLog.Info("添加销售与联系人的关系失败,错误原因:,其他信息:user_id:%s;seller_id:%s;product_id:%s", tmpErr.Error(), userId, companyProduct.SellerId, companyProduct.ProductId)
- }
- }
- }
-
- _ = company_user.ClearUserChartPermission(userId)
- return
- }
- func MoveUser(userId, companyId, sellerId int, wxUser *models.WxUser, adminId int) (err error) {
- productId := 1
- var toSellerInfo *system.Admin
-
- if sellerId > 0 {
-
- tmpSellerInfo, queryErr := system.GetSysAdminById(sellerId)
- if queryErr != nil {
- err = errors.New("获取即将移动的销售信息失败,Err:" + queryErr.Error())
- return
- }
- companyProduct, tmpErr := company.GetCompanyProductByCompanyIdAndSellerId(companyId, sellerId)
- if tmpErr != nil {
- err = errors.New("获取即将移动的销售与客户信息失败,err:" + tmpErr.Error())
- return
- }
- productId = companyProduct.ProductId
-
-
-
-
-
- toSellerInfo = tmpSellerInfo
- }
-
- err = models.MoveUser(userId, companyId)
- if err != nil {
- return
- }
-
- _ = models.DeleteUserSellerRelationByUserId(userId)
-
- if toSellerInfo != nil {
-
- _, tmpErr := models.AddUserSellerRelation(int64(userId), companyId, sellerId, productId, toSellerInfo.AdminName, wxUser.Mobile, wxUser.Email)
- if tmpErr != nil {
- utils.FileLog.Info("添加销售与联系人的关系失败,错误原因:,其他信息:user_id:%s;seller_id:%s;product_id:%s", tmpErr.Error(), userId, sellerId, productId)
- }
- }
-
- _ = company_user.ClearUserChartPermission(userId)
-
- _ = DealFiccApply(userId, adminId)
- return
- }
- func DeleteWxUser(userId int) (err error) {
-
- err = models.DelUserRecordByUserId(userId)
- if err != nil {
- return
- }
-
- err = company.DeleteCompanyUser(userId)
-
- _ = company_user.ClearUserChartPermission(userId)
- return
- }
- func DeleteCompanyWxUserByProductId(companyId, productId int) (list []*models.UserSellerRelation, userRecordList []*models.UserRecord, wxUserList []*models.WxUser, chartClassifyPermission []*company_user.ChartClassifyPermission, err error) {
-
-
- list, err = models.GetCompanyUserSellerRelationByProductId(companyId, productId)
-
-
-
- notList, err := models.GetNotCompanyUserSellerRelationByProductId(companyId, productId)
-
-
- wxUserMap := make(map[int]int)
- for _, wxUser := range notList {
- wxUserMap[wxUser.UserId] = 1
- }
-
- userIdSlice := make([]string, 0)
- for _, wxUser := range list {
-
-
- if _, ok := wxUserMap[wxUser.UserId]; ok == false {
- userIdSlice = append(userIdSlice, strconv.Itoa(wxUser.UserId))
- }
- }
-
-
- err = models.DelCompanyUserSellerRelationByProductId(companyId, productId)
- if err != nil {
- return
- }
- if len(userIdSlice) > 0 {
- userIdStr := strings.Join(userIdSlice, ",")
-
- userRecordList, err = models.GetUserRecordListByUserIds(userIdStr)
- if err != nil {
- return
- }
-
- err = models.DelUserRecordByUserIds(userIdStr)
- if err != nil {
- return
- }
-
- wxUserList, err = models.GetWxUserListByUserIds(userIdStr)
- if err != nil {
- return
- }
-
- err = models.DeleteWxUserByUserIds(userIdStr)
-
- chartClassifyPermission, err = company_user.GetCompanyChartPermissionByCompanyId(companyId)
- if err != nil {
- return
- }
- _ = company_user.ClearCompanyChartPermission(companyId)
- }
- return
- }
- func GetUserCountByCompanyId(companyId, productId int) (total int, err error) {
- var condition string
- var pars []interface{}
- if productId > 0 {
- condition += ` AND a.company_id = ? AND b.company_id = ? AND b.product_id = ? `
- pars = append(pars, companyId, companyId, productId)
- tmpTotal, tmpErr := company.GetCompanyUserListCountV2(condition, pars, companyId)
- total = tmpTotal
- err = tmpErr
- } else {
- tmpTotal, tmpErr := company.GetCompanyUserCount(companyId)
- total = tmpTotal
- err = tmpErr
- }
- return
- }
- func DealPotentialUser(userId, sysUserId int) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
-
- wxUser, err := models.GetWxUserByUserId(userId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- err = errors.New(fmt.Sprint("用户不存在", err))
- return
- }
- return
- }
- if wxUser.IsDeal != 0 {
- err = errors.New(fmt.Sprint("用户标记状态有误", err))
- return
- }
- nowTime := time.Now()
- wxUser.IsDeal = 1
- wxUser.LastUpdatedTime = nowTime
- userUpdateCols := make([]string, 0)
- userUpdateCols = append(userUpdateCols, "IsDeal", "LastUpdatedTime")
- err = wxUser.Update(userUpdateCols)
-
- err = yb.DealUserApplyRecords(userId, sysUserId, nowTime)
- return
- }
- func MovePotentialUserByApplyRecord(adminId, userId, companyId, applyRecordId int, realName, mobile, email, remark string, companyProductList []*company.CompanyProduct, wxUser *models.WxUser, updateWxCol []string) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
-
- err = MovePotentialUser(userId, companyId, realName, mobile, email, remark, companyProductList, wxUser, updateWxCol)
- if err != nil {
- return
- }
-
- if applyRecordId > 0 {
- applyRecord, tErr := yb.GetApplyRecordById(applyRecordId)
- if tErr != nil {
- if tErr.Error() == utils.ErrNoRow() {
- err = errors.New(fmt.Sprint("申请记录不存在:", err))
- }
- err = tErr
- return
- }
- updateCols := make([]string, 0)
- applyRecord.IsMove = 1
- updateCols = append(updateCols, "IsMove")
- if applyRecord.OpStatus == 0 {
- applyRecord.OpStatus = 1
- applyRecord.DealTime = time.Now()
- applyRecord.SysUserId = adminId
- updateCols = append(updateCols, "OpStatus", "DealTime", "SysUserId")
- }
- err = applyRecord.Update(updateCols)
- if err != nil {
- err = errors.New(fmt.Sprint("标记申请单失败", err))
- return
- }
- }
- return
- }
- func SwitchHzUserEnabledByMobile(opEnabled int, mobile string) (err error) {
- if mobile == "" {
- return
- }
- userInfo, err := models.GetWxUserByMobile(mobile)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
-
- if userInfo != nil {
-
- updateCols := make([]string, 0)
- if opEnabled == 0 && userInfo.CompanyId == 16 {
- userInfo.CompanyId = 1
- userInfo.LastUpdatedTime = time.Now()
- updateCols = append(updateCols, "CompanyId", "LastUpdatedTime")
- _ = userInfo.Update(updateCols)
- }
-
- if opEnabled == 1 && userInfo.CompanyId == 1 {
- userInfo.CompanyId = 16
- userInfo.LastUpdatedTime = time.Now()
- updateCols = append(updateCols, "CompanyId", "LastUpdatedTime")
- _ = userInfo.Update(updateCols)
- }
- }
- return nil
- }
- func DeleteHzUserByMobile(mobile string) (err error) {
- if mobile == "" {
- return
- }
- companyId := 16
- userInfo, err := models.GetWxUserByCompanyIdAndMobile(companyId, mobile)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- if userInfo != nil {
- userId := int(userInfo.UserId)
- if err = models.DeleteWxUserAndRecordByUserId(userId); err != nil {
- return
- }
-
- _ = models.DeleteUserSellerRelationByProductId(userId, 1)
- _ = models.DeleteUserSellerRelationByProductId(userId, 2)
-
- go ModifyCompanyProductLastViewData([]int{userInfo.CompanyId})
- }
- return nil
- }
- func ForbiddenSysUserByMobile(mobile string) (err error) {
- if mobile == "" {
- return
- }
- adminInfo, _ := system.GetSysUserByMobile(mobile)
- if adminInfo == nil {
- return
- }
-
- if err = system.DisableAdmin(adminInfo.AdminId); err != nil {
- return
- }
- return
- }
- func DealFiccApply(userId, sysUserId int) (err error) {
-
- nowTime := time.Now()
- err = yb.DealUnDealFiccApplyByIds(userId, sysUserId, nowTime)
- return
- }
- func AddHzCompanyUser(mobile, realName string, adminId int, adminName string) {
- var err error
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("自动添加弘则联系人失败, Err: "+err.Error(), 3)
- }
- }()
- mobile = utils.TrimStr(mobile)
- realName = utils.TrimStr(realName)
- if mobile == "" || realName == "" {
- return
- }
-
- cacheKey := "wx_user:mobile:" + mobile
- isHas := utils.Rc.IsExist(cacheKey)
- if isHas == true {
- return
- }
- utils.Rc.SetNX(cacheKey, 1, time.Second*300)
- defer func() {
- _ = utils.Rc.Delete(cacheKey)
- }()
-
- userItem, e := company.GetUserCountByMobile(mobile)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("获取手机号对应联系人失败, Err: " + e.Error())
- return
- }
- if userItem != nil {
- return
- }
-
- companyId := utils.HZ_COMPANY_ID
- newUser := &models.WxUser{
- RealName: realName,
- Sex: 1,
- CountryCode: utils.DEFAULT_COUNTRY_CODE,
- Mobile: mobile,
- OutboundCountryCode: utils.DEFAULT_COUNTRY_CODE,
- IsMaker: 0,
- CompanyId: companyId,
- CreatedTime: time.Now().Local(),
- RegisterPlatform: utils.WX_USER_REGISTER_PLATFORM_ADMIN,
- }
- userId, e := models.AddWxUser(newUser)
- if e != nil {
- err = errors.New("新增弘则联系人失败, Err: " + e.Error())
- return
- }
- newUser.UserId = userId
-
- productList := make([]*company.CompanyProduct, 0)
- ficcProduct, e := company.GetCompanyProductByCompanyIdAndProductId(companyId, 1)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("获取弘则FICC产品失败, Err: " + e.Error())
- return
- }
- if ficcProduct != nil {
- productList = append(productList, ficcProduct)
- }
- equityProduct, e := company.GetCompanyProductByCompanyIdAndProductId(companyId, 2)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("获取弘则权益产品失败, Err: " + e.Error())
- return
- }
- if equityProduct != nil {
- productList = append(productList, equityProduct)
- }
-
- for _, p := range productList {
- if _, e = models.AddUserSellerRelation(userId, p.CompanyId, p.SellerId, p.ProductId, p.SellerName, mobile, ""); e != nil {
- err = errors.New(fmt.Sprintf("新增弘则联系人与销售关系失败, ProductId: %d, Err: %s", p.ProductId, e.Error()))
- return
- }
- }
-
- userJson, _ := json.Marshal(newUser)
- go AddWxUserOpLog(company.WxUserOpLog{
- LogType: "add",
- UserId: int(newUser.UserId),
- CompanyId: newUser.CompanyId,
- Mobile: newUser.Mobile,
- Email: newUser.Email,
- OriginalUserInfo: "",
- UserInfo: string(userJson),
- OriginalUserSellerInfo: "",
- UserSellerInfo: "",
- OpUserId: adminId,
- OpUserName: adminName,
- CreateTime: time.Now(),
- })
- return
- }
|