user.go 32 KB

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