business_calendar.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package business_trip
  2. import (
  3. "fmt"
  4. "hongze/hz_crm_api/models"
  5. "hongze/hz_crm_api/models/business_trip"
  6. "hongze/hz_crm_api/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // BusinessTripCalendar
  12. // @Title 出差日历表
  13. // @Description 出差日历表接口
  14. // @Param AdminId query string false "用户id,多个用英文逗号分开"
  15. // @Param WeekQuery query int false "周查询: 0-本期(本周及下周); 1-前两周; 2-后两周"
  16. // @Param BaseQueryDate query string false "周查询时的开始日期"
  17. // @Success 200 {object} roadshow.BusinessTripResp
  18. // @router /calendar [get]
  19. func (this *BusinessTrip) BusinessTripCalendar() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. sysUser := this.SysUser
  26. if sysUser == nil {
  27. br.Msg = "请登录"
  28. br.ErrMsg = "请登录,SysUser Is Empty"
  29. br.Ret = 408
  30. return
  31. }
  32. adminId := this.GetString("AdminId")
  33. weekQuery, _ := this.GetInt("WeekQuery")
  34. baseQueryDate := this.GetString("BaseQueryDate")
  35. approveAdminId := 66 // 测试、生产都是这个审批人id
  36. groupList := make([]*business_trip.BusinessTripCalendarGroup, 0)
  37. researcherGroup := new(business_trip.BusinessTripCalendarGroup)
  38. researcherGroup.GroupId = 1
  39. researcherGroup.GroupName = "研究员"
  40. researcherGroup.DepartmentId = "1"
  41. groupList = append(groupList, researcherGroup)
  42. sellerGroup := new(business_trip.BusinessTripCalendarGroup)
  43. sellerGroup.GroupId = 2
  44. sellerGroup.GroupName = "销售"
  45. if utils.RunMode == "debug" {
  46. sellerGroup.DepartmentId = "2,5"
  47. } else {
  48. sellerGroup.DepartmentId = "2,4,5,9"
  49. }
  50. groupList = append(groupList, sellerGroup)
  51. otherGroup := new(business_trip.BusinessTripCalendarGroup)
  52. otherGroup.GroupId = 3
  53. otherGroup.GroupName = "其他"
  54. if utils.RunMode == "debug" {
  55. otherGroup.DepartmentId = "3,7"
  56. } else {
  57. otherGroup.DepartmentId = "3,6,7,8"
  58. }
  59. groupList = append(groupList, otherGroup)
  60. groupLen := len(groupList)
  61. //fmt.Println("groupLen:", groupLen)
  62. var nowWeekStart time.Time
  63. if baseQueryDate == "" {
  64. now := time.Now()
  65. weekDay := int(now.Weekday())
  66. if weekDay == 6 {
  67. nowWeekStart, _ = time.ParseInLocation(utils.FormatDate, now.Format(utils.FormatDate), time.Local)
  68. } else {
  69. nowWeekStart, _ = time.ParseInLocation(utils.FormatDate, now.AddDate(0, 0, -(weekDay+1)).Format(utils.FormatDate), time.Local)
  70. }
  71. }
  72. // 查询需求是可以无限往前后两周查询, 以入参为本周开始日期
  73. if baseQueryDate != "" {
  74. t, e := time.ParseInLocation(utils.FormatDate, baseQueryDate, time.Local)
  75. if e != nil {
  76. br.Msg = "查询开始日期格式有误"
  77. return
  78. }
  79. nowWeekStart = t
  80. }
  81. // 前/后两周
  82. if weekQuery == 1 {
  83. nowWeekStart = nowWeekStart.AddDate(0, 0, -14)
  84. }
  85. if weekQuery == 2 {
  86. nowWeekStart = nowWeekStart.AddDate(0, 0, +14)
  87. }
  88. //nowWeekEnd := utils.GetNowWeekLastDay()
  89. //获取基于开始周的下周日期
  90. nextWeekStart := nowWeekStart.AddDate(0, 0, +7)
  91. nextWeekEnd := nowWeekStart.AddDate(0, 0, +13)
  92. //获取出差信息
  93. businessTripList, err := business_trip.GetBusinessTripList(adminId, nowWeekStart.Format(utils.FormatDate), nextWeekEnd.Format(utils.FormatDate))
  94. if err != nil {
  95. br.Msg = "获取信息失败!"
  96. br.ErrMsg = "获取路演信息失败!,GetBusinessTripList Err:" + err.Error()
  97. return
  98. }
  99. adminIdList := make([]string, 0)
  100. if adminId != `` {
  101. adminIdList = strings.Split(adminId, ",")
  102. }
  103. lenAdminIdList := len(adminIdList)
  104. btMap := make(map[int][]business_trip.BusinessApplyView)
  105. for _, v := range businessTripList {
  106. fmt.Println(v.ApplyRealName, v.Status, v.ArriveDate, v.ReturnDate, approveAdminId)
  107. // 只有沛总/admin才需要看 “待审批” 的出差, 但admin无审批权
  108. if v.Status == `待审批` && (sysUser.AdminId != approveAdminId && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN) {
  109. continue
  110. }
  111. if lenAdminIdList > 0 {
  112. if utils.InArrayByStr(adminIdList, strconv.Itoa(v.ApplyAdminId)) {
  113. findValList, ok := btMap[v.ApplyAdminId]
  114. if !ok {
  115. findValList = make([]business_trip.BusinessApplyView, 0)
  116. }
  117. btMap[v.ApplyAdminId] = append(findValList, *v)
  118. }
  119. } else {
  120. findValList, ok := btMap[v.ApplyAdminId]
  121. if !ok {
  122. findValList = make([]business_trip.BusinessApplyView, 0)
  123. }
  124. btMap[v.ApplyAdminId] = append(findValList, *v)
  125. }
  126. // 如果出差申请已通过,同时存在出行人的情况
  127. if v.Status == "已通过" && v.PeerPeopleId != `` {
  128. peerPeopleIdList := strings.Split(v.PeerPeopleId, ",")
  129. for _, peerPeopleIdStr := range peerPeopleIdList {
  130. peerPeopleId, err := strconv.Atoi(peerPeopleIdStr)
  131. if err != nil {
  132. br.Msg = "获取信息失败!"
  133. br.ErrMsg = "获取路演信息失败!,随行人异常:" + v.PeerPeopleId + "; Err:" + err.Error()
  134. return
  135. }
  136. // 如果不是筛选,或者,该用户在随行人名单中,那么就插入到数据中
  137. if lenAdminIdList <= 0 || utils.InArrayByStr(adminIdList, strconv.Itoa(peerPeopleId)) {
  138. findpeerPeopleValList, peerPeoPleOk := btMap[peerPeopleId]
  139. if !peerPeoPleOk {
  140. findpeerPeopleValList = make([]business_trip.BusinessApplyView, 0)
  141. }
  142. btMap[peerPeopleId] = append(findpeerPeopleValList, *v)
  143. }
  144. }
  145. }
  146. }
  147. for i := 0; i < groupLen; i++ {
  148. group := groupList[i]
  149. fmt.Println("group:", group.DepartmentId)
  150. adminList, err := business_trip.GetBusinessTripCalendar(group.DepartmentId)
  151. if err != nil {
  152. br.Msg = "获取信息失败!"
  153. br.ErrMsg = "获取分组信息失败!,GetBusinessTripCalendar Err:" + err.Error()
  154. return
  155. }
  156. setAdminList := make([]*business_trip.BusinessTripCalendarAdmin, 0)
  157. for _, v := range adminList {
  158. if findTripList, ok := btMap[v.AdminId]; ok {
  159. tripList := make([]*business_trip.BusinessTripCalendar, 0)
  160. for day := 0; day < 7; day++ {
  161. newDay, _ := time.ParseInLocation(utils.FormatDate, nowWeekStart.AddDate(0, 0, day).Format(utils.FormatDate), time.Local)
  162. weekDate := newDay.Format(utils.FormatDate)
  163. tripItem := new(business_trip.BusinessTripCalendar)
  164. for _, r := range findTripList {
  165. startDateT, _ := time.ParseInLocation(utils.FormatDate, r.ArriveDate, time.Local)
  166. endDateT, _ := time.ParseInLocation(utils.FormatDate, r.ReturnDate, time.Local)
  167. if r.BusinessApplyId > 0 && (newDay.Equal(startDateT) || (newDay.Equal(endDateT)) || (newDay.Before(endDateT) && newDay.After(startDateT))) {
  168. tripItem.City = r.City
  169. tripItem.BusinessApplyId = r.BusinessApplyId
  170. tripItem.Status = r.Status
  171. tripItem.Reason = r.Reason
  172. }
  173. }
  174. tripItem.WeekDate = weekDate
  175. tripItem.WeekType = "current"
  176. tripItem.Week = newDay.Weekday().String()
  177. tripList = append(tripList, tripItem)
  178. }
  179. for day := 0; day < 7; day++ {
  180. newDay := nextWeekStart.AddDate(0, 0, day)
  181. weekDate := newDay.Format(utils.FormatDate)
  182. tripItem := new(business_trip.BusinessTripCalendar)
  183. for _, r := range findTripList {
  184. startDateT, _ := time.ParseInLocation(utils.FormatDate, r.ArriveDate, time.Local)
  185. endDateT, _ := time.ParseInLocation(utils.FormatDate, r.ReturnDate, time.Local)
  186. if r.BusinessApplyId > 0 && (newDay.Equal(startDateT) || (newDay.Equal(endDateT)) || (newDay.Before(endDateT) && newDay.After(startDateT))) {
  187. tripItem.City = r.City
  188. tripItem.BusinessApplyId = r.BusinessApplyId
  189. tripItem.Status = r.Status
  190. tripItem.Reason = r.Reason
  191. }
  192. }
  193. tripItem.WeekDate = weekDate
  194. tripItem.WeekType = "next"
  195. tripItem.Week = newDay.Weekday().String()
  196. tripList = append(tripList, tripItem)
  197. }
  198. v.BusinessTripList = tripList
  199. setAdminList = append(setAdminList, v)
  200. }
  201. }
  202. groupList[i].AdminList = setAdminList
  203. }
  204. resp := new(business_trip.BusinessTripCalendarResp)
  205. resp.GroupList = groupList
  206. resp.BaseDate = nowWeekStart.Format(utils.FormatDate)
  207. br.Ret = 200
  208. br.Success = true
  209. br.Msg = "获取成功"
  210. br.Data = resp
  211. return
  212. }