PHPExcel.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\common\util;
  3. use app\api\controller\BaseAuthorized;
  4. use PHPExcel_IOFactory;
  5. use think\facade\Db;
  6. class PHPExcel{
  7. public static function import($path,$login_staff)
  8. {
  9. $LeaveModel = new \app\common\model\Leave();
  10. $result = [
  11. 'code'=>999,
  12. 'msg'=>'错误'
  13. ];
  14. //实例化PHPExcel类
  15. $path = public_path().'storage/'.$path;
  16. $PHPExcel = new \PHPExcel();
  17. //默认用excel2007读取excel,若格式不对,则用之前的版本进行读取
  18. $PHPReader = new \PHPExcel_Reader_Excel2007();
  19. if (!$PHPReader->canRead($path)) {
  20. $PHPReader = new \PHPExcel_Reader_Excel5();
  21. if (!$PHPReader->canRead($path)) {
  22. $result['msg'] = '请上传excel文件';
  23. return $result;
  24. }
  25. }
  26. //读取Excel文件
  27. $PHPExcel = $PHPReader->load($path);
  28. //读取excel文件中的第一个工作表
  29. $sheet = $PHPExcel->getSheet(0);
  30. //取得最大的列号
  31. $allColumn = $sheet->getHighestColumn();
  32. //取得最大的行号
  33. $allRow = $sheet->getHighestRow();
  34. #清除表
  35. //从第二行开始插入,第一行是列名
  36. $month = [];
  37. $month2 = [];
  38. $i = 0;
  39. //检测数据
  40. for ($currentRow = 4; $currentRow <= $allRow; $currentRow++){
  41. $number = $PHPExcel->getActiveSheet()->getCell("A" . $currentRow)->getValue();
  42. if(!$number){
  43. break;
  44. }
  45. $array = [
  46. 'month'=>$PHPExcel->getActiveSheet()->getCell("B" . $currentRow)->getValue(),
  47. 'phone'=>$PHPExcel->getActiveSheet()->getCell("C" . $currentRow)->getValue(),
  48. // 'name'=>$PHPExcel->getActiveSheet()->getCell("D" . $currentRow)->getValue(),
  49. 'days'=>$PHPExcel->getActiveSheet()->getCell("E" . $currentRow)->getValue(),
  50. 'real_days'=>$PHPExcel->getActiveSheet()->getCell("F" . $currentRow)->getValue(),
  51. 'late'=>$PHPExcel->getActiveSheet()->getCell("G" . $currentRow)->getValue(),
  52. 'leave'=>$PHPExcel->getActiveSheet()->getCell("H" . $currentRow)->getValue(),
  53. 'absenteeism'=>$PHPExcel->getActiveSheet()->getCell("I" . $currentRow)->getValue(),
  54. 'late_deduct'=>$PHPExcel->getActiveSheet()->getCell("J" . $currentRow)->getValue(),
  55. 'leave_deduct'=>$PHPExcel->getActiveSheet()->getCell("K" . $currentRow)->getValue(),
  56. 'absenteeism_deduct'=>$PHPExcel->getActiveSheet()->getCell("L" . $currentRow)->getValue(),
  57. 'other_deduct'=>$PHPExcel->getActiveSheet()->getCell("M" . $currentRow)->getValue(),
  58. 'sum_deduct'=>$PHPExcel->getActiveSheet()->getCell("N" . $currentRow)->getValue(),
  59. 'other_reward'=>$PHPExcel->getActiveSheet()->getCell("O" . $currentRow)->getValue(),
  60. 'remark'=>$PHPExcel->getActiveSheet()->getCell("P" . $currentRow)->getValue(),
  61. 'create_time'=>date('Y-m-d'),
  62. 'valid'=>1,
  63. 'enterprise_id'=>$login_staff->enterprise_id,
  64. ];
  65. $month[$i][$array['month']] = $array['phone'];
  66. $month2[$i][$array['phone']] = $array['month'];
  67. $i++;
  68. $user = \app\common\model\User::where('phone',$array['phone'])->find();
  69. if (!$user){
  70. $msg = '导入数据错误,请检查报表格式或者手机号数据·1';
  71. $result['msg'] = $msg;
  72. return $result;
  73. }
  74. $staff = \app\common\model\Staff::where(['user_id'=>$user->id,'enterprise_id'=>$login_staff->enterprise_id])->find();
  75. if (!$staff){
  76. $msg = '导入数据错误,请检查报表格式或者手机号数据·2';
  77. $result['msg'] = $msg;
  78. return $result;
  79. }
  80. if ($array['days']>31){
  81. $msg = $staff->name.'应出勤天数不得超过31天';
  82. $result['msg'] = $msg;
  83. return $result;
  84. }
  85. if ($array['late_deduct']+$array['leave_deduct']+$array['absenteeism_deduct']+$array['other_deduct']!=$array['sum_deduct']){
  86. $msg = $staff->name.'扣款合计不正确';
  87. $result['msg'] = $msg;
  88. return $result;
  89. }
  90. }
  91. if (count(self::get_repeat_data($month))){
  92. $msg = '导入数据重复';
  93. $result['msg'] = $msg;
  94. return $result;
  95. }
  96. for ($currentRow = 4; $currentRow <= $allRow; $currentRow++) {
  97. //获取B列的值
  98. $number = $PHPExcel->getActiveSheet()->getCell("A" . $currentRow)->getValue();
  99. if(!$number){
  100. $msg = '导入成功';
  101. $result['code'] = 0;
  102. $result['msg'] = $msg;
  103. break;
  104. }
  105. $data = [
  106. 'month'=>$PHPExcel->getActiveSheet()->getCell("B" . $currentRow)->getValue(),
  107. 'phone'=>$PHPExcel->getActiveSheet()->getCell("C" . $currentRow)->getValue(),
  108. // 'name'=>$PHPExcel->getActiveSheet()->getCell("D" . $currentRow)->getValue(),
  109. 'days'=>$PHPExcel->getActiveSheet()->getCell("E" . $currentRow)->getValue(),
  110. 'real_days'=>$PHPExcel->getActiveSheet()->getCell("F" . $currentRow)->getValue(),
  111. 'late'=>$PHPExcel->getActiveSheet()->getCell("G" . $currentRow)->getValue(),
  112. 'leave'=>$PHPExcel->getActiveSheet()->getCell("H" . $currentRow)->getValue(),
  113. 'absenteeism'=>$PHPExcel->getActiveSheet()->getCell("I" . $currentRow)->getValue(),
  114. 'late_deduct'=>$PHPExcel->getActiveSheet()->getCell("J" . $currentRow)->getValue(),
  115. 'leave_deduct'=>$PHPExcel->getActiveSheet()->getCell("K" . $currentRow)->getValue(),
  116. 'absenteeism_deduct'=>$PHPExcel->getActiveSheet()->getCell("L" . $currentRow)->getValue(),
  117. 'other_deduct'=>$PHPExcel->getActiveSheet()->getCell("M" . $currentRow)->getValue(),
  118. 'sum_deduct'=>$PHPExcel->getActiveSheet()->getCell("N" . $currentRow)->getValue(),
  119. 'other_reward'=>$PHPExcel->getActiveSheet()->getCell("O" . $currentRow)->getValue(),
  120. 'remark'=>$PHPExcel->getActiveSheet()->getCell("P" . $currentRow)->getValue(),
  121. 'create_time'=>date('Y-m-d'),
  122. 'valid'=>1,
  123. 'enterprise_id'=>$login_staff->enterprise_id,
  124. // 'time'=>self::get_date_by_excel($PHPExcel->getActiveSheet()->getCell("F" . $currentRow)->getValue()),
  125. ];
  126. $user = \app\common\model\User::where('phone',$data['phone'])->find();
  127. $staff = \app\common\model\Staff::where(['user_id'=>$user->id,'enterprise_id'=>$login_staff->enterprise_id])->find();
  128. $data['staff_id'] = $staff->id;
  129. $data['name'] = $staff->name;
  130. $where['enterprise_id']=$login_staff->enterprise_id;
  131. $where['month']=$data['month'];
  132. $where['staff_id']=$data['staff_id'];
  133. $LeaveModel->where($where)->delete();
  134. #插入表
  135. $LeaveModel = new \app\common\model\Leave();
  136. $LeaveModel->save($data);
  137. }
  138. return $result;
  139. }
  140. //获取二维数组的重复数据
  141. public static function get_repeat_data($array){
  142. //这里的$array在数据库查询的时候要group by排序一下
  143. //计算出数组的总数量
  144. $count = count($array);
  145. $repeat_data = [];
  146. //循环从0开始逐次+1
  147. for($i=0;$i<$count;$i++){
  148. //每次都比第一层循环+1,如果第一层循环0数据,第二层就循环1数据,以此类推
  149. for($j=$i+1;$j<$count;$j++){
  150. //比较两次数据是否相等
  151. if($array[$i] == $array[$j]){
  152. $repeat_data[$i]=$array[$i];
  153. $repeat_data[$j]=$array[$j];
  154. }
  155. }
  156. }
  157. return $repeat_data;
  158. }
  159. }