user.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. package eta_business
  2. import (
  3. "encoding/json"
  4. "eta/eta_forum_admin/controllers"
  5. "eta/eta_forum_admin/models"
  6. "eta/eta_forum_admin/models/eta_business"
  7. eta_business2 "eta/eta_forum_admin/services/eta_business"
  8. "eta/eta_forum_admin/utils"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "github.com/tealeg/xlsx"
  12. "os"
  13. "strings"
  14. "time"
  15. )
  16. // 客户联系人管理
  17. type EtaBusinessUserController struct {
  18. controllers.BaseAuthController
  19. }
  20. // List
  21. // @Title 联系人列表
  22. // @Description 联系人列表接口
  23. // @Param PageSize query int true "每页数据条数"
  24. // @Param CurrentIndex query int true "当前页页码,从1开始"
  25. // @Param CompanyId query int true "公司id,必填"
  26. // @Param KeyWord query string true "搜索关键词"
  27. // @Param ProductType query int true "产品类型"
  28. // @Param ProductId query int true "产品id"
  29. // @Param IsSubscribe query int true "是否关注了公众号: -1-默认全部; 0-未关注; 1-已关注"
  30. // @Param IsSubscribeHzyj query int true "是否关注了弘则研究公众号: -1-默认全部; 0-未关注; 1-已关注"
  31. // @Param IsSubscribeCygx query int true "是否关注了查研观向小助手公众号: -1-默认全部; 0-未关注; 1-已关注"
  32. // @Success 200 {object} company.CompanyUserListResp
  33. // @router /user/list [get]
  34. func (this *EtaBusinessUserController) List() {
  35. br := new(models.BaseResponse).Init()
  36. defer func() {
  37. this.Data["json"] = br
  38. this.ServeJSON()
  39. }()
  40. sysUser := this.SysUser
  41. if sysUser == nil {
  42. br.Msg = "请登录"
  43. br.ErrMsg = "请登录,SysUser Is Empty"
  44. br.Ret = 408
  45. return
  46. }
  47. pageSize, _ := this.GetInt("PageSize")
  48. currentIndex, _ := this.GetInt("CurrentIndex")
  49. positionStatus, _ := this.GetInt("PositionStatus", -1)
  50. enabled, _ := this.GetInt("Enabled", -1)
  51. businessCode := this.GetString("BusinessCode")
  52. keyword := utils.TrimStr(this.GetString("Keyword"))
  53. /*if businessCode <= 0 {
  54. br.Msg = "请选择客户"
  55. br.ErrMsg = "客户参数错误"
  56. return
  57. }*/
  58. var startSize int
  59. if pageSize <= 0 {
  60. pageSize = utils.PageSize20
  61. }
  62. if currentIndex <= 0 {
  63. currentIndex = 1
  64. }
  65. startSize = paging.StartIndex(currentIndex, pageSize)
  66. var condition string
  67. var pars []interface{}
  68. if keyword != "" {
  69. condition += ` AND (real_name LIKE '%` + keyword + `%' OR mobile LIKE '%` + keyword + `%' OR email LIKE '%` + keyword + `%') `
  70. }
  71. if businessCode != "" {
  72. condition += ` AND business_code = ? `
  73. pars = append(pars, businessCode)
  74. }
  75. if positionStatus > 0 {
  76. condition += ` AND position_status = ? `
  77. pars = append(pars, positionStatus)
  78. }
  79. if enabled > 0 { // 0-禁用 1-启用
  80. condition += ` AND enabled = ? `
  81. pars = append(pars, enabled)
  82. }
  83. total, err := models.GetUserCountByCondition(condition, pars)
  84. if err != nil {
  85. br.Msg = "获取失败"
  86. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  87. return
  88. }
  89. list, err := models.GetUserPageListByCondition(condition, pars, startSize, pageSize)
  90. if err != nil {
  91. br.Msg = "获取失败"
  92. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  93. return
  94. }
  95. page := paging.GetPaging(currentIndex, pageSize, total)
  96. resp := new(models.UserListResp)
  97. userList := make([]*models.BusinessUser, 0)
  98. if len(list) == 0 {
  99. resp.List = userList
  100. resp.Paging = page
  101. br.Ret = 200
  102. br.Success = true
  103. br.Msg = "获取成功"
  104. br.Data = resp
  105. return
  106. }
  107. // 查找商户信息
  108. businessCodeStr := make([]string, 0)
  109. businessCodeMap := make(map[string]string)
  110. for _, v := range list {
  111. if _, ok := businessCodeMap[v.BusinessCode]; ok {
  112. continue
  113. }
  114. businessCodeStr = append(businessCodeStr, v.BusinessCode)
  115. }
  116. obj := new(eta_business.EtaBusiness)
  117. condition1 := " AND business_code IN (" + utils.GetOrmInReplace(len(businessCodeStr)) + ") "
  118. var pars1 []interface{}
  119. pars1 = append(pars1, businessCodeStr)
  120. businessList, err := obj.GetItemsByCondition(condition1, pars1, []string{"business_code", "business_name"}, "")
  121. if err != nil {
  122. br.Msg = "获取失败"
  123. br.ErrMsg = "获取商户信息失败,Err:" + err.Error()
  124. return
  125. }
  126. for _, v := range businessList {
  127. businessCodeMap[v.BusinessCode] = v.BusinessName
  128. }
  129. for _, v := range list {
  130. tmp := new(models.BusinessUser)
  131. if businessName, ok := businessCodeMap[v.BusinessCode]; ok {
  132. tmp.BusinessName = businessName
  133. }
  134. tmp.BusinessCode = v.BusinessCode
  135. tmp.UserId = v.UserId
  136. tmp.RealName = v.RealName
  137. tmp.Mobile = v.Mobile
  138. tmp.CountryCode = v.CountryCode
  139. tmp.Email = v.Email
  140. tmp.Enabled = v.Enabled
  141. tmp.PositionStatus = v.PositionStatus
  142. tmp.Position = v.Position
  143. tmp.DepartmentName = v.DepartmentName
  144. // 判断time类型是否为零值
  145. if !v.LastLoginTime.IsZero() {
  146. tmp.LastLoginTime = v.LastLoginTime.Format(utils.FormatDateTime)
  147. }
  148. tmp.CreatedTime = v.CreatedTime.Format(utils.FormatDateTime)
  149. tmp.LastUpdatedTime = v.LastUpdatedTime.Format(utils.FormatDateTime)
  150. userList = append(userList, tmp)
  151. }
  152. resp.List = userList
  153. resp.Paging = page
  154. br.Ret = 200
  155. br.Success = true
  156. br.Msg = "获取成功"
  157. br.Data = resp
  158. }
  159. // @Title 新增客户联系人
  160. // @Description 新增客户联系人接口
  161. // @Param request body company.AddUserReq true "type json string"
  162. // @router /user/add [post]
  163. func (this *EtaBusinessUserController) AddUser() {
  164. br := new(models.BaseResponse).Init()
  165. defer func() {
  166. this.Data["json"] = br
  167. this.ServeJSON()
  168. }()
  169. sysUser := this.SysUser
  170. if sysUser == nil {
  171. br.Msg = "请登录"
  172. br.ErrMsg = "请登录,SysUser Is Empty"
  173. br.Ret = 408
  174. return
  175. }
  176. var req models.AddUserReq
  177. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  178. if err != nil {
  179. br.Msg = "参数解析异常!"
  180. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  181. return
  182. }
  183. if req.RealName == "" {
  184. br.Msg = "请填写姓名"
  185. br.IsSendEmail = false
  186. return
  187. }
  188. if req.Mobile == "" {
  189. br.Msg = "请输入手机号"
  190. br.IsSendEmail = false
  191. return
  192. }
  193. if req.BusinessCode == "" {
  194. br.Msg = "请选择客户"
  195. br.IsSendEmail = false
  196. return
  197. }
  198. if req.CountryCode == "" {
  199. req.CountryCode = "86"
  200. }
  201. //空格移除
  202. req.Mobile = utils.TrimStr(req.Mobile)
  203. req.RealName = utils.TrimStr(req.RealName)
  204. //判断该手机号、邮箱是否已经添加,如果已经添加,那么就不再添加
  205. var key string
  206. if req.Mobile != "" {
  207. key = fmt.Sprintf("user:mobile:%s_%s", req.CountryCode, req.Mobile)
  208. }
  209. isHas := utils.Rc.IsExist(key)
  210. if isHas == true {
  211. br.Msg = "重复添加"
  212. return
  213. } else {
  214. //设置3分钟缓存,不允许重复添加
  215. utils.Rc.SetNX(key, 1, time.Second*300)
  216. //添加完成删除对应的缓存
  217. defer utils.Rc.Delete(key)
  218. }
  219. businessObj := new(eta_business.EtaBusiness)
  220. businessInfo, err := businessObj.GetItemByBusinessCode(req.BusinessCode)
  221. if err != nil {
  222. if err.Error() == utils.ErrNoRow() {
  223. br.Msg = "商户不存在"
  224. return
  225. }
  226. br.Msg = "客户信息有误"
  227. br.ErrMsg = "获取客户信息失败, Err: " + err.Error()
  228. return
  229. }
  230. //操作权限校验
  231. ok := eta_business2.CheckBusinessUserButton(sysUser.RoleTypeCode, businessInfo.SellerId, sysUser.AdminId)
  232. if !ok {
  233. br.Msg = "没有操作权限"
  234. br.ErrMsg = "没有操作权限"
  235. return
  236. }
  237. //systemUser models.Company.CompanyUser
  238. //var sysemUser company.CompanyUser
  239. //校验主手机号
  240. mobileItem, err := models.GetUserByMobileCountryCode(req.Mobile, req.CountryCode)
  241. if err != nil && err.Error() != utils.ErrNoRow() {
  242. br.Msg = "校验手机号有误"
  243. br.ErrMsg = "校验手机号有误, Err: " + err.Error()
  244. return
  245. }
  246. if err == nil && mobileItem.UserId > 0 {
  247. br.Msg = "手机号已存在"
  248. br.Success = true
  249. br.Data = mobileItem
  250. br.IsSendEmail = false
  251. br.Ret = 600
  252. return
  253. }
  254. err = nil
  255. user := new(models.User)
  256. user.RealName = utils.TrimStr(req.RealName)
  257. user.CountryCode = utils.TrimStr(req.CountryCode)
  258. user.Mobile = utils.TrimStr(req.Mobile)
  259. user.Position = req.Position
  260. user.PositionStatus = req.PositionStatus
  261. user.Enabled = 1
  262. user.BusinessCode = req.BusinessCode
  263. user.DepartmentName = req.DepartmentName
  264. user.CreatedTime = time.Now()
  265. user.RegisterTime = time.Now()
  266. newId, err := models.AddUser(user)
  267. user.UserId = int(newId)
  268. if err != nil {
  269. br.Msg = "新增失败"
  270. br.ErrMsg = "新增失败,Err:" + err.Error()
  271. return
  272. }
  273. br.Ret = 200
  274. br.Success = true
  275. br.Msg = "新增成功"
  276. br.Data = newId
  277. br.IsAddLog = true
  278. }
  279. // @Title 删除客户联系人
  280. // @Description 删除客户联系人接口
  281. // @Param request body company.DeleteUserReq true "type json string"
  282. // @router /user/delete [post]
  283. func (this *EtaBusinessUserController) DeleteUser() {
  284. br := new(models.BaseResponse).Init()
  285. defer func() {
  286. this.Data["json"] = br
  287. this.ServeJSON()
  288. }()
  289. sysUser := this.SysUser
  290. if sysUser == nil {
  291. br.Msg = "请登录"
  292. br.ErrMsg = "请登录,SysUser Is Empty"
  293. br.Ret = 408
  294. return
  295. }
  296. var req models.DeleteUserReq
  297. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  298. if err != nil {
  299. br.Msg = "参数解析异常!"
  300. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  301. return
  302. }
  303. if req.UserId <= 0 {
  304. br.Msg = "参数错误!"
  305. return
  306. }
  307. //获取联系人详情
  308. userInfo, err := models.GetUserByUserId(req.UserId)
  309. if err != nil {
  310. if err.Error() == utils.ErrNoRow() {
  311. br.Msg = "用户不存在"
  312. return
  313. }
  314. br.Msg = "获取联系人异常!"
  315. br.ErrMsg = "获取联系人异常,Err:" + err.Error()
  316. return
  317. }
  318. // 查询商户关联的销售信息
  319. businessObj := new(eta_business.EtaBusiness)
  320. businessInfo, err := businessObj.GetItemByBusinessCode(userInfo.BusinessCode)
  321. if err != nil {
  322. if err.Error() == utils.ErrNoRow() {
  323. br.Msg = "商户不存在"
  324. return
  325. }
  326. br.Msg = "客户信息有误"
  327. br.ErrMsg = "获取客户信息失败, Err: " + err.Error()
  328. return
  329. }
  330. //操作权限校验
  331. ok := eta_business2.CheckBusinessUserButton(sysUser.RoleTypeCode, businessInfo.SellerId, sysUser.AdminId)
  332. if !ok {
  333. br.Msg = "删除失败,没有权限"
  334. br.ErrMsg = "删除失败,没有权限"
  335. return
  336. }
  337. err = models.DeleteUser(req.UserId)
  338. if err != nil {
  339. br.Msg = "删除失败"
  340. br.ErrMsg = "删除失败,Err:" + err.Error()
  341. return
  342. }
  343. br.Ret = 200
  344. br.Success = true
  345. br.Msg = "删除成功"
  346. br.IsAddLog = true
  347. }
  348. // @Title 编辑客户联系人
  349. // @Description 编辑客户联系人接口
  350. // @Param request body company.EditUserReq true "type json string"
  351. // @router /user/edit [post]
  352. func (this *EtaBusinessUserController) EditUser() {
  353. br := new(models.BaseResponse).Init()
  354. defer func() {
  355. this.Data["json"] = br
  356. this.ServeJSON()
  357. }()
  358. sysUser := this.SysUser
  359. if sysUser == nil {
  360. br.Msg = "请登录"
  361. br.ErrMsg = "请登录,SysUser Is Empty"
  362. br.Ret = 408
  363. return
  364. }
  365. var req models.EditUserReq
  366. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  367. if err != nil {
  368. br.Msg = "参数解析异常!"
  369. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  370. return
  371. }
  372. if req.UserId <= 0 {
  373. br.Msg = "参数错误!"
  374. return
  375. }
  376. //移除空格
  377. req.Mobile = utils.TrimStr(req.Mobile)
  378. req.RealName = utils.TrimStr(req.RealName)
  379. if req.RealName == "" {
  380. br.Msg = "请填写姓名"
  381. br.IsSendEmail = false
  382. return
  383. }
  384. if req.Mobile == "" {
  385. br.Msg = "请输入手机号"
  386. br.IsSendEmail = false
  387. return
  388. }
  389. if req.BusinessCode == "" {
  390. br.Msg = "请选择客户"
  391. br.IsSendEmail = false
  392. return
  393. }
  394. if req.CountryCode == "" {
  395. req.CountryCode = "86"
  396. }
  397. //操作权限校验
  398. userInfo, err := models.GetUserByUserId(req.UserId)
  399. if err != nil {
  400. if err.Error() == utils.ErrNoRow() {
  401. br.Msg = "用户不存在"
  402. return
  403. }
  404. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  405. br.Msg = "获取信息失败"
  406. return
  407. }
  408. businessObj := new(eta_business.EtaBusiness)
  409. businessInfo, err := businessObj.GetItemByBusinessCode(req.BusinessCode)
  410. if err != nil {
  411. if err.Error() == utils.ErrNoRow() {
  412. br.Msg = "商户不存在"
  413. return
  414. }
  415. br.Msg = "客户信息有误"
  416. br.ErrMsg = "获取客户信息失败, Err: " + err.Error()
  417. return
  418. }
  419. //操作权限校验
  420. ok := eta_business2.CheckBusinessUserButton(sysUser.RoleTypeCode, businessInfo.SellerId, sysUser.AdminId)
  421. if !ok {
  422. br.Msg = "没有操作权限"
  423. br.ErrMsg = "没有操作权限"
  424. return
  425. }
  426. if req.Mobile != "" && req.Mobile != userInfo.Mobile && req.CountryCode != userInfo.CountryCode {
  427. mobileItem, e := models.GetUserByMobileCountryCode(req.Mobile, req.CountryCode)
  428. if e != nil && e.Error() != utils.ErrNoRow() {
  429. br.Msg = "校验手机号有误"
  430. br.ErrMsg = "校验手机号有误, Err: " + err.Error()
  431. return
  432. }
  433. if e == nil && mobileItem.UserId > 0 && mobileItem.UserId != req.UserId {
  434. {
  435. br.Msg = "手机号已存在"
  436. br.Success = true
  437. br.Data = mobileItem
  438. br.IsSendEmail = false
  439. br.Ret = 600
  440. return
  441. }
  442. }
  443. }
  444. //待更新字段
  445. updateCol := []string{"RealName", "Mobile", "LastUpdatedTime", "Position", "PositionStatus", "DepartmentName", "BusinessCode", "CountryCode"}
  446. userInfo.RealName = req.RealName
  447. userInfo.Mobile = req.Mobile
  448. userInfo.LastUpdatedTime = time.Now()
  449. userInfo.Position = req.Position
  450. userInfo.DepartmentName = req.DepartmentName
  451. userInfo.BusinessCode = req.BusinessCode
  452. userInfo.CountryCode = req.CountryCode
  453. userInfo.PositionStatus = req.PositionStatus
  454. err = userInfo.Update(updateCol)
  455. if err != nil {
  456. br.Msg = "编辑失败!"
  457. br.ErrMsg = "编辑失败,Err:" + err.Error()
  458. return
  459. }
  460. br.Ret = 200
  461. br.Success = true
  462. br.Msg = "编辑成功"
  463. br.IsAddLog = true
  464. }
  465. // @Title 获取批量导入联系人数据
  466. // @Description 获取批量导入联系人数据
  467. // @Param File query file true "文件"
  468. // @Param CompanyId query file true "客户id"
  469. // @Success 200 {object} models.ImportListResp
  470. // @router /user/import/list [post]
  471. func (this *EtaBusinessUserController) ImportList() {
  472. br := new(models.BaseResponse).Init()
  473. defer func() {
  474. this.Data["json"] = br
  475. this.ServeJSON()
  476. }()
  477. sysUser := this.SysUser
  478. if sysUser == nil {
  479. br.Msg = "请重新登录"
  480. return
  481. }
  482. file, h, err := this.GetFile("File")
  483. if err != nil {
  484. br.Msg = "获取文件失败"
  485. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  486. return
  487. }
  488. //todo 没有选择对应的商户所以无操作权限校验
  489. uploadDir := "static/xls"
  490. err = os.MkdirAll(uploadDir, 766)
  491. if err != nil {
  492. br.Msg = "存储目录创建失败"
  493. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  494. return
  495. }
  496. path := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  497. defer file.Close()
  498. err = this.SaveToFile("File", path)
  499. if err != nil {
  500. br.Msg = "文件保存失败"
  501. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  502. return
  503. }
  504. xlFile, err := xlsx.OpenFile(path)
  505. if err != nil {
  506. br.Msg = "获取失败"
  507. br.ErrMsg = "打开文件失败,Err:" + err.Error() + ";path:" + path
  508. return
  509. }
  510. existUser := make([]*models.User, 0)
  511. addUser := make([]*models.User, 0)
  512. //excel中已经存在的数据,用来判断excel中是否存在相同手机号/邮箱,避免重复提交
  513. excelData := make(map[string]string)
  514. //重复数据
  515. repeatUser := make([]*models.User, 0)
  516. // 遍历sheet页读取
  517. for _, sheet := range xlFile.Sheets {
  518. //遍历行读取
  519. maxRow := sheet.MaxRow
  520. fmt.Println("maxRow:", maxRow)
  521. fmt.Println("maxRow")
  522. for i := 0; i < maxRow; i++ {
  523. if i == 1 {
  524. row := sheet.Row(i)
  525. cells := row.Cells
  526. for k, cell := range cells {
  527. text := cell.String()
  528. if k == 0 {
  529. if text != "姓名" {
  530. br.Msg = "模板格式错误"
  531. br.ErrMsg = "模板格式错误,title:" + text
  532. return
  533. }
  534. }
  535. if k == 1 {
  536. if !strings.Contains(text, "区号") {
  537. br.Msg = "模板格式错误"
  538. br.ErrMsg = "模板格式错误,title:" + text
  539. return
  540. }
  541. }
  542. if k == 2 {
  543. if !strings.Contains(text, "手机号") {
  544. br.Msg = "模板格式错误"
  545. br.ErrMsg = "模板格式错误,title:" + text
  546. return
  547. }
  548. }
  549. if k == 3 {
  550. if !strings.Contains(text, "岗位") {
  551. br.Msg = "模板格式错误"
  552. br.ErrMsg = "模板格式错误,title:" + text
  553. return
  554. }
  555. }
  556. if k == 4 {
  557. if !strings.Contains(text, "部门") {
  558. br.Msg = "模板格式错误"
  559. br.ErrMsg = "模板格式错误,title:" + text
  560. return
  561. }
  562. }
  563. if k == 5 {
  564. if !strings.Contains(text, "客户名称") {
  565. br.Msg = "模板格式错误"
  566. br.ErrMsg = "模板格式错误,title:" + text
  567. return
  568. }
  569. }
  570. if k == 6 {
  571. if !strings.Contains(text, "社会信用码") {
  572. br.Msg = "模板格式错误"
  573. br.ErrMsg = "模板格式错误,title:" + text
  574. return
  575. }
  576. }
  577. if k == 7 {
  578. if !strings.Contains(text, "在职状态") {
  579. br.Msg = "模板格式错误"
  580. br.ErrMsg = "模板格式错误,title:" + text
  581. return
  582. }
  583. }
  584. }
  585. }
  586. if i >= 2 {
  587. row := sheet.Row(i)
  588. cells := row.Cells
  589. var userName, countryCode, mobileOne, position, departmentName, businessName, creditCode, positionStatus string
  590. for k, cell := range cells {
  591. if k == 0 {
  592. userName = cell.String()
  593. }
  594. if k == 1 {
  595. countryCode = cell.String()
  596. }
  597. if k == 2 {
  598. mobileOne = cell.String()
  599. }
  600. if k == 3 {
  601. position = cell.String()
  602. }
  603. if k == 4 {
  604. departmentName = cell.String()
  605. }
  606. if k == 5 {
  607. businessName = cell.String()
  608. }
  609. if k == 6 {
  610. creditCode = cell.String()
  611. }
  612. if k == 7 {
  613. positionStatus = cell.String()
  614. }
  615. }
  616. //移除空格
  617. mobileOne = utils.TrimStr(mobileOne)
  618. //这些字段都没有的话,系统认为excel到底了
  619. if userName == "" {
  620. utils.FileLog.Info("姓名必填")
  621. continue
  622. }
  623. //如果手机号或者邮箱都没有填写的情况下
  624. if mobileOne == "" {
  625. utils.FileLog.Info("手机号必填")
  626. continue
  627. }
  628. if countryCode == "" {
  629. utils.FileLog.Info("导入数据中存在【国际区号】为空的用户,请检查")
  630. continue
  631. }
  632. if businessName == "" && creditCode == "" {
  633. utils.FileLog.Info("导入数据中存在【姓名】和【社会信用码】都为空的用户,请检查")
  634. continue
  635. }
  636. item := new(models.User)
  637. item.CountryCode = countryCode
  638. item.RealName = userName
  639. if positionStatus == "在职" {
  640. item.PositionStatus = 1
  641. } else if positionStatus == "离职" {
  642. item.PositionStatus = 0
  643. }
  644. item.Mobile = mobileOne
  645. item.Position = position
  646. item.DepartmentName = departmentName
  647. // 查询商户信息
  648. if creditCode != "" {
  649. businessObj := new(eta_business.EtaBusiness)
  650. businessInfo := new(eta_business.EtaBusiness)
  651. businessInfo, err = businessObj.GetItemByCreditCode(creditCode)
  652. if err != nil {
  653. if err.Error() == utils.ErrNoRow() {
  654. utils.FileLog.Info("商户不存在")
  655. continue
  656. }
  657. utils.FileLog.Info("获取商户信息失败, Err: " + err.Error())
  658. continue
  659. }
  660. item.BusinessCode = businessInfo.BusinessCode
  661. }
  662. if item.BusinessCode == "" && businessName != "" {
  663. businessObj := new(eta_business.EtaBusiness)
  664. businessInfo := new(eta_business.EtaBusiness)
  665. businessInfo, err = businessObj.GetItemByBusinessName(businessName)
  666. if err != nil {
  667. if err.Error() == utils.ErrNoRow() {
  668. utils.FileLog.Info("商户不存在")
  669. continue
  670. }
  671. utils.FileLog.Info("获取商户信息失败, Err: " + err.Error())
  672. continue
  673. }
  674. item.BusinessCode = businessInfo.BusinessCode
  675. }
  676. if _, ok := excelData[item.CountryCode+"_"+item.Mobile]; ok {
  677. //将用户插入其中,然后退出当前循环,进入下一循环
  678. //repeatUser = append(repeatUser, item)
  679. continue
  680. }
  681. excelData[item.CountryCode+"_"+item.Mobile] = item.CountryCode
  682. //没问题数据
  683. existUser = append(existUser, item)
  684. }
  685. }
  686. }
  687. // 判断系统里该手机号是否已存在
  688. mobiles := make([]string, 0)
  689. for k, _ := range excelData {
  690. mobile := strings.Split(k, "_")[1]
  691. mobiles = append(mobiles, mobile)
  692. }
  693. // 查询系统里是否存在
  694. existUserMap := make(map[string]struct{})
  695. if len(mobiles) > 0 {
  696. existUsers, e := models.GetUserByMobiles(mobiles)
  697. if e != nil {
  698. br.Msg = "查询用户失败"
  699. br.ErrMsg = "查询用户失败,Err:" + e.Error()
  700. return
  701. }
  702. for _, user := range existUsers {
  703. existUserMap[user.CountryCode+"_"+user.Mobile] = struct{}{}
  704. }
  705. }
  706. for _, v := range existUser {
  707. if _, ok := existUserMap[v.CountryCode+"_"+v.Mobile]; ok {
  708. repeatUser = append(repeatUser, v)
  709. } else {
  710. addUser = append(addUser, v)
  711. }
  712. }
  713. //defer func() {
  714. // os.Remove(path)
  715. //}()
  716. br.Msg = "获取成功"
  717. br.Ret = 200
  718. br.Success = true
  719. br.Data = models.ImportListResp{
  720. ValidUser: addUser,
  721. RepeatUser: repeatUser,
  722. }
  723. }
  724. // 需要更新的联系人数据实体
  725. type UpdateWxUser struct {
  726. OldWxUser models.User `description:"旧的联系人数据orm实体(未变更前的)"`
  727. WxUser *models.User `description:"联系人数据orm实体"`
  728. Cols []string `description:"需要修改的字段名切片"`
  729. }
  730. // @Title 批量导入联系人数据
  731. // @Description 批量导入联系人数据
  732. // @Param File query file true "文件"
  733. // @Param CompanyId query file true "客户id"
  734. // @Success 600 {object} []*company.CompanyUser
  735. // @Success 200 Ret=200 导入成功
  736. // @router /user/import [post]
  737. func (this *EtaBusinessUserController) Import() {
  738. br := new(models.BaseResponse).Init()
  739. defer func() {
  740. this.Data["json"] = br
  741. this.ServeJSON()
  742. }()
  743. sysUser := this.SysUser
  744. if sysUser == nil {
  745. br.Msg = "请重新登录"
  746. return
  747. }
  748. file, h, err := this.GetFile("File")
  749. if err != nil {
  750. br.Msg = "获取文件失败"
  751. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  752. return
  753. }
  754. //todo 没有选择对应的商户所以无操作权限校验
  755. uploadDir := "static/xls"
  756. err = os.MkdirAll(uploadDir, 766)
  757. if err != nil {
  758. br.Msg = "存储目录创建失败"
  759. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  760. return
  761. }
  762. path := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  763. defer file.Close()
  764. err = this.SaveToFile("File", path)
  765. if err != nil {
  766. br.Msg = "文件保存失败"
  767. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  768. return
  769. }
  770. xlFile, err := xlsx.OpenFile(path)
  771. if err != nil {
  772. br.Msg = "获取失败"
  773. br.ErrMsg = "打开文件失败,Err:" + err.Error() + ";path:" + path
  774. return
  775. }
  776. existUser := make([]*models.User, 0)
  777. addUser := make([]*models.User, 0)
  778. //excel中已经存在的数据,用来判断excel中是否存在相同手机号/邮箱,避免重复提交
  779. excelData := make(map[string]string)
  780. // 遍历sheet页读取
  781. for _, sheet := range xlFile.Sheets {
  782. //遍历行读取
  783. maxRow := sheet.MaxRow
  784. fmt.Println("maxRow:", maxRow)
  785. fmt.Println("maxRow")
  786. for i := 0; i < maxRow; i++ {
  787. if i == 1 {
  788. row := sheet.Row(i)
  789. cells := row.Cells
  790. for k, cell := range cells {
  791. text := cell.String()
  792. if k == 0 {
  793. if text != "姓名" {
  794. br.Msg = "模板格式错误"
  795. br.ErrMsg = "模板格式错误,title:" + text
  796. return
  797. }
  798. }
  799. if k == 1 {
  800. if !strings.Contains(text, "区号") {
  801. br.Msg = "模板格式错误"
  802. br.ErrMsg = "模板格式错误,title:" + text
  803. return
  804. }
  805. }
  806. if k == 2 {
  807. if !strings.Contains(text, "手机号") {
  808. br.Msg = "模板格式错误"
  809. br.ErrMsg = "模板格式错误,title:" + text
  810. return
  811. }
  812. }
  813. if k == 3 {
  814. if !strings.Contains(text, "岗位") {
  815. br.Msg = "模板格式错误"
  816. br.ErrMsg = "模板格式错误,title:" + text
  817. return
  818. }
  819. }
  820. if k == 4 {
  821. if !strings.Contains(text, "部门") {
  822. br.Msg = "模板格式错误"
  823. br.ErrMsg = "模板格式错误,title:" + text
  824. return
  825. }
  826. }
  827. if k == 5 {
  828. if !strings.Contains(text, "客户名称") {
  829. br.Msg = "模板格式错误"
  830. br.ErrMsg = "模板格式错误,title:" + text
  831. return
  832. }
  833. }
  834. if k == 6 {
  835. if !strings.Contains(text, "社会信用码") {
  836. br.Msg = "模板格式错误"
  837. br.ErrMsg = "模板格式错误,title:" + text
  838. return
  839. }
  840. }
  841. if k == 7 {
  842. if !strings.Contains(text, "在职状态") {
  843. br.Msg = "模板格式错误"
  844. br.ErrMsg = "模板格式错误,title:" + text
  845. return
  846. }
  847. }
  848. }
  849. }
  850. if i >= 2 {
  851. row := sheet.Row(i)
  852. cells := row.Cells
  853. var userName, countryCode, mobileOne, position, departmentName, businessName, creditCode, positionStatus string
  854. for k, cell := range cells {
  855. if k == 0 {
  856. userName = cell.String()
  857. }
  858. if k == 1 {
  859. countryCode = cell.String()
  860. }
  861. if k == 2 {
  862. mobileOne = cell.String()
  863. }
  864. if k == 3 {
  865. position = cell.String()
  866. }
  867. if k == 4 {
  868. departmentName = cell.String()
  869. }
  870. if k == 5 {
  871. businessName = cell.String()
  872. }
  873. if k == 6 {
  874. creditCode = cell.String()
  875. }
  876. if k == 7 {
  877. positionStatus = cell.String()
  878. }
  879. }
  880. //移除空格
  881. mobileOne = utils.TrimStr(mobileOne)
  882. //这些字段都没有的话,系统认为excel到底了
  883. if userName == "" {
  884. utils.FileLog.Info("姓名必填")
  885. continue
  886. }
  887. //如果手机号或者邮箱都没有填写的情况下
  888. if mobileOne == "" {
  889. utils.FileLog.Info("手机号必填")
  890. continue
  891. }
  892. if countryCode == "" {
  893. utils.FileLog.Info("导入数据中存在【国际区号】为空的用户,请检查")
  894. continue
  895. }
  896. if businessName == "" && creditCode == "" {
  897. utils.FileLog.Info("导入数据中存在【姓名】和【社会信用码】都为空的用户,请检查")
  898. continue
  899. }
  900. item := new(models.User)
  901. item.CountryCode = countryCode
  902. item.RealName = userName
  903. if positionStatus == "在职" {
  904. item.PositionStatus = 1
  905. } else if positionStatus == "离职" {
  906. item.PositionStatus = 0
  907. }
  908. item.Mobile = mobileOne
  909. item.Position = position
  910. item.DepartmentName = departmentName
  911. // 查询商户信息
  912. if creditCode != "" {
  913. businessObj := new(eta_business.EtaBusiness)
  914. businessInfo := new(eta_business.EtaBusiness)
  915. businessInfo, err = businessObj.GetItemByCreditCode(creditCode)
  916. if err != nil {
  917. if err.Error() == utils.ErrNoRow() {
  918. utils.FileLog.Info("商户不存在")
  919. continue
  920. }
  921. utils.FileLog.Info("获取商户信息失败, Err: " + err.Error())
  922. continue
  923. }
  924. item.BusinessCode = businessInfo.BusinessCode
  925. }
  926. if item.BusinessCode == "" && businessName != "" {
  927. businessObj := new(eta_business.EtaBusiness)
  928. businessInfo := new(eta_business.EtaBusiness)
  929. businessInfo, err = businessObj.GetItemByBusinessName(businessName)
  930. if err != nil {
  931. if err.Error() == utils.ErrNoRow() {
  932. utils.FileLog.Info("商户不存在")
  933. continue
  934. }
  935. utils.FileLog.Info("获取商户信息失败, Err: " + err.Error())
  936. continue
  937. }
  938. item.BusinessCode = businessInfo.BusinessCode
  939. }
  940. if _, ok := excelData[item.CountryCode+"_"+item.Mobile]; ok {
  941. //将用户插入其中,然后退出当前循环,进入下一循环
  942. //repeatUser = append(repeatUser, item)
  943. continue
  944. }
  945. excelData[item.CountryCode+"_"+item.Mobile] = item.CountryCode
  946. //没问题数据
  947. existUser = append(existUser, item)
  948. }
  949. }
  950. }
  951. // 判断系统里该手机号是否已存在
  952. mobiles := make([]string, 0)
  953. for k, _ := range excelData {
  954. mobile := strings.Split(k, "_")[1]
  955. mobiles = append(mobiles, mobile)
  956. }
  957. // 查询系统里是否存在
  958. existUserMap := make(map[string]struct{})
  959. if len(mobiles) > 0 {
  960. existUsers, e := models.GetUserByMobiles(mobiles)
  961. if e != nil {
  962. br.Msg = "查询用户失败"
  963. br.ErrMsg = "查询用户失败,Err:" + e.Error()
  964. return
  965. }
  966. for _, user := range existUsers {
  967. existUserMap[user.CountryCode+"_"+user.Mobile] = struct{}{}
  968. }
  969. }
  970. for _, v := range existUser {
  971. if _, ok := existUserMap[v.CountryCode+"_"+v.Mobile]; !ok {
  972. addUser = append(addUser, v)
  973. }
  974. }
  975. fmt.Println("表格中用户与系统中存在用户没有冲突")
  976. //如果表格中用户与系统中存在用户 没有冲突,那么继续执行
  977. for _, user := range addUser {
  978. if user.RealName != "" && user.Mobile != "" {
  979. userData := new(models.User)
  980. userData.RealName = user.RealName
  981. userData.Mobile = user.Mobile
  982. userData.CountryCode = user.CountryCode
  983. userData.Email = user.Email
  984. userData.Position = user.Position
  985. userData.PositionStatus = user.PositionStatus
  986. userData.BusinessCode = user.BusinessCode
  987. userData.DepartmentName = user.DepartmentName
  988. userData.CountryCode = user.CountryCode
  989. //判断该手机号、邮箱是否已经添加,如果已经添加,那么就不再添加
  990. var key string
  991. key = "user:mobile:" + userData.Mobile
  992. isHas := utils.Rc.IsExist(key)
  993. if isHas == false {
  994. newId, err := models.AddUser(userData)
  995. userData.UserId = int(newId)
  996. if err != nil {
  997. br.Msg = "导入失败"
  998. br.ErrMsg = "导入失败,Err:" + err.Error()
  999. return
  1000. }
  1001. }
  1002. }
  1003. }
  1004. defer func() {
  1005. os.Remove(path)
  1006. }()
  1007. br.Msg = "导入成功"
  1008. br.Ret = 200
  1009. br.Success = true
  1010. }
  1011. // @Title 移动用户至新的商户下
  1012. // @Description 移动用户至新的商户下
  1013. // @Param request body company.EditUserReq true "type json string"
  1014. // @router /user/changeBusiness [post]
  1015. func (this *EtaBusinessUserController) ChangeUserBusiness() {
  1016. br := new(models.BaseResponse).Init()
  1017. defer func() {
  1018. this.Data["json"] = br
  1019. this.ServeJSON()
  1020. }()
  1021. sysUser := this.SysUser
  1022. if sysUser == nil {
  1023. br.Msg = "请登录"
  1024. br.ErrMsg = "请登录,SysUser Is Empty"
  1025. br.Ret = 408
  1026. return
  1027. }
  1028. var req models.ChangeUserBusinessReq
  1029. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1030. if err != nil {
  1031. br.Msg = "参数解析异常!"
  1032. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1033. return
  1034. }
  1035. if req.UserId <= 0 {
  1036. br.Msg = "参数错误!"
  1037. return
  1038. }
  1039. if req.BusinessCode == "" {
  1040. br.Msg = "请选择商户"
  1041. br.IsSendEmail = false
  1042. return
  1043. }
  1044. //操作权限校验
  1045. userInfo, err := models.GetUserByUserId(req.UserId)
  1046. if err != nil {
  1047. if err.Error() == utils.ErrNoRow() {
  1048. br.Msg = "用户不存在"
  1049. return
  1050. }
  1051. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  1052. br.Msg = "获取信息失败"
  1053. return
  1054. }
  1055. if userInfo.BusinessCode == req.BusinessCode {
  1056. br.Msg = "用户已经在该商户下"
  1057. return
  1058. }
  1059. businessObj := new(eta_business.EtaBusiness)
  1060. businessInfo, err := businessObj.GetItemByBusinessCode(req.BusinessCode)
  1061. if err != nil {
  1062. if err.Error() == utils.ErrNoRow() {
  1063. br.Msg = "商户不存在"
  1064. return
  1065. }
  1066. br.Msg = "商户信息有误"
  1067. br.ErrMsg = "获取商户信息失败, Err: " + err.Error()
  1068. return
  1069. }
  1070. //操作权限校验
  1071. ok := eta_business2.CheckBusinessUserButton(sysUser.RoleTypeCode, businessInfo.SellerId, sysUser.AdminId)
  1072. if !ok {
  1073. br.Msg = "没有操作权限"
  1074. br.ErrMsg = "没有操作权限"
  1075. return
  1076. }
  1077. //待更新字段
  1078. updateCol := []string{"LastUpdatedTime", "BusinessCode"}
  1079. userInfo.LastUpdatedTime = time.Now()
  1080. userInfo.BusinessCode = req.BusinessCode
  1081. err = userInfo.Update(updateCol)
  1082. if err != nil {
  1083. br.Msg = "操作失败!"
  1084. br.ErrMsg = "操作失败,Err:" + err.Error()
  1085. return
  1086. }
  1087. br.Ret = 200
  1088. br.Success = true
  1089. br.Msg = "操作成功"
  1090. br.IsAddLog = true
  1091. }