user.go 37 KB

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