report_author.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/utils"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "strings"
  8. "time"
  9. )
  10. // ReportAuthorController 报告作者
  11. type ReportAuthorController struct {
  12. BaseAuthController
  13. }
  14. // ReportAuthorCommonController 报告作者
  15. type ReportAuthorCommonController struct {
  16. BaseCommonController
  17. }
  18. // Author
  19. // @Title 获取报告作者接口
  20. // @Description 获取报告作者
  21. // @Param AuthorType query int true "来源类型,1:中文,2:英文"
  22. // @Param Keyword query string true "搜索关键词"
  23. // @Param CurrentIndex query int true "当前页页码,从1开始"
  24. // @Param StartDate query string true "开始时间"
  25. // @Success 200 {object} models.ReportAuthorResp
  26. // @router /author [get]
  27. func (this *ReportAuthorController) Author() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. //来源类型,1:中文,2:英文
  34. authorType := this.GetString("AuthorType", "1")
  35. pageSize, _ := this.GetInt("PageSize")
  36. currentIndex, _ := this.GetInt("CurrentIndex")
  37. keyword := this.GetString("Keyword")
  38. var startSize int
  39. if pageSize <= 0 {
  40. pageSize = utils.PageSize50
  41. }
  42. if currentIndex <= 0 {
  43. currentIndex = 1
  44. }
  45. startSize = utils.StartIndex(currentIndex, pageSize)
  46. var condition string
  47. var pars []interface{}
  48. condition += ` AND author_type = ? `
  49. pars = append(pars, authorType)
  50. if keyword != `` {
  51. condition += ` AND report_author like ? `
  52. pars = append(pars, utils.GetLikeKeyword(keyword))
  53. }
  54. total, items, err := models.GetReportAuthorList(condition, pars, startSize, pageSize)
  55. if err != nil {
  56. br.Msg = "获取失败!"
  57. br.ErrMsg = "获取失败,Err:" + err.Error()
  58. return
  59. }
  60. page := paging.GetPaging(currentIndex, pageSize, total)
  61. resp := models.ReportAuthorResp{
  62. List: items,
  63. Paging: page,
  64. }
  65. br.Ret = 200
  66. br.Success = true
  67. br.Msg = "获取成功"
  68. br.Data = resp
  69. }
  70. // AddAuthor
  71. // @Title 新增报告作者接口
  72. // @Description 新增报告作者接口
  73. // @Param request body models.AddReportAuthorReq true "type json string"
  74. // @Success 200 Ret=200 添加成功
  75. // @router /author/add [post]
  76. func (this *ReportAuthorController) AddAuthor() {
  77. br := new(models.BaseResponse).Init()
  78. defer func() {
  79. this.Data["json"] = br
  80. this.ServeJSON()
  81. }()
  82. var req models.AddReportAuthorReq
  83. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  84. if err != nil {
  85. br.Msg = "参数解析异常!"
  86. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  87. return
  88. }
  89. authorName := strings.TrimSuffix(req.Author, " ")
  90. authorName = strings.TrimPrefix(authorName, " ")
  91. if authorName == `` {
  92. br.Msg = "请输入名称"
  93. br.ErrMsg = "请输入名称"
  94. br.IsSendEmail = false
  95. return
  96. }
  97. if req.AuthorType != 1 && req.AuthorType != 2 {
  98. br.Msg = "请选择类型"
  99. br.ErrMsg = "请选择类型"
  100. br.IsSendEmail = false
  101. return
  102. }
  103. item, err := models.GetReportAuthorByAuthor(authorName, req.AuthorType)
  104. if err != nil && !utils.IsErrNoRow(err) {
  105. br.Msg = "获取数据异常!"
  106. br.ErrMsg = "获取数据异常,Err:" + err.Error()
  107. return
  108. }
  109. if item != nil && item.Id > 0 {
  110. br.Msg = "已存在该作者"
  111. br.ErrMsg = "已存在该作者"
  112. br.IsSendEmail = false
  113. return
  114. }
  115. item = &models.ReportAuthor{
  116. Id: 0,
  117. ReportAuthor: req.Author,
  118. AuthorType: req.AuthorType,
  119. Enable: 1,
  120. IsDelete: 0,
  121. CreateTime: time.Now(),
  122. ModifyTime: time.Now(),
  123. }
  124. authorId, err := models.AddReportAuthor(item)
  125. if err != nil {
  126. br.Msg = "添加作者失败!"
  127. br.ErrMsg = "添加作者失败,Err:" + err.Error()
  128. return
  129. }
  130. item.Id = int(authorId)
  131. br.Ret = 200
  132. br.Success = true
  133. br.IsAddLog = true
  134. br.Msg = "添加成功"
  135. //br.Data = resp
  136. }
  137. // EditAuthor
  138. // @Title 编辑报告作者接口
  139. // @Description 编辑报告作者接口
  140. // @Param request body models.AddReportAuthorReq true "type json string"
  141. // @Success 200 Ret=200 编辑成功
  142. // @router /author/edit [post]
  143. func (this *ReportAuthorController) EditAuthor() {
  144. br := new(models.BaseResponse).Init()
  145. defer func() {
  146. this.Data["json"] = br
  147. this.ServeJSON()
  148. }()
  149. var req models.AddReportAuthorReq
  150. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  151. if err != nil {
  152. br.Msg = "参数解析异常!"
  153. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  154. return
  155. }
  156. authorName := strings.TrimSuffix(req.Author, " ")
  157. authorName = strings.TrimPrefix(authorName, " ")
  158. otherItem, err := models.GetReportAuthorByAuthorAndId(authorName, req.AuthorType, req.Id)
  159. if err != nil && !utils.IsErrNoRow(err) {
  160. br.Msg = "获取数据异常!"
  161. br.ErrMsg = "获取数据异常,Err:" + err.Error()
  162. return
  163. }
  164. if otherItem != nil && otherItem.Id > 0 {
  165. br.Msg = "已存在该作者名称,请重新输入"
  166. br.ErrMsg = "已存在该作者名称,请重新输入"
  167. br.IsSendEmail = false
  168. return
  169. }
  170. item, err := models.GetReportAuthorById(req.Id)
  171. if err != nil && !utils.IsErrNoRow(err) {
  172. br.Msg = "获取数据异常!"
  173. br.ErrMsg = "获取数据异常,Err:" + err.Error()
  174. return
  175. }
  176. if item == nil {
  177. br.Msg = "不存在该作者"
  178. br.ErrMsg = "不存在该作者"
  179. br.IsSendEmail = false
  180. return
  181. }
  182. //原名称
  183. oldAuthorName := item.ReportAuthor
  184. item.ReportAuthor = authorName
  185. item.ModifyTime = time.Now()
  186. err = item.Update([]string{"ReportAuthor", "ModifyTime"})
  187. if err != nil {
  188. br.Msg = "编辑作者失败!"
  189. br.ErrMsg = "编辑作者失败,Err:" + err.Error()
  190. return
  191. }
  192. // 更改原有报告中的名称
  193. {
  194. var condition string
  195. var pars []interface{}
  196. var count int
  197. condition = " AND author = ? "
  198. pars = append(pars, oldAuthorName)
  199. count, err = models.ModifyReportAuthor(condition, pars, authorName)
  200. if err != nil && !utils.IsErrNoRow(err) {
  201. br.Msg = "获取数据异常!"
  202. br.ErrMsg = "获取是否存在该作者的报告数据异常,Err:" + err.Error()
  203. return
  204. }
  205. if count > 0 {
  206. br.Msg = "该作者名称有关联报告,不可删除"
  207. br.ErrMsg = "该作者名称有关联报告,不可删除"
  208. br.IsSendEmail = false
  209. return
  210. }
  211. }
  212. br.Ret = 200
  213. br.Success = true
  214. br.IsAddLog = true
  215. br.Msg = "编辑成功"
  216. }
  217. // EnableAuthor
  218. // @Title 禁用/启用报告作者接口
  219. // @Description 禁用/启用报告作者接口
  220. // @Param request body models.EnableReportAuthorReq true "type json string"
  221. // @Success 200 Ret=200 启用成功
  222. // @router /author/enable [post]
  223. func (this *ReportAuthorController) EnableAuthor() {
  224. br := new(models.BaseResponse).Init()
  225. defer func() {
  226. this.Data["json"] = br
  227. this.ServeJSON()
  228. }()
  229. var req models.EnableReportAuthorReq
  230. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  231. if err != nil {
  232. br.Msg = "参数解析异常!"
  233. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  234. return
  235. }
  236. item, err := models.GetReportAuthorById(req.Id)
  237. if err != nil && !utils.IsErrNoRow(err) {
  238. br.Msg = "获取数据异常!"
  239. br.ErrMsg = "获取数据异常,Err:" + err.Error()
  240. return
  241. }
  242. if item == nil {
  243. br.Msg = "不存在该作者"
  244. br.ErrMsg = "不存在该作者"
  245. br.IsSendEmail = false
  246. return
  247. }
  248. // 剩余作者数
  249. {
  250. var condition string
  251. var pars []interface{}
  252. condition = " AND author_type = ?"
  253. pars = append(pars, item.AuthorType)
  254. total, err := models.GetReportAuthorCount(condition, pars)
  255. if err != nil && !utils.IsErrNoRow(err) {
  256. br.Msg = "获取数据异常!"
  257. br.ErrMsg = "获取剩余作者数据异常,Err:" + err.Error()
  258. return
  259. }
  260. if total <= 1 {
  261. br.Msg = "该作者名称为最后一个,不可禁用"
  262. br.ErrMsg = "该作者名称为最后一个,不可禁用"
  263. br.IsSendEmail = false
  264. return
  265. }
  266. }
  267. item.Enable = req.EnableType
  268. item.ModifyTime = time.Now()
  269. err = item.Update([]string{"Enable", "ModifyTime"})
  270. if err != nil {
  271. br.Msg = "操作失败!"
  272. br.ErrMsg = "操作失败,Err:" + err.Error()
  273. return
  274. }
  275. br.Ret = 200
  276. br.Success = true
  277. br.IsAddLog = true
  278. br.Msg = "编辑成功"
  279. }
  280. // DeleteAuthor
  281. // @Title 删除报告作者接口
  282. // @Description 删除报告作者接口
  283. // @Param request body models.AddReportAuthorReq true "type json string"
  284. // @Success 200 Ret=200 启用成功
  285. // @router /author/delete [post]
  286. func (this *ReportAuthorController) DeleteAuthor() {
  287. br := new(models.BaseResponse).Init()
  288. defer func() {
  289. this.Data["json"] = br
  290. this.ServeJSON()
  291. }()
  292. var req models.DeleteReportAuthorReq
  293. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  294. if err != nil {
  295. br.Msg = "参数解析异常!"
  296. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  297. return
  298. }
  299. item, err := models.GetReportAuthorById(req.Id)
  300. if err != nil && !utils.IsErrNoRow(err) {
  301. br.Msg = "获取数据异常!"
  302. br.ErrMsg = "获取数据异常,Err:" + err.Error()
  303. return
  304. }
  305. if item == nil {
  306. br.Msg = "不存在该作者"
  307. br.ErrMsg = "不存在该作者"
  308. br.IsSendEmail = false
  309. return
  310. }
  311. // 剩余作者数
  312. {
  313. var condition string
  314. var pars []interface{}
  315. condition = " AND author_type = ?"
  316. pars = append(pars, item.AuthorType)
  317. total, err := models.GetReportAuthorCount(condition, pars)
  318. if err != nil && !utils.IsErrNoRow(err) {
  319. br.Msg = "获取数据异常!"
  320. br.ErrMsg = "获取剩余作者数据异常,Err:" + err.Error()
  321. return
  322. }
  323. if total <= 1 {
  324. br.Msg = "该作者名称为最后一个,不可删除"
  325. br.ErrMsg = "该作者名称为最后一个,不可删除"
  326. br.IsSendEmail = false
  327. return
  328. }
  329. }
  330. // 获取是否存在该作者的报告
  331. {
  332. var condition string
  333. var pars []interface{}
  334. var count int
  335. condition = " AND author = ? "
  336. pars = append(pars, item.ReportAuthor)
  337. count, err = models.GetReportListCount(condition, pars)
  338. if err != nil && !utils.IsErrNoRow(err) {
  339. br.Msg = "获取数据异常!"
  340. br.ErrMsg = "获取是否存在该作者的报告数据异常,Err:" + err.Error()
  341. return
  342. }
  343. if count > 0 {
  344. br.Msg = "该作者名称有关联报告,不可删除"
  345. br.ErrMsg = "该作者名称有关联报告,不可删除"
  346. br.IsSendEmail = false
  347. return
  348. }
  349. }
  350. item.IsDelete = 1
  351. item.ModifyTime = time.Now()
  352. err = item.Update([]string{"IsDelete", "ModifyTime"})
  353. if err != nil {
  354. br.Msg = "操作失败!"
  355. br.ErrMsg = "操作失败,Err:" + err.Error()
  356. return
  357. }
  358. br.Ret = 200
  359. br.Success = true
  360. br.IsAddLog = true
  361. br.Msg = "删除成功"
  362. }