user.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm/models"
  5. "eta/eta_mini_crm/models/request"
  6. "eta/eta_mini_crm/models/response"
  7. "eta/eta_mini_crm/services"
  8. "eta/eta_mini_crm/utils"
  9. "math"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. "github.com/beego/beego/v2/client/orm"
  15. "github.com/rdlucklib/rdluck_tools/paging"
  16. "github.com/tealeg/xlsx"
  17. )
  18. type UserController struct {
  19. BaseAuthController
  20. }
  21. // Add
  22. // @Title 添加新客户
  23. // @Description 添加新客户
  24. // @Param request body request.UserAddReq true "type json string"
  25. // @Success 200 {object} request.UserAddReq
  26. // @router /add [post]
  27. func (this *UserController) Add() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. var req request.UserAddReq
  34. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  35. if err != nil {
  36. br.Msg = "参数解析异常!"
  37. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  38. return
  39. }
  40. if req.RealName == "" {
  41. br.Msg = "请输入姓名"
  42. return
  43. }
  44. req.Phone = strings.TrimSpace(req.Phone)
  45. req.Email = strings.TrimSpace(req.Email)
  46. if req.Phone == "" && req.Email == "" {
  47. br.Msg = "至少输入一个手机号或邮箱"
  48. return
  49. }
  50. var userByPhone, userByEmail *models.User
  51. if req.Phone != "" {
  52. if req.AreaCode == "86" {
  53. if !utils.ValidateMobileFormatat(req.Phone) {
  54. br.Msg = "手机号格式有误, 请检查"
  55. return
  56. }
  57. }
  58. userByPhone, err = models.GetUserByPhone(req.Phone, req.AreaCode)
  59. if err != nil && err != orm.ErrNoRows {
  60. br.Msg = "添加用户失败"
  61. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  62. return
  63. }
  64. if userByPhone != nil {
  65. switch userByPhone.Status {
  66. case 0:
  67. userByPhone.Status = 2
  68. case 1:
  69. userByPhone.Status = 2
  70. default:
  71. br.Msg = "手机号已存在,请重新输入"
  72. return
  73. }
  74. }
  75. }
  76. if req.Email != "" {
  77. if !utils.ValidateEmailFormatat(req.Email) {
  78. br.Msg = "邮箱格式有误, 请检查"
  79. return
  80. }
  81. userByEmail, err = models.GetUserByEmail(req.Email)
  82. if err != nil && err != orm.ErrNoRows {
  83. br.Msg = "添加用户失败"
  84. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  85. return
  86. }
  87. if userByEmail != nil {
  88. switch userByEmail.Status {
  89. case 0:
  90. userByEmail.Status = 2
  91. case 1:
  92. userByEmail.Status = 2
  93. default:
  94. br.Msg = "邮箱已存在,请重新输入"
  95. return
  96. }
  97. }
  98. }
  99. if req.SellerDepartmentId <= 0 {
  100. br.Msg = "请选择营业部"
  101. return
  102. }
  103. if req.ValidStartTime == "" || req.ValidEndTime == "" {
  104. br.Msg = "请选择合理的有效期范围"
  105. return
  106. }
  107. validStartTime, err := time.Parse(utils.FormatDate, req.ValidStartTime)
  108. if err != nil {
  109. br.Msg = "错误的日期格式"
  110. return
  111. }
  112. validEndTime, err := time.Parse(utils.FormatDate, req.ValidEndTime)
  113. if err != nil {
  114. br.Msg = "错误的日期格式"
  115. return
  116. }
  117. if !validStartTime.Before(validEndTime) {
  118. br.Msg = "请选择合理的有效期范围"
  119. return
  120. }
  121. if req.Company == "" {
  122. br.Msg = "请输入所属公司"
  123. return
  124. }
  125. if userByPhone != nil && userByEmail != nil && userByPhone.UserId != userByEmail.UserId {
  126. br.Msg = "邮箱已存在,请重新输入"
  127. return
  128. }
  129. user := &models.User{}
  130. curTime := time.Now()
  131. if userByPhone == nil && userByEmail == nil {
  132. user.CreateTime = curTime
  133. user.ModifyTime = curTime
  134. }
  135. if userByEmail != nil {
  136. user = userByEmail
  137. }
  138. if userByPhone != nil {
  139. user = userByPhone
  140. }
  141. validStartTime = validStartTime.Local().Add(-time.Hour * 8)
  142. validEndTime = validEndTime.Local().Add(-time.Hour*8 + time.Hour*24 - time.Second)
  143. if curTime.Before(validEndTime) {
  144. user.Status = 2
  145. } else {
  146. br.Msg = "有效期截至时间必须大于当前时间"
  147. return
  148. }
  149. sysDepartment, err := models.GetSysDepartmentById(req.SellerDepartmentId)
  150. if err != nil {
  151. br.Msg = "添加用户失败"
  152. br.ErrMsg = "查询营业部失败,系统错误,Err:" + err.Error()
  153. return
  154. }
  155. user.RealName = req.RealName
  156. user.AreaCode = req.AreaCode
  157. user.Phone = req.Phone
  158. user.Email = req.Email
  159. user.SellerDepartmentId = req.SellerDepartmentId
  160. user.SellerDepartmentName = sysDepartment.SysDepartmentName
  161. user.ValidStartTime = validStartTime
  162. user.ValidEndTime = validEndTime
  163. user.Company = req.Company
  164. user.ModifyTime = curTime
  165. err = models.SaveUser(user, req.ChartPermission)
  166. if err != nil {
  167. br.Msg = "添加客户失败"
  168. br.ErrMsg = "添加客户失败,系统错误,Err:" + err.Error()
  169. return
  170. }
  171. if user.Status == 2 {
  172. userRecord := &models.UserChangeRecord{}
  173. userRecord.UserId = user.UserId
  174. userRecord.SysUserId = this.SysUser.SysUserId
  175. userRecord.Content = this.SysUser.SysRealName + "新增用户"
  176. userRecord.Insert()
  177. }
  178. br.Msg = "添加成功"
  179. br.Success = true
  180. br.Ret = 200
  181. }
  182. // edit
  183. // @Title 编辑客户
  184. // @Description 编辑客户
  185. // @Param request body request.UserEidtReq true "type json string"
  186. // @Success 200 {object} request.UserAddReq
  187. // @router /edit [post]
  188. func (this *UserController) Edit() {
  189. br := new(models.BaseResponse).Init()
  190. defer func() {
  191. this.Data["json"] = br
  192. this.ServeJSON()
  193. }()
  194. var req request.UserEditReq
  195. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  196. if err != nil {
  197. br.Msg = "参数解析异常!"
  198. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  199. return
  200. }
  201. if req.RealName == "" {
  202. br.Msg = "请输入姓名"
  203. return
  204. }
  205. req.Phone = strings.TrimSpace(req.Phone)
  206. req.Email = strings.TrimSpace(req.Email)
  207. if req.Phone == "" && req.Email == "" {
  208. br.Msg = "至少输入一个手机号或邮箱"
  209. return
  210. }
  211. if req.Phone != "" {
  212. if req.AreaCode == "86" {
  213. if !utils.ValidateMobileFormatat(req.Phone) {
  214. br.Msg = "手机号格式有误, 请检查"
  215. return
  216. }
  217. }
  218. user, err := models.GetUserByPhone(req.Phone, req.AreaCode)
  219. if err != nil && err != orm.ErrNoRows {
  220. br.Msg = "编辑用户失败"
  221. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  222. return
  223. }
  224. if user != nil && user.UserId != req.UserId {
  225. br.Msg = "手机号已存在,请重新输入"
  226. return
  227. }
  228. }
  229. if req.Email != "" {
  230. if !utils.ValidateEmailFormatat(req.Email) {
  231. br.Msg = "邮箱格式有误, 请检查"
  232. return
  233. }
  234. user, err := models.GetUserByEmail(req.Email)
  235. if err != nil && err != orm.ErrNoRows {
  236. br.Msg = "编辑用户失败"
  237. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  238. return
  239. }
  240. if user != nil && user.UserId != req.UserId {
  241. br.Msg = "邮箱已存在,请重新输入"
  242. return
  243. }
  244. }
  245. if req.SellerDepartmentId <= 0 {
  246. br.Msg = "请选择营业部"
  247. return
  248. }
  249. if req.ValidStartTime == "" || req.ValidEndTime == "" {
  250. br.Msg = "请选择合理的有效期范围"
  251. return
  252. }
  253. validStartTime, err := time.Parse(utils.FormatDate, req.ValidStartTime)
  254. if err != nil {
  255. br.Msg = "错误的日期格式"
  256. return
  257. }
  258. validStartTime = validStartTime.In(time.Local).Add(-time.Hour * 8)
  259. validEndTime, err := time.Parse(utils.FormatDate, req.ValidEndTime)
  260. if err != nil {
  261. br.Msg = "错误的日期格式"
  262. return
  263. }
  264. validEndTime = validEndTime.In(time.Local).Add(-time.Hour*8 + time.Hour*24 - time.Second)
  265. if !validStartTime.Before(validEndTime) {
  266. br.Msg = "请选择合理的有效期范围"
  267. return
  268. }
  269. if req.Company == "" {
  270. br.Msg = "请输入所属公司"
  271. return
  272. }
  273. if req.IsEnabled && time.Now().After(validEndTime) {
  274. br.Msg = "启用后,有效期必须大于当前时间"
  275. return
  276. }
  277. user, err := models.GetUserById(req.UserId)
  278. if err != nil {
  279. if err.Error() == utils.ErrNoRow() {
  280. br.Msg = "用户不存在或已删除,请重新刷新页面"
  281. br.ErrMsg = "用户不存在或已删除,请重新刷新页面,Err:" + err.Error()
  282. return
  283. }
  284. br.Msg = "编辑用户失败"
  285. br.ErrMsg = "编辑用户失败, 系统错误,Err:" + err.Error()
  286. return
  287. }
  288. sysDepartment, err := models.GetSysDepartmentById(req.SellerDepartmentId)
  289. if err != nil {
  290. br.Msg = "添加用户失败"
  291. br.ErrMsg = "查询营业部失败,系统错误,Err:" + err.Error()
  292. return
  293. }
  294. user.RealName = req.RealName
  295. user.AreaCode = req.AreaCode
  296. user.Phone = req.Phone
  297. user.Email = req.Email
  298. user.SellerDepartmentId = req.SellerDepartmentId
  299. user.SellerDepartmentName = sysDepartment.SysDepartmentName
  300. user.ValidStartTime = validStartTime
  301. user.ValidEndTime = validEndTime
  302. user.Company = req.Company
  303. user.ModifyTime = time.Now()
  304. // 当用户状态初始为潜在客户时,才更新创建时间
  305. if user.Status == utils.UserStatusPotential {
  306. user.CreateTime = time.Now()
  307. }
  308. oldStatus := user.Status
  309. if !req.IsEnabled {
  310. user.Status = 0
  311. } else {
  312. user.Status = 2
  313. }
  314. err = models.SaveUser(user, req.ChartPermission)
  315. if err != nil {
  316. br.Msg = "添加客户失败"
  317. br.ErrMsg = "添加客户失败,系统错误,Err:" + err.Error()
  318. return
  319. }
  320. record := &models.UserChangeRecord{}
  321. record.UserId = req.UserId
  322. record.SysUserId = this.SysUser.SysUserId
  323. if oldStatus == utils.UserStatusPotential && user.Status == utils.UserStatusFormal {
  324. record.Content = this.SysUser.SysRealName + "新增用户"
  325. } else {
  326. record.Content = this.SysUser.SysRealName + "编辑用户"
  327. }
  328. record.Insert()
  329. br.Msg = "编辑成功"
  330. br.Success = true
  331. br.Ret = 200
  332. }
  333. // Check
  334. // @Title 编辑客户
  335. // @Description 编辑客户
  336. // @Param request body request.UserEidtReq true "type json string"
  337. // @Success 200 {object} request.UserAddReq
  338. // @router /check [post]
  339. func (this *UserController) Check() {
  340. br := new(models.BaseResponse).Init()
  341. defer func() {
  342. this.Data["json"] = br
  343. this.ServeJSON()
  344. }()
  345. var req request.UserCheckReq
  346. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  347. if err != nil {
  348. br.Msg = "参数解析异常!"
  349. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  350. return
  351. }
  352. req.Phone = strings.TrimSpace(req.Phone)
  353. req.Email = strings.TrimSpace(req.Email)
  354. if req.Phone == "" && req.Email == "" {
  355. br.Msg = "至少输入一个手机号或邮箱"
  356. return
  357. }
  358. userCheckResp := new(response.UserCheckResp)
  359. if req.Phone != "" {
  360. if req.AreaCode == "86" {
  361. if !utils.ValidateMobileFormatat(req.Phone) {
  362. br.Msg = "手机号格式有误, 请检查"
  363. return
  364. }
  365. }
  366. user, err := models.GetUserByPhone(req.Phone, req.AreaCode)
  367. if err != nil && err != orm.ErrNoRows {
  368. br.Msg = "编辑用户失败"
  369. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  370. return
  371. }
  372. if user != nil {
  373. userCheckResp.UserId = user.UserId
  374. userCheckResp.Status = user.Status
  375. if user.Status == utils.UserStatusNo {
  376. br.Ret = 200
  377. br.Success = true
  378. br.Msg = "该用户已被禁用,确认启用并更新用户信息吗?"
  379. br.Data = userCheckResp
  380. return
  381. }
  382. if user.Status == utils.UserStatusPotential {
  383. br.Ret = 200
  384. br.Success = true
  385. br.Msg = "该用户已在潜在列表,确认转客户并更新用户信息吗?"
  386. br.Data = userCheckResp
  387. return
  388. }
  389. if user.Status == utils.UserStatusFormal {
  390. br.Msg = "手机号已存在,请重新输入"
  391. return
  392. }
  393. }
  394. }
  395. if req.Email != "" {
  396. if !utils.ValidateEmailFormatat(req.Email) {
  397. br.Msg = "邮箱格式有误, 请检查"
  398. return
  399. }
  400. user, err := models.GetUserByEmail(req.Email)
  401. if err != nil && err != orm.ErrNoRows {
  402. br.Msg = "编辑用户失败"
  403. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  404. return
  405. }
  406. if user != nil {
  407. userCheckResp.UserId = user.UserId
  408. if user.Status == 0 {
  409. br.Ret = 200
  410. br.Success = true
  411. br.Msg = "该用户已被禁用,确认启用并更新用户信息吗?"
  412. br.Data = userCheckResp
  413. return
  414. }
  415. if user.Status == 1 {
  416. br.Ret = 200
  417. br.Success = true
  418. br.Msg = "该用户已被禁用,确认启用并更新用户信息吗?"
  419. br.Data = userCheckResp
  420. return
  421. }
  422. if user.Status == 2 {
  423. br.Msg = "邮箱已存在,请重新输入"
  424. return
  425. }
  426. return
  427. }
  428. }
  429. br.Ret = 200
  430. br.Success = true
  431. br.Msg = "邮箱或手机号合格"
  432. }
  433. // List
  434. // @Title 用户列表
  435. // @Description 用户列表
  436. // @Param PageSize query int true "每页数据条数"
  437. // @Param CurrentIndex query int true "当前页页码,从1开始"
  438. // @Param SellerDepartmentId query string true "营业部门id"
  439. // @Param Status query int true "用户状态"
  440. // @Param KeyWord query string true "手机号/邮箱/姓名"
  441. // @Param IsRegistered query string true "是否注册"
  442. // @Param IsSubscribed query string true "是否关注"
  443. // @Param RegisterStartDate query string true "注册开始时间"
  444. // @Param RegisterEndDate query string true "注册结束时间"
  445. // @Param CreateStartDate query string true "创建开始时间"
  446. // @Param CreateEndDate query string true "创建结束时间"
  447. // @Param SortParam query string true "排序字段"
  448. // @Param SortType query string true "排序方式"
  449. // @Success 200 {object} response.UserListResp
  450. // @router /list [get]
  451. func (this *UserController) List() {
  452. br := new(models.BaseResponse).Init()
  453. defer func() {
  454. this.Data["json"] = br
  455. this.ServeJSON()
  456. }()
  457. pageSize, _ := this.GetInt("PageSize")
  458. currentIndex, _ := this.GetInt("CurrentIndex")
  459. sellerDepartmentId, _ := this.GetInt("SellerDepartmentId", 0)
  460. status := this.GetString("Status")
  461. keyWord := this.GetString("KeyWord")
  462. IsRegistered := this.GetString("IsRegistered")
  463. IsSubscribed := this.GetString("IsSubscribed")
  464. registerStartDate := this.GetString("RegisterStartDate")
  465. registerEndDate := this.GetString("RegisterEndDate")
  466. createStartDate := this.GetString("CreateStartDate")
  467. createEndDate := this.GetString("CreateEndDate")
  468. sortParma := this.GetString("SortParam")
  469. sortType := this.GetString("SortType")
  470. var condition string
  471. var sortCondition string
  472. var pars []interface{}
  473. if keyWord != "" {
  474. condition += ` AND (u.real_name LIKE ? OR u.phone LIKE ? OR u.email LIKE ?) `
  475. pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
  476. }
  477. if pageSize <= 0 {
  478. pageSize = utils.PageSize20
  479. } else if pageSize > utils.PageSize100 {
  480. pageSize = utils.PageSize100
  481. }
  482. if currentIndex <= 0 {
  483. currentIndex = 1
  484. }
  485. if sortParma != "" && sortType != "" {
  486. sortCondition = " ORDER BY "
  487. var param, sort string
  488. switch sortParma {
  489. case "RegisterTime":
  490. param = "u.register_time"
  491. case "CreateTime":
  492. param = "u.create_time"
  493. case "RestDate":
  494. param = "u.valid_end_time"
  495. }
  496. switch sortType {
  497. case "asc":
  498. sort = " ASC "
  499. case "desc":
  500. sort = " DESC "
  501. }
  502. if param != "" && sort != "" {
  503. sortCondition += param + " " + sort
  504. } else {
  505. sortCondition = ""
  506. }
  507. }
  508. if sortCondition == "" {
  509. sortCondition = " ORDER BY u.create_time DESC"
  510. }
  511. if sellerDepartmentId > 0 {
  512. condition += ` AND u.seller_department_id = ? `
  513. pars = append(pars, sellerDepartmentId)
  514. }
  515. switch status {
  516. case "禁用":
  517. condition += " AND u.status=? "
  518. pars = append(pars, 0)
  519. case "潜在":
  520. condition += " AND u.status=? "
  521. pars = append(pars, 1)
  522. case "正式":
  523. condition += " AND u.status=? "
  524. pars = append(pars, 2)
  525. case "":
  526. condition += " AND (u.status=? OR u.status=?) "
  527. pars = append(pars, 0, 2)
  528. }
  529. switch IsRegistered {
  530. case "是":
  531. condition += " AND u.is_registered=? "
  532. pars = append(pars, true)
  533. case "否":
  534. condition += " AND u.is_registered=? "
  535. pars = append(pars, false)
  536. }
  537. switch IsSubscribed {
  538. case "是":
  539. condition += " AND u.is_subscribed=? "
  540. pars = append(pars, true)
  541. case "否":
  542. condition += " AND u.is_subscribed=? "
  543. pars = append(pars, false)
  544. }
  545. if registerStartDate != "" {
  546. registerStartTime, er := time.Parse(utils.FormatDate, registerStartDate)
  547. if er != nil {
  548. br.Msg = "日期格式有误"
  549. return
  550. }
  551. condition += " AND u.register_time>=? "
  552. registerStartDateStr := registerStartTime.Format(utils.FormatDateTime)
  553. pars = append(pars, registerStartDateStr)
  554. }
  555. if registerEndDate != "" {
  556. registerEndTime, er := time.Parse(utils.FormatDate, registerEndDate)
  557. if er != nil {
  558. br.Msg = "日期格式有误"
  559. return
  560. }
  561. condition += " AND u.register_time<=? "
  562. // 结束时间包含今天
  563. registerEndTime = registerEndTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
  564. registerEndDateStr := registerEndTime.Format(utils.FormatDateTime)
  565. pars = append(pars, registerEndDateStr)
  566. }
  567. if createStartDate != "" {
  568. createStartTime, er := time.Parse(utils.FormatDate, createStartDate)
  569. if er != nil {
  570. br.Msg = "日期格式有误"
  571. return
  572. }
  573. condition += " AND u.create_time>=? "
  574. createStartDateStr := createStartTime.Format(utils.FormatDateTime)
  575. pars = append(pars, createStartDateStr)
  576. }
  577. if createEndDate != "" {
  578. createEndTime, er := time.Parse(utils.FormatDate, createEndDate)
  579. if er != nil {
  580. br.Msg = "日期格式有误"
  581. return
  582. }
  583. condition += " AND u.create_time<=? "
  584. // 结束时间包含今天
  585. createEndTime = createEndTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
  586. createEndDateStr := createEndTime.Format(utils.FormatDateTime)
  587. pars = append(pars, createEndDateStr)
  588. }
  589. if pageSize <= 0 {
  590. pageSize = utils.PageSize20
  591. } else if pageSize > utils.PageSize100 {
  592. pageSize = utils.PageSize100
  593. }
  594. if currentIndex <= 0 {
  595. currentIndex = 1
  596. }
  597. startSize := utils.StartIndex(currentIndex, pageSize)
  598. total, err := models.GetUserCount(condition, pars)
  599. if err != nil {
  600. br.Msg = "获取失败"
  601. br.ErrMsg = "获取失败,Err:" + err.Error()
  602. return
  603. }
  604. userList, err := models.GetUserListByConditonSort(condition, sortCondition, pars, startSize, pageSize)
  605. if err != nil {
  606. br.Msg = "查询用户失败"
  607. br.Msg = "查询用户失败,系统错误,Err:" + err.Error()
  608. return
  609. }
  610. for _, u := range userList {
  611. if u.ValidEndTime == "" {
  612. u.RestDate = 0
  613. } else {
  614. endTime, err := time.Parse(utils.FormatDateTime, u.ValidEndTime)
  615. if err != nil {
  616. br.Msg = "用户有效时间格式有误"
  617. br.ErrMsg = "用户有效时间格式有误,Err:" + err.Error()
  618. return
  619. }
  620. // 对天数向上取整
  621. daysBetween := int(math.Ceil(time.Until(endTime).Hours() / 24))
  622. if daysBetween < 0 {
  623. daysBetween = 0
  624. }
  625. u.RestDate = daysBetween
  626. }
  627. }
  628. page := paging.GetPaging(currentIndex, pageSize, total)
  629. resp := new(response.UserListResp)
  630. resp.Paging = page
  631. resp.List = userList
  632. br.Data = resp
  633. br.Ret = 200
  634. br.Success = true
  635. br.Msg = "获取成功"
  636. }
  637. // PotentialList
  638. // @Title 潜在用户列表
  639. // @Description 潜在用户列表
  640. // @Param PageSize query int true "每页数据条数"
  641. // @Param CurrentIndex query int true "当前页页码,从1开始"
  642. // @Param KeyWord query string true "手机号/邮箱"
  643. // @Param SellerDepartmentId query int true "营业部id"
  644. // @Param ApplyStatus query int true "申请状态"
  645. // @Param RegisterStartDate query string true "注册开始时间"
  646. // @Param RegisterEndDate query string true "注册结束时间"
  647. // @Param LastUpdateStartDate query string true "最后一次阅读开始时间"
  648. // @Param LastUpdateEndDate query string true "最后一次阅读结束时间"
  649. // @Param ApplyStartDate query string true "申请开始时间"
  650. // @Param ApplyEndDate query string true "申请结束时间"
  651. // @Param SortParam query string true "排序字段"
  652. // @Param SortType query string true "排序方式"
  653. // @Success 200 {object} response.UserListResp
  654. // @router /potential/list [get]
  655. func (this *UserController) PotentialList() {
  656. br := new(models.BaseResponse).Init()
  657. defer func() {
  658. this.Data["json"] = br
  659. this.ServeJSON()
  660. }()
  661. pageSize, _ := this.GetInt("PageSize")
  662. currentIndex, _ := this.GetInt("CurrentIndex")
  663. keyWord := this.GetString("KeyWord")
  664. sellerDepartmentId, _ := this.GetInt("SellerDepartmentId")
  665. applyStatus := this.GetString("ApplyStatus")
  666. applyStartDate := this.GetString("ApplyStartDate")
  667. applyEndDate := this.GetString("ApplyEndDate")
  668. registerStartDate := this.GetString("RegisterStartDate")
  669. registerEndDate := this.GetString("RegisterEndDate")
  670. lastUpdateStartDate := this.GetString("LastUpdateStartDate")
  671. lastUpdateEndDate := this.GetString("LastUpdateEndDate")
  672. sortParam := this.GetString("SortParam")
  673. sortType := this.GetString("SortType")
  674. userList, total, errMsg, msg, err := services.GetPotentialList(keyWord, sellerDepartmentId, applyStatus, pageSize, currentIndex, sortParam, sortType, registerStartDate, registerEndDate, lastUpdateStartDate, lastUpdateEndDate, applyStartDate, applyEndDate)
  675. if err != nil {
  676. if msg != "" {
  677. br.Msg = msg
  678. } else {
  679. br.Msg = "获取失败"
  680. }
  681. if errMsg != "" {
  682. br.ErrMsg = errMsg + ", Err:" + err.Error()
  683. } else {
  684. br.ErrMsg = "获取失败, Err:" + err.Error()
  685. }
  686. }
  687. resp := new(response.UserListResp)
  688. page := paging.GetPaging(currentIndex, pageSize, total)
  689. resp.Paging = page
  690. resp.List = userList
  691. br.Data = resp
  692. br.Ret = 200
  693. br.Success = true
  694. br.Msg = "获取成功"
  695. }
  696. // PotentialEdit
  697. // @Title 编辑潜在客户(转客户)
  698. // @Description 编辑潜在客户(转客户)
  699. // @Param request body request.UserEidtReq true "type json string"
  700. // @Success 200 {object} request.UserAddReq
  701. // @router /potential/edit [post]
  702. func (this *UserController) PotentialEdit() {
  703. br := new(models.BaseResponse).Init()
  704. defer func() {
  705. this.Data["json"] = br
  706. this.ServeJSON()
  707. }()
  708. var req request.UserEditReq
  709. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  710. if err != nil {
  711. br.Msg = "参数解析异常!"
  712. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  713. return
  714. }
  715. if req.RealName == "" {
  716. br.Msg = "请输入姓名"
  717. return
  718. }
  719. req.Phone = strings.TrimSpace(req.Phone)
  720. req.Email = strings.TrimSpace(req.Email)
  721. if req.Phone == "" && req.Email == "" {
  722. br.Msg = "至少输入一个手机号或邮箱"
  723. return
  724. }
  725. if req.Phone != "" {
  726. if req.AreaCode == "86" {
  727. if !utils.ValidateMobileFormatat(req.Phone) {
  728. br.Msg = "手机号格式有误, 请检查"
  729. return
  730. }
  731. }
  732. user, err := models.GetUserByPhone(req.Phone, req.AreaCode)
  733. if err != nil && err != orm.ErrNoRows {
  734. br.Msg = "编辑用户失败"
  735. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  736. return
  737. }
  738. if user != nil && user.UserId != req.UserId {
  739. br.Msg = "手机号已存在,请重新输入"
  740. return
  741. }
  742. }
  743. if req.Email != "" {
  744. if !utils.ValidateEmailFormatat(req.Email) {
  745. br.Msg = "邮箱格式有误, 请检查"
  746. return
  747. }
  748. user, err := models.GetUserByEmail(req.Email)
  749. if err != nil && err != orm.ErrNoRows {
  750. br.Msg = "编辑用户失败"
  751. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  752. return
  753. }
  754. if user != nil && user.UserId != req.UserId {
  755. br.Msg = "邮箱已存在,请重新输入"
  756. return
  757. }
  758. }
  759. if req.SellerDepartmentId <= 0 {
  760. br.Msg = "请选择营业部"
  761. return
  762. }
  763. if req.ValidStartTime == "" || req.ValidEndTime == "" {
  764. br.Msg = "请选择合理的有效期范围"
  765. return
  766. }
  767. validStartTime, err := time.Parse(utils.FormatDate, req.ValidStartTime)
  768. if err != nil {
  769. br.Msg = "错误的日期格式"
  770. return
  771. }
  772. validStartTime = validStartTime.In(time.Local).Add(-time.Hour * 8)
  773. validEndTime, err := time.Parse(utils.FormatDate, req.ValidEndTime)
  774. if err != nil {
  775. br.Msg = "错误的日期格式"
  776. return
  777. }
  778. validEndTime = validEndTime.In(time.Local).Add(-time.Hour*8 + time.Hour*24 - time.Second)
  779. if !validStartTime.Before(validEndTime) {
  780. br.Msg = "请选择合理的有效期范围"
  781. return
  782. }
  783. if req.Company == "" {
  784. br.Msg = "请输入所属公司"
  785. return
  786. }
  787. if req.IsEnabled && time.Now().After(validEndTime) {
  788. br.Msg = "启用后,有效期必须大于当前时间"
  789. return
  790. }
  791. user, err := models.GetUserById(req.UserId)
  792. if err != nil {
  793. if err.Error() == utils.ErrNoRow() {
  794. br.Msg = "用户不存在或已删除,请重新刷新页面"
  795. br.ErrMsg = "用户不存在或已删除,请重新刷新页面,Err:" + err.Error()
  796. return
  797. }
  798. br.Msg = "编辑用户失败"
  799. br.ErrMsg = "编辑用户失败, 系统错误,Err:" + err.Error()
  800. return
  801. }
  802. department, err := models.GetSysDepartmentById(req.SellerDepartmentId)
  803. if err != nil {
  804. br.Msg = "编辑用户失败"
  805. br.ErrMsg = "编辑用户失败, 系统错误,Err:" + err.Error()
  806. return
  807. }
  808. user.RealName = req.RealName
  809. user.AreaCode = req.AreaCode
  810. user.Phone = req.Phone
  811. user.Email = req.Email
  812. user.SellerDepartmentId = req.SellerDepartmentId
  813. user.SellerDepartmentName = department.SysDepartmentName
  814. user.ValidStartTime = validStartTime
  815. user.ValidEndTime = validEndTime
  816. user.Company = req.Company
  817. user.ModifyTime = time.Now()
  818. // 当用户状态初始为潜在客户时,才更新创建时间
  819. if user.Status == utils.UserStatusPotential {
  820. user.CreateTime = time.Now()
  821. }
  822. oldStatus := user.Status
  823. if !req.IsEnabled {
  824. user.Status = 0
  825. } else {
  826. user.Status = 2
  827. }
  828. err = models.SaveUser(user, req.ChartPermission)
  829. if err != nil {
  830. br.Msg = "添加客户失败"
  831. br.ErrMsg = "添加客户失败,系统错误,Err:" + err.Error()
  832. return
  833. }
  834. record := &models.UserChangeRecord{}
  835. record.UserId = req.UserId
  836. record.SysUserId = this.SysUser.SysUserId
  837. if oldStatus == utils.UserStatusPotential && user.Status == utils.UserStatusFormal {
  838. record.Content = this.SysUser.SysRealName + "新增用户"
  839. } else {
  840. record.Content = this.SysUser.SysRealName + "编辑用户"
  841. }
  842. record.Insert()
  843. br.Msg = "编辑成功"
  844. br.Success = true
  845. br.Ret = 200
  846. }
  847. // ExportPotentialExcel
  848. // @Title 导出潜在用户Excel
  849. // @Description 导出潜在用户Excel
  850. // @Param KeyWord query string true "手机号/邮箱"
  851. // @Param SellerDepartmentId query int true "营业部id"
  852. // @Param ApplyStatus query int true "申请状态"
  853. // @Param RegisterStartDate query string true "注册开始时间"
  854. // @Param RegisterEndDate query string true "注册结束时间"
  855. // @Param LastUpdateStartDate query string true "最后一次阅读开始时间"
  856. // @Param LastUpdateEndDate query string true "最后一次阅读结束时间"
  857. // @Param ApplyStartDate query string true "申请开始时间"
  858. // @Param ApplyEndDate query string true "申请结束时间"
  859. // @Success 200 {object} request.UserAddReq
  860. // @router /potential/export/excel [get]
  861. func (this *UserController) ExportPotentialExcel() {
  862. br := new(models.BaseResponse).Init()
  863. defer func() {
  864. this.Data["json"] = br
  865. this.ServeJSON()
  866. }()
  867. keyword := this.GetString("KeyWord")
  868. sellerDepartmentId, _ := this.GetInt("SellerDepartmentId")
  869. applyStatus := this.GetString("ApplyStatus")
  870. registerStartDate := this.GetString("RegisterStartDate")
  871. registerEndDate := this.GetString("RegisterEndDate")
  872. lastUpdateStartDate := this.GetString("LastUpdateStartDate")
  873. lastUpdateEndDate := this.GetString("LastUpdateEndDate")
  874. applyStartDate := this.GetString("ApplyStartDate")
  875. applyEndDate := this.GetString("ApplyEndDate")
  876. total, err := models.GetPotentialUserTotal()
  877. if err != nil {
  878. br.Msg = "获取数据失败"
  879. br.ErrMsg = "获取数据失败,系统错误,Err:" + err.Error()
  880. return
  881. }
  882. userList, _, errMsg, msg, err := services.GetPotentialList(keyword, sellerDepartmentId, applyStatus, total, 0, "", "", registerStartDate, registerEndDate, lastUpdateStartDate, lastUpdateEndDate, applyStartDate, applyEndDate)
  883. if err != nil {
  884. if msg != "" {
  885. br.Msg = msg
  886. } else {
  887. br.Msg = "获取失败"
  888. }
  889. if errMsg != "" {
  890. br.ErrMsg = errMsg + ", Err:" + err.Error()
  891. } else {
  892. br.ErrMsg = "获取失败, Err:" + err.Error()
  893. }
  894. }
  895. dir, _ := os.Executable()
  896. exPath := filepath.Dir(dir)
  897. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  898. xlsxFile := xlsx.NewFile()
  899. sheetNew := new(xlsx.Sheet)
  900. sheetNew, err = xlsxFile.AddSheet("潜在客户列表")
  901. if err != nil {
  902. br.Msg = "创建sheet失败"
  903. br.ErrMsg = "创建sheet失败,系统错误,Err:" + err.Error()
  904. return
  905. }
  906. titleRow := sheetNew.AddRow()
  907. titleRow.AddCell().SetString("手机号")
  908. titleRow.AddCell().SetString("姓名")
  909. titleRow.AddCell().SetString("营业部")
  910. titleRow.AddCell().SetString("邮箱")
  911. titleRow.AddCell().SetString("注册时间")
  912. titleRow.AddCell().SetString("申请时间")
  913. titleRow.AddCell().SetString("最近一次阅读")
  914. titleRow.AddCell().SetString("累计阅读次数")
  915. titleRow.AddCell().SetString("申请状态")
  916. for _, user := range userList {
  917. row := sheetNew.AddRow()
  918. row.AddCell().SetString(user.Phone)
  919. row.AddCell().SetString(user.RealName)
  920. row.AddCell().SetString(user.SellerDepartmentName)
  921. row.AddCell().SetString(user.Email)
  922. row.AddCell().SetString(user.RegisterTime)
  923. row.AddCell().SetString(user.ApplyTime)
  924. row.AddCell().SetString(user.LastUpdateTime)
  925. row.AddCell().SetInt(user.ReadCnt)
  926. switch user.ApplyStatus {
  927. case 0:
  928. row.AddCell().SetString("未申请")
  929. case 1:
  930. row.AddCell().SetString("已申请")
  931. }
  932. }
  933. err = xlsxFile.Save(downLoadnFilePath)
  934. if err != nil {
  935. sheet, err := xlsxFile.AddSheet("无数据")
  936. if err != nil {
  937. br.Msg = "创建sheet失败"
  938. br.ErrMsg = "创建sheet失败,系统错误,Err:" + err.Error()
  939. return
  940. }
  941. rowSecName := sheet.AddRow()
  942. celSecName := rowSecName.AddCell()
  943. celSecName.SetString("无数据")
  944. err = xlsxFile.Save(downLoadnFilePath)
  945. if err != nil {
  946. br.Msg = "保存文件失败"
  947. br.ErrMsg = "保存文件失败,系统错误,Err:" + err.Error()
  948. return
  949. }
  950. }
  951. fileName := "潜在客户列表" + time.Now().Format("06.1.2")
  952. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  953. defer os.Remove(downLoadnFilePath)
  954. br.Ret = 200
  955. br.Success = true
  956. br.Msg = "success"
  957. }
  958. // Detail
  959. // @Title 用户详情信息
  960. // @Description 用户详情信息
  961. // @Param UserId query int true "用户id"
  962. // @Success 200 {object} models.LoginResp
  963. // @router /detail [get]
  964. func (this *UserController) Detail() {
  965. br := new(models.BaseResponse).Init()
  966. defer func() {
  967. this.Data["json"] = br
  968. this.ServeJSON()
  969. }()
  970. UserId, _ := this.GetInt("UserId")
  971. if UserId <= 0 {
  972. br.Msg = "查询用户不存在"
  973. return
  974. }
  975. user, err := models.GetUserViewById(UserId)
  976. if err != nil {
  977. if err.Error() == utils.ErrNoRow() {
  978. br.Msg = "用户不存在或已删除,请刷新页面"
  979. return
  980. }
  981. br.Msg = "查询用户失败"
  982. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  983. return
  984. }
  985. if user == nil {
  986. br.Msg = "用户不存在或已删除,请刷新页面"
  987. return
  988. }
  989. permissionList, err := services.GetUserPermissionById(UserId)
  990. if err != nil {
  991. br.Msg = "用户权限获取失败,请重新尝试"
  992. return
  993. }
  994. permissionMap := make(map[string][]string)
  995. for _, pm := range permissionList {
  996. permissionMap[pm.ParentName] = append(permissionMap[pm.ParentName], pm.PermissionName)
  997. }
  998. resp := new(response.UserDetailResp)
  999. resp.Detail = user
  1000. resp.Permission = permissionMap
  1001. br.Msg = "查询成功"
  1002. br.Ret = 200
  1003. br.Success = true
  1004. br.Data = resp
  1005. }
  1006. // Delete
  1007. // @Title 系统用户详情信息
  1008. // @Description 用户详情信息
  1009. // @Param request body request.UserDeleteReq true "type json string"
  1010. // @Success 200 {object} models.LoginResp
  1011. // @router /delete [post]
  1012. func (this *UserController) Delete() {
  1013. br := new(models.BaseResponse).Init()
  1014. defer func() {
  1015. this.Data["json"] = br
  1016. this.ServeJSON()
  1017. }()
  1018. var req request.UserDeleteReq
  1019. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1020. if err != nil {
  1021. br.Msg = "参数解析异常!"
  1022. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1023. return
  1024. }
  1025. err = models.DeleteUserById(req.UserId)
  1026. if err != nil {
  1027. br.Msg = "删除失败"
  1028. br.ErrMsg = "删除失败,系统错误,Err:" + err.Error()
  1029. return
  1030. }
  1031. br.Msg = "删除成功"
  1032. br.Success = true
  1033. br.Ret = 200
  1034. }
  1035. // @Title 开启/禁用用户
  1036. // @Description 开启/禁用用户接口
  1037. // @Param request body system.SysuserEditReq true "type json string"
  1038. // @Success 200 操作成功
  1039. // @router /editEnabled [post]
  1040. func (this *UserController) EditEnabled() {
  1041. br := new(models.BaseResponse).Init()
  1042. defer func() {
  1043. this.Data["json"] = br
  1044. this.ServeJSON()
  1045. }()
  1046. var req request.UserEditEnabledReq
  1047. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1048. if err != nil {
  1049. br.Msg = "参数解析异常!"
  1050. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1051. return
  1052. }
  1053. user, err := models.GetUserById(req.UserId)
  1054. if err != nil {
  1055. br.Msg = "获取数据失败"
  1056. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  1057. return
  1058. }
  1059. if !req.IsEnabled {
  1060. user.Status = 0
  1061. err = user.Update([]string{"status"})
  1062. if err != nil {
  1063. br.Msg = "修改失败"
  1064. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  1065. return
  1066. }
  1067. } else {
  1068. validStartTime, err := time.Parse(utils.FormatDate, req.ValidStartTime)
  1069. if err != nil {
  1070. br.Msg = "日期格式不正确"
  1071. return
  1072. }
  1073. validEndTime, err := time.Parse(utils.FormatDate, req.ValidEndTime)
  1074. if err != nil {
  1075. br.Msg = "日期格式不正确"
  1076. return
  1077. }
  1078. if !validEndTime.After(validStartTime) {
  1079. br.Msg = "有效期结束日期在开始日期之前,日期格式不正确"
  1080. return
  1081. }
  1082. validStartTime = validStartTime.Local().Add(-time.Hour * 8)
  1083. validEndTime = validEndTime.Local().Add(-time.Hour*8 + time.Hour*24 - time.Second)
  1084. curTime := time.Now()
  1085. if curTime.After(validEndTime) {
  1086. br.Msg = "有效期已到期,请重新设置"
  1087. return
  1088. }
  1089. user.Status = 2
  1090. user.ModifyTime = curTime
  1091. user.ValidStartTime = validStartTime
  1092. user.ValidEndTime = validEndTime
  1093. err = user.Update([]string{"status", "modify_time", "valid_start_time", "valid_end_time"})
  1094. if err != nil {
  1095. br.Msg = "修改失败"
  1096. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  1097. return
  1098. }
  1099. }
  1100. // 记录操作
  1101. record := &models.UserChangeRecord{}
  1102. record.UserId = req.UserId
  1103. record.SysUserId = this.SysUser.SysUserId
  1104. if user.Status == 0 {
  1105. record.Content = this.SysUser.SysRealName + "禁用用户"
  1106. } else {
  1107. record.Content = this.SysUser.SysRealName + "启用用户"
  1108. }
  1109. record.Insert()
  1110. br.Ret = 200
  1111. br.Success = true
  1112. br.Msg = "操作成功"
  1113. }
  1114. // @Title 用户信息变更记录
  1115. // @Description 用户信息变更记录
  1116. // @Param UserId query int true "用户id"
  1117. // @Success 200 操作成功
  1118. // @router /change_list [get]
  1119. func (this *UserController) ChangeList() {
  1120. br := new(models.BaseResponse).Init()
  1121. defer func() {
  1122. this.Data["json"] = br
  1123. this.ServeJSON()
  1124. }()
  1125. userId, _ := this.GetInt("UserId")
  1126. if userId <= 0 {
  1127. br.Msg = "用户信息错误"
  1128. return
  1129. }
  1130. userRcord, err := models.GetUserChangeRecordListById(userId)
  1131. if err != nil {
  1132. br.Msg = "用户信息变更查询失败"
  1133. br.ErrMsg = "用户信息变更查询失败,系统错误,Err:" + err.Error()
  1134. return
  1135. }
  1136. resp := new(response.UserChangeRecordResp)
  1137. resp.List = userRcord
  1138. br.Data = resp
  1139. br.Msg = "查询成功"
  1140. br.Success = true
  1141. br.Ret = 200
  1142. }
  1143. // GlobalSearch
  1144. // @Title 全局用户列表
  1145. // @Description 全局用户列表
  1146. // @Param PageSize query int true "每页数据条数"
  1147. // @Param CurrentIndex query int true "当前页页码,从1开始"
  1148. // @Param KeyWord query string true "手机号/邮箱/姓名"
  1149. // @Param SortParam query string true "排序字段"
  1150. // @Param SortType query string true "排序方式"
  1151. // @Success 200 {object} response.UserListResp
  1152. // @router /global/list [get]
  1153. func (this *UserController) GlobalSearch() {
  1154. br := new(models.BaseResponse).Init()
  1155. defer func() {
  1156. this.Data["json"] = br
  1157. this.ServeJSON()
  1158. }()
  1159. pageSize, _ := this.GetInt("PageSize")
  1160. currentIndex, _ := this.GetInt("CurrentIndex")
  1161. keyWord := this.GetString("KeyWord")
  1162. sortParam := this.GetString("SortParam")
  1163. sortType := this.GetString("SortType")
  1164. if pageSize <= 0 {
  1165. pageSize = utils.PageSize20
  1166. }
  1167. if currentIndex <= 0 {
  1168. currentIndex = 1
  1169. }
  1170. var condition string
  1171. var pars []interface{}
  1172. var sortCondition string
  1173. if keyWord == "" {
  1174. br.Ret = 200
  1175. br.Msg = "查询成功"
  1176. br.Success = true
  1177. return
  1178. } else {
  1179. condition = ` AND (real_name like ? OR phone like ? OR email like ?)`
  1180. pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
  1181. }
  1182. if sortParam != "" && sortType != "" {
  1183. var param, sort string
  1184. switch sortParam {
  1185. case "RegisterTime":
  1186. param = "register_time"
  1187. case "ReadCnt":
  1188. param = "read_cnt"
  1189. case "LastUpdateTime":
  1190. param = "last_update_time"
  1191. }
  1192. switch sortType {
  1193. case "asc":
  1194. sort = "ASC"
  1195. case "desc":
  1196. sort = "DESC"
  1197. }
  1198. if param != "" && sort != "" {
  1199. sortCondition = ` ORDER BY ` + param + ` ` + sort
  1200. }
  1201. }
  1202. if sortCondition == "" {
  1203. sortCondition = ` ORDER BY u.register_time DESC`
  1204. }
  1205. startSize := utils.StartIndex(currentIndex, pageSize)
  1206. userIds, err := models.GetUserIdListByCondition(condition, pars)
  1207. if err != nil {
  1208. br.Msg = "查询失败"
  1209. br.ErrMsg = "查询失败,系统错误,Err:" + err.Error()
  1210. return
  1211. }
  1212. userList, err := models.GetGlobalUserByCondition(userIds, sortCondition, startSize, pageSize)
  1213. if err != nil {
  1214. br.Msg = "查询失败"
  1215. br.ErrMsg = "查询失败,系统错误,Err:" + err.Error()
  1216. return
  1217. }
  1218. page := paging.GetPaging(currentIndex, pageSize, len(userIds))
  1219. resp := new(response.UserListResp)
  1220. resp.List = userList
  1221. resp.Paging = page
  1222. br.Data = resp
  1223. br.Msg = "查询成功"
  1224. br.Success = true
  1225. br.Ret = 200
  1226. }