123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- package controllers
- import (
- "encoding/json"
- "eta_gn/eta_api/models"
- "eta_gn/eta_api/utils"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strings"
- "time"
- )
- // ReportAuthorController 报告作者
- type ReportAuthorController struct {
- BaseAuthController
- }
- // ReportAuthorCommonController 报告作者
- type ReportAuthorCommonController struct {
- BaseCommonController
- }
- // Author
- // @Title 获取报告作者接口
- // @Description 获取报告作者
- // @Param AuthorType query int true "来源类型,1:中文,2:英文"
- // @Param Keyword query string true "搜索关键词"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param StartDate query string true "开始时间"
- // @Success 200 {object} models.ReportAuthorResp
- // @router /author [get]
- func (this *ReportAuthorController) Author() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- //来源类型,1:中文,2:英文
- authorType := this.GetString("AuthorType", "1")
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- keyword := this.GetString("Keyword")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize50
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- var condition string
- var pars []interface{}
- condition += ` AND author_type = ? `
- pars = append(pars, authorType)
- if keyword != `` {
- condition += ` AND report_author like ? `
- pars = append(pars, utils.GetLikeKeyword(keyword))
- }
- total, items, err := models.GetReportAuthorList(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败!"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := models.ReportAuthorResp{
- List: items,
- Paging: page,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // AddAuthor
- // @Title 新增报告作者接口
- // @Description 新增报告作者接口
- // @Param request body models.AddReportAuthorReq true "type json string"
- // @Success 200 Ret=200 添加成功
- // @router /author/add [post]
- func (this *ReportAuthorController) AddAuthor() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.AddReportAuthorReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- authorName := strings.TrimSuffix(req.Author, " ")
- authorName = strings.TrimPrefix(authorName, " ")
- if authorName == `` {
- br.Msg = "请输入名称"
- br.ErrMsg = "请输入名称"
- br.IsSendEmail = false
- return
- }
- if req.AuthorType != 1 && req.AuthorType != 2 {
- br.Msg = "请选择类型"
- br.ErrMsg = "请选择类型"
- br.IsSendEmail = false
- return
- }
- item, err := models.GetReportAuthorByAuthor(authorName, req.AuthorType)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取数据异常!"
- br.ErrMsg = "获取数据异常,Err:" + err.Error()
- return
- }
- if item != nil {
- br.Msg = "已存在该作者"
- br.ErrMsg = "已存在该作者"
- br.IsSendEmail = false
- return
- }
- item = &models.ReportAuthor{
- Id: 0,
- ReportAuthor: req.Author,
- AuthorType: req.AuthorType,
- Enable: 1,
- IsDelete: 0,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- authorId, err := models.AddReportAuthor(item)
- if err != nil {
- br.Msg = "添加作者失败!"
- br.ErrMsg = "添加作者失败,Err:" + err.Error()
- return
- }
- item.Id = int(authorId)
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "添加成功"
- //br.Data = resp
- }
- // EditAuthor
- // @Title 编辑报告作者接口
- // @Description 编辑报告作者接口
- // @Param request body models.AddReportAuthorReq true "type json string"
- // @Success 200 Ret=200 编辑成功
- // @router /author/edit [post]
- func (this *ReportAuthorController) EditAuthor() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.AddReportAuthorReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- authorName := strings.TrimSuffix(req.Author, " ")
- authorName = strings.TrimPrefix(authorName, " ")
- otherItem, err := models.GetReportAuthorByAuthorAndId(authorName, req.AuthorType, req.Id)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取数据异常!"
- br.ErrMsg = "获取数据异常,Err:" + err.Error()
- return
- }
- if otherItem != nil {
- br.Msg = "已存在该作者名称,请重新输入"
- br.ErrMsg = "已存在该作者名称,请重新输入"
- br.IsSendEmail = false
- return
- }
- item, err := models.GetReportAuthorById(req.Id)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取数据异常!"
- br.ErrMsg = "获取数据异常,Err:" + err.Error()
- return
- }
- if item == nil {
- br.Msg = "不存在该作者"
- br.ErrMsg = "不存在该作者"
- br.IsSendEmail = false
- return
- }
- //原名称
- oldAuthorName := item.ReportAuthor
- item.ReportAuthor = authorName
- item.ModifyTime = time.Now()
- err = item.Update([]string{"ReportAuthor", "ModifyTime"})
- if err != nil {
- br.Msg = "编辑作者失败!"
- br.ErrMsg = "编辑作者失败,Err:" + err.Error()
- return
- }
- // 更改原有报告中的名称
- {
- var condition string
- var pars []interface{}
- var count int
- condition = " AND author = ? "
- pars = append(pars, oldAuthorName)
- if item.AuthorType == 1 {
- count, err = models.ModifyReportAuthor(condition, pars, authorName)
- } else {
- count, err = models.ModifyEnglishReportAuthor(condition, pars, authorName)
- }
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取数据异常!"
- br.ErrMsg = "获取是否存在该作者的报告数据异常,Err:" + err.Error()
- return
- }
- if count > 0 {
- br.Msg = "该作者名称有关联报告,不可删除"
- br.ErrMsg = "该作者名称有关联报告,不可删除"
- br.IsSendEmail = false
- return
- }
- }
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "编辑成功"
- }
- // EnableAuthor
- // @Title 禁用/启用报告作者接口
- // @Description 禁用/启用报告作者接口
- // @Param request body models.EnableReportAuthorReq true "type json string"
- // @Success 200 Ret=200 启用成功
- // @router /author/enable [post]
- func (this *ReportAuthorController) EnableAuthor() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.EnableReportAuthorReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- item, err := models.GetReportAuthorById(req.Id)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取数据异常!"
- br.ErrMsg = "获取数据异常,Err:" + err.Error()
- return
- }
- if item == nil {
- br.Msg = "不存在该作者"
- br.ErrMsg = "不存在该作者"
- br.IsSendEmail = false
- return
- }
- // 剩余作者数
- {
- var condition string
- var pars []interface{}
- condition = " AND author_type = ?"
- pars = append(pars, item.AuthorType)
- total, err := models.GetReportAuthorCount(condition, pars)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取数据异常!"
- br.ErrMsg = "获取剩余作者数据异常,Err:" + err.Error()
- return
- }
- if total <= 1 {
- br.Msg = "该作者名称为最后一个,不可禁用"
- br.ErrMsg = "该作者名称为最后一个,不可禁用"
- br.IsSendEmail = false
- return
- }
- }
- item.Enable = req.EnableType
- item.ModifyTime = time.Now()
- err = item.Update([]string{"Enable", "ModifyTime"})
- if err != nil {
- br.Msg = "操作失败!"
- br.ErrMsg = "操作失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "编辑成功"
- }
- // DeleteAuthor
- // @Title 删除报告作者接口
- // @Description 删除报告作者接口
- // @Param request body models.AddReportAuthorReq true "type json string"
- // @Success 200 Ret=200 启用成功
- // @router /author/delete [post]
- func (this *ReportAuthorController) DeleteAuthor() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.DeleteReportAuthorReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- item, err := models.GetReportAuthorById(req.Id)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取数据异常!"
- br.ErrMsg = "获取数据异常,Err:" + err.Error()
- return
- }
- if item == nil {
- br.Msg = "不存在该作者"
- br.ErrMsg = "不存在该作者"
- br.IsSendEmail = false
- return
- }
- // 剩余作者数
- {
- var condition string
- var pars []interface{}
- condition = " AND author_type = ?"
- pars = append(pars, item.AuthorType)
- total, err := models.GetReportAuthorCount(condition, pars)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取数据异常!"
- br.ErrMsg = "获取剩余作者数据异常,Err:" + err.Error()
- return
- }
- if total <= 1 {
- br.Msg = "该作者名称为最后一个,不可删除"
- br.ErrMsg = "该作者名称为最后一个,不可删除"
- br.IsSendEmail = false
- return
- }
- }
- // 获取是否存在该作者的报告
- {
- var condition string
- var pars []interface{}
- var count int
- condition = " AND author = ? "
- pars = append(pars, item.ReportAuthor)
- if item.AuthorType == 1 {
- count, err = models.GetReportListCount(condition, pars)
- } else {
- count, err = models.GetEnglishReportListCount(condition, pars, "")
- }
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取数据异常!"
- br.ErrMsg = "获取是否存在该作者的报告数据异常,Err:" + err.Error()
- return
- }
- if count > 0 {
- br.Msg = "该作者名称有关联报告,不可删除"
- br.ErrMsg = "该作者名称有关联报告,不可删除"
- br.IsSendEmail = false
- return
- }
- }
- item.IsDelete = 1
- item.ModifyTime = time.Now()
- err = item.Update([]string{"IsDelete", "ModifyTime"})
- if err != nil {
- br.Msg = "操作失败!"
- br.ErrMsg = "操作失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "删除成功"
- }
|