Holiday.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\admin\controller\base\other;
  3. /**
  4. * @title: 每年法定节假日
  5. */
  6. use app\admin\controller\Base;
  7. use app\common\model\base\other\Festival as festivalModel;
  8. use app\common\model\base\other\Holiday as holidayModel;
  9. class Holiday extends Base
  10. {
  11. protected $noNeedAuth = ['getyearholiday'];
  12. protected $holidayModel = null;
  13. protected $festivalModel = null;
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. $this->holidayModel = new holidayModel;
  18. $this->festivalModel = new festivalModel;
  19. }
  20. /**
  21. * @title: 获取各年度节日假期
  22. * @param {int} {pageNo} {非必填,默认值为1} {页码}
  23. * @param {int} {pageSize} {非必填,默认值为10} {每页数量}
  24. * @return array
  25. * @Author: wangkewei
  26. * @Date: 2021/5/18 9:20
  27. */
  28. public function getList($pageNo=1,$pageSize=10)
  29. {
  30. $list = [];
  31. // 年度列表
  32. $years = $this->holidayModel->field(['year'])->group('year')->select()->toArray();
  33. $years = array_column($years,'year');
  34. //节日列表
  35. $festivals = $this->festivalModel->order("weigh ASC")->select()->toArray();
  36. foreach($years as $year){
  37. $item = [];
  38. $item['year'] = $year;
  39. //年度所有假期
  40. $holidays = $this->holidayModel->where('year',$year)->select()->toArray();
  41. $holidays = array_column($holidays,null,'festival_id');
  42. foreach($festivals as $festival){
  43. $holiday = isset($holidays[$festival['festival_id']])?$holidays[$festival['festival_id']]:[];
  44. $festival = array_merge($festival,$holiday);
  45. $item['festival_'.$festival['festival_id']] = $festival;
  46. }
  47. $list[] = $item;
  48. }
  49. $list = FieldConverList($list);
  50. return res(1,"获取成功",$list);
  51. }
  52. /**
  53. * @title: 获取某一年度的假期情况
  54. * @param {string} {year} {非必填} {年}
  55. * @return array
  56. * @Author: wangkewei
  57. * @Date: 2021/5/18 9:21
  58. */
  59. public function getYearHoliday($year="")
  60. {
  61. $list = [];
  62. $festivals = $this->festivalModel->order("weigh ASC")->select()->toArray();
  63. foreach($festivals as $festival){
  64. if(!empty($year)){
  65. $holiday = $this->holidayModel->where('festival_id',$festival['festival_id'])->where('year',$year)->find();
  66. if($holiday){
  67. $holiday = $holiday->toArray();
  68. $holiday['daterange'][] = $holiday['start_date'];
  69. $holiday['daterange'][] = $holiday['end_date'];
  70. }else{
  71. $holiday = [];
  72. $holiday['daterange'] = [];
  73. }
  74. $list[] = array_merge($festival,$holiday);
  75. }else{
  76. $list[] = $festival;
  77. }
  78. }
  79. return res(1,"获取成功",$list);
  80. }
  81. /**
  82. * @title: 一年法定节假日编辑
  83. * @param array
  84. * @return array
  85. * @Author: wangkewei
  86. * @Date: 2021/5/18 9:23
  87. */
  88. public function doEdit()
  89. {
  90. $data = $this->request->param();
  91. $year = $data['year'];
  92. $list = $data['list'];
  93. try{
  94. $saveData = [];
  95. foreach($list as $row){
  96. if(!empty($row['daterange'])){
  97. $rangeInfo = $this->getDaysListRange($row['daterange']);
  98. $row['start_date'] = date("Y-m-d",strtotime($rangeInfo['start']));
  99. $row['end_date'] = date("Y-m-d",strtotime($rangeInfo['end']));
  100. $row['days'] = $rangeInfo['days'];
  101. $row['year'] = $year;
  102. $saveData[] = $row;
  103. }
  104. }
  105. $this->holidayModel->replace()->saveAll($saveData);
  106. slog(1,"编辑了".$year."年法定节假日");
  107. return res(1,"保存成功",$data);
  108. }catch(Exception $e){
  109. return res(2,"保存失败".$e->getMessage());
  110. }
  111. }
  112. /**
  113. * @title: 一年法定节假日删除
  114. * @param {string} {year} {非必填} {年}
  115. * @return array
  116. * @Author: wangkewei
  117. * @Date: 2021/5/18 9:24
  118. */
  119. public function doDelete($year = "")
  120. {
  121. if(empty($year))return res(2,"参数错误");
  122. $this->holidayModel->where("year",$year)->delete();
  123. slog(1,"删除了".$year."年法定节假日");
  124. return res(1,"删除成功");
  125. }
  126. /**
  127. * @title: 根据用户所传时间范围,列出范围内的每一天
  128. * @param {array} {datetimerange} {非必填} {时间区间}
  129. * @return array
  130. * @Author: wangkewei
  131. * @Date: 2021/5/18 9:25
  132. */
  133. private function getDaysListRange($datetimerange=[])
  134. {
  135. $list = [];
  136. if(!empty($datetimerange) && 2==count($datetimerange)){
  137. $start = date('Y-m-d 00:00:00',strtotime($datetimerange[0]));
  138. $end = date('Y-m-d 23:59:59',strtotime($datetimerange[1]));
  139. }else{
  140. $start = date('Y-m-01 00:00:00');
  141. $end = date('Y-m-d 23:59:59');
  142. }
  143. $days = (strtotime($end) - strtotime($start)) / 86400;
  144. for($i=0;$i<$days;$i++){
  145. $day = date('d',strtotime($start." + $i day"));
  146. $month = date('m',strtotime($start." + $i day"));
  147. $year = date('Y',strtotime($start." + $i day"));
  148. $date = date('Y-m-d',strtotime($start." + $i day"));
  149. $list[] = [
  150. 'year' => $year,
  151. 'month' => $month,
  152. 'day' => $day,
  153. 'date' => $date
  154. ];
  155. }
  156. return ['start'=>$start,'end'=>$end,'list'=>$list,'days'=>$days];
  157. }
  158. }