fe_calendar.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package fe_calendar
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "hongze/hongze_yb/controller/response"
  6. "hongze/hongze_yb/models/request"
  7. "hongze/hongze_yb/models/tables/fe_calendar_matter"
  8. "hongze/hongze_yb/models/tables/rddp/chart_permission"
  9. "hongze/hongze_yb/services/company"
  10. userService "hongze/hongze_yb/services/user"
  11. "hongze/hongze_yb/utils"
  12. "time"
  13. )
  14. // PermissionList 品种列表
  15. func PermissionList(c *gin.Context) {
  16. userInfo := userService.GetInfoByClaims(c)
  17. // 获取用户有权限的品种
  18. validPermissionIds, e := company.GetValidPermissionIdListByCompany2ProductId(userInfo.CompanyID, int64(utils.FiccProductId))
  19. if e != nil {
  20. response.FailMsg("获取失败", "获取有权限品种列表失败, Err: "+e.Error(), c)
  21. return
  22. }
  23. // 遍历品种树
  24. permissions, e := chart_permission.GetChartPermissionsByProductId()
  25. if e != nil {
  26. response.FailMsg("获取失败", "获取品种列表失败, Err: "+e.Error(), c)
  27. return
  28. }
  29. parents := make([]*chart_permission.SimpleChartPermission, 0)
  30. parentPermissions := make(map[int][]*chart_permission.SimpleChartPermission, 0)
  31. for _, v := range permissions {
  32. if v.ParentId > 0 && utils.InArrayByInt(validPermissionIds, int(v.ChartPermissionID)) {
  33. if parentPermissions[v.ParentId] == nil {
  34. parentPermissions[v.ParentId] = make([]*chart_permission.SimpleChartPermission, 0)
  35. }
  36. parentPermissions[v.ParentId] = append(parentPermissions[v.ParentId], chart_permission.FormatChartPermission2Simple(v))
  37. continue
  38. }
  39. parents = append(parents, chart_permission.FormatChartPermission2Simple(v))
  40. }
  41. resp := make([]*chart_permission.SimpleChartPermission, 0)
  42. for _, v := range parents {
  43. if parentPermissions[v.ChartPermissionId] == nil {
  44. continue
  45. }
  46. v.Children = parentPermissions[v.ChartPermissionId]
  47. resp = append(resp, v)
  48. }
  49. response.OkData("获取成功", resp, c)
  50. }
  51. // LastestPermissionMonth 获取品种有数据最近的月份
  52. func LastestPermissionMonth(c *gin.Context) {
  53. var req request.FeCalendarLastestPermissionMonthReq
  54. if err := c.Bind(&req); err != nil {
  55. response.Fail("参数有误", c)
  56. return
  57. }
  58. if req.ChartPermissionId <= 0 {
  59. response.Fail("请选择品种", c)
  60. return
  61. }
  62. var month string
  63. matterOb := new(fe_calendar_matter.FeCalendarMatter)
  64. // 当月事项
  65. currentMonth := time.Now().Format("2006-01")
  66. cond := ` chart_permission_id = ? AND matter_month = ?`
  67. pars := make([]interface{}, 0)
  68. pars = append(pars, req.ChartPermissionId, currentMonth)
  69. item, e := matterOb.GetItemByCondition(cond, pars)
  70. if e != nil && e != utils.ErrNoRow {
  71. response.FailMsg("获取失败", "获取当月事项失败, Err: "+e.Error(), c)
  72. return
  73. }
  74. if item != nil && item.FeCalendarMatterId > 0 {
  75. month = currentMonth
  76. }
  77. // 无当月事项则查询最近的
  78. if month == "" {
  79. lastest, e := matterOb.GetLastestMonthItemByPermissionId(req.ChartPermissionId)
  80. if e != nil && e != utils.ErrNoRow {
  81. response.FailMsg("获取失败", "获取事项失败, Err: "+e.Error(), c)
  82. return
  83. }
  84. if lastest != nil && lastest.FeCalendarMatterId > 0 {
  85. month = lastest.MatterMonth
  86. }
  87. }
  88. response.OkData("获取成功", month, c)
  89. }
  90. // MatterList 事项列表
  91. func MatterList(c *gin.Context) {
  92. var req request.FeCalendarMatterListReq
  93. if err := c.Bind(&req); err != nil {
  94. response.Fail("参数有误", c)
  95. return
  96. }
  97. if req.ChartPermissionId <= 0 {
  98. response.Fail("请选择品种", c)
  99. return
  100. }
  101. //userInfo := userService.GetInfoByClaims(c)
  102. cond := ` 1=1`
  103. pars := make([]interface{}, 0)
  104. if req.ChartPermissionId > 0 {
  105. cond += fmt.Sprintf(` AND %s = ?`, fe_calendar_matter.FeCalendarMatterColumns.ChartPermissionId)
  106. pars = append(pars, req.ChartPermissionId)
  107. }
  108. if req.StartDate != "" && req.EndDate != "" {
  109. _, e := time.Parse(utils.FormatDate, req.StartDate)
  110. if e != nil {
  111. response.Fail("开始日期格式有误", c)
  112. return
  113. }
  114. _, e = time.Parse(utils.FormatDate, req.EndDate)
  115. if e != nil {
  116. response.Fail("结束日期格式有误", c)
  117. return
  118. }
  119. cond += fmt.Sprintf(` AND %s >= ? AND %s <= ?`, fe_calendar_matter.FeCalendarMatterColumns.MatterDate, fe_calendar_matter.FeCalendarMatterColumns.MatterDate)
  120. pars = append(pars, req.StartDate, req.EndDate)
  121. }
  122. matterOb := new(fe_calendar_matter.FeCalendarMatter)
  123. order := fmt.Sprintf(`%s ASC, %s ASC`, fe_calendar_matter.FeCalendarMatterColumns.MatterDate, fe_calendar_matter.FeCalendarMatterColumns.Sort)
  124. matters, e := matterOb.GetItemsByCondition(cond, pars, []string{}, order)
  125. if e != nil {
  126. response.FailMsg("获取失败", "获取事项列表失败, Err: "+e.Error(), c)
  127. return
  128. }
  129. dateMatter := make(map[string][]*fe_calendar_matter.FeCalendarMatterItem)
  130. for _, v := range matters {
  131. d := v.MatterDate.Format(utils.FormatDate)
  132. if dateMatter[d] == nil {
  133. dateMatter[d] = make([]*fe_calendar_matter.FeCalendarMatterItem, 0)
  134. }
  135. dateMatter[d] = append(dateMatter[d], fe_calendar_matter.FormatFeCalendarMatter2Item(v))
  136. }
  137. dateExist := make(map[string]bool)
  138. resp := make([]*fe_calendar_matter.FeCalendarMatterListItem, 0)
  139. for _, v := range matters {
  140. d := v.MatterDate.Format(utils.FormatDate)
  141. if dateExist[d] {
  142. continue
  143. }
  144. dateExist[d] = true
  145. resp = append(resp, &fe_calendar_matter.FeCalendarMatterListItem{
  146. Date: d,
  147. Matters: dateMatter[d],
  148. })
  149. }
  150. response.OkData("获取成功", resp, c)
  151. }
  152. // MatterDetail 事项详情
  153. func MatterDetail(c *gin.Context) {
  154. var req request.FeCalendarMatterDetailReq
  155. if err := c.Bind(&req); err != nil {
  156. response.Fail("参数有误", c)
  157. return
  158. }
  159. if req.ChartPermissionId <= 0 {
  160. response.Fail("请选择品种", c)
  161. return
  162. }
  163. if req.MatterDate == "" {
  164. response.Fail("请选择日期", c)
  165. return
  166. }
  167. _, e := time.Parse(utils.FormatDate, req.MatterDate)
  168. if e != nil {
  169. response.Fail("日期格式有误", c)
  170. return
  171. }
  172. //userInfo := userService.GetInfoByClaims(c)
  173. matterOb := new(fe_calendar_matter.FeCalendarMatter)
  174. cond := fmt.Sprintf(` %s = ? AND %s = ?`, fe_calendar_matter.FeCalendarMatterColumns.ChartPermissionId, fe_calendar_matter.FeCalendarMatterColumns.MatterDate)
  175. pars := make([]interface{}, 0)
  176. pars = append(pars, req.ChartPermissionId, req.MatterDate)
  177. order := fmt.Sprintf(`%s ASC`, fe_calendar_matter.FeCalendarMatterColumns.Sort)
  178. matters, e := matterOb.GetItemsByCondition(cond, pars, []string{}, order)
  179. if e != nil {
  180. response.FailMsg("获取失败", "获取事项列表失败, Err: "+e.Error(), c)
  181. return
  182. }
  183. resp := make([]*fe_calendar_matter.FeCalendarMatterItem, 0)
  184. for _, v := range matters {
  185. resp = append(resp, fe_calendar_matter.FormatFeCalendarMatter2Item(v))
  186. }
  187. response.OkData("获取成功", resp, c)
  188. }