AttendanceMachine.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class AttendanceMachine extends Auth
  5. {
  6. public function index(){
  7. if(request()->isAjax()){
  8. //分页参数
  9. $length = input('rows',10,'intval'); //每页条数
  10. $page = input('page',1,'intval'); //第几页
  11. $start = ($page - 1) * $length; //分页开始位置
  12. //排序
  13. $sortRow = input('sidx','id','trim'); //排序列
  14. $sort = input('sord','desc','trim'); //排序方式
  15. $order = $sortRow.' '.$sort;
  16. $sn = input('sn','','trim');
  17. if($sn){
  18. $map[] = ['sn','like','%'.$sn.'%'];
  19. }
  20. $map[] = ['org_id','=',$this->orgId];
  21. $map[] = ['del','=',0];
  22. $map= empty($map) ? true: $map;
  23. //数据查询
  24. $lists = db('attendance_machine')->limit($start,$length)->where($map)->order(['id'=>'desc'])->select();
  25. foreach ($lists as $k=>$v){
  26. $lists[$k]['org_name'] = db('org')->where('id',$v['org_id'])->value('name');
  27. }
  28. //数据返回
  29. $totalCount = db('attendance_machine')->where($map)->count();
  30. $totalPage = ceil($totalCount/$length);
  31. $result['page'] = $page;
  32. $result['total'] = $totalPage;
  33. $result['records'] = $totalCount;
  34. $result['rows'] = $lists;
  35. return json($result);
  36. }else{
  37. $orgList = db('org')->where('del',0)->where('enable',1)->where('type',2)->select();
  38. $this->assign('orgList',$orgList);
  39. return $this->fetch();
  40. }
  41. }
  42. /**
  43. * 新增/编辑
  44. */
  45. public function add($id=0){
  46. if(request()->isPost()){
  47. $res = model('AttendanceMachine')->updates();
  48. if($res){
  49. $this->success('操作成功',url('index'));
  50. }else{
  51. $this->error(model('AttendanceMachine')->getError());
  52. }
  53. }else{
  54. $title = '新增';
  55. if($id){
  56. $title = '编辑';
  57. $info = db('attendance_machine')->where('id',$id)->find();
  58. $this->assign('info',$info);
  59. }
  60. $orgList = db('org')->where('del',0)->where('enable',1)->where('type',2)->select();
  61. $this->assign('orgList',$orgList);
  62. $this->assign('title',$title);
  63. return $this->fetch();
  64. }
  65. }
  66. /**
  67. * 删除记录
  68. * @param int $id
  69. */
  70. public function del($id=0){
  71. if(!$id){
  72. $this->error('参数错误');
  73. }
  74. $res = db('attendance_machine')->where('id',$id)->setField('del',1);
  75. if($res){
  76. $this->success('删除成功');
  77. }else{
  78. $this->error('删除失败');
  79. }
  80. }
  81. // 清空设备数据
  82. public function clear(){
  83. $id = input('id/d',0);
  84. $info = model('AttendanceMachine')->info($id);
  85. $title = "清除全部数据";
  86. $command = 'C:${CmdID}:CLEAR DATA';
  87. $ret = model('AttendanceMachineCmd')->add($id,$title,$command);
  88. if($ret){
  89. $this->success('命令添加成功');
  90. }else{
  91. $this->error('命令添加失败');
  92. }
  93. }
  94. // 重新导入数据
  95. public function reload(){
  96. $id = input('id/d',0);
  97. $info = model('AttendanceMachine')->info($id);
  98. if($info['org_id'] > 0){
  99. $users = db('user')
  100. ->alias('u')
  101. ->join('user_org uo','uo.user_id = u.id')
  102. ->where('u.del',0)
  103. ->where('u.enable',1)
  104. ->where('uo.org_id',$info['org_id'])
  105. ->where('u.kq_img','<>','')
  106. ->field('u.id,u.kq_name as name,u.kq_img as img')
  107. ->select();
  108. $users = $users?$users:[];
  109. foreach ($users as $k=>$v){
  110. $users[$k]['card'] = 1000000000 + $v['id']; // 物业用户
  111. $users[$k]['id'] = "WY{$v['id']}";
  112. }
  113. }
  114. if(empty($users) && !isset($users)){
  115. $this->error('当前项目无打卡人员');
  116. }
  117. Db::startTrans();
  118. try{
  119. // 先清空数据,再导入
  120. $title = "清除全部数据";
  121. $command = 'C:${CmdID}:CLEAR DATA';
  122. $ret = model('AttendanceMachineCmd')->add($id,$title,$command);
  123. if(!$ret){
  124. exception('清空数据添加失败');
  125. }
  126. // 导入超级管理员
  127. $admin = config('app.machine_admin');
  128. $ret = model('AttendanceMachineCmd')->updateAdminInfo($info['id'],$admin['id'],$admin['name'],$admin['card']);
  129. if(!$ret){
  130. exception('操作失败');
  131. }
  132. foreach ($users as $k=>$v){
  133. $ret = model('AttendanceMachineCmd')->updateUserInfo($info['id'],$v['id'],$v['name'],$v['card']);
  134. if(!$ret){
  135. exception('操作失败');
  136. }
  137. $ret = model('AttendanceMachineCmd')->updateUserImg($info['id'],$v['id'],$v['name'],$v['img']);
  138. if(!$ret){
  139. exception('操作失败');
  140. }
  141. }
  142. Db::commit();
  143. $this->success('命令添加成功');
  144. }catch (Exception $e){
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. }
  148. }
  149. }