user.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. pars = append(pars, registerStartTime)
  547. }
  548. if registerEndDate != "" {
  549. registerEndTime, er := time.Parse(utils.FormatDate, registerEndDate)
  550. if er != nil {
  551. br.Msg = "日期格式有误"
  552. return
  553. }
  554. condition += " AND u.register_time<? "
  555. pars = append(pars, registerEndTime)
  556. }
  557. if createStartDate != "" {
  558. createStartTime, er := time.Parse(utils.FormatDate, createStartDate)
  559. if er != nil {
  560. br.Msg = "日期格式有误"
  561. return
  562. }
  563. condition += " AND u.create_time>? "
  564. pars = append(pars, createStartTime)
  565. }
  566. if createEndDate != "" {
  567. createEndTime, er := time.Parse(utils.FormatDate, createEndDate)
  568. if er != nil {
  569. br.Msg = "日期格式有误"
  570. return
  571. }
  572. condition += " AND u.create_time<? "
  573. pars = append(pars, createEndTime)
  574. }
  575. if pageSize <= 0 {
  576. pageSize = utils.PageSize20
  577. } else if pageSize > utils.PageSize100 {
  578. pageSize = utils.PageSize100
  579. }
  580. if currentIndex <= 0 {
  581. currentIndex = 1
  582. }
  583. startSize := utils.StartIndex(currentIndex, pageSize)
  584. total, err := models.GetUserCount(condition, pars)
  585. if err != nil {
  586. br.Msg = "获取失败"
  587. br.ErrMsg = "获取失败,Err:" + err.Error()
  588. return
  589. }
  590. userList, err := models.GetUserListByConditonSort(condition, sortCondition, pars, startSize, pageSize)
  591. if err != nil {
  592. br.Msg = "查询用户失败"
  593. br.Msg = "查询用户失败,系统错误,Err:" + err.Error()
  594. return
  595. }
  596. for _, u := range userList {
  597. if u.ValidEndTime == "" {
  598. u.RestDate = 0
  599. } else {
  600. endTime, err := time.Parse(utils.FormatDateTime, u.ValidEndTime)
  601. if err != nil {
  602. br.Msg = "用户有效时间格式有误"
  603. br.ErrMsg = "用户有效时间格式有误,Err:" + err.Error()
  604. return
  605. }
  606. // 对天数向上取整
  607. daysBetween := int(math.Ceil(time.Until(endTime).Hours() / 24))
  608. if daysBetween < 0 {
  609. daysBetween = 0
  610. }
  611. u.RestDate = daysBetween
  612. }
  613. }
  614. page := paging.GetPaging(currentIndex, pageSize, total)
  615. resp := new(response.UserListResp)
  616. resp.Paging = page
  617. resp.List = userList
  618. br.Data = resp
  619. br.Ret = 200
  620. br.Success = true
  621. br.Msg = "获取成功"
  622. }
  623. // PotentialList
  624. // @Title 潜在用户列表
  625. // @Description 潜在用户列表
  626. // @Param PageSize query int true "每页数据条数"
  627. // @Param CurrentIndex query int true "当前页页码,从1开始"
  628. // @Param KeyWord query string true "手机号/邮箱"
  629. // @Param RegisterStartDate query string true "注册开始时间"
  630. // @Param RegisterEndDate query string true "注册结束时间"
  631. // @Param LastUpdateStartDate query string true "最后一次阅读开始时间"
  632. // @Param LastUpdateEndDate query string true "最后一次阅读结束时间"
  633. // @Param SortParam query string true "排序字段"
  634. // @Param SortType query string true "排序方式"
  635. // @Success 200 {object} response.UserListResp
  636. // @router /potential/list [get]
  637. func (this *UserController) PotentialList() {
  638. br := new(models.BaseResponse).Init()
  639. defer func() {
  640. this.Data["json"] = br
  641. this.ServeJSON()
  642. }()
  643. pageSize, _ := this.GetInt("PageSize")
  644. currentIndex, _ := this.GetInt("CurrentIndex")
  645. keyWord := this.GetString("KeyWord")
  646. registerStartDate := this.GetString("RegisterStartDate")
  647. registerEndDate := this.GetString("RegisterEndDate")
  648. lastUpdateStartDate := this.GetString("LastUpdateStartDate")
  649. lastUpdateEndDate := this.GetString("LastUpdateEndDate")
  650. sortParma := this.GetString("SortParam")
  651. sortType := this.GetString("SortType")
  652. var sortCondition string
  653. var condition string
  654. var pars []interface{}
  655. if keyWord != "" {
  656. condition += ` AND ( u.phone LIKE ? OR u.email LIKE ?) `
  657. pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  658. }
  659. if pageSize <= 0 {
  660. pageSize = utils.PageSize20
  661. } else if pageSize > utils.PageSize100 {
  662. pageSize = utils.PageSize100
  663. }
  664. if currentIndex <= 0 {
  665. currentIndex = 1
  666. }
  667. if sortParma != "" && sortType != "" {
  668. sortCondition = " ORDER BY "
  669. var param, sort string
  670. switch sortParma {
  671. case "RegisterTime":
  672. param = "u.register_time"
  673. case "LastUpdateTime":
  674. param = "last_update_time"
  675. case "ReadCnt":
  676. param = "read_cnt"
  677. }
  678. switch sortType {
  679. case "asc":
  680. sort = " ASC "
  681. case "desc":
  682. sort = " DESC "
  683. }
  684. if param != "" && sort != "" {
  685. sortCondition += param + " " + sort
  686. } else {
  687. sortCondition = ""
  688. }
  689. }
  690. if registerStartDate != "" {
  691. condition += " AND u.register_time>? "
  692. pars = append(pars, registerStartDate)
  693. }
  694. if registerEndDate != "" {
  695. condition += " AND u.register_time<? "
  696. pars = append(pars, registerEndDate)
  697. }
  698. if lastUpdateStartDate != "" {
  699. condition += " AND ur.create_time>? "
  700. pars = append(pars, lastUpdateStartDate)
  701. }
  702. if lastUpdateEndDate != "" {
  703. condition += " AND ur.create_time<? "
  704. pars = append(pars, lastUpdateEndDate)
  705. }
  706. if pageSize <= 0 {
  707. pageSize = utils.PageSize20
  708. } else if pageSize > utils.PageSize100 {
  709. pageSize = utils.PageSize100
  710. }
  711. if currentIndex <= 0 {
  712. currentIndex = 1
  713. }
  714. startSize := utils.StartIndex(currentIndex, pageSize)
  715. total, err := models.GetPotentialUserCountByConditonV2(condition, pars)
  716. if err != nil {
  717. br.Msg = "获取失败"
  718. br.ErrMsg = "获取失败,Err:" + err.Error()
  719. return
  720. }
  721. resp := new(response.UserListResp)
  722. if total == 0 {
  723. page := paging.GetPaging(currentIndex, pageSize, total)
  724. resp.Paging = page
  725. br.Msg = "获取成功"
  726. br.Ret = 200
  727. br.Success = true
  728. br.Data = resp
  729. return
  730. }
  731. userList, err := models.GetPotentialUserIdsByConditonV2(condition, pars, sortCondition, startSize, pageSize)
  732. if err != nil {
  733. br.Msg = "查询用户失败"
  734. br.Msg = "查询用户失败,系统错误,Err:" + err.Error()
  735. return
  736. }
  737. page := paging.GetPaging(currentIndex, pageSize, total)
  738. resp.Paging = page
  739. resp.List = userList
  740. br.Data = resp
  741. br.Ret = 200
  742. br.Success = true
  743. br.Msg = "获取成功"
  744. }
  745. // PotentialEdit
  746. // @Title 编辑潜在客户(转客户)
  747. // @Description 编辑潜在客户(转客户)
  748. // @Param request body request.UserEidtReq true "type json string"
  749. // @Success 200 {object} request.UserAddReq
  750. // @router /potential/edit [post]
  751. func (this *UserController) PotentialEdit() {
  752. br := new(models.BaseResponse).Init()
  753. defer func() {
  754. this.Data["json"] = br
  755. this.ServeJSON()
  756. }()
  757. var req request.UserEidtReq
  758. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  759. if err != nil {
  760. br.Msg = "参数解析异常!"
  761. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  762. return
  763. }
  764. if req.RealName == "" {
  765. br.Msg = "请输入姓名"
  766. return
  767. }
  768. req.Phone = strings.TrimSpace(req.Phone)
  769. req.Email = strings.TrimSpace(req.Email)
  770. if req.Phone == "" && req.Email == "" {
  771. br.Msg = "至少输入一个手机号或邮箱"
  772. return
  773. }
  774. if req.Phone != "" {
  775. if req.AreaCode == "86" {
  776. if !utils.ValidateMobileFormatat(req.Phone) {
  777. br.Msg = "手机号格式有误, 请检查"
  778. return
  779. }
  780. }
  781. user, err := models.GetUserByPhone(req.Phone, req.AreaCode)
  782. if err != nil && err != orm.ErrNoRows {
  783. br.Msg = "编辑用户失败"
  784. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  785. return
  786. }
  787. if user != nil && user.UserId != req.UserId {
  788. br.Msg = "手机号已存在,请重新输入"
  789. return
  790. }
  791. }
  792. if req.Email != "" {
  793. if !utils.ValidateEmailFormatat(req.Email) {
  794. br.Msg = "邮箱格式有误, 请检查"
  795. return
  796. }
  797. user, err := models.GetUserByEmail(req.Email)
  798. if err != nil && err != orm.ErrNoRows {
  799. br.Msg = "编辑用户失败"
  800. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  801. return
  802. }
  803. if user != nil && user.UserId != req.UserId {
  804. br.Msg = "邮箱已存在,请重新输入"
  805. return
  806. }
  807. }
  808. if req.SellerId <= 0 {
  809. br.Msg = "请选择营业部/销售"
  810. return
  811. }
  812. if req.ValidStartTime == "" || req.ValidEndTime == "" {
  813. br.Msg = "请选择合理的有效期范围"
  814. return
  815. }
  816. validStartTime, err := time.Parse(utils.FormatDate, req.ValidStartTime)
  817. if err != nil {
  818. br.Msg = "错误的日期格式"
  819. return
  820. }
  821. validStartTime = validStartTime.In(time.Local).Add(-time.Hour * 8)
  822. validEndTime, err := time.Parse(utils.FormatDate, req.ValidEndTime)
  823. if err != nil {
  824. br.Msg = "错误的日期格式"
  825. return
  826. }
  827. validEndTime = validEndTime.In(time.Local).Add(-time.Hour*8 + time.Hour*24 - time.Second)
  828. if !validStartTime.Before(validEndTime) {
  829. br.Msg = "请选择合理的有效期范围"
  830. return
  831. }
  832. if req.Company == "" {
  833. br.Msg = "请输入所属公司"
  834. return
  835. }
  836. if req.IsEnabled && time.Now().After(validEndTime) {
  837. br.Msg = "启用后,有效期必须大于当前时间"
  838. return
  839. }
  840. user, err := models.GetUserById(req.UserId)
  841. if err != nil {
  842. if err == orm.ErrNoRows {
  843. br.Msg = "用户不存在或已删除,请重新刷新页面"
  844. br.ErrMsg = "用户不存在或已删除,请重新刷新页面,Err:" + err.Error()
  845. return
  846. }
  847. br.Msg = "编辑用户失败"
  848. br.ErrMsg = "编辑用户失败, 系统错误,Err:" + err.Error()
  849. return
  850. }
  851. user.RealName = req.RealName
  852. user.AreaCode = req.AreaCode
  853. user.Phone = req.Phone
  854. user.Email = req.Email
  855. user.SellerId = req.SellerId
  856. user.ValidStartTime = validStartTime
  857. user.ValidEndTime = validEndTime
  858. user.Company = req.Company
  859. user.ModifyTime = time.Now()
  860. // 当用户状态初始为潜在客户时,才更新创建时间
  861. if user.Status == utils.UserStatusPotential {
  862. user.CreateTime = time.Now()
  863. }
  864. oldStatus := user.Status
  865. if !req.IsEnabled {
  866. user.Status = 0
  867. } else {
  868. user.Status = 2
  869. }
  870. err = models.SaveUser(user, req.ChartPermission)
  871. if err != nil {
  872. br.Msg = "添加客户失败"
  873. br.ErrMsg = "添加客户失败,系统错误,Err:" + err.Error()
  874. return
  875. }
  876. record := &models.UserChangeRecord{}
  877. record.UserId = req.UserId
  878. record.SysUserId = this.SysUser.SysUserId
  879. if oldStatus == utils.UserStatusPotential && user.Status == utils.UserStatusFormal {
  880. record.Content = this.SysUser.SysRealName + "新增用户"
  881. } else {
  882. record.Content = this.SysUser.SysRealName + "编辑用户"
  883. }
  884. record.Insert()
  885. br.Msg = "编辑成功"
  886. br.Success = true
  887. br.Ret = 200
  888. }
  889. // Detail
  890. // @Title 用户详情信息
  891. // @Description 用户详情信息
  892. // @Param UserId query int true "用户id"
  893. // @Success 200 {object} models.LoginResp
  894. // @router /detail [get]
  895. func (this *UserController) Detail() {
  896. br := new(models.BaseResponse).Init()
  897. defer func() {
  898. this.Data["json"] = br
  899. this.ServeJSON()
  900. }()
  901. UserId, _ := this.GetInt("UserId")
  902. if UserId <= 0 {
  903. br.Msg = "查询用户不存在"
  904. return
  905. }
  906. user, err := models.GetUserViewById(UserId)
  907. if err != nil {
  908. if err == orm.ErrNoRows {
  909. br.Msg = "用户不存在或已删除,请刷新页面"
  910. return
  911. }
  912. br.Msg = "查询用户失败"
  913. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  914. return
  915. }
  916. if user == nil {
  917. br.Msg = "用户不存在或已删除,请刷新页面"
  918. return
  919. }
  920. permissionList, err := services.GetUserPermissionById(UserId)
  921. if err != nil {
  922. br.Msg = "用户权限获取失败,请重新尝试"
  923. return
  924. }
  925. permissionMap := make(map[string][]string)
  926. for _, pm := range permissionList {
  927. permissionMap[pm.ParentName] = append(permissionMap[pm.ParentName], pm.PermissionName)
  928. }
  929. resp := new(response.UserDetailResp)
  930. resp.Detail = user
  931. resp.Permission = permissionMap
  932. br.Msg = "查询成功"
  933. br.Ret = 200
  934. br.Success = true
  935. br.Data = resp
  936. }
  937. // Delete
  938. // @Title 系统用户详情信息
  939. // @Description 用户详情信息
  940. // @Param request body request.UserDeleteReq true "type json string"
  941. // @Success 200 {object} models.LoginResp
  942. // @router /delete [post]
  943. func (this *UserController) Delete() {
  944. br := new(models.BaseResponse).Init()
  945. defer func() {
  946. this.Data["json"] = br
  947. this.ServeJSON()
  948. }()
  949. var req request.UserDeleteReq
  950. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  951. if err != nil {
  952. br.Msg = "参数解析异常!"
  953. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  954. return
  955. }
  956. err = models.DeleteUserById(req.UserId)
  957. if err != nil {
  958. br.Msg = "删除失败"
  959. br.ErrMsg = "删除失败,系统错误,Err:" + err.Error()
  960. return
  961. }
  962. br.Msg = "删除成功"
  963. br.Success = true
  964. br.Ret = 200
  965. }
  966. // @Title 开启/禁用用户
  967. // @Description 开启/禁用用户接口
  968. // @Param request body system.SysuserEditReq true "type json string"
  969. // @Success 200 操作成功
  970. // @router /editEnabled [post]
  971. func (this *UserController) EditEnabled() {
  972. br := new(models.BaseResponse).Init()
  973. defer func() {
  974. this.Data["json"] = br
  975. this.ServeJSON()
  976. }()
  977. var req request.UserEditEnabledReq
  978. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  979. if err != nil {
  980. br.Msg = "参数解析异常!"
  981. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  982. return
  983. }
  984. user, err := models.GetUserById(req.UserId)
  985. if err != nil {
  986. br.Msg = "获取数据失败"
  987. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  988. return
  989. }
  990. if !req.IsEnabled {
  991. user.Status = 0
  992. err = user.Update([]string{"status"})
  993. if err != nil {
  994. br.Msg = "修改失败"
  995. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  996. return
  997. }
  998. } else {
  999. validStartTime, err := time.Parse(utils.FormatDate, req.ValidStartTime)
  1000. if err != nil {
  1001. br.Msg = "日期格式不正确"
  1002. return
  1003. }
  1004. validEndTime, err := time.Parse(utils.FormatDate, req.ValidEndTime)
  1005. if err != nil {
  1006. br.Msg = "日期格式不正确"
  1007. return
  1008. }
  1009. if !validEndTime.After(validStartTime) {
  1010. br.Msg = "有效期结束日期在开始日期之前,日期格式不正确"
  1011. return
  1012. }
  1013. validStartTime = validStartTime.Local().Add(-time.Hour * 8)
  1014. validEndTime = validEndTime.Local().Add(-time.Hour*8 + time.Hour*24 - time.Second)
  1015. curTime := time.Now()
  1016. if curTime.After(validEndTime) {
  1017. br.Msg = "有效期已到期,请重新设置"
  1018. return
  1019. }
  1020. user.Status = 2
  1021. user.ModifyTime = curTime
  1022. user.ValidStartTime = validStartTime
  1023. user.ValidEndTime = validEndTime
  1024. err = user.Update([]string{"status", "modify_time", "valid_start_time", "valid_end_time"})
  1025. if err != nil {
  1026. br.Msg = "修改失败"
  1027. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  1028. return
  1029. }
  1030. }
  1031. // 记录操作
  1032. record := &models.UserChangeRecord{}
  1033. record.UserId = req.UserId
  1034. record.SysUserId = this.SysUser.SysUserId
  1035. if user.Status == 0 {
  1036. record.Content = this.SysUser.SysRealName + "禁用用户"
  1037. } else {
  1038. record.Content = this.SysUser.SysRealName + "启用用户"
  1039. }
  1040. record.Insert()
  1041. br.Ret = 200
  1042. br.Success = true
  1043. br.Msg = "操作成功"
  1044. }
  1045. // @Title 用户信息变更记录
  1046. // @Description 用户信息变更记录
  1047. // @Param UserId query int true "用户id"
  1048. // @Success 200 操作成功
  1049. // @router /change_list [get]
  1050. func (this *UserController) ChangeList() {
  1051. br := new(models.BaseResponse).Init()
  1052. defer func() {
  1053. this.Data["json"] = br
  1054. this.ServeJSON()
  1055. }()
  1056. userId, _ := this.GetInt("UserId")
  1057. if userId <= 0 {
  1058. br.Msg = "用户信息错误"
  1059. return
  1060. }
  1061. userRcord, err := models.GetUserChangeRecordListById(userId)
  1062. if err != nil {
  1063. br.Msg = "用户信息变更查询失败"
  1064. br.ErrMsg = "用户信息变更查询失败,系统错误,Err:" + err.Error()
  1065. return
  1066. }
  1067. resp := new(response.UserChangeRecordResp)
  1068. resp.List = userRcord
  1069. br.Data = resp
  1070. br.Msg = "查询成功"
  1071. br.Success = true
  1072. br.Ret = 200
  1073. }