12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298 |
- package controllers
- import (
- "encoding/json"
- "eta/eta_mini_crm/models"
- "eta/eta_mini_crm/models/request"
- "eta/eta_mini_crm/models/response"
- "eta/eta_mini_crm/services"
- "eta/eta_mini_crm/utils"
- "math"
- "os"
- "path/filepath"
- "strings"
- "time"
- "github.com/beego/beego/v2/client/orm"
- "github.com/rdlucklib/rdluck_tools/paging"
- "github.com/tealeg/xlsx"
- )
- type UserController struct {
- BaseAuthController
- }
- // Add
- // @Title 添加新客户
- // @Description 添加新客户
- // @Param request body request.UserAddReq true "type json string"
- // @Success 200 {object} request.UserAddReq
- // @router /add [post]
- func (this *UserController) Add() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.UserAddReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.RealName == "" {
- br.Msg = "请输入姓名"
- return
- }
- req.Phone = strings.TrimSpace(req.Phone)
- req.Email = strings.TrimSpace(req.Email)
- if req.Phone == "" && req.Email == "" {
- br.Msg = "至少输入一个手机号或邮箱"
- return
- }
- var userByPhone, userByEmail *models.User
- if req.Phone != "" {
- if req.AreaCode == "86" {
- if !utils.ValidateMobileFormatat(req.Phone) {
- br.Msg = "手机号格式有误, 请检查"
- return
- }
- }
- userByPhone, err = models.GetUserByPhone(req.Phone, req.AreaCode)
- if err != nil && err != orm.ErrNoRows {
- br.Msg = "添加用户失败"
- br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
- return
- }
- if userByPhone != nil {
- switch userByPhone.Status {
- case 0:
- userByPhone.Status = 2
- case 1:
- userByPhone.Status = 2
- default:
- br.Msg = "手机号已存在,请重新输入"
- return
- }
- }
- }
- if req.Email != "" {
- if !utils.ValidateEmailFormatat(req.Email) {
- br.Msg = "邮箱格式有误, 请检查"
- return
- }
- userByEmail, err = models.GetUserByEmail(req.Email)
- if err != nil && err != orm.ErrNoRows {
- br.Msg = "添加用户失败"
- br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
- return
- }
- if userByEmail != nil {
- switch userByEmail.Status {
- case 0:
- userByEmail.Status = 2
- case 1:
- userByEmail.Status = 2
- default:
- br.Msg = "邮箱已存在,请重新输入"
- return
- }
- }
- }
- if req.SellerDepartmentId <= 0 {
- br.Msg = "请选择营业部"
- return
- }
- if req.ValidStartTime == "" || req.ValidEndTime == "" {
- br.Msg = "请选择合理的有效期范围"
- return
- }
- validStartTime, err := time.Parse(utils.FormatDate, req.ValidStartTime)
- if err != nil {
- br.Msg = "错误的日期格式"
- return
- }
- validEndTime, err := time.Parse(utils.FormatDate, req.ValidEndTime)
- if err != nil {
- br.Msg = "错误的日期格式"
- return
- }
- if !validStartTime.Before(validEndTime) {
- br.Msg = "请选择合理的有效期范围"
- return
- }
- if req.Company == "" {
- br.Msg = "请输入所属公司"
- return
- }
- if userByPhone != nil && userByEmail != nil && userByPhone.UserId != userByEmail.UserId {
- br.Msg = "邮箱已存在,请重新输入"
- return
- }
- user := &models.User{}
- curTime := time.Now()
- if userByPhone == nil && userByEmail == nil {
- user.CreateTime = curTime
- user.ModifyTime = curTime
- }
- if userByEmail != nil {
- user = userByEmail
- }
- if userByPhone != nil {
- user = userByPhone
- }
- validStartTime = validStartTime.Local().Add(-time.Hour * 8)
- validEndTime = validEndTime.Local().Add(-time.Hour*8 + time.Hour*24 - time.Second)
- if curTime.Before(validEndTime) {
- user.Status = 2
- } else {
- br.Msg = "有效期截至时间必须大于当前时间"
- return
- }
- sysDepartment, err := models.GetSysDepartmentById(req.SellerDepartmentId)
- if err != nil {
- br.Msg = "添加用户失败"
- br.ErrMsg = "查询营业部失败,系统错误,Err:" + err.Error()
- return
- }
- user.RealName = req.RealName
- user.AreaCode = req.AreaCode
- user.Phone = req.Phone
- user.Email = req.Email
- user.SellerDepartmentId = req.SellerDepartmentId
- user.SellerDepartmentName = sysDepartment.SysDepartmentName
- user.ValidStartTime = validStartTime
- user.ValidEndTime = validEndTime
- user.Company = req.Company
- user.ModifyTime = curTime
- err = models.SaveUser(user, req.ChartPermission)
- if err != nil {
- br.Msg = "添加客户失败"
- br.ErrMsg = "添加客户失败,系统错误,Err:" + err.Error()
- return
- }
- if user.Status == 2 {
- userRecord := &models.UserChangeRecord{}
- userRecord.UserId = user.UserId
- userRecord.SysUserId = this.SysUser.SysUserId
- userRecord.Content = this.SysUser.SysRealName + "新增用户"
- userRecord.Insert()
- }
- br.Msg = "添加成功"
- br.Success = true
- br.Ret = 200
- }
- // edit
- // @Title 编辑客户
- // @Description 编辑客户
- // @Param request body request.UserEidtReq true "type json string"
- // @Success 200 {object} request.UserAddReq
- // @router /edit [post]
- func (this *UserController) Edit() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.UserEditReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.RealName == "" {
- br.Msg = "请输入姓名"
- return
- }
- req.Phone = strings.TrimSpace(req.Phone)
- req.Email = strings.TrimSpace(req.Email)
- if req.Phone == "" && req.Email == "" {
- br.Msg = "至少输入一个手机号或邮箱"
- return
- }
- if req.Phone != "" {
- if req.AreaCode == "86" {
- if !utils.ValidateMobileFormatat(req.Phone) {
- br.Msg = "手机号格式有误, 请检查"
- return
- }
- }
- user, err := models.GetUserByPhone(req.Phone, req.AreaCode)
- if err != nil && err != orm.ErrNoRows {
- br.Msg = "编辑用户失败"
- br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
- return
- }
- if user != nil && user.UserId != req.UserId {
- br.Msg = "手机号已存在,请重新输入"
- return
- }
- }
- if req.Email != "" {
- if !utils.ValidateEmailFormatat(req.Email) {
- br.Msg = "邮箱格式有误, 请检查"
- return
- }
- user, err := models.GetUserByEmail(req.Email)
- if err != nil && err != orm.ErrNoRows {
- br.Msg = "编辑用户失败"
- br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
- return
- }
- if user != nil && user.UserId != req.UserId {
- br.Msg = "邮箱已存在,请重新输入"
- return
- }
- }
- if req.SellerDepartmentId <= 0 {
- br.Msg = "请选择营业部"
- return
- }
- if req.ValidStartTime == "" || req.ValidEndTime == "" {
- br.Msg = "请选择合理的有效期范围"
- return
- }
- validStartTime, err := time.Parse(utils.FormatDate, req.ValidStartTime)
- if err != nil {
- br.Msg = "错误的日期格式"
- return
- }
- validStartTime = validStartTime.In(time.Local).Add(-time.Hour * 8)
- validEndTime, err := time.Parse(utils.FormatDate, req.ValidEndTime)
- if err != nil {
- br.Msg = "错误的日期格式"
- return
- }
- validEndTime = validEndTime.In(time.Local).Add(-time.Hour*8 + time.Hour*24 - time.Second)
- if !validStartTime.Before(validEndTime) {
- br.Msg = "请选择合理的有效期范围"
- return
- }
- if req.Company == "" {
- br.Msg = "请输入所属公司"
- return
- }
- if req.IsEnabled && time.Now().After(validEndTime) {
- br.Msg = "启用后,有效期必须大于当前时间"
- return
- }
- user, err := models.GetUserById(req.UserId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "用户不存在或已删除,请重新刷新页面"
- br.ErrMsg = "用户不存在或已删除,请重新刷新页面,Err:" + err.Error()
- return
- }
- br.Msg = "编辑用户失败"
- br.ErrMsg = "编辑用户失败, 系统错误,Err:" + err.Error()
- return
- }
- sysDepartment, err := models.GetSysDepartmentById(req.SellerDepartmentId)
- if err != nil {
- br.Msg = "添加用户失败"
- br.ErrMsg = "查询营业部失败,系统错误,Err:" + err.Error()
- return
- }
- user.RealName = req.RealName
- user.AreaCode = req.AreaCode
- user.Phone = req.Phone
- user.Email = req.Email
- user.SellerDepartmentId = req.SellerDepartmentId
- user.SellerDepartmentName = sysDepartment.SysDepartmentName
- user.ValidStartTime = validStartTime
- user.ValidEndTime = validEndTime
- user.Company = req.Company
- user.ModifyTime = time.Now()
- // 当用户状态初始为潜在客户时,才更新创建时间
- if user.Status == utils.UserStatusPotential {
- user.CreateTime = time.Now()
- }
- oldStatus := user.Status
- if !req.IsEnabled {
- user.Status = 0
- } else {
- user.Status = 2
- }
- err = models.SaveUser(user, req.ChartPermission)
- if err != nil {
- br.Msg = "添加客户失败"
- br.ErrMsg = "添加客户失败,系统错误,Err:" + err.Error()
- return
- }
- record := &models.UserChangeRecord{}
- record.UserId = req.UserId
- record.SysUserId = this.SysUser.SysUserId
- if oldStatus == utils.UserStatusPotential && user.Status == utils.UserStatusFormal {
- record.Content = this.SysUser.SysRealName + "新增用户"
- } else {
- record.Content = this.SysUser.SysRealName + "编辑用户"
- }
- record.Insert()
- br.Msg = "编辑成功"
- br.Success = true
- br.Ret = 200
- }
- // Check
- // @Title 编辑客户
- // @Description 编辑客户
- // @Param request body request.UserEidtReq true "type json string"
- // @Success 200 {object} request.UserAddReq
- // @router /check [post]
- func (this *UserController) Check() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.UserCheckReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.Phone = strings.TrimSpace(req.Phone)
- req.Email = strings.TrimSpace(req.Email)
- if req.Phone == "" && req.Email == "" {
- br.Msg = "至少输入一个手机号或邮箱"
- return
- }
- userCheckResp := new(response.UserCheckResp)
- if req.Phone != "" {
- if req.AreaCode == "86" {
- if !utils.ValidateMobileFormatat(req.Phone) {
- br.Msg = "手机号格式有误, 请检查"
- return
- }
- }
- user, err := models.GetUserByPhone(req.Phone, req.AreaCode)
- if err != nil && err != orm.ErrNoRows {
- br.Msg = "编辑用户失败"
- br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
- return
- }
- if user != nil {
- userCheckResp.UserId = user.UserId
- userCheckResp.Status = user.Status
- if user.Status == utils.UserStatusNo {
- br.Ret = 200
- br.Success = true
- br.Msg = "该用户已被禁用,确认启用并更新用户信息吗?"
- br.Data = userCheckResp
- return
- }
- if user.Status == utils.UserStatusPotential {
- br.Ret = 200
- br.Success = true
- br.Msg = "该用户已在潜在列表,确认转客户并更新用户信息吗?"
- br.Data = userCheckResp
- return
- }
- if user.Status == utils.UserStatusFormal {
- br.Msg = "手机号已存在,请重新输入"
- return
- }
- }
- }
- if req.Email != "" {
- if !utils.ValidateEmailFormatat(req.Email) {
- br.Msg = "邮箱格式有误, 请检查"
- return
- }
- user, err := models.GetUserByEmail(req.Email)
- if err != nil && err != orm.ErrNoRows {
- br.Msg = "编辑用户失败"
- br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
- return
- }
- if user != nil {
- userCheckResp.UserId = user.UserId
- if user.Status == 0 {
- br.Ret = 200
- br.Success = true
- br.Msg = "该用户已被禁用,确认启用并更新用户信息吗?"
- br.Data = userCheckResp
- return
- }
- if user.Status == 1 {
- br.Ret = 200
- br.Success = true
- br.Msg = "该用户已被禁用,确认启用并更新用户信息吗?"
- br.Data = userCheckResp
- return
- }
- if user.Status == 2 {
- br.Msg = "邮箱已存在,请重新输入"
- return
- }
- return
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "邮箱或手机号合格"
- }
- // List
- // @Title 用户列表
- // @Description 用户列表
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param SellerDepartmentId query string true "营业部门id"
- // @Param Status query int true "用户状态"
- // @Param KeyWord query string true "手机号/邮箱/姓名"
- // @Param IsRegistered query string true "是否注册"
- // @Param IsSubscribed query string true "是否关注"
- // @Param RegisterStartDate query string true "注册开始时间"
- // @Param RegisterEndDate query string true "注册结束时间"
- // @Param CreateStartDate query string true "创建开始时间"
- // @Param CreateEndDate query string true "创建结束时间"
- // @Param SortParam query string true "排序字段"
- // @Param SortType query string true "排序方式"
- // @Success 200 {object} response.UserListResp
- // @router /list [get]
- func (this *UserController) List() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- sellerDepartmentId, _ := this.GetInt("SellerDepartmentId", 0)
- status := this.GetString("Status")
- keyWord := this.GetString("KeyWord")
- IsRegistered := this.GetString("IsRegistered")
- IsSubscribed := this.GetString("IsSubscribed")
- registerStartDate := this.GetString("RegisterStartDate")
- registerEndDate := this.GetString("RegisterEndDate")
- createStartDate := this.GetString("CreateStartDate")
- createEndDate := this.GetString("CreateEndDate")
- sortParma := this.GetString("SortParam")
- sortType := this.GetString("SortType")
- var condition string
- var sortCondition string
- var pars []interface{}
- if keyWord != "" {
- condition += ` AND (u.real_name LIKE ? OR u.phone LIKE ? OR u.email LIKE ?) `
- pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
- }
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- } else if pageSize > utils.PageSize100 {
- pageSize = utils.PageSize100
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- if sortParma != "" && sortType != "" {
- sortCondition = " ORDER BY "
- var param, sort string
- switch sortParma {
- case "RegisterTime":
- param = "u.register_time"
- case "CreateTime":
- param = "u.create_time"
- case "RestDate":
- param = "u.valid_end_time"
- }
- switch sortType {
- case "asc":
- sort = " ASC "
- case "desc":
- sort = " DESC "
- }
- if param != "" && sort != "" {
- sortCondition += param + " " + sort
- } else {
- sortCondition = ""
- }
- }
- if sortCondition == "" {
- sortCondition = " ORDER BY u.create_time DESC"
- }
- if sellerDepartmentId > 0 {
- condition += ` AND u.seller_department_id = ? `
- pars = append(pars, sellerDepartmentId)
- }
- switch status {
- case "禁用":
- condition += " AND u.status=? "
- pars = append(pars, 0)
- case "潜在":
- condition += " AND u.status=? "
- pars = append(pars, 1)
- case "正式":
- condition += " AND u.status=? "
- pars = append(pars, 2)
- case "":
- condition += " AND (u.status=? OR u.status=?) "
- pars = append(pars, 0, 2)
- }
- switch IsRegistered {
- case "是":
- condition += " AND u.is_registered=? "
- pars = append(pars, true)
- case "否":
- condition += " AND u.is_registered=? "
- pars = append(pars, false)
- }
- switch IsSubscribed {
- case "是":
- condition += " AND u.is_subscribed=? "
- pars = append(pars, true)
- case "否":
- condition += " AND u.is_subscribed=? "
- pars = append(pars, false)
- }
- if registerStartDate != "" {
- registerStartTime, er := time.Parse(utils.FormatDate, registerStartDate)
- if er != nil {
- br.Msg = "日期格式有误"
- return
- }
- condition += " AND u.register_time>=? "
- registerStartDateStr := registerStartTime.Format(utils.FormatDateTime)
- pars = append(pars, registerStartDateStr)
- }
- if registerEndDate != "" {
- registerEndTime, er := time.Parse(utils.FormatDate, registerEndDate)
- if er != nil {
- br.Msg = "日期格式有误"
- return
- }
- condition += " AND u.register_time<=? "
- // 结束时间包含今天
- registerEndTime = registerEndTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
- registerEndDateStr := registerEndTime.Format(utils.FormatDateTime)
- pars = append(pars, registerEndDateStr)
- }
- if createStartDate != "" {
- createStartTime, er := time.Parse(utils.FormatDate, createStartDate)
- if er != nil {
- br.Msg = "日期格式有误"
- return
- }
- condition += " AND u.create_time>=? "
- createStartDateStr := createStartTime.Format(utils.FormatDateTime)
- pars = append(pars, createStartDateStr)
- }
- if createEndDate != "" {
- createEndTime, er := time.Parse(utils.FormatDate, createEndDate)
- if er != nil {
- br.Msg = "日期格式有误"
- return
- }
- condition += " AND u.create_time<=? "
- // 结束时间包含今天
- createEndTime = createEndTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
- createEndDateStr := createEndTime.Format(utils.FormatDateTime)
- pars = append(pars, createEndDateStr)
- }
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- } else if pageSize > utils.PageSize100 {
- pageSize = utils.PageSize100
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize := utils.StartIndex(currentIndex, pageSize)
- total, err := models.GetUserCount(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- userList, err := models.GetUserListByConditonSort(condition, sortCondition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "查询用户失败"
- br.Msg = "查询用户失败,系统错误,Err:" + err.Error()
- return
- }
- for _, u := range userList {
- if u.ValidEndTime == "" {
- u.RestDate = 0
- } else {
- endTime, err := time.Parse(utils.FormatDateTime, u.ValidEndTime)
- if err != nil {
- br.Msg = "用户有效时间格式有误"
- br.ErrMsg = "用户有效时间格式有误,Err:" + err.Error()
- return
- }
- // 对天数向上取整
- daysBetween := int(math.Ceil(time.Until(endTime).Hours() / 24))
- if daysBetween < 0 {
- daysBetween = 0
- }
- u.RestDate = daysBetween
- }
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := new(response.UserListResp)
- resp.Paging = page
- resp.List = userList
- br.Data = resp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // PotentialList
- // @Title 潜在用户列表
- // @Description 潜在用户列表
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param KeyWord query string true "手机号/邮箱"
- // @Param SellerDepartmentId query int true "营业部id"
- // @Param ApplyStatus query int true "申请状态"
- // @Param RegisterStartDate query string true "注册开始时间"
- // @Param RegisterEndDate query string true "注册结束时间"
- // @Param LastUpdateStartDate query string true "最后一次阅读开始时间"
- // @Param LastUpdateEndDate query string true "最后一次阅读结束时间"
- // @Param ApplyStartDate query string true "申请开始时间"
- // @Param ApplyEndDate query string true "申请结束时间"
- // @Param SortParam query string true "排序字段"
- // @Param SortType query string true "排序方式"
- // @Success 200 {object} response.UserListResp
- // @router /potential/list [get]
- func (this *UserController) PotentialList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- keyWord := this.GetString("KeyWord")
- sellerDepartmentId, _ := this.GetInt("SellerDepartmentId")
- applyStatus := this.GetString("ApplyStatus")
- applyStartDate := this.GetString("ApplyStartDate")
- applyEndDate := this.GetString("ApplyEndDate")
- registerStartDate := this.GetString("RegisterStartDate")
- registerEndDate := this.GetString("RegisterEndDate")
- lastUpdateStartDate := this.GetString("LastUpdateStartDate")
- lastUpdateEndDate := this.GetString("LastUpdateEndDate")
- sortParam := this.GetString("SortParam")
- sortType := this.GetString("SortType")
- userList, total, errMsg, msg, err := services.GetPotentialList(keyWord, sellerDepartmentId, applyStatus, pageSize, currentIndex, sortParam, sortType, registerStartDate, registerEndDate, lastUpdateStartDate, lastUpdateEndDate, applyStartDate, applyEndDate)
- if err != nil {
- if msg != "" {
- br.Msg = msg
- } else {
- br.Msg = "获取失败"
- }
- if errMsg != "" {
- br.ErrMsg = errMsg + ", Err:" + err.Error()
- } else {
- br.ErrMsg = "获取失败, Err:" + err.Error()
- }
- }
- resp := new(response.UserListResp)
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp.Paging = page
- resp.List = userList
- br.Data = resp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // PotentialEdit
- // @Title 编辑潜在客户(转客户)
- // @Description 编辑潜在客户(转客户)
- // @Param request body request.UserEidtReq true "type json string"
- // @Success 200 {object} request.UserAddReq
- // @router /potential/edit [post]
- func (this *UserController) PotentialEdit() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.UserEditReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.RealName == "" {
- br.Msg = "请输入姓名"
- return
- }
- req.Phone = strings.TrimSpace(req.Phone)
- req.Email = strings.TrimSpace(req.Email)
- if req.Phone == "" && req.Email == "" {
- br.Msg = "至少输入一个手机号或邮箱"
- return
- }
- if req.Phone != "" {
- if req.AreaCode == "86" {
- if !utils.ValidateMobileFormatat(req.Phone) {
- br.Msg = "手机号格式有误, 请检查"
- return
- }
- }
- user, err := models.GetUserByPhone(req.Phone, req.AreaCode)
- if err != nil && err != orm.ErrNoRows {
- br.Msg = "编辑用户失败"
- br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
- return
- }
- if user != nil && user.UserId != req.UserId {
- br.Msg = "手机号已存在,请重新输入"
- return
- }
- }
- if req.Email != "" {
- if !utils.ValidateEmailFormatat(req.Email) {
- br.Msg = "邮箱格式有误, 请检查"
- return
- }
- user, err := models.GetUserByEmail(req.Email)
- if err != nil && err != orm.ErrNoRows {
- br.Msg = "编辑用户失败"
- br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
- return
- }
- if user != nil && user.UserId != req.UserId {
- br.Msg = "邮箱已存在,请重新输入"
- return
- }
- }
- if req.SellerDepartmentId <= 0 {
- br.Msg = "请选择营业部"
- return
- }
- if req.ValidStartTime == "" || req.ValidEndTime == "" {
- br.Msg = "请选择合理的有效期范围"
- return
- }
- validStartTime, err := time.Parse(utils.FormatDate, req.ValidStartTime)
- if err != nil {
- br.Msg = "错误的日期格式"
- return
- }
- validStartTime = validStartTime.In(time.Local).Add(-time.Hour * 8)
- validEndTime, err := time.Parse(utils.FormatDate, req.ValidEndTime)
- if err != nil {
- br.Msg = "错误的日期格式"
- return
- }
- validEndTime = validEndTime.In(time.Local).Add(-time.Hour*8 + time.Hour*24 - time.Second)
- if !validStartTime.Before(validEndTime) {
- br.Msg = "请选择合理的有效期范围"
- return
- }
- if req.Company == "" {
- br.Msg = "请输入所属公司"
- return
- }
- if req.IsEnabled && time.Now().After(validEndTime) {
- br.Msg = "启用后,有效期必须大于当前时间"
- return
- }
- user, err := models.GetUserById(req.UserId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "用户不存在或已删除,请重新刷新页面"
- br.ErrMsg = "用户不存在或已删除,请重新刷新页面,Err:" + err.Error()
- return
- }
- br.Msg = "编辑用户失败"
- br.ErrMsg = "编辑用户失败, 系统错误,Err:" + err.Error()
- return
- }
- department, err := models.GetSysDepartmentById(req.SellerDepartmentId)
- if err != nil {
- br.Msg = "编辑用户失败"
- br.ErrMsg = "编辑用户失败, 系统错误,Err:" + err.Error()
- return
- }
- user.RealName = req.RealName
- user.AreaCode = req.AreaCode
- user.Phone = req.Phone
- user.Email = req.Email
- user.SellerDepartmentId = req.SellerDepartmentId
- user.SellerDepartmentName = department.SysDepartmentName
- user.ValidStartTime = validStartTime
- user.ValidEndTime = validEndTime
- user.Company = req.Company
- user.ModifyTime = time.Now()
- // 当用户状态初始为潜在客户时,才更新创建时间
- if user.Status == utils.UserStatusPotential {
- user.CreateTime = time.Now()
- }
- oldStatus := user.Status
- if !req.IsEnabled {
- user.Status = 0
- } else {
- user.Status = 2
- }
- err = models.SaveUser(user, req.ChartPermission)
- if err != nil {
- br.Msg = "添加客户失败"
- br.ErrMsg = "添加客户失败,系统错误,Err:" + err.Error()
- return
- }
- record := &models.UserChangeRecord{}
- record.UserId = req.UserId
- record.SysUserId = this.SysUser.SysUserId
- if oldStatus == utils.UserStatusPotential && user.Status == utils.UserStatusFormal {
- record.Content = this.SysUser.SysRealName + "新增用户"
- } else {
- record.Content = this.SysUser.SysRealName + "编辑用户"
- }
- record.Insert()
- br.Msg = "编辑成功"
- br.Success = true
- br.Ret = 200
- }
- // ExportPotentialExcel
- // @Title 导出潜在用户Excel
- // @Description 导出潜在用户Excel
- // @Param KeyWord query string true "手机号/邮箱"
- // @Param SellerDepartmentId query int true "营业部id"
- // @Param ApplyStatus query int true "申请状态"
- // @Param RegisterStartDate query string true "注册开始时间"
- // @Param RegisterEndDate query string true "注册结束时间"
- // @Param LastUpdateStartDate query string true "最后一次阅读开始时间"
- // @Param LastUpdateEndDate query string true "最后一次阅读结束时间"
- // @Param ApplyStartDate query string true "申请开始时间"
- // @Param ApplyEndDate query string true "申请结束时间"
- // @Success 200 {object} request.UserAddReq
- // @router /potential/export/excel [get]
- func (this *UserController) ExportPotentialExcel() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- keyword := this.GetString("KeyWord")
- sellerDepartmentId, _ := this.GetInt("SellerDepartmentId")
- applyStatus := this.GetString("ApplyStatus")
- registerStartDate := this.GetString("RegisterStartDate")
- registerEndDate := this.GetString("RegisterEndDate")
- lastUpdateStartDate := this.GetString("LastUpdateStartDate")
- lastUpdateEndDate := this.GetString("LastUpdateEndDate")
- applyStartDate := this.GetString("ApplyStartDate")
- applyEndDate := this.GetString("ApplyEndDate")
- total, err := models.GetPotentialUserTotal()
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,系统错误,Err:" + err.Error()
- return
- }
- userList, _, errMsg, msg, err := services.GetPotentialList(keyword, sellerDepartmentId, applyStatus, total, 0, "", "", registerStartDate, registerEndDate, lastUpdateStartDate, lastUpdateEndDate, applyStartDate, applyEndDate)
- if err != nil {
- if msg != "" {
- br.Msg = msg
- } else {
- br.Msg = "获取失败"
- }
- if errMsg != "" {
- br.ErrMsg = errMsg + ", Err:" + err.Error()
- } else {
- br.ErrMsg = "获取失败, Err:" + err.Error()
- }
- }
- dir, _ := os.Executable()
- exPath := filepath.Dir(dir)
- downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- xlsxFile := xlsx.NewFile()
- sheetNew := new(xlsx.Sheet)
- sheetNew, err = xlsxFile.AddSheet("潜在客户列表")
- if err != nil {
- br.Msg = "创建sheet失败"
- br.ErrMsg = "创建sheet失败,系统错误,Err:" + err.Error()
- return
- }
- titleRow := sheetNew.AddRow()
- titleRow.AddCell().SetString("手机号")
- titleRow.AddCell().SetString("姓名")
- titleRow.AddCell().SetString("营业部")
- titleRow.AddCell().SetString("邮箱")
- titleRow.AddCell().SetString("注册时间")
- titleRow.AddCell().SetString("申请时间")
- titleRow.AddCell().SetString("最近一次阅读")
- titleRow.AddCell().SetString("累计阅读次数")
- titleRow.AddCell().SetString("申请状态")
- for _, user := range userList {
- row := sheetNew.AddRow()
- row.AddCell().SetString(user.Phone)
- row.AddCell().SetString(user.RealName)
- row.AddCell().SetString(user.SellerDepartmentName)
- row.AddCell().SetString(user.Email)
- row.AddCell().SetString(user.RegisterTime)
- row.AddCell().SetString(user.ApplyTime)
- row.AddCell().SetString(user.LastUpdateTime)
- row.AddCell().SetInt(user.ReadCnt)
- switch user.ApplyStatus {
- case 0:
- row.AddCell().SetString("未申请")
- case 1:
- row.AddCell().SetString("已申请")
- }
- }
- err = xlsxFile.Save(downLoadnFilePath)
- if err != nil {
- sheet, err := xlsxFile.AddSheet("无数据")
- if err != nil {
- br.Msg = "创建sheet失败"
- br.ErrMsg = "创建sheet失败,系统错误,Err:" + err.Error()
- return
- }
- rowSecName := sheet.AddRow()
- celSecName := rowSecName.AddCell()
- celSecName.SetString("无数据")
- err = xlsxFile.Save(downLoadnFilePath)
- if err != nil {
- br.Msg = "保存文件失败"
- br.ErrMsg = "保存文件失败,系统错误,Err:" + err.Error()
- return
- }
- }
- fileName := "潜在客户列表" + time.Now().Format("06.1.2")
- this.Ctx.Output.Download(downLoadnFilePath, fileName)
- defer os.Remove(downLoadnFilePath)
- br.Ret = 200
- br.Success = true
- br.Msg = "success"
- }
- // Detail
- // @Title 用户详情信息
- // @Description 用户详情信息
- // @Param UserId query int true "用户id"
- // @Success 200 {object} models.LoginResp
- // @router /detail [get]
- func (this *UserController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- UserId, _ := this.GetInt("UserId")
- if UserId <= 0 {
- br.Msg = "查询用户不存在"
- return
- }
- user, err := models.GetUserViewById(UserId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "用户不存在或已删除,请刷新页面"
- return
- }
- br.Msg = "查询用户失败"
- br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
- return
- }
- if user == nil {
- br.Msg = "用户不存在或已删除,请刷新页面"
- return
- }
- permissionList, err := services.GetUserPermissionById(UserId)
- if err != nil {
- br.Msg = "用户权限获取失败,请重新尝试"
- return
- }
- permissionMap := make(map[string][]string)
- for _, pm := range permissionList {
- permissionMap[pm.ParentName] = append(permissionMap[pm.ParentName], pm.PermissionName)
- }
- resp := new(response.UserDetailResp)
- resp.Detail = user
- resp.Permission = permissionMap
- br.Msg = "查询成功"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
- // Delete
- // @Title 系统用户详情信息
- // @Description 用户详情信息
- // @Param request body request.UserDeleteReq true "type json string"
- // @Success 200 {object} models.LoginResp
- // @router /delete [post]
- func (this *UserController) Delete() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.UserDeleteReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- err = models.DeleteUserById(req.UserId)
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除失败,系统错误,Err:" + err.Error()
- return
- }
- br.Msg = "删除成功"
- br.Success = true
- br.Ret = 200
- }
- // @Title 开启/禁用用户
- // @Description 开启/禁用用户接口
- // @Param request body system.SysuserEditReq true "type json string"
- // @Success 200 操作成功
- // @router /editEnabled [post]
- func (this *UserController) EditEnabled() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.UserEditEnabledReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- user, err := models.GetUserById(req.UserId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
- return
- }
- if !req.IsEnabled {
- user.Status = 0
- err = user.Update([]string{"status"})
- if err != nil {
- br.Msg = "修改失败"
- br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
- return
- }
- } else {
- validStartTime, err := time.Parse(utils.FormatDate, req.ValidStartTime)
- if err != nil {
- br.Msg = "日期格式不正确"
- return
- }
- validEndTime, err := time.Parse(utils.FormatDate, req.ValidEndTime)
- if err != nil {
- br.Msg = "日期格式不正确"
- return
- }
- if !validEndTime.After(validStartTime) {
- br.Msg = "有效期结束日期在开始日期之前,日期格式不正确"
- return
- }
- validStartTime = validStartTime.Local().Add(-time.Hour * 8)
- validEndTime = validEndTime.Local().Add(-time.Hour*8 + time.Hour*24 - time.Second)
- curTime := time.Now()
- if curTime.After(validEndTime) {
- br.Msg = "有效期已到期,请重新设置"
- return
- }
- user.Status = 2
- user.ModifyTime = curTime
- user.ValidStartTime = validStartTime
- user.ValidEndTime = validEndTime
- err = user.Update([]string{"status", "modify_time", "valid_start_time", "valid_end_time"})
- if err != nil {
- br.Msg = "修改失败"
- br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
- return
- }
- }
- // 记录操作
- record := &models.UserChangeRecord{}
- record.UserId = req.UserId
- record.SysUserId = this.SysUser.SysUserId
- if user.Status == 0 {
- record.Content = this.SysUser.SysRealName + "禁用用户"
- } else {
- record.Content = this.SysUser.SysRealName + "启用用户"
- }
- record.Insert()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // @Title 用户信息变更记录
- // @Description 用户信息变更记录
- // @Param UserId query int true "用户id"
- // @Success 200 操作成功
- // @router /change_list [get]
- func (this *UserController) ChangeList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- userId, _ := this.GetInt("UserId")
- if userId <= 0 {
- br.Msg = "用户信息错误"
- return
- }
- userRcord, err := models.GetUserChangeRecordListById(userId)
- if err != nil {
- br.Msg = "用户信息变更查询失败"
- br.ErrMsg = "用户信息变更查询失败,系统错误,Err:" + err.Error()
- return
- }
- resp := new(response.UserChangeRecordResp)
- resp.List = userRcord
- br.Data = resp
- br.Msg = "查询成功"
- br.Success = true
- br.Ret = 200
- }
- // GlobalSearch
- // @Title 全局用户列表
- // @Description 全局用户列表
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param KeyWord query string true "手机号/邮箱/姓名"
- // @Param SortParam query string true "排序字段"
- // @Param SortType query string true "排序方式"
- // @Success 200 {object} response.UserListResp
- // @router /global/list [get]
- func (this *UserController) GlobalSearch() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- keyWord := this.GetString("KeyWord")
- sortParam := this.GetString("SortParam")
- sortType := this.GetString("SortType")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- var condition string
- var pars []interface{}
- var sortCondition string
- if keyWord == "" {
- br.Ret = 200
- br.Msg = "查询成功"
- br.Success = true
- return
- } else {
- condition = ` AND (real_name like ? OR phone like ? OR email like ?)`
- pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
- }
- if sortParam != "" && sortType != "" {
- var param, sort string
- switch sortParam {
- case "RegisterTime":
- param = "register_time"
- case "ReadCnt":
- param = "read_cnt"
- case "LastUpdateTime":
- param = "last_update_time"
- }
- switch sortType {
- case "asc":
- sort = "ASC"
- case "desc":
- sort = "DESC"
- }
- if param != "" && sort != "" {
- sortCondition = ` ORDER BY ` + param + ` ` + sort
- }
- }
- if sortCondition == "" {
- sortCondition = ` ORDER BY u.register_time DESC`
- }
- startSize := utils.StartIndex(currentIndex, pageSize)
- userIds, err := models.GetUserIdListByCondition(condition, pars)
- if err != nil {
- br.Msg = "查询失败"
- br.ErrMsg = "查询失败,系统错误,Err:" + err.Error()
- return
- }
- userList, err := models.GetGlobalUserByCondition(userIds, sortCondition, startSize, pageSize)
- if err != nil {
- br.Msg = "查询失败"
- br.ErrMsg = "查询失败,系统错误,Err:" + err.Error()
- return
- }
- page := paging.GetPaging(currentIndex, pageSize, len(userIds))
- resp := new(response.UserListResp)
- resp.List = userList
- resp.Paging = page
- br.Data = resp
- br.Msg = "查询成功"
- br.Success = true
- br.Ret = 200
- }
|