123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- package english_report
- import (
- "encoding/json"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- "eta/eta_api/services"
- "eta/eta_api/utils"
- "fmt"
- "strings"
- "time"
- )
- // EnPermissionController 英文权限
- type EnPermissionController struct {
- controllers.BaseAuthController
- }
- // Add
- // @Title 新增品种权限
- // @Description 新增品种权限
- // @Param request body models.EnPermissionAddReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /add [post]
- func (this *EnPermissionController) Add() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnPermissionAddReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- req.PermissionName = strings.TrimSpace(req.PermissionName)
- if req.PermissionName == "" {
- br.Msg = "请输入品种名称"
- return
- }
- if req.Enabled != 1 && req.Enabled != 0 {
- br.Msg = "请设置正确的状态"
- return
- }
- // 重名校验
- existOB := new(models.EnPermission)
- existCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, models.EnPermissionColumns.EnPermissionName, models.EnPermissionColumns.ParentId)
- existPars := make([]interface{}, 0)
- existPars = append(existPars, req.PermissionName, req.ParentId)
- exist, e := existOB.GetItemByCondition(existCond, existPars)
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "获取重名品种权限失败, Err: " + e.Error()
- return
- }
- if exist != nil && exist.EnPermissionId > 0 {
- br.Msg = "品种名称已存在"
- return
- }
- // 获取最大的排序值
- item := new(models.EnPermission)
- maxSort, e := item.GetMaxSort()
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "查询品种排序失败, Err: " + e.Error()
- return
- }
- // 新增
- item.EnPermissionName = req.PermissionName
- item.ParentId = req.ParentId
- item.Sort = maxSort + 1
- item.Enabled = req.Enabled
- item.CreateTime = time.Now().Local()
- item.ModifyTime = time.Now().Local()
- if e = item.Create(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "新增品种权限失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- br.Data = item.EnPermissionId
- }
- // Edit
- // @Title 编辑品种权限
- // @Description 编辑品种权限
- // @Param request body models.EnPermissionAddReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /edit [post]
- func (this *EnPermissionController) Edit() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnPermissionEditReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if req.PermissionId <= 0 {
- br.Msg = "参数有误"
- return
- }
- req.PermissionName = strings.TrimSpace(req.PermissionName)
- if req.PermissionName == "" {
- br.Msg = "请输入品种名称"
- return
- }
- // 重名校验
- ob := new(models.EnPermission)
- existCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s <> ?`, models.EnPermissionColumns.EnPermissionName, models.EnPermissionColumns.ParentId, models.EnPermissionColumns.EnPermissionId)
- existPars := make([]interface{}, 0)
- existPars = append(existPars, req.PermissionName, req.ParentId, req.PermissionId)
- exist, e := ob.GetItemByCondition(existCond, existPars)
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "获取重名品种权限失败, Err: " + e.Error()
- return
- }
- if exist != nil && exist.EnPermissionId > 0 {
- br.Msg = "品种名称已存在"
- return
- }
- item, e := ob.GetItemById(req.PermissionId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "品种不存在, 请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取品种权限失败, Err: " + e.Error()
- return
- }
- originPid := item.ParentId
- item.EnPermissionName = req.PermissionName
- item.ParentId = req.ParentId
- item.ModifyTime = time.Now().Local()
- if e = item.Update([]string{"EnPermissionName", "ParentId", "ModifyTime"}); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新品种权限失败, Err: " + e.Error()
- return
- }
- // 若二级分类转为一级分类, 则清空对应客户、分类权限
- if originPid > 0 && req.ParentId == 0 {
- if e = models.ClearEnPermissionsByPermissionId(item.EnPermissionId); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "清空英文分类权限失败, Err: " + e.Error()
- return
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- br.Data = item.EnPermissionId
- }
- // ParentList
- // @Title 父级品种列表
- // @Description 父级品种列表
- // @Success 200 Ret=200 操作成功
- // @router /parent/list [get]
- func (this *EnPermissionController) ParentList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- cond := fmt.Sprintf(` AND %s = ?`, models.EnPermissionColumns.ParentId)
- pars := make([]interface{}, 0)
- pars = append(pars, 0)
- ob := new(models.EnPermission)
- list, e := ob.GetItemsByCondition(cond, pars, []string{}, "sort ASC, create_time ASC")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取父级品种列表失败, Err: " + e.Error()
- return
- }
- resp := make([]*models.EnPermissionItem, 0)
- for _, v := range list {
- t := new(models.EnPermissionItem)
- t.PermissionId = v.EnPermissionId
- t.PermissionName = v.EnPermissionName
- t.CnPermissionName = v.CnPermissionName
- t.ParentId = v.ParentId
- t.Sort = v.Sort
- t.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
- t.Child = make([]*models.EnPermissionItem, 0)
- resp = append(resp, t)
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // List
- // @Title 品种列表
- // @Description 品种列表
- // @Param Keyword query string false "关键词"
- // @Param ReportId query int false "报告ID-用于邮件推送时过滤掉无权限的品种"
- // @Param VideoId query int false "路演视频ID-用于邮件推送时过滤掉无权限的品种"
- // @Success 200 Ret=200 操作成功
- // @router /list [get]
- func (this *EnPermissionController) List() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- keyword := this.GetString("Keyword", "")
- keyword = strings.TrimSpace(keyword)
- enabled, _ := this.GetInt("Enabled", -1)
- // 禁用指定报告、指定路演无权限的品种
- reportId, _ := this.GetInt("ReportId", 0)
- videoId, _ := this.GetInt("VideoId", 0)
- limitIds := make([]int, 0)
- if reportId > 0 {
- ps, e := models.GetEnPermissionIdsByEnglishReportId(reportId)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取报告权限品种失败, Err: " + e.Error()
- return
- }
- limitIds = ps
- }
- if videoId > 0 {
- ps, e := models.GetEnPermissionIdsByEnglishVideoId(videoId)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取视频权限品种失败, Err: " + e.Error()
- return
- }
- limitIds = ps
- }
- cond := ``
- pars := make([]interface{}, 0)
- if keyword != "" {
- k := fmt.Sprint("%", keyword, "%")
- cond += fmt.Sprintf(` AND %s LIKE ?`, models.EnPermissionColumns.EnPermissionName)
- pars = append(pars, k)
- }
- list, e := models.GetEnPermissionUnionList(cond, pars)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取品种Union列表失败, Err: " + e.Error()
- return
- }
- // 品种树
- resp := make([]*models.EnPermissionItem, 0)
- childMap := make(map[int][]*models.EnPermissionItem)
- for _, v := range list {
- t := new(models.EnPermissionItem)
- t.PermissionId = v.EnPermissionId
- t.PermissionName = v.EnPermissionName
- t.CnPermissionName = v.CnPermissionName
- t.Enabled = v.Enabled
- t.ParentId = v.ParentId
- t.Sort = v.Sort
- t.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
- t.Child = make([]*models.EnPermissionItem, 0)
- if v.ParentId == 0 {
- resp = append(resp, t)
- continue
- }
- if v.ParentId > 0 {
- if v.Enabled == 0 && enabled == 1 {
- continue
- }
- if childMap[v.ParentId] == nil {
- childMap[v.ParentId] = make([]*models.EnPermissionItem, 0)
- }
- // 无权限则隐藏
- if (reportId > 0 || videoId > 0) && !utils.InArrayByInt(limitIds, t.PermissionId) {
- continue
- }
- childMap[v.ParentId] = append(childMap[v.ParentId], t)
- }
- }
- for _, r := range resp {
- r.Child = childMap[r.PermissionId]
- }
- // todo 过滤禁用品种的一级分类,如果一级分类没有二级分类
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Remove
- // @Title 删除品种权限
- // @Description 删除品种权限
- // @Param request body models.EnPermissionRemoveReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /remove [post]
- func (this *EnPermissionController) Remove() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnPermissionRemoveReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if req.PermissionId <= 0 {
- br.Msg = "参数有误"
- return
- }
- ob := new(models.EnPermission)
- item, e := ob.GetItemById(req.PermissionId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "品种不存在, 请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取品种权限失败, Err: " + e.Error()
- return
- }
- // 校验是否有子品种
- childCond := fmt.Sprintf(` AND %s = ?`, models.EnPermissionColumns.ParentId)
- childPars := make([]interface{}, 0)
- childPars = append(childPars, req.PermissionId)
- num, e := ob.GetCountByCondition(childCond, childPars)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取品种子权限数量失败, Err: " + e.Error()
- return
- }
- if num > 0 {
- br.Msg = "请先删除该品种下关联品种"
- return
- }
- // 校验是否关联客户
- {
- cond := fmt.Sprintf(` AND %s = ?`, models.EnCompanyPermissionColumns.EnPermissionId)
- pars := make([]interface{}, 0)
- pars = append(pars, item.EnPermissionId)
- ob := new(models.EnCompanyPermission)
- num, e := ob.GetCountByCondition(cond, pars)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取品种关联客户数量失败, Err: " + e.Error()
- return
- }
- if num > 0 {
- br.Msg = "该品种有关联客户, 不允许删除"
- return
- }
- }
- // 删除品种
- if e = item.Del(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "删除品种失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // Move
- // @Title 移动品种权限
- // @Description 移动品种权限
- // @Param request body models.EnPermissionMoveReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /move [post]
- func (this *EnPermissionController) Move() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnPermissionMoveReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if req.PermissionId <= 0 {
- br.Msg = "参数有误"
- return
- }
- e, msg := services.MoveEnPermission(req)
- if e != nil {
- br.Msg = msg
- br.ErrMsg = "移动品种失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // SetEnabled
- // @Title 启用/禁用品种
- // @Description 启用/禁用品种
- // @Param request body models.EnPermissionEnabledReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /enabled/set [post]
- func (this *EnPermissionController) SetEnabled() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnPermissionEnabledReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if req.PermissionId <= 0 {
- br.Msg = "参数有误"
- return
- }
- // 重名校验
- ob := new(models.EnPermission)
- item, e := ob.GetItemById(req.PermissionId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "品种不存在, 请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取品种权限失败, Err: " + e.Error()
- return
- }
- item.Enabled = req.Enabled
- item.ModifyTime = time.Now().Local()
- if e = item.Update([]string{"Enabled", "ModifyTime"}); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新品种权限失败, Err: " + e.Error()
- return
- }
- // 如果是一级品种被启用,则所有二级设置成启用, 如果一级品种被禁用,则所有二级设置成禁用
- if item.ParentId == 0 {
- if e = item.SetEnabled(item.EnPermissionId, req.Enabled); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新英文分类权限失败, Err: " + e.Error()
- return
- }
- } else if item.ParentId > 0 && req.Enabled == 1 {
- // 如果二级品种被启用,则他的上级品种设置成启用
- if e = item.SetEnabledByPermissionId(item.ParentId, req.Enabled); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新英文分类权限失败, Err: " + e.Error()
- return
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- br.Data = item.EnPermissionId
- }
|