fe_calendar_ai_article.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package fe_calendar
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/fe_calendar"
  8. "eta/eta_api/services"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "time"
  12. )
  13. // FeCalendarAiArticleController 外汇日历-AI数据点评数据点评
  14. type FeCalendarAiArticleController struct {
  15. controllers.BaseAuthController
  16. }
  17. // Detail
  18. // @Title AI数据点评详情
  19. // @Description AI数据点评详情
  20. // @Param FeCalendarAiArticleId query int true "数据点评ID"
  21. // @Success 200 {object} fe_calendar.FeCalendarAiArticle
  22. // @router /ai_article/detail [get]
  23. func (this *FeCalendarAiArticleController) Detail() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. if br.ErrMsg == "" {
  27. br.IsSendEmail = false
  28. }
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. sysUser := this.SysUser
  33. if sysUser == nil {
  34. br.Msg = "请登录"
  35. br.ErrMsg = "请登录,SysUser Is Empty"
  36. br.Ret = 408
  37. return
  38. }
  39. articleId, err := this.GetInt("FeCalendarAiArticleId")
  40. if err != nil || articleId <= 0 {
  41. br.Msg = "请提供有效的数据点评ID"
  42. return
  43. }
  44. articleOb := new(fe_calendar.FeCalendarAiArticle)
  45. if err := articleOb.GetByID(uint(articleId)); err != nil {
  46. if utils.IsErrNoRow(err) {
  47. br.Msg = "数据点评不存在"
  48. return
  49. }
  50. br.Msg = "获取失败"
  51. br.ErrMsg = fmt.Sprintf("获取AI数据点评详情失败, ArticleId: %d, Err: %s", articleId, err.Error())
  52. return
  53. }
  54. // 查询指标
  55. edbInfo, err := data_manage.GetEdbInfoById(articleOb.EdbInfoId)
  56. if err != nil {
  57. if utils.IsErrNoRow(err) {
  58. br.Msg = "指标不存在"
  59. return
  60. }
  61. br.Msg = "操作失败"
  62. br.ErrMsg = "获取指标失败, Err: " + err.Error()
  63. return
  64. }
  65. data := &fe_calendar.FeCalendarAiArticleDetailResp{
  66. FeCalendarAiArticleId: articleOb.FeCalendarAiArticleId,
  67. EdbName: edbInfo.EdbName,
  68. EdbInfoId: articleOb.EdbInfoId,
  69. EdbCode: edbInfo.EdbCode,
  70. Content: articleOb.Content,
  71. ModifyTime: articleOb.ModifyTime.Format(utils.FormatDateTime),
  72. }
  73. br.Data = data
  74. br.Ret = 200
  75. br.Success = true
  76. br.Msg = "获取成功"
  77. }
  78. // Create
  79. // @Title 创建AI数据点评
  80. // @Description 创建AI数据点评
  81. // @Param request body fe_calendar.FeCalendarAiArticle true "type json string"
  82. // @Success 200 string "操作成功"
  83. // @router /ai_article/generate [post]
  84. func (this *FeCalendarAiArticleController) Generate() {
  85. br := new(models.BaseResponse).Init()
  86. defer func() {
  87. if br.ErrMsg == "" {
  88. br.IsSendEmail = false
  89. }
  90. this.Data["json"] = br
  91. this.ServeJSON()
  92. }()
  93. sysUser := this.SysUser
  94. if sysUser == nil {
  95. br.Msg = "请登录"
  96. br.ErrMsg = "请登录,SysUser Is Empty"
  97. br.Ret = 408
  98. return
  99. }
  100. var req *fe_calendar.FeCalendarAiArticleGenerateReq
  101. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  102. br.Msg = "参数有误"
  103. br.ErrMsg = "参数解析失败, Err: " + err.Error()
  104. return
  105. }
  106. if req.EdbInfoId <= 0 {
  107. br.Msg = "请选择指标"
  108. return
  109. }
  110. // 查询指标
  111. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  112. if err != nil {
  113. if utils.IsErrNoRow(err) {
  114. br.Msg = "指标不存在"
  115. return
  116. }
  117. br.Msg = "操作失败"
  118. br.ErrMsg = "获取指标失败, Err: " + err.Error()
  119. return
  120. }
  121. if req.EdbInfoId <= 0 {
  122. br.Msg = "请选择指标"
  123. return
  124. }
  125. if req.MatterDate == "" {
  126. br.Msg = "请选择日期"
  127. return
  128. }
  129. // 获取当月的最后一天
  130. matterDateTime, e := time.Parse(utils.FormatDate, req.MatterDate)
  131. if e != nil {
  132. br.Msg = "请选择正确的日期"
  133. return
  134. }
  135. data, err := services.GetFeCalendarAiArticle(req, matterDateTime, edbInfo, sysUser)
  136. if err != nil {
  137. br.Msg = "操作失败"
  138. br.ErrMsg = "保存AI数据点评失败, Err: " + err.Error()
  139. return
  140. }
  141. br.Ret = 200
  142. br.Success = true
  143. br.Msg = "操作成功"
  144. br.Data = data
  145. }
  146. // Save
  147. // @Title 保存AI数据点评
  148. // @Description 保存AI数据点评
  149. // @Param request body fe_calendar.FeCalendarAiArticle true "type json string"
  150. // @Success 200 string "操作成功"
  151. // @router /ai_article/save [post]
  152. func (this *FeCalendarAiArticleController) Save() {
  153. br := new(models.BaseResponse).Init()
  154. defer func() {
  155. if br.ErrMsg == "" {
  156. br.IsSendEmail = false
  157. }
  158. this.Data["json"] = br
  159. this.ServeJSON()
  160. }()
  161. sysUser := this.SysUser
  162. if sysUser == nil {
  163. br.Msg = "请登录"
  164. br.ErrMsg = "请登录,SysUser Is Empty"
  165. br.Ret = 408
  166. return
  167. }
  168. var req *fe_calendar.FeCalendarAiArticleSaveReq
  169. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  170. br.Msg = "参数有误"
  171. br.ErrMsg = "参数解析失败, Err: " + err.Error()
  172. return
  173. }
  174. if req.EdbInfoId <= 0 {
  175. br.Msg = "请选择指标"
  176. return
  177. }
  178. // 查询指标
  179. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  180. if err != nil {
  181. if utils.IsErrNoRow(err) {
  182. br.Msg = "指标不存在"
  183. return
  184. }
  185. br.Msg = "操作失败"
  186. br.ErrMsg = "获取指标失败, Err: " + err.Error()
  187. return
  188. }
  189. // 参数校验
  190. if req.ChartPermissionId <= 0 {
  191. br.Msg = "请选择品种"
  192. return
  193. }
  194. if req.MatterDate == "" {
  195. br.Msg = "请选择日期"
  196. return
  197. }
  198. if req.Content == "" {
  199. br.Msg = "请输入内容"
  200. return
  201. }
  202. feCalendarAiArticleId := req.FeCalendarAiArticleId
  203. // 获取当月的最后一天
  204. matterDateTime, e := time.Parse(utils.FormatDate, req.MatterDate)
  205. if e != nil {
  206. br.Msg = "请选择正确的日期"
  207. return
  208. }
  209. // 获取品种信息
  210. permissionItem, err := models.GetChartPermissionById(req.ChartPermissionId)
  211. if err != nil {
  212. br.Msg = "操作失败"
  213. br.ErrMsg = "获取品种信息失败, Err: " + err.Error()
  214. return
  215. }
  216. matterOb := new(fe_calendar.FeCalendarMatter)
  217. if req.FeCalendarMatterId > 0 {
  218. matterItem, err := matterOb.GetItemById(req.FeCalendarMatterId)
  219. if err != nil {
  220. br.Msg = "操作失败"
  221. br.ErrMsg = "获取事项失败, Err: " + err.Error()
  222. return
  223. }
  224. if matterItem.MatterType != fe_calendar.MatterTypePredict {
  225. br.Msg = "事项类型不匹配"
  226. return
  227. }
  228. }
  229. if req.FeCalendarAiArticleId <= 0 { // 创建数据点评
  230. article := new(fe_calendar.FeCalendarAiArticle)
  231. article.Title = edbInfo.EdbName
  232. article.Content = req.Content
  233. article.Prompt = ""
  234. article.ChartPermissionId = req.ChartPermissionId
  235. article.ChartPermissionName = permissionItem.PermissionName
  236. article.EdbCode = edbInfo.EdbCode
  237. article.EdbInfoId = edbInfo.EdbInfoId
  238. article.MatterDate = matterDateTime
  239. article.MatterMonth = matterDateTime.Format("2006-01")
  240. article.SysUserId = sysUser.AdminId
  241. article.SysUserName = sysUser.RealName
  242. err = article.Create()
  243. if err != nil {
  244. br.Msg = "操作失败"
  245. br.ErrMsg = "保存AI数据点评失败, Err: " + err.Error()
  246. return
  247. }
  248. feCalendarAiArticleId = article.FeCalendarAiArticleId
  249. }else{ // 更新数据点评
  250. articleOb := new(fe_calendar.FeCalendarAiArticle)
  251. if err := articleOb.GetByID(uint(req.FeCalendarAiArticleId)); err != nil {
  252. if utils.IsErrNoRow(err) {
  253. br.Msg = "数据点评不存在"
  254. return
  255. }
  256. br.Msg = "操作失败"
  257. br.ErrMsg = "获取AI数据点评失败, Err: " + err.Error()
  258. return
  259. }
  260. if articleOb.EdbInfoId != edbInfo.EdbInfoId {
  261. br.Msg = "指标不匹配"
  262. return
  263. }
  264. if articleOb.MatterDate.Format(utils.FormatDate) != req.MatterDate {
  265. br.Msg = "日期不匹配"
  266. return
  267. }
  268. articleOb.Content = req.Content
  269. articleOb.ModifyTime = time.Now()
  270. err = articleOb.Update([]string{"content", "modify_time"})
  271. if err != nil {
  272. br.Msg = "操作失败"
  273. br.ErrMsg = "更新AI数据点评失败, Err: " + err.Error()
  274. return
  275. }
  276. }
  277. // 更新事项
  278. if req.FeCalendarMatterId > 0 && matterOb != nil && matterOb.FeCalendarMatterId > 0 && matterOb.FeCalendarAiArticleId != feCalendarAiArticleId {
  279. matterOb.FeCalendarAiArticleId = feCalendarAiArticleId
  280. matterOb.ModifyTime = time.Now()
  281. err = matterOb.Update([]string{"fe_calendar_ai_article_id", "modify_time"})
  282. if err != nil {
  283. br.Msg = "操作失败"
  284. br.ErrMsg = "更新事项失败, Err: " + err.Error()
  285. return
  286. }
  287. }
  288. data := &fe_calendar.FeCalendarAiArticleSaveResp{
  289. FeCalendarAiArticleId: feCalendarAiArticleId,
  290. }
  291. br.Ret = 200
  292. br.Success = true
  293. br.Msg = "操作成功"
  294. br.Data = data
  295. }