UserLabor.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class UserLabor extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'user_id' => input('uid/d',0),
  13. 'status' => input('status/d',0),
  14. 'labor' => input('labor/f','','trim'),
  15. 'other' => input('other/f','','trim'),
  16. 'child' => input('child/f','','trim'),
  17. 'remark' => input('remark','','trim'),
  18. 'company_id' => input('companyId/d',0)
  19. ];
  20. $result = validate('UserLabor')->check($data,[],'');
  21. if(true !== $result){
  22. HelpHander::error(validate('UserLabor')->getError());
  23. }
  24. $id = $data['id'];
  25. unset($data['id']);
  26. if($id > 0){
  27. $data['update_time'] = date('Y-m-d H:i:s');
  28. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  29. }else{
  30. $data['create_time'] = date('Y-m-d H:i:s');
  31. $ret = $this->allowField(true)->save($data);
  32. }
  33. if(!$ret){
  34. HelpHander::error('操作失败');
  35. }
  36. return true;
  37. }
  38. public function info($id){
  39. $info = $this->where('id',$id)->find();
  40. if(!$info){
  41. HelpHander::error('数据不存在');
  42. }
  43. $data = $info->toArray();
  44. $data['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  45. return $data;
  46. }
  47. public function lists($page,$size,$orgId,$name,$phone,$status){
  48. if($name != ''){
  49. $map[] = ['ui.name','like','%'.$name.'%'];
  50. }
  51. if($phone != ''){
  52. $map[] = ['u.phone','like','%'.$phone.'%'];
  53. }
  54. if($status >= 0){
  55. $map[] = ['ur.status','=',$status];
  56. }
  57. $map = empty($map)?true:$map;
  58. $lists = Db::name('user_labor')
  59. ->alias('ur')
  60. ->join('user u','u.id = ur.user_id')
  61. ->join('user_info ui','ui.user_id = ur.user_id')
  62. ->where($map)
  63. ->field('ur.*,u.phone,ui.name,ui.gender')
  64. ->page($page,$size)
  65. ->order('id desc')
  66. ->select();
  67. foreach ($lists as $k=>$v){
  68. $lists[$k]['company'] = Db::name('company')->where('id',$v['company_id'])->value('title');
  69. }
  70. $total = Db::name('user_labor')
  71. ->alias('ur')
  72. ->join('user u','u.id = ur.user_id')
  73. ->join('user_info ui','ui.user_id = ur.user_id')
  74. ->where($map)->count();
  75. $data = [
  76. 'total' => $total,
  77. 'list' => $lists?$lists:[]
  78. ];
  79. return $data;
  80. }
  81. public function del($id){
  82. $ret = $this->where('id',$id)->delete();
  83. if(!$ret){
  84. HelpHander::error('删除失败');
  85. }
  86. return true;
  87. }
  88. public function changeStatus($id,$enable){
  89. $ret = Db::name('user_labor')->where('id',$id)->setField('status',$enable);
  90. if(!$ret){
  91. HelpHander::error('操作失败');
  92. }
  93. return true;
  94. }
  95. public function laborUser($page,$size,$name,$phone,$orgId){
  96. // $uids = Db::name('user_labor')->column('user_id');
  97. $map[] = ['ui.is_working','in','1,2'];
  98. if($phone){
  99. $map[] = ['u.phone','like','%'.$phone.'%'];
  100. }
  101. if($name){
  102. $map[] = ['ui.name','like','%'.$name.'%'];
  103. }
  104. // if($uids){
  105. // $map[] = ['u.id','not in',$uids];
  106. // }
  107. $lists = Db::name('user')
  108. ->alias('u')
  109. ->join('user_info ui','ui.user_id = u.id')
  110. ->where($map)
  111. ->field('u.id,u.phone,ui.name,ui.join_time,ui.gender')
  112. ->page($page,$size)
  113. ->order('ui.id desc')
  114. ->select();
  115. $total = Db::name('user')
  116. ->alias('u')
  117. ->join('user_info ui','ui.user_id = u.id')
  118. ->where($map)->count();
  119. $data = [
  120. 'total' => $total,
  121. 'list' => $lists?$lists:[]
  122. ];
  123. return $data;
  124. }
  125. }