user.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "hongze/hz_crm_api/models"
  8. "hongze/hz_crm_api/models/company"
  9. "hongze/hz_crm_api/models/company_user"
  10. "hongze/hz_crm_api/models/system"
  11. "hongze/hz_crm_api/models/yb"
  12. "hongze/hz_crm_api/services/alarm_msg"
  13. "hongze/hz_crm_api/utils"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // 移动潜在客户名下的联系人
  19. func MovePotentialUser(userId, companyId int, realName, mobile, email, remark string, companyProductList []*company.CompanyProduct, wxUser *models.WxUser, updateWxCol []string) (err error) {
  20. wxUser.CompanyId = companyId
  21. wxUser.Remark = remark
  22. wxUser.LastUpdatedTime = time.Now()
  23. updateWxCol = append(updateWxCol, "CompanyId", "Remark", "LastUpdatedTime")
  24. err = wxUser.Update(updateWxCol)
  25. if err != nil {
  26. return
  27. }
  28. //删除原有的销售关系
  29. _ = models.DeleteUserSellerRelationByUserId(userId)
  30. //添加新的联系人与销售员的关系
  31. if companyProductList != nil && len(companyProductList) > 0 {
  32. for _, companyProduct := range companyProductList {
  33. _, tmpErr := models.AddUserSellerRelation(int64(userId), companyProduct.CompanyId, companyProduct.SellerId, companyProduct.ProductId, companyProduct.SellerName, mobile, email)
  34. if tmpErr != nil {
  35. utils.FileLog.Info("添加销售与联系人的关系失败,错误原因:,其他信息:user_id:%s;seller_id:%s;product_id:%s", tmpErr.Error(), userId, companyProduct.SellerId, companyProduct.ProductId)
  36. }
  37. }
  38. }
  39. // 若联系人有图表权限则清除
  40. _ = company_user.ClearUserChartPermission(userId)
  41. return
  42. }
  43. // 移动销售名下的联系人
  44. func MoveUser(userId, companyId, sellerId int, wxUser *models.WxUser, adminId int) (err error) {
  45. productId := 1
  46. var toSellerInfo *system.Admin
  47. //有选择销售的情况下(正常来说就是不是移动到潜在客户中去的情况下)
  48. if sellerId > 0 {
  49. //获取新的销售信息
  50. tmpSellerInfo, queryErr := system.GetSysAdminById(sellerId)
  51. if queryErr != nil {
  52. err = errors.New("获取即将移动的销售信息失败,Err:" + queryErr.Error())
  53. return
  54. }
  55. companyProduct, tmpErr := company.GetCompanyProductByCompanyIdAndSellerId(companyId, sellerId)
  56. if tmpErr != nil {
  57. err = errors.New("获取即将移动的销售与客户信息失败,err:" + tmpErr.Error())
  58. return
  59. }
  60. productId = companyProduct.ProductId
  61. //productId = GetProductId(tmpSellerInfo.RoleTypeCode)
  62. //if productId == 0 {
  63. // err = errors.New("销售信息异常")
  64. // return
  65. //}
  66. toSellerInfo = tmpSellerInfo
  67. }
  68. //将联系人挪移到 对应的 企业客户 下面
  69. err = models.MoveUser(userId, companyId)
  70. if err != nil {
  71. return
  72. }
  73. //删除联系人与原有的销售关系
  74. _ = models.DeleteUserSellerRelationByUserId(userId)
  75. //不是挪到潜在客户的话,那么就不需要重新 添加新的联系人与销售员的关系(正常来说就是不是移动到潜在客户中去的情况下)
  76. if toSellerInfo != nil {
  77. //添加新的联系人与销售员的关系
  78. _, tmpErr := models.AddUserSellerRelation(int64(userId), companyId, sellerId, productId, toSellerInfo.AdminName, wxUser.Mobile, wxUser.Email)
  79. if tmpErr != nil {
  80. utils.FileLog.Info("添加销售与联系人的关系失败,错误原因:,其他信息:user_id:%s;seller_id:%s;product_id:%s", tmpErr.Error(), userId, sellerId, productId)
  81. }
  82. }
  83. // 若联系人有图表权限则清除
  84. _ = company_user.ClearUserChartPermission(userId)
  85. // 若联系人发起过ficc申请则标记为已处理,已移动
  86. _ = DealFiccApply(userId, adminId)
  87. return
  88. }
  89. // 删除微信用户
  90. func DeleteWxUser(userId int) (err error) {
  91. //删除用户与第三方(微信用户)的关系
  92. err = models.DelUserRecordByUserId(userId)
  93. if err != nil {
  94. return
  95. }
  96. //删除用户
  97. err = company.DeleteCompanyUser(userId)
  98. // 若联系人有图表权限则清除
  99. _ = company_user.ClearUserChartPermission(userId)
  100. return
  101. }
  102. // 删除微信用户
  103. func DeleteCompanyWxUserByProductId(companyId, productId int) (list []*models.UserSellerRelation, userRecordList []*models.UserRecord, wxUserList []*models.WxUser, chartClassifyPermission []*company_user.ChartClassifyPermission, err error) {
  104. //productId = 1
  105. //获取当前产品 的 所有联系人
  106. list, err = models.GetCompanyUserSellerRelationByProductId(companyId, productId)
  107. //只删除自己的联系人(如果该联系人是共享,那么也不删除)
  108. //fmt.Println(list)
  109. //获取不是当前产品的所有联系人
  110. notList, err := models.GetNotCompanyUserSellerRelationByProductId(companyId, productId)
  111. //fmt.Println(notList)
  112. //不是当前产品的所有联系人map,真正删除的时候,用来做判断是否存在该联系人
  113. wxUserMap := make(map[int]int)
  114. for _, wxUser := range notList {
  115. wxUserMap[wxUser.UserId] = 1
  116. }
  117. //fmt.Println(wxUserMap)
  118. userIdSlice := make([]string, 0)
  119. for _, wxUser := range list {
  120. //_, ok := wxUserMap[wxUser.UserId]
  121. //fmt.Println(wxUser.UserId, "===:", ok)
  122. if _, ok := wxUserMap[wxUser.UserId]; ok == false {
  123. userIdSlice = append(userIdSlice, strconv.Itoa(wxUser.UserId))
  124. }
  125. }
  126. //fmt.Println(userIdSlice)
  127. //删除用户与销售员的关系
  128. err = models.DelCompanyUserSellerRelationByProductId(companyId, productId)
  129. if err != nil {
  130. return
  131. }
  132. if len(userIdSlice) > 0 {
  133. userIdStr := strings.Join(userIdSlice, ",")
  134. //根据用户id字符串获取用户与第三方(微信用户)的关系列表
  135. userRecordList, err = models.GetUserRecordListByUserIds(userIdStr)
  136. if err != nil {
  137. return
  138. }
  139. //删除用户与第三方(微信用户)的关系
  140. err = models.DelUserRecordByUserIds(userIdStr)
  141. if err != nil {
  142. return
  143. }
  144. //删除用户
  145. wxUserList, err = models.GetWxUserListByUserIds(userIdStr)
  146. if err != nil {
  147. return
  148. }
  149. //删除用户
  150. err = models.DeleteWxUserByUserIds(userIdStr)
  151. // 清除客户联系人图表权限设置
  152. chartClassifyPermission, err = company_user.GetCompanyChartPermissionByCompanyId(companyId)
  153. if err != nil {
  154. return
  155. }
  156. _ = company_user.ClearCompanyChartPermission(companyId)
  157. }
  158. return
  159. }
  160. // 根绝客户id获取客户下面的联系人数量
  161. func GetUserCountByCompanyId(companyId, productId int) (total int, err error) {
  162. var condition string
  163. var pars []interface{}
  164. if productId > 0 {
  165. condition += ` AND a.company_id = ? AND b.company_id = ? AND b.product_id = ? `
  166. pars = append(pars, companyId, companyId, productId)
  167. tmpTotal, tmpErr := company.GetCompanyUserListCountV2(condition, pars, companyId)
  168. total = tmpTotal
  169. err = tmpErr
  170. } else {
  171. tmpTotal, tmpErr := company.GetCompanyUserCount(companyId)
  172. total = tmpTotal
  173. err = tmpErr
  174. }
  175. return
  176. }
  177. // DealPotentialUser 标记潜在用户
  178. func DealPotentialUser(userId, sysUserId int) (err error) {
  179. o := orm.NewOrm()
  180. to, err := o.Begin()
  181. if err != nil {
  182. return
  183. }
  184. defer func() {
  185. if err != nil {
  186. _ = to.Rollback()
  187. } else {
  188. _ = to.Commit()
  189. }
  190. }()
  191. // 1.标记用户表
  192. wxUser, err := models.GetWxUserByUserId(userId)
  193. if err != nil {
  194. if err.Error() == utils.ErrNoRow() {
  195. err = errors.New(fmt.Sprint("用户不存在", err))
  196. return
  197. }
  198. return
  199. }
  200. if wxUser.IsDeal != 0 {
  201. err = errors.New(fmt.Sprint("用户标记状态有误", err))
  202. return
  203. }
  204. nowTime := time.Now()
  205. wxUser.IsDeal = 1
  206. wxUser.LastUpdatedTime = nowTime
  207. userUpdateCols := make([]string, 0)
  208. userUpdateCols = append(userUpdateCols, "IsDeal", "LastUpdatedTime")
  209. err = wxUser.Update(userUpdateCols)
  210. // 2.标记申请记录表中所有该用户的申请
  211. err = yb.DealUserApplyRecords(userId, sysUserId, nowTime)
  212. return
  213. }
  214. // MovePotentialUserByApplyRecord 通过FICC申请单移动客户
  215. func MovePotentialUserByApplyRecord(adminId, userId, companyId, applyRecordId int, realName, mobile, email, remark string, companyProductList []*company.CompanyProduct, wxUser *models.WxUser, updateWxCol []string) (err error) {
  216. o := orm.NewOrm()
  217. to, err := o.Begin()
  218. if err != nil {
  219. return
  220. }
  221. defer func() {
  222. if err != nil {
  223. _ = to.Rollback()
  224. } else {
  225. _ = to.Commit()
  226. }
  227. }()
  228. // 移动潜在客户
  229. err = MovePotentialUser(userId, companyId, realName, mobile, email, remark, companyProductList, wxUser, updateWxCol)
  230. if err != nil {
  231. return
  232. }
  233. // 标记FICC申请单为已移动
  234. if applyRecordId > 0 {
  235. applyRecord, tErr := yb.GetApplyRecordById(applyRecordId)
  236. if tErr != nil {
  237. if tErr.Error() == utils.ErrNoRow() {
  238. err = errors.New(fmt.Sprint("申请记录不存在:", err))
  239. }
  240. err = tErr
  241. return
  242. }
  243. updateCols := make([]string, 0)
  244. applyRecord.IsMove = 1
  245. updateCols = append(updateCols, "IsMove")
  246. if applyRecord.OpStatus == 0 {
  247. applyRecord.OpStatus = 1
  248. applyRecord.DealTime = time.Now()
  249. applyRecord.SysUserId = adminId
  250. updateCols = append(updateCols, "OpStatus", "DealTime", "SysUserId")
  251. }
  252. err = applyRecord.Update(updateCols)
  253. if err != nil {
  254. err = errors.New(fmt.Sprint("标记申请单失败", err))
  255. return
  256. }
  257. }
  258. return
  259. }
  260. // SwitchHzUserEnabledByMobile 根据手机号启用/禁用弘则研究下的联系人
  261. func SwitchHzUserEnabledByMobile(opEnabled int, mobile string) (err error) {
  262. if mobile == "" {
  263. return
  264. }
  265. userInfo, err := models.GetWxUserByMobile(mobile)
  266. if err != nil && err.Error() != utils.ErrNoRow() {
  267. return
  268. }
  269. // 存在相应联系人
  270. if userInfo != nil {
  271. // 禁用-联系人为弘则研究下的则设置为潜在用户
  272. updateCols := make([]string, 0)
  273. if opEnabled == 0 && userInfo.CompanyId == 16 {
  274. userInfo.CompanyId = 1
  275. userInfo.LastUpdatedTime = time.Now()
  276. updateCols = append(updateCols, "CompanyId", "LastUpdatedTime")
  277. _ = userInfo.Update(updateCols)
  278. }
  279. // 启用-联系人为潜在用户则设置为弘则研究下
  280. if opEnabled == 1 && userInfo.CompanyId == 1 {
  281. userInfo.CompanyId = 16
  282. userInfo.LastUpdatedTime = time.Now()
  283. updateCols = append(updateCols, "CompanyId", "LastUpdatedTime")
  284. _ = userInfo.Update(updateCols)
  285. }
  286. }
  287. return nil
  288. }
  289. // DeleteHzUserByMobile 根据手机号删除弘则研究下的联系人
  290. func DeleteHzUserByMobile(mobile string) (err error) {
  291. if mobile == "" {
  292. return
  293. }
  294. companyId := 16
  295. userInfo, err := models.GetWxUserByCompanyIdAndMobile(companyId, mobile)
  296. if err != nil && err.Error() != utils.ErrNoRow() {
  297. return
  298. }
  299. if userInfo != nil {
  300. userId := int(userInfo.UserId)
  301. if err = models.DeleteWxUserAndRecordByUserId(userId); err != nil {
  302. return
  303. }
  304. // 删除所有的标识,并真正删除数据
  305. _ = models.DeleteUserSellerRelationByProductId(userId, 1)
  306. _ = models.DeleteUserSellerRelationByProductId(userId, 2)
  307. // 更新客户最近阅读次数以及最近阅读时间
  308. go ModifyCompanyProductLastViewData([]int{userInfo.CompanyId})
  309. }
  310. return nil
  311. }
  312. // ForbiddenSysUserByMobile 根据手机号禁用联系人关联的系统管理员
  313. func ForbiddenSysUserByMobile(mobile string) (err error) {
  314. if mobile == "" {
  315. return
  316. }
  317. adminInfo, _ := system.GetSysUserByMobile(mobile)
  318. if adminInfo == nil {
  319. return
  320. }
  321. // 禁用管理员
  322. if err = system.DisableAdmin(adminInfo.AdminId); err != nil {
  323. return
  324. }
  325. return
  326. }
  327. // DealFiccApply 用户一旦被移动将未被标记处理的用户都标记成已处理
  328. func DealFiccApply(userId, sysUserId int) (err error) {
  329. //更新申请为标记处理
  330. nowTime := time.Now()
  331. err = yb.DealUnDealFiccApplyByIds(userId, sysUserId, nowTime)
  332. return
  333. }
  334. // AddHzCompanyUser 新增弘则联系人
  335. func AddHzCompanyUser(mobile, realName string, adminId int, adminName string) {
  336. var err error
  337. defer func() {
  338. if err != nil {
  339. go alarm_msg.SendAlarmMsg("自动添加弘则联系人失败, Err: "+err.Error(), 3)
  340. }
  341. }()
  342. mobile = utils.TrimStr(mobile)
  343. realName = utils.TrimStr(realName)
  344. if mobile == "" || realName == "" {
  345. return
  346. }
  347. // 设置3分钟缓存, 不允许重复添加, 添加完成删除对应的缓存
  348. cacheKey := "wx_user:mobile:" + mobile
  349. isHas := utils.Rc.IsExist(cacheKey)
  350. if isHas == true {
  351. return
  352. }
  353. utils.Rc.SetNX(cacheKey, 1, time.Second*300)
  354. defer func() {
  355. _ = utils.Rc.Delete(cacheKey)
  356. }()
  357. // 此处简化操作, 但凡能查出手机号对应的联系人, 不管是否为弘则研究均直接返回, 手动加联系人
  358. userItem, e := company.GetUserCountByMobile(mobile)
  359. if e != nil && e.Error() != utils.ErrNoRow() {
  360. err = errors.New("获取手机号对应联系人失败, Err: " + e.Error())
  361. return
  362. }
  363. if userItem != nil {
  364. return
  365. }
  366. // 新增用户
  367. companyId := utils.HZ_COMPANY_ID
  368. newUser := &models.WxUser{
  369. RealName: realName,
  370. Sex: 1, // 默认1-男
  371. CountryCode: utils.DEFAULT_COUNTRY_CODE,
  372. Mobile: mobile,
  373. OutboundCountryCode: utils.DEFAULT_COUNTRY_CODE,
  374. IsMaker: 0,
  375. CompanyId: companyId,
  376. CreatedTime: time.Now().Local(),
  377. RegisterPlatform: utils.WX_USER_REGISTER_PLATFORM_ADMIN,
  378. }
  379. userId, e := models.AddWxUser(newUser)
  380. if e != nil {
  381. err = errors.New("新增弘则联系人失败, Err: " + e.Error())
  382. return
  383. }
  384. newUser.UserId = userId
  385. // 弘则联系人两种产品都有
  386. productList := make([]*company.CompanyProduct, 0)
  387. ficcProduct, e := company.GetCompanyProductByCompanyIdAndProductId(companyId, 1)
  388. if e != nil && e.Error() != utils.ErrNoRow() {
  389. err = errors.New("获取弘则FICC产品失败, Err: " + e.Error())
  390. return
  391. }
  392. if ficcProduct != nil {
  393. productList = append(productList, ficcProduct)
  394. }
  395. equityProduct, e := company.GetCompanyProductByCompanyIdAndProductId(companyId, 2)
  396. if e != nil && e.Error() != utils.ErrNoRow() {
  397. err = errors.New("获取弘则权益产品失败, Err: " + e.Error())
  398. return
  399. }
  400. if equityProduct != nil {
  401. productList = append(productList, equityProduct)
  402. }
  403. // 将联系人与销售员绑定
  404. for _, p := range productList {
  405. if _, e = models.AddUserSellerRelation(userId, p.CompanyId, p.SellerId, p.ProductId, p.SellerName, mobile, ""); e != nil {
  406. err = errors.New(fmt.Sprintf("新增弘则联系人与销售关系失败, ProductId: %d, Err: %s", p.ProductId, e.Error()))
  407. return
  408. }
  409. }
  410. // 记录新增用户日志
  411. userJson, _ := json.Marshal(newUser)
  412. go AddWxUserOpLog(company.WxUserOpLog{
  413. LogType: "add",
  414. UserId: int(newUser.UserId),
  415. CompanyId: newUser.CompanyId,
  416. Mobile: newUser.Mobile,
  417. Email: newUser.Email,
  418. OriginalUserInfo: "",
  419. UserInfo: string(userJson),
  420. OriginalUserSellerInfo: "",
  421. UserSellerInfo: "",
  422. OpUserId: adminId,
  423. OpUserName: adminName,
  424. CreateTime: time.Now(),
  425. })
  426. return
  427. }