AttendancePunchRecord.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\ExcelUtil;
  4. use think\Db;
  5. use think\Exception;
  6. class AttendancePunchRecord extends Auth
  7. {
  8. public function index(){
  9. if(request()->isAjax()){
  10. //分页参数
  11. $length = input('rows',10,'intval'); //每页条数
  12. $page = input('page',1,'intval'); //第几页
  13. $start = ($page - 1) * $length; //分页开始位置
  14. //排序
  15. $sortRow = input('sidx','id','trim'); //排序列
  16. $sort = input('sord','desc','trim'); //排序方式
  17. $order = $sortRow.' '.$sort;
  18. $title = input('title','','trim');
  19. if($title){
  20. $map[] = ['user_name','like','%'.$title.'%'];
  21. }
  22. $month = input('month',date('Y-m'),'trim');
  23. if($month){
  24. $map[] = ['month','=',$month];
  25. }
  26. $map[] = ['del','=',0];
  27. $map[] = ['org_id','=',$this->orgId];
  28. $map= empty($map) ? true: $map;
  29. //数据查询
  30. $lists = db('attendance_punch_record')
  31. ->where($map)
  32. ->limit($start,$length)
  33. ->order($order)
  34. ->select();
  35. foreach ($lists as $k=>$v){
  36. $lists[$k]['k'] = $k+1;
  37. }
  38. //数据返回
  39. $totalCount = db('attendance_punch_record')->where($map)->count();
  40. $totalPage = ceil($totalCount/$length);
  41. $result['page'] = $page;
  42. $result['total'] = $totalPage;
  43. $result['records'] = $totalCount;
  44. $result['rows'] = $lists;
  45. return json($result);
  46. }else{
  47. $this->assign('month',date('Y-m'));
  48. return $this->fetch();
  49. }
  50. }
  51. /**
  52. * 删除记录
  53. * @param int $id
  54. */
  55. public function del($id=0){
  56. if(!$id){
  57. $this->error('参数错误');
  58. }
  59. $res = db('attendance_punch_record')->where('id',$id)->setField('del',1);
  60. if($res){
  61. $this->success('删除成功');
  62. }else{
  63. $this->error('删除失败');
  64. }
  65. }
  66. public function import(){
  67. return $this->fetch();
  68. }
  69. /**
  70. * 下载点模板
  71. */
  72. public function downloadtem(){
  73. set_time_limit(0);
  74. ini_set("memory_limit","512M");
  75. $header = [
  76. ['title' => '序号', 'name' => 'key','width'=>'20'],
  77. ['title' => '项目名称', 'name' => 'org_name','width'=>'20'],
  78. ['title' => '部门', 'name' => 'dep_name','width'=>'20'],
  79. ['title' => '打卡人员', 'name' => 'user_name','width'=>'20'],
  80. ['title' => '打卡方式', 'name' => 'way','width'=>'20'],
  81. ['title' => '班次', 'name' => 'class_content','width'=>'20'],
  82. ['title' => '打卡日期', 'name' => 'date','width'=>'20'],
  83. ['title' => '打卡时间', 'name' => 'time','width'=>'20'],
  84. ['title' => '打卡地点', 'name' => 'address','width'=>'20'],
  85. ['title' => '打卡设备识别码', 'name' => 'device_sn','width'=>'20'],
  86. ];
  87. $lists = [
  88. [
  89. 'key' => '1',
  90. 'org_name' => 'xxx项目',
  91. 'dep_name' => '内科',
  92. 'user_name' => '张三',
  93. 'way' => '移动考勤',
  94. 'class_content' => '7:30-5:30',
  95. 'date' => '2024/5/10',
  96. 'time' => '8:12:29',
  97. 'address' => '山东省济南市历下区经十路16766号',
  98. 'device_sn' => '1C52EE95-1226-4C85-AB5E-0C3E13494FEC',
  99. ]
  100. ];
  101. $filename = '打卡记录模板';
  102. ExcelUtil::export($filename,$header,$lists);
  103. }
  104. public function importexcel(){
  105. set_time_limit(0);
  106. ini_set("memory_limit", -1);
  107. ob_flush();//清空缓存
  108. flush();//刷新缓存
  109. try{
  110. $cols = ['key','org_name','dep_name','user_name','way','class_content','date','time','address','device_sn'];
  111. $lists = ExcelUtil::importExcel('file',$cols,2);
  112. if($lists === false){
  113. exit(ExcelUtil::getError());
  114. }
  115. if(empty($lists)){
  116. exit('文件内容为空');
  117. }
  118. foreach ($lists as $k=>$v){
  119. if(!$v['user_name']){
  120. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称不存在,未导入</font><br />";
  121. continue;
  122. }
  123. $dt = [
  124. 'org_id'=>$this->orgId,
  125. 'user_name'=>$v['user_name'],
  126. 'dep_name'=>$v['dep_name'],
  127. 'org_name'=>$v['org_name'],
  128. 'way'=>$v['way'],
  129. 'class_content'=>$v['class_content'],
  130. 'date'=>date('Y-m-d',strtotime($v['date'])),
  131. 'time'=>$v['time'],
  132. 'address'=>$v['address'],
  133. 'device_sn'=>$v['device_sn'],
  134. 'month'=>date('Y-m',strtotime($v['date'])),
  135. 'create_time'=>date('Y-m-d H:i:s'),
  136. ];
  137. $inset = Db::name('attendance_punch_record')->insert($dt);
  138. if(!$inset){
  139. $msg = "第".($k+2)."行,导入失败";
  140. echo "<font color=\"red\">".$msg."</font><br />";
  141. continue;
  142. }
  143. }
  144. echo "<font color=\"green\">导入完成</font><br />";
  145. }catch (Exception $e){
  146. trace($e->getMessage(),'error');
  147. echo $e->getMessage();
  148. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  149. }
  150. }
  151. public function export(){
  152. set_time_limit(0);
  153. ini_set("memory_limit","512M");
  154. $title = input('title','','trim');
  155. if($title){
  156. $map[] = ['user_name','like','%'.$title.'%'];
  157. }
  158. $month = input('month',date('Y-m'),'trim');
  159. if($month){
  160. $map[] = ['month','=',$month];
  161. }
  162. $map[] = ['del','=',0];
  163. $map[] = ['org_id','=',$this->orgId];
  164. $map= empty($map) ? true: $map;
  165. //数据查询
  166. $lists = db('attendance_punch_record')
  167. ->where($map)
  168. ->order('id desc')
  169. ->select();
  170. foreach ($lists as $k=>$v){
  171. $lists[$k]['k'] = $k+1;
  172. }
  173. $header = [
  174. ['title' => '序号', 'name' => 'k','width'=>'20'],
  175. ['title' => '项目名称', 'name' => 'org_name','width'=>'20'],
  176. ['title' => '部门', 'name' => 'dep_name','width'=>'20'],
  177. ['title' => '打卡人员', 'name' => 'user_name','width'=>'20'],
  178. ['title' => '打卡方式', 'name' => 'way','width'=>'20'],
  179. ['title' => '班次', 'name' => 'class_content','width'=>'20'],
  180. ['title' => '打卡日期', 'name' => 'date','width'=>'20'],
  181. ['title' => '打卡时间', 'name' => 'time','width'=>'20'],
  182. ['title' => '打卡地点', 'name' => 'address','width'=>'20'],
  183. ['title' => '打卡设备识别码', 'name' => 'device_sn','width'=>'20'],
  184. ];
  185. $filename = '打卡记录';
  186. ExcelUtil::export($filename,$header,$lists);
  187. }
  188. public function delAll(){
  189. if(request()->isPost()){
  190. $data = request()->post();
  191. $ids = isset($data['ids'])&&!empty($data['ids']) ? $data['ids']:[];
  192. if(!$ids){
  193. $this->error('请选择记录');
  194. }
  195. $res = Db::name('attendance_punch_record')->whereIn('id',$ids)->setField('del',1);
  196. if($res){
  197. $this->success('操作成功',url('index'));
  198. }else{
  199. $this->error('操作失败');
  200. }
  201. }else{
  202. return $this->fetch();
  203. }
  204. }
  205. }