report_author.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/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 && err.Error() != utils.ErrNoRow() {
  105. br.Msg = "获取数据异常!"
  106. br.ErrMsg = "获取数据异常,Err:" + err.Error()
  107. return
  108. }
  109. if item != nil {
  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 && err.Error() != utils.ErrNoRow() {
  160. br.Msg = "获取数据异常!"
  161. br.ErrMsg = "获取数据异常,Err:" + err.Error()
  162. return
  163. }
  164. if otherItem != nil {
  165. br.Msg = "已存在该作者名称,请重新输入"
  166. br.ErrMsg = "已存在该作者名称,请重新输入"
  167. br.IsSendEmail = false
  168. return
  169. }
  170. item, err := models.GetReportAuthorById(req.Id)
  171. if err != nil && err.Error() != utils.ErrNoRow() {
  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. if item.AuthorType == 1 {
  200. count, err = models.ModifyReportAuthor(condition, pars, authorName)
  201. } else {
  202. count, err = models.ModifyEnglishReportAuthor(condition, pars, authorName)
  203. }
  204. if err != nil && err.Error() != utils.ErrNoRow() {
  205. br.Msg = "获取数据异常!"
  206. br.ErrMsg = "获取是否存在该作者的报告数据异常,Err:" + err.Error()
  207. return
  208. }
  209. if count > 0 {
  210. br.Msg = "该作者名称有关联报告,不可删除"
  211. br.ErrMsg = "该作者名称有关联报告,不可删除"
  212. br.IsSendEmail = false
  213. return
  214. }
  215. }
  216. br.Ret = 200
  217. br.Success = true
  218. br.IsAddLog = true
  219. br.Msg = "编辑成功"
  220. }
  221. // EnableAuthor
  222. // @Title 禁用/启用报告作者接口
  223. // @Description 禁用/启用报告作者接口
  224. // @Param request body models.EnableReportAuthorReq true "type json string"
  225. // @Success 200 Ret=200 启用成功
  226. // @router /author/enable [post]
  227. func (this *ReportAuthorController) EnableAuthor() {
  228. br := new(models.BaseResponse).Init()
  229. defer func() {
  230. this.Data["json"] = br
  231. this.ServeJSON()
  232. }()
  233. var req models.EnableReportAuthorReq
  234. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  235. if err != nil {
  236. br.Msg = "参数解析异常!"
  237. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  238. return
  239. }
  240. item, err := models.GetReportAuthorById(req.Id)
  241. if err != nil && err.Error() != utils.ErrNoRow() {
  242. br.Msg = "获取数据异常!"
  243. br.ErrMsg = "获取数据异常,Err:" + err.Error()
  244. return
  245. }
  246. if item == nil {
  247. br.Msg = "不存在该作者"
  248. br.ErrMsg = "不存在该作者"
  249. br.IsSendEmail = false
  250. return
  251. }
  252. // 剩余作者数
  253. {
  254. var condition string
  255. var pars []interface{}
  256. condition = " AND author_type = ?"
  257. pars = append(pars, item.AuthorType)
  258. total, err := models.GetReportAuthorCount(condition, pars)
  259. if err != nil && err.Error() != utils.ErrNoRow() {
  260. br.Msg = "获取数据异常!"
  261. br.ErrMsg = "获取剩余作者数据异常,Err:" + err.Error()
  262. return
  263. }
  264. if total <= 1 {
  265. br.Msg = "该作者名称为最后一个,不可禁用"
  266. br.ErrMsg = "该作者名称为最后一个,不可禁用"
  267. br.IsSendEmail = false
  268. return
  269. }
  270. }
  271. item.Enable = req.EnableType
  272. item.ModifyTime = time.Now()
  273. err = item.Update([]string{"Enable", "ModifyTime"})
  274. if err != nil {
  275. br.Msg = "操作失败!"
  276. br.ErrMsg = "操作失败,Err:" + err.Error()
  277. return
  278. }
  279. br.Ret = 200
  280. br.Success = true
  281. br.IsAddLog = true
  282. br.Msg = "编辑成功"
  283. }
  284. // DeleteAuthor
  285. // @Title 删除报告作者接口
  286. // @Description 删除报告作者接口
  287. // @Param request body models.AddReportAuthorReq true "type json string"
  288. // @Success 200 Ret=200 启用成功
  289. // @router /author/delete [post]
  290. func (this *ReportAuthorController) DeleteAuthor() {
  291. br := new(models.BaseResponse).Init()
  292. defer func() {
  293. this.Data["json"] = br
  294. this.ServeJSON()
  295. }()
  296. var req models.DeleteReportAuthorReq
  297. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  298. if err != nil {
  299. br.Msg = "参数解析异常!"
  300. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  301. return
  302. }
  303. item, err := models.GetReportAuthorById(req.Id)
  304. if err != nil && err.Error() != utils.ErrNoRow() {
  305. br.Msg = "获取数据异常!"
  306. br.ErrMsg = "获取数据异常,Err:" + err.Error()
  307. return
  308. }
  309. if item == nil {
  310. br.Msg = "不存在该作者"
  311. br.ErrMsg = "不存在该作者"
  312. br.IsSendEmail = false
  313. return
  314. }
  315. // 剩余作者数
  316. {
  317. var condition string
  318. var pars []interface{}
  319. condition = " AND author_type = ?"
  320. pars = append(pars, item.AuthorType)
  321. total, err := models.GetReportAuthorCount(condition, pars)
  322. if err != nil && err.Error() != utils.ErrNoRow() {
  323. br.Msg = "获取数据异常!"
  324. br.ErrMsg = "获取剩余作者数据异常,Err:" + err.Error()
  325. return
  326. }
  327. if total <= 1 {
  328. br.Msg = "该作者名称为最后一个,不可删除"
  329. br.ErrMsg = "该作者名称为最后一个,不可删除"
  330. br.IsSendEmail = false
  331. return
  332. }
  333. }
  334. // 获取是否存在该作者的报告
  335. {
  336. var condition string
  337. var pars []interface{}
  338. var count int
  339. condition = " AND author = ? "
  340. pars = append(pars, item.ReportAuthor)
  341. if item.AuthorType == 1 {
  342. count, err = models.GetReportListCount(condition, pars)
  343. } else {
  344. count, err = models.GetEnglishReportListCount(condition, pars, "")
  345. }
  346. if err != nil && err.Error() != utils.ErrNoRow() {
  347. br.Msg = "获取数据异常!"
  348. br.ErrMsg = "获取是否存在该作者的报告数据异常,Err:" + err.Error()
  349. return
  350. }
  351. if count > 0 {
  352. br.Msg = "该作者名称有关联报告,不可删除"
  353. br.ErrMsg = "该作者名称有关联报告,不可删除"
  354. br.IsSendEmail = false
  355. return
  356. }
  357. }
  358. item.IsDelete = 1
  359. item.ModifyTime = time.Now()
  360. err = item.Update([]string{"IsDelete", "ModifyTime"})
  361. if err != nil {
  362. br.Msg = "操作失败!"
  363. br.ErrMsg = "操作失败,Err:" + err.Error()
  364. return
  365. }
  366. br.Ret = 200
  367. br.Success = true
  368. br.IsAddLog = true
  369. br.Msg = "删除成功"
  370. }