user.go 34 KB

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