fe_calendar_matter.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. package fe_calendar
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/fe_calendar"
  7. "eta/eta_api/services/data"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. // FeCalendarMatterController 外汇日历-事项
  14. type FeCalendarMatterController struct {
  15. controllers.BaseAuthController
  16. }
  17. // List
  18. // @Title 事项列表
  19. // @Description 事项列表
  20. // @Param ChartPermissionId query int true "品种ID"
  21. // @Param StartDate query string true "开始日期"
  22. // @Param EndDate query string true "结束日期"
  23. // @Success 200 {object} fe_calendar.FeCalendarMatterListItem
  24. // @router /matter/list [get]
  25. func (this *FeCalendarMatterController) List() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. if br.ErrMsg == "" {
  29. br.IsSendEmail = false
  30. }
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. sysUser := this.SysUser
  35. if sysUser == nil {
  36. br.Msg = "请登录"
  37. br.ErrMsg = "请登录,SysUser Is Empty"
  38. br.Ret = 408
  39. return
  40. }
  41. params := new(fe_calendar.FeCalendarMatterListReq)
  42. if e := this.ParseForm(params); e != nil {
  43. br.Msg = "获取失败"
  44. br.ErrMsg = "入参解析失败, Err: " + e.Error()
  45. return
  46. }
  47. if params.ChartPermissionId <= 0 {
  48. br.Msg = "请选择品种"
  49. return
  50. }
  51. cond := ``
  52. pars := make([]interface{}, 0)
  53. if params.ChartPermissionId > 0 {
  54. cond += fmt.Sprintf(` AND %s = ?`, fe_calendar.FeCalendarMatterCols.ChartPermissionId)
  55. pars = append(pars, params.ChartPermissionId)
  56. }
  57. if params.StartDate != "" && params.EndDate != "" {
  58. _, e := time.Parse(utils.FormatDate, params.StartDate)
  59. if e != nil {
  60. br.Msg = "开始日期格式有误"
  61. return
  62. }
  63. _, e = time.Parse(utils.FormatDate, params.EndDate)
  64. if e != nil {
  65. br.Msg = "结束日期格式有误"
  66. return
  67. }
  68. cond += fmt.Sprintf(` AND %s >= ? AND %s <= ?`, fe_calendar.FeCalendarMatterCols.MatterDate, fe_calendar.FeCalendarMatterCols.MatterDate)
  69. pars = append(pars, params.StartDate, params.EndDate)
  70. }
  71. matterOb := new(fe_calendar.FeCalendarMatter)
  72. order := fmt.Sprintf(`%s ASC, %s ASC`, fe_calendar.FeCalendarMatterCols.MatterDate, fe_calendar.FeCalendarMatterCols.Sort)
  73. matters, e := matterOb.GetItemsByCondition(cond, pars, []string{}, order)
  74. if e != nil {
  75. br.Msg = "获取失败"
  76. br.ErrMsg = "获取事项列表失败, Err: " + e.Error()
  77. return
  78. }
  79. dateMatter := make(map[string][]*fe_calendar.FeCalendarMatterItem)
  80. for _, v := range matters {
  81. d := v.MatterDate.Format(utils.FormatDate)
  82. if dateMatter[d] == nil {
  83. dateMatter[d] = make([]*fe_calendar.FeCalendarMatterItem, 0)
  84. }
  85. dateMatter[d] = append(dateMatter[d], fe_calendar.FormatFeCalendarMatter2Item(v))
  86. }
  87. dateExist := make(map[string]bool)
  88. resp := make([]*fe_calendar.FeCalendarMatterListItem, 0)
  89. for _, v := range matters {
  90. d := v.MatterDate.Format(utils.FormatDate)
  91. if dateExist[d] {
  92. continue
  93. }
  94. dateExist[d] = true
  95. resp = append(resp, &fe_calendar.FeCalendarMatterListItem{
  96. Date: d,
  97. Matters: dateMatter[d],
  98. })
  99. }
  100. br.Data = resp
  101. br.Ret = 200
  102. br.Success = true
  103. br.Msg = "获取成功"
  104. }
  105. // Detail
  106. // @Title 事项详情
  107. // @Description 事项详情
  108. // @Param ChartPermissionId query int true "品种ID"
  109. // @Param MatterDate query string true "事项日期"
  110. // @Success 200 {object} fe_calendar.FeCalendarMatterItem
  111. // @router /matter/detail [get]
  112. func (this *FeCalendarMatterController) Detail() {
  113. br := new(models.BaseResponse).Init()
  114. defer func() {
  115. if br.ErrMsg == "" {
  116. br.IsSendEmail = false
  117. }
  118. this.Data["json"] = br
  119. this.ServeJSON()
  120. }()
  121. sysUser := this.SysUser
  122. if sysUser == nil {
  123. br.Msg = "请登录"
  124. br.ErrMsg = "请登录,SysUser Is Empty"
  125. br.Ret = 408
  126. return
  127. }
  128. permissionId, _ := this.GetInt("ChartPermissionId")
  129. if permissionId <= 0 {
  130. br.Msg = "请选择品种"
  131. return
  132. }
  133. matterDate := this.GetString("MatterDate")
  134. if matterDate == "" {
  135. br.Msg = "请选择日期"
  136. return
  137. }
  138. _, e := time.Parse(utils.FormatDate, matterDate)
  139. if e != nil {
  140. br.Msg = "日期格式有误"
  141. return
  142. }
  143. matterOb := new(fe_calendar.FeCalendarMatter)
  144. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, fe_calendar.FeCalendarMatterCols.ChartPermissionId, fe_calendar.FeCalendarMatterCols.MatterDate)
  145. pars := make([]interface{}, 0)
  146. pars = append(pars, permissionId, matterDate)
  147. order := fmt.Sprintf(`%s ASC`, fe_calendar.FeCalendarMatterCols.Sort)
  148. matters, e := matterOb.GetItemsByCondition(cond, pars, []string{}, order)
  149. if e != nil {
  150. br.Msg = "获取失败"
  151. br.ErrMsg = "获取事项列表失败, Err: " + e.Error()
  152. return
  153. }
  154. resp := make([]*fe_calendar.FeCalendarMatterItem, 0)
  155. for _, v := range matters {
  156. resp = append(resp, fe_calendar.FormatFeCalendarMatter2Item(v))
  157. }
  158. br.Data = resp
  159. br.Ret = 200
  160. br.Success = true
  161. br.Msg = "获取成功"
  162. }
  163. // Save
  164. // @Title 保存事项
  165. // @Description 保存事项
  166. // @Param request body fe_calendar.FeCalendarMatterSaveReq true "type json string"
  167. // @Success 200 string "操作成功"
  168. // @router /matter/save [post]
  169. func (this *FeCalendarMatterController) Save() {
  170. br := new(models.BaseResponse).Init()
  171. defer func() {
  172. if br.ErrMsg == "" {
  173. br.IsSendEmail = false
  174. }
  175. this.Data["json"] = br
  176. this.ServeJSON()
  177. }()
  178. sysUser := this.SysUser
  179. if sysUser == nil {
  180. br.Msg = "请登录"
  181. br.ErrMsg = "请登录,SysUser Is Empty"
  182. br.Ret = 408
  183. return
  184. }
  185. var req fe_calendar.FeCalendarMatterSaveReq
  186. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  187. br.Msg = "参数有误"
  188. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  189. return
  190. }
  191. // 参数校验
  192. if req.ChartPermissionId <= 0 {
  193. br.Msg = "请选择品种"
  194. return
  195. }
  196. if req.MatterDate == "" {
  197. br.Msg = "请选择日期"
  198. return
  199. }
  200. var matterMonth string
  201. matterDate, e := time.ParseInLocation(utils.FormatDate, req.MatterDate, time.Local)
  202. if e != nil {
  203. br.Msg = "日期格式有误"
  204. return
  205. }
  206. matterMonth = matterDate.Format("2006-01")
  207. if len(req.Matters) > 0 {
  208. for _, v := range req.Matters {
  209. if v.MatterType != fe_calendar.MatterTypeFree && v.MatterType != fe_calendar.MatterTypeEdb && v.MatterType != fe_calendar.MatterTypePredict {
  210. br.Msg = "事项类型有误"
  211. br.ErrMsg = fmt.Sprintf("事项类型有误, MatterType: %d", v.MatterType)
  212. return
  213. }
  214. v.Title = strings.TrimSpace(v.Title)
  215. if v.Title == "" {
  216. br.Msg = "请输入指标/事项名称"
  217. return
  218. }
  219. if len([]rune(v.Title)) > 15 {
  220. br.Msg = "名称超出15个字,请重新编辑"
  221. return
  222. }
  223. if v.MatterType == fe_calendar.MatterTypeEdb || v.MatterType == fe_calendar.MatterTypePredict {
  224. if v.EdbInfoId <= 0 || v.EdbUniqueCode == "" || v.EdbCode == "" {
  225. br.Msg = "指标信息有误"
  226. br.ErrMsg = fmt.Sprintf("指标信息有误, EdbInfoId: %d, EdbUniqueCode: %s, EdbCode: %s", v.EdbInfoId, v.EdbUniqueCode, v.EdbCode)
  227. return
  228. }
  229. }
  230. }
  231. }
  232. // 品种信息
  233. permissionItem, e := models.GetChartPermissionById(req.ChartPermissionId)
  234. if e != nil {
  235. br.Msg = "操作失败"
  236. br.ErrMsg = "获取品种失败, Err: " + e.Error()
  237. return
  238. }
  239. // 获取已有事项
  240. matterOb := new(fe_calendar.FeCalendarMatter)
  241. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, fe_calendar.FeCalendarMatterCols.ChartPermissionId, fe_calendar.FeCalendarMatterCols.MatterDate)
  242. pars := make([]interface{}, 0)
  243. pars = append(pars, req.ChartPermissionId, req.MatterDate)
  244. order := fmt.Sprintf(`%s ASC`, fe_calendar.FeCalendarMatterCols.Sort)
  245. matters, e := matterOb.GetItemsByCondition(cond, pars, []string{}, order)
  246. if e != nil {
  247. br.Msg = "操作失败"
  248. br.ErrMsg = "获取已有事项列表失败, Err: " + e.Error()
  249. return
  250. }
  251. addMatters := make([]*fe_calendar.FeCalendarMatter, 0)
  252. editMatters := make([]*fe_calendar.FeCalendarMatter, 0)
  253. removeMatters := make([]*fe_calendar.FeCalendarMatter, 0)
  254. updateCols := []string{fe_calendar.FeCalendarMatterCols.Title, fe_calendar.FeCalendarMatterCols.FontColor, fe_calendar.FeCalendarMatterCols.FillingColor, fe_calendar.FeCalendarMatterCols.FontBold, fe_calendar.FeCalendarMatterCols.Sort, fe_calendar.FeCalendarMatterCols.ChartPermissionName, fe_calendar.FeCalendarMatterCols.ModifyTime}
  255. editIds := make([]int, 0)
  256. editMatterMap := make(map[int]*fe_calendar.FeCalendarMatterSaveItem)
  257. for _, v := range req.Matters {
  258. // 更新的事项map
  259. if v.FeCalendarMatterId > 0 {
  260. editIds = append(editIds, v.FeCalendarMatterId)
  261. editMatterMap[v.FeCalendarMatterId] = v
  262. continue
  263. }
  264. // 新增事项
  265. addMatters = append(addMatters, &fe_calendar.FeCalendarMatter{
  266. ChartPermissionId: req.ChartPermissionId,
  267. ChartPermissionName: permissionItem.PermissionName,
  268. MatterMonth: matterMonth,
  269. MatterDate: matterDate,
  270. Title: strings.TrimSpace(v.Title),
  271. MatterType: v.MatterType,
  272. EdbInfoId: v.EdbInfoId,
  273. EdbUniqueCode: v.EdbUniqueCode,
  274. EdbCode: v.EdbCode,
  275. FontColor: v.FontColor,
  276. FillingColor: v.FillingColor,
  277. FontBold: v.FontBold,
  278. Sort: v.Sort,
  279. SysUserId: sysUser.AdminId,
  280. SysUserName: sysUser.RealName,
  281. CreateTime: time.Now().Local(),
  282. ModifyTime: time.Now().Local(),
  283. })
  284. }
  285. for _, v := range matters {
  286. // 移除的事项
  287. if !utils.InArrayByInt(editIds, v.FeCalendarMatterId) {
  288. removeMatters = append(removeMatters, v)
  289. continue
  290. }
  291. // 编辑的事项
  292. ed := editMatterMap[v.FeCalendarMatterId]
  293. if ed != nil {
  294. v.Title = strings.TrimSpace(ed.Title)
  295. v.FontColor = ed.FontColor
  296. v.FillingColor = ed.FillingColor
  297. v.FontBold = ed.FontBold
  298. v.Sort = ed.Sort
  299. v.ChartPermissionName = permissionItem.PermissionName
  300. v.ModifyTime = time.Now().Local()
  301. editMatters = append(editMatters, v)
  302. }
  303. }
  304. // 保存/删除
  305. if e = matterOb.Save(addMatters, editMatters, removeMatters, updateCols); e != nil {
  306. br.Msg = "操作失败"
  307. br.ErrMsg = "保存事项失败, Err: " + e.Error()
  308. return
  309. }
  310. _ = data.SaveCalendarEdbInfoRelation(req.ChartPermissionId, req.MatterDate, editMatters, removeMatters)
  311. br.Ret = 200
  312. br.Success = true
  313. br.Msg = "操作成功"
  314. }
  315. // PermissionList
  316. // @Title 品种列表
  317. // @Description 品种列表
  318. // @Success 200 {object} models.SimpleChartPermission
  319. // @router /permission/list [get]
  320. func (this *FeCalendarMatterController) PermissionList() {
  321. br := new(models.BaseResponse).Init()
  322. defer func() {
  323. if br.ErrMsg == "" {
  324. br.IsSendEmail = false
  325. }
  326. this.Data["json"] = br
  327. this.ServeJSON()
  328. }()
  329. permissions, e := models.GetChartPermissionsByProductId()
  330. if e != nil {
  331. br.Msg = "获取失败"
  332. br.ErrMsg = "获取品种列表失败, Err: " + e.Error()
  333. return
  334. }
  335. var resp models.FaCalendarPermissionResp
  336. list := make([]*models.SimpleChartPermission, 0)
  337. parentPermissions := make(map[int][]*models.SimpleChartPermission, 0)
  338. for _, v := range permissions {
  339. if v.ParentId > 0 {
  340. if parentPermissions[v.ParentId] == nil {
  341. parentPermissions[v.ParentId] = make([]*models.SimpleChartPermission, 0)
  342. }
  343. parentPermissions[v.ParentId] = append(parentPermissions[v.ParentId], models.FormatChartPermission2Simple(v))
  344. continue
  345. }
  346. list = append(list, models.FormatChartPermission2Simple(v))
  347. }
  348. for _, v := range list {
  349. v.Children = parentPermissions[v.ChartPermissionId]
  350. }
  351. lastEditPermissionId := 0
  352. // 查询最近被编辑过的品种ID
  353. matterOb := new(fe_calendar.FeCalendarMatter)
  354. cond := ""
  355. pars := make([]interface{}, 0)
  356. order := fmt.Sprintf(`%s Desc`, fe_calendar.FeCalendarMatterCols.ModifyTime)
  357. matter, e := matterOb.GetItemByCondition(cond, pars, order)
  358. if e != nil && e.Error() != utils.ErrNoRow() {
  359. br.Msg = "获取失败"
  360. br.ErrMsg = "获取事项列表失败, Err: " + e.Error()
  361. return
  362. }
  363. if e == nil {
  364. lastEditPermissionId = matter.ChartPermissionId
  365. }
  366. resp.CheckedPermissionId = lastEditPermissionId
  367. resp.List = list
  368. br.Data = resp
  369. br.Ret = 200
  370. br.Success = true
  371. br.Msg = "获取成功"
  372. }