english_report.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_hub/models"
  5. "eta/eta_hub/services"
  6. "eta/eta_hub/utils"
  7. "fmt"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "sort"
  10. "strings"
  11. "time"
  12. )
  13. // EnglishReportController 英文研报
  14. type EnglishReportController struct {
  15. BaseAuthController
  16. }
  17. // List
  18. // @Title 报告列表
  19. // @Description 报告列表
  20. // @Param PageSize query int true "每页数据条数"
  21. // @Param CurrentIndex query int true "当前页页码,从1开始"
  22. // @Param Keyword query string false "搜索关键词"
  23. // @Param State query int false "状态"
  24. // @Success 200 {object} models.EnglishReportListResp
  25. // @router /list [get]
  26. func (this *EnglishReportController) List() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. pageSize, _ := this.GetInt("PageSize")
  33. currentIndex, _ := this.GetInt("CurrentIndex")
  34. var startSize int
  35. if pageSize <= 0 {
  36. pageSize = utils.PageSize20
  37. }
  38. if currentIndex <= 0 {
  39. currentIndex = 1
  40. }
  41. startSize = utils.StartIndex(currentIndex, pageSize)
  42. var condition string
  43. var pars []interface{}
  44. keyword := this.GetString("Keyword")
  45. keyword = strings.TrimSpace(keyword)
  46. if keyword != "" {
  47. kw := fmt.Sprint("%", keyword, "%")
  48. condition += ` AND (title LIKE ? OR author LIKE ? OR admin_real_name LIKE ?) `
  49. pars = append(pars, kw, kw, kw)
  50. }
  51. state, _ := this.GetInt("State")
  52. if state > 0 {
  53. condition += ` AND state = ? `
  54. pars = append(pars, state)
  55. }
  56. admindId, _ := this.GetInt("AdminId")
  57. if admindId > 0 {
  58. condition += ` AND admin_id = ? `
  59. pars = append(pars, admindId)
  60. }
  61. classifyId, _ := this.GetInt("ClassifyId")
  62. if classifyId > 0 {
  63. condition += ` AND (classify_id_first = ? OR classify_id_second = ?) `
  64. pars = append(pars, classifyId, classifyId)
  65. }
  66. reportOb := new(models.EnglishReport)
  67. total, e := reportOb.GetCountByCondition(condition, pars)
  68. if e != nil {
  69. br.Msg = "获取失败"
  70. br.ErrMsg = "获取英文报告列表总数失败, Err: " + e.Error()
  71. return
  72. }
  73. respList := make([]*models.EnglishReportItem, 0)
  74. list, e := reportOb.GetPageItemsByCondition(condition, pars, []string{}, "", startSize, pageSize)
  75. if e != nil {
  76. br.Msg = "获取失败"
  77. br.ErrMsg = "获取英文报告列表失败, Err: " + e.Error()
  78. return
  79. }
  80. for _, v := range list {
  81. t := models.FormatEnglishReport2Item(v)
  82. respList = append(respList, t)
  83. }
  84. page := paging.GetPaging(currentIndex, pageSize, total)
  85. resp := new(models.EnglishReportListResp)
  86. resp.Paging = page
  87. resp.List = respList
  88. br.Ret = 200
  89. br.Msg = "获取成功"
  90. br.Data = resp
  91. }
  92. // Detail
  93. // @Title 报告详情
  94. // @Description 报告详情
  95. // @Param ReportId query int true "报告ID"
  96. // @Success 200 {object} models.EnglishReportItem
  97. // @router /detail [get]
  98. func (this *EnglishReportController) Detail() {
  99. br := new(models.BaseResponse).Init()
  100. defer func() {
  101. this.Data["json"] = br
  102. this.ServeJSON()
  103. }()
  104. reportId, err := this.GetInt("ReportId")
  105. if err != nil {
  106. br.Msg = "获取参数失败!"
  107. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  108. return
  109. }
  110. if reportId <= 0 {
  111. br.Msg = "参数错误"
  112. return
  113. }
  114. reportOb := new(models.EnglishReport)
  115. item, e := reportOb.GetItemById(reportId)
  116. if e != nil {
  117. if e.Error() == utils.ErrNoRow() {
  118. br.Msg = "报告已被删除"
  119. return
  120. }
  121. br.Msg = "获取失败"
  122. br.ErrMsg = "获取失败,Err:" + e.Error()
  123. return
  124. }
  125. detail := models.FormatEnglishReport2Item(item)
  126. br.Data = detail
  127. br.Ret = 200
  128. br.Msg = "获取成功"
  129. }
  130. // Approve
  131. // @Title 审批
  132. // @Description 审批报告
  133. // @Param request body models.EnglishReportApproveReq true "type json string"
  134. // @Success 200 string "操作成功"
  135. // @router /approve [post]
  136. func (this *EnglishReportController) Approve() {
  137. br := new(models.BaseResponse).Init()
  138. defer func() {
  139. this.Data["json"] = br
  140. this.ServeJSON()
  141. }()
  142. var req models.EnglishReportApproveReq
  143. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  144. if err != nil {
  145. br.Msg = "参数解析异常!"
  146. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  147. return
  148. }
  149. if req.ReportId <= 0 {
  150. br.Msg = "参数错误"
  151. br.ErrMsg = fmt.Sprintf("参数有误, EnglishReportId: %d", req.ReportId)
  152. return
  153. }
  154. ob := new(models.EnglishReport)
  155. item, e := ob.GetItemById(req.ReportId)
  156. if e != nil {
  157. if e.Error() == utils.ErrNoRow() {
  158. br.Msg = "报告已被删除"
  159. return
  160. }
  161. br.Msg = "操作失败"
  162. br.ErrMsg = "获取英文研报失败, Err: " + e.Error()
  163. return
  164. }
  165. if item.State != models.ReportStateWaitApprove {
  166. br.Msg = "报告状态有误"
  167. br.ErrMsg = fmt.Sprintf("报告状态有误, State: %d", item.State)
  168. return
  169. }
  170. if item.Content == "" {
  171. br.Msg = "报告内容为空,请检查内容"
  172. return
  173. }
  174. // 校验审批配置
  175. confMap, e := models.GetBusinessConf()
  176. if e != nil {
  177. br.Msg = "操作失败"
  178. br.ErrMsg = "获取审批配置失败, Err: " + e.Error()
  179. return
  180. }
  181. if confMap[models.BusinessConfIsReportApprove] != "true" {
  182. br.Msg = "未开启审批, 请开启后重新操作"
  183. return
  184. }
  185. if confMap[models.BusinessConfReportApproveType] != models.BusinessConfReportApproveTypeOther {
  186. br.Msg = "未开启第三方审批, 请开启后重新操作"
  187. return
  188. }
  189. stateMap := map[bool]int{false: models.ReportStateRefused, true: models.ReportStatePass}
  190. // 驳回
  191. now := time.Now().Local()
  192. if !req.Pass {
  193. cols := []string{"State", "ModifyTime", "ApproveTime"}
  194. item.State = stateMap[req.Pass]
  195. item.ModifyTime = now
  196. item.ApproveTime = now
  197. if e = item.Update(cols); e != nil {
  198. br.Msg = "审批失败"
  199. br.ErrMsg = "报告审批驳回失败, Err: " + e.Error()
  200. return
  201. }
  202. }
  203. // 通过审批
  204. if req.Pass {
  205. cols := []string{"State", "ModifyTime", "ApproveTime", "PublishTime"}
  206. item.State = stateMap[req.Pass]
  207. item.ModifyTime = now
  208. item.ApproveTime = now
  209. item.PublishTime = now
  210. if e = item.Update(cols); e != nil {
  211. br.Msg = "审批失败"
  212. br.ErrMsg = "报告审批通过失败, Err: " + e.Error()
  213. return
  214. }
  215. // 更新报告ES
  216. go func() {
  217. _ = services.UpdateEnglishReportEs(item.Id, models.ReportStatePublished)
  218. }()
  219. }
  220. // 记录报告变更状态
  221. go func() {
  222. recordItem := &models.ReportStateRecord{
  223. ReportId: req.ReportId,
  224. ReportType: models.ReportTypeEnglish,
  225. State: item.State,
  226. CreateTime: time.Now(),
  227. }
  228. _, _ = models.AddReportStateRecord(recordItem)
  229. }()
  230. br.Ret = 200
  231. br.Msg = "审批成功"
  232. }
  233. // ListClassify
  234. // @Title 获取分类列表
  235. // @Description 获取分类列表
  236. // @Param PageSize query int true "每页数据条数"
  237. // @Param CurrentIndex query int true "当前页页码,从1开始"
  238. // @Param KeyWord query string true "检索关键词"
  239. // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
  240. // @Success 200 {object} models.EnglishClassifyListResp
  241. // @router /classify/list [get]
  242. func (this *EnglishReportController) ListClassify() {
  243. br := new(models.BaseResponse).Init()
  244. defer func() {
  245. this.Data["json"] = br
  246. this.ServeJSON()
  247. }()
  248. pageSize, _ := this.GetInt("PageSize")
  249. currentIndex, _ := this.GetInt("CurrentIndex")
  250. keyWord := this.GetString("KeyWord")
  251. var startSize int
  252. if pageSize <= 0 {
  253. pageSize = utils.PageSize20
  254. }
  255. if currentIndex <= 0 {
  256. currentIndex = 1
  257. }
  258. startSize = utils.StartIndex(currentIndex, pageSize)
  259. page := paging.GetPaging(currentIndex, pageSize, 0)
  260. resp := new(models.EnglishClassifyListResp)
  261. // 处理一级分类分页的情况
  262. rootList, err := models.GetEnglishClassifyRootId(startSize, pageSize, keyWord)
  263. if err != nil {
  264. br.Msg = "获取失败"
  265. br.ErrMsg = "获取失败,Err:" + err.Error()
  266. return
  267. }
  268. var ids []int
  269. var rootIds []int
  270. rootMap := make(map[int]struct{}, 0)
  271. for _, v := range rootList {
  272. rootIds = append(rootIds, v.Id)
  273. rootMap[v.Id] = struct{}{}
  274. }
  275. total, err := models.GetEnglishClassifyListCount(keyWord)
  276. if err != nil {
  277. br.Msg = "获取失败"
  278. br.ErrMsg = "获取失败,Err:" + err.Error()
  279. return
  280. }
  281. if total == 0 {
  282. resp.List = make([]*models.EnglishClassifyList, 0)
  283. resp.Paging = page
  284. br.Data = resp
  285. br.Ret = 200
  286. br.Msg = "获取成功"
  287. return
  288. }
  289. page = paging.GetPaging(currentIndex, pageSize, total)
  290. //获取相关的分类ID
  291. idList, err := models.GetEnglishClassifyListByRootId(rootIds, keyWord)
  292. if err != nil {
  293. br.Msg = "获取失败"
  294. br.ErrMsg = "获取失败,Err:" + err.Error()
  295. return
  296. }
  297. secondListMap := make(map[int][]*models.EnglishClassifyList)
  298. thirdListMap := make(map[int][]*models.EnglishClassifyList)
  299. var thirdIds []int
  300. var sortChildList models.RSChildClassifyList
  301. // 三级分类-品种权限
  302. permissionMap := make(map[int][]int)
  303. if len(idList) > 0 {
  304. childIdMap := make(map[int]struct{}, 0)
  305. for _, v := range idList {
  306. if _, ok := childIdMap[v.ParentId]; !ok {
  307. ids = append(ids, v.ParentId)
  308. childIdMap[v.ParentId] = struct{}{}
  309. }
  310. if _, ok := childIdMap[v.Id]; !ok {
  311. ids = append(ids, v.Id)
  312. childIdMap[v.Id] = struct{}{}
  313. }
  314. }
  315. tmpList, err := models.GetEnglishClassifyChildByIds(ids)
  316. if err != nil {
  317. br.Msg = "获取二级分类失败"
  318. br.ErrMsg = "获取二级分类失败,Err:" + err.Error()
  319. return
  320. }
  321. for _, v := range tmpList {
  322. if _, ok := rootMap[v.ParentId]; !ok {
  323. thirdIds = append(thirdIds, v.Id)
  324. }
  325. }
  326. {
  327. classifyIds := thirdIds
  328. if len(classifyIds) > 0 {
  329. cond := fmt.Sprintf(` AND %s IN (%s)`, models.EnClassifyPermissionColumns.EnClassifyId, utils.GetOrmInReplace(len(classifyIds)))
  330. pars := make([]interface{}, 0)
  331. pars = append(pars, classifyIds)
  332. ob := new(models.EnClassifyPermission)
  333. items, e := ob.GetItemsByCondition(cond, pars, []string{}, "")
  334. if e != nil {
  335. br.Msg = "获取失败"
  336. br.ErrMsg = "获取客户权限列表失败, Err: " + e.Error()
  337. return
  338. }
  339. for _, v := range items {
  340. if permissionMap[v.EnClassifyId] == nil {
  341. permissionMap[v.EnClassifyId] = make([]int, 0)
  342. }
  343. permissionMap[v.EnClassifyId] = append(permissionMap[v.EnClassifyId], v.EnPermissionId)
  344. }
  345. }
  346. }
  347. // 处理三级分类
  348. for _, v := range tmpList {
  349. if _, ok := rootMap[v.ParentId]; !ok {
  350. if p, ok1 := permissionMap[v.Id]; ok1 {
  351. v.EnPermissions = p
  352. }
  353. thirdListMap[v.ParentId] = append(thirdListMap[v.ParentId], v)
  354. }
  355. }
  356. //处理二级分类
  357. for _, v := range tmpList {
  358. if _, ok := rootMap[v.ParentId]; ok {
  359. if child, ok1 := thirdListMap[v.Id]; ok1 {
  360. sortChildList = child
  361. sort.Sort(sortChildList)
  362. v.Child = sortChildList
  363. }
  364. secondListMap[v.ParentId] = append(secondListMap[v.ParentId], v)
  365. }
  366. }
  367. }
  368. //处理一级分类
  369. var sortList models.RSClassifyList
  370. for _, v := range rootList {
  371. if child, ok := secondListMap[v.Id]; ok {
  372. sortChildList = child
  373. sort.Sort(sortChildList)
  374. v.Child = sortChildList
  375. }
  376. sortList = append(sortList, v)
  377. }
  378. sort.Sort(sortList)
  379. resp.List = sortList
  380. resp.Paging = page
  381. br.Data = resp
  382. br.Ret = 200
  383. br.Msg = "获取成功"
  384. }