123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use tools\Idcard;
- class Worker extends Base
- {
- public $status = [
- 0=>'待审核',
- 1=>'同意',
- 2=>'拒绝',
- ];
- public function updates($orgId){
- $data = request()->post();
- $validate = new \app\common\validate\User();
- $result = $validate->check($data,[],'add');
- if(true !== $result){
- $this->error = $validate->getError();
- return false;
- }
- $validate = new \app\common\validate\Worker();
- $result = $validate->check($data,[]);
- if(true !== $result){
- $this->error = $validate->getError();
- return false;
- }
- $idCard = new Idcard();
- if(!$idCard->isChinaIDCard($data['card'])){
- $this->error = '身份证格式错误';
- return false;
- }
- $userData = [
- 'id' => input('id/d',0),
- 'account' => input('account','','trim'),
- 'real_name' => input('real_name','','trim'),
- 'mobile' => input('mobile','','trim'),
- 'password' => input('password','','trim'),
- 'head_image' => input('head_image','','trim'),
- 'depId' => input('depId/d',0),
- 'rolesId' => input('rolesId/d',0),
- 'enable' => input('enable/d',1),
- ];
- Db::startTrans();
- try{
- $id = $userData['id'];
- unset($userData['id']);
- $depId = $userData['depId'];
- $rolesId = $userData['rolesId'];
- unset($userData['depId']);
- unset($userData['rolesId']);
- if($id > 0){
- if(!$userData['password']){
- unset($userData['password']);
- }
- $userData['update_time'] = date('Y-m-d H:i:s');
- $ret =Db::name('user')
- ->where(['id'=>$id])
- ->update($userData);
- if(!$ret){
- \exception('操作失败');
- }
- Db::name('user_dep')
- ->where('user_id',$id)
- ->setField('dep_id',$depId);
- Db::name('user_roles')
- ->where('user_id',$id)
- ->setField('roles_id',$rolesId);
- $workerData = [
- 'birthday'=>$idCard->getBirthday($data['card']),
- 'gender'=>$idCard->getChinaIDCardSex($data['card'])=='男'?1:2,
- 'card_imgs'=>!empty($data['card_imgs'])?implode(',',$data['card_imgs']):'' ,
- 'id_imgs'=>!empty($data['id_imgs'])?implode(',',$data['id_imgs']):'',
- 'work'=>$data['work']?$data['work']:null,
- 'content'=>$data['content'],
- 'recommend'=>$data['recommend'],
- 'card'=>$data['card'],
- 'update_time'=>getTime(),
- ];
- $res = $this->where('user_id',$id)
- ->update($workerData);
- if(!$res){
- \exception('护工信息更新失败');
- }
- }else{
- $userData['create_time'] = date('Y-m-d H:i:s');
- $userData['ip'] = request()->ip();
- $ret = Db::name('user')->insertGetId($userData);
- if(!$ret){
- \exception('操作失败');
- }
- Db::name('user_org')
- ->insert(['user_id'=>$ret,'org_id'=>$orgId]);
- Db::name('user_dep')
- ->insert(['user_id'=>$ret,'dep_id'=>$depId]);
- Db::name('user_roles')
- ->insert(['user_id'=>$ret,'roles_id'=>$rolesId]);
- }
- $workerData = [
- 'user_id'=>$ret,
- 'birthday'=>$idCard->getBirthday($data['card']),
- 'gender'=>$idCard->getChinaIDCardSex($data['card'])=='男'?1:2,
- 'card_imgs'=>!empty($data['card_imgs'])?implode(',',$data['card_imgs']):'' ,
- 'id_imgs'=>!empty($data['id_imgs'])?implode(',',$data['id_imgs']):'',
- 'work'=>$data['work']?$data['work']:null,
- 'content'=>$data['content'],
- 'recommend'=>$data['recommend'],
- 'card'=>$data['card'],
- ];
- $res = $this->save($workerData);
- if(!$res){
- \exception('护工信息更新失败');
- }
- Db::commit();
- return true;
- }catch (Exception $e){
- Db::rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- //护工列表
- public function lists($orgId,$where,$page,$size){
- $map[] = ['o.org_id','=',$orgId];
- $map[] = ['u.del','=',0];
- if(isset($where['name']) && $where['name']!=''){
- $map[] = ['u.real_name','like','%'.$where['name'].'%'];
- }
- if(isset($where['enable']) && $where['enable']!=''){
- $map[] = ['u.enable','=',$where['enable']];
- }
- if(isset($where['recommend']) && $where['recommend']!=''){
- $map[] = ['a.recommend','=',$where['recommend']];
- }
- if(isset($where['gender']) && $where['gender']!=''){
- $map[] = ['a.gender','=',$where['gender']];
- }
- if(isset($where['balance']) && $where['balance']!=''){
- $map[] = ['a.balance','>',0];
- }
- $roles = Db::name('roles')
- ->where('parent_id',11)
- ->where('del',0)
- ->where('enable',1)
- ->column('id');
- if(empty($roles)){
- $result['zhZye'] = 0;
- $result['page'] = $page;
- $result['total'] = 0;
- $result['records'] = [];
- $result['rows'] = 0;
- return $result;
- }
- $map[] = ['r.roles_id','in',$roles];
- $offset = ($page-1)*$size;
- $list = $this->alias('a')
- ->join('user u','a.user_id=u.id')
- ->join('user_org o','a.user_id=o.user_id')
- ->join('user_roles r','a.user_id=r.user_id')
- ->field('u.real_name,u.enable,u.mobile,u.create_time,u.head_image,a.*')
- ->limit($offset,$size)
- ->where($map)
- ->order('a.id','desc')
- ->select();
- $count = $this->alias('a')
- ->join('user u','a.user_id=u.id')
- ->join('user_org o','a.user_id=o.user_id')
- ->join('user_roles r','a.user_id=r.user_id')
- ->field('u.real_name,u.mobile,u.create_time,u.head_image,a.*')
- ->where($map)
- ->count();
- $lists = $list?$list->toArray():[];
- foreach ($lists as $k=>$v){
- $lists[$k]['work'] = $v['work']?birthday($v['work']):0;
- $lists[$k]['content'] = $v['content']?$v['content']:'';
- $lists[$k]['birthday'] = $v['birthday']?$v['birthday']:'';
- $lists[$k]['card_imgs'] = $v['card_imgs']?$v['card_imgs']:'';
- $lists[$k]['id_imgs'] = $v['id_imgs']?$v['id_imgs']:'';
- $lists[$k]['update_time'] = $v['update_time']?$v['update_time']:'';
- $lists[$k]['card'] = $v['card']?$v['card']:'';
- $lists[$k]['age'] = $v['birthday']?birthday($v['birthday']):0;
- }
- $result['zhZye'] = $this->alias('a')
- ->join('user u','a.user_id=u.id')
- ->join('user_org o','a.user_id=o.user_id')
- ->join('user_roles r','a.user_id=r.user_id')
- ->field('u.real_name,u.mobile,u.create_time,u.head_image,a.*')
- ->where($map)
- ->sum('a.balance');;
- $totalPage = ceil($count/$size);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $count;
- $result['rows'] = $lists;
- return $result;
- }
- public function apiLists($orgId,$where,$page,$size){
- $map[] = ['o.org_id','=',$orgId];
- $map[] = ['u.del','=',0];
- $map[] = ['u.enable','=',1];
- if(isset($where['name']) && $where['name']!=''){
- $map[] = ['u.real_name','like','%'.$where['name'].'%'];
- }
- if(isset($where['enable']) && $where['enable']!=''){
- $map[] = ['u.enable','=',$where['enable']];
- }
- if(isset($where['recommend']) && $where['recommend']!=''){
- $map[] = ['a.recommend','=',$where['recommend']];
- }
- if(isset($where['gender']) && $where['gender']!=''){
- $map[] = ['a.gender','=',$where['gender']];
- }
- if(isset($where['balance']) && $where['balance']!=''){
- $map[] = ['a.balance','>',0];
- }
- $roles = Db::name('roles')
- ->where('parent_id',11)
- ->where('del',0)
- ->where('enable',1)
- ->column('id');
- if(empty($roles)){
- $result['zhZye'] = 0;
- $result['page'] = $page;
- $result['total'] = 0;
- $result['records'] = [];
- $result['rows'] = 0;
- return $result;
- }
- $map[] = ['r.roles_id','in',$roles];
- $offset = ($page-1)*$size;
- $list = $this->alias('a')
- ->join('user u','a.user_id=u.id')
- ->join('user_org o','a.user_id=o.user_id')
- ->join('user_roles r','a.user_id=r.user_id')
- ->field('u.real_name,u.enable,u.mobile,u.create_time,u.head_image,a.*')
- ->limit($offset,$size)
- ->where($map)
- ->order('a.id','desc')
- ->select();
- $count = $this->alias('a')
- ->join('user u','a.user_id=u.id')
- ->join('user_org o','a.user_id=o.user_id')
- ->join('user_roles r','a.user_id=r.user_id')
- ->field('u.real_name,u.mobile,u.create_time,u.head_image,a.*')
- ->where($map)
- ->count();
- $lists = $list?$list->toArray():[];
- foreach ($lists as $k=>$v){
- $lists[$k]['work'] = $v['work']?birthday($v['work']):0;
- $lists[$k]['content'] = $v['content']?$v['content']:'';
- $lists[$k]['birthday'] = $v['birthday']?$v['birthday']:'';
- $lists[$k]['card_imgs'] = $v['card_imgs']?$v['card_imgs']:'';
- $lists[$k]['id_imgs'] = $v['id_imgs']?$v['id_imgs']:'';
- $lists[$k]['update_time'] = $v['update_time']?$v['update_time']:'';
- $lists[$k]['card'] = $v['card']?$v['card']:'';
- $lists[$k]['age'] = $v['birthday']?birthday($v['birthday']):0;
- }
- $result['zhZye'] = $this->alias('a')
- ->join('user u','a.user_id=u.id')
- ->join('user_org o','a.user_id=o.user_id')
- ->join('user_roles r','a.user_id=r.user_id')
- ->field('u.real_name,u.mobile,u.create_time,u.head_image,a.*')
- ->where($map)
- ->sum('a.balance');;
- $totalPage = ceil($count/$size);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $count;
- $result['rows'] = $lists;
- return $result;
- }
- public function getUserInfo($id){
- $info = db('user')
- ->alias('u')
- ->join('user_dep ud','ud.user_id = u.id')
- ->join('user_roles ur','ur.user_id = u.id')
- ->join('worker a','u.id = a.user_id')
- ->field('u.account,u.enable,u.real_name,u.mobile,u.create_time,u.head_image,ud.dep_id,ur.roles_id,a.*')
- ->where('u.id',$id)->find();
- return $info;
- }
- public function getAllByOrg($org_id){
- $map[] = ['o.org_id','=',$org_id];
- $map[] = ['u.del','=',0];
- $map[] = ['u.enable','=',1];
- $roles = Db::name('roles')
- ->where('parent_id',11)
- ->where('del',0)
- ->where('enable',1)
- ->column('id');
- $map[] = ['r.roles_id','in',$roles];
- $lists = $this->alias('a')
- ->join('user u','a.user_id=u.id')
- ->join('user_org o','a.user_id=o.user_id')
- ->join('user_roles r','a.user_id=r.user_id')
- ->field('a.id,u.real_name as name,u.enable,u.mobile,u.create_time,u.head_image as img')
- ->where($map)
- ->order('a.id','desc')
- ->select();
- $lists = $lists?$lists->toArray():[];
- foreach ($lists as $k=>$v){
- $lists[$k]['count'] = Db::name('ph_todo')
- ->where('worker_id',$v['id'])
- ->where('status',1)->count();
- $lists[$k]['title'] = $v['name'].'('.$lists[$k]['count'].')';
- }
- return $lists;
- }
- //护工人数
- public function getAllByOrgCount($org_id,$map=[]){
- $map[] = ['o.org_id','=',$org_id];
- $map[] = ['u.enable','=',1];
- $map[] = ['u.del','=',0];
- $roles = Db::name('roles')
- ->where('parent_id',11)
- ->where('del',0)
- ->where('enable',1)
- ->column('id');
- $map[] = ['r.roles_id','in',$roles];
- $lists = $this->alias('a')
- ->join('user u','a.user_id=u.id')
- ->join('user_org o','a.user_id=o.user_id')
- ->join('user_roles r','a.user_id=r.user_id')
- ->field('a.id,u.real_name as name,u.enable,u.mobile,u.create_time,u.head_image as img')
- ->where($map)
- ->count();
- return $lists;
- }
- public function hotList($org_id){
- $map[] = ['o.org_id','=',$org_id];
- $map[] = ['u.del','=',0];
- $map[] = ['u.enable','=',1];
- $roles = Db::name('roles')
- ->where('parent_id',11)
- ->where('del',0)
- ->where('enable',1)
- ->column('id');
- $map[] = ['r.roles_id','in',$roles];
- $lists = $this->alias('a')
- ->join('user u','a.user_id=u.id')
- ->join('user_org o','a.user_id=o.user_id')
- ->join('user_roles r','a.user_id=r.user_id')
- ->field('a.id,a.birthday,a.work,u.real_name as name,u.enable,u.mobile,u.create_time,u.head_image as img')
- ->where($map)
- ->where('a.recommend',1)
- ->order('a.id','desc')
- ->select();
- $lists = $lists?$lists->toArray():[];
- foreach ($lists as $k=>$v){
- $lists[$k]['age'] = $v['birthday']?birthday($v['birthday']):0;
- $lists[$k]['worker'] = $v['work']?birthday($v['work']):0;;
- }
- return $lists;
- }
- public function getAddressName($id){
- $addressInfo = Db::name('address')
- ->where('id',$id)
- ->find();
- $name = $addressInfo['title'];
- if($addressInfo['pid'] > 0){
- $pInfo = Db::name('address')
- ->where('id',$addressInfo['pid'])
- ->value('title');
- $name = $pInfo.$name;
- }
- return $name;
- }
- //护工详情
- public function workerDetail($id,$orgId,$userId=0){
- $info = Db::name('worker')
- ->where('id',$id)
- ->find();
- if(empty($info)) HelpHander::error('护工信息不存在');
- $info['serviceList'] = $this->getService(explode(',',$info['services']));
- $info['age'] = birthday($info['birthday']);
- $info['work'] = strpos($info['work'],'-')!==false?birthday($info['work']):'';
- $info['serviceStatus'] =$this->checkServiceStatus($info['id'])?'服务中':'空闲中';
- $info['minPrice'] =$this->getWorkerMinPrice($info['id']);
- $info['maxPrice'] =$this->getWorkerMaxPrice($info['id']);
- $info['servicesNum'] =count(Db::name('orders')
- ->where('worker_id',$id)
- ->group('user_id')
- ->select()->toArray());
- $info['servicesDay'] =diffBetweenTwoDays($info['create_time'],date('Y-m-d H:i:s'));
- $info['commentNum'] = (new Comment())->getCommentNum($info['id']);
- $info['cateList'] = $this->getWorkerPrice($id,$orgId);
- if($userId > 0){
- $info['isCollect'] = (new Collect())->checkIsCollect($info['id'],$userId);//是否收藏该护工
- }
- $info['serviceInfo'] = implode('、',array_column($this->getService(explode(',',$info['services'])),'title'));
- return $info;
- }
- }
|