Worker.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use tools\Idcard;
  7. class Worker extends Base
  8. {
  9. public $status = [
  10. 0=>'待审核',
  11. 1=>'同意',
  12. 2=>'拒绝',
  13. ];
  14. public function updates($orgId){
  15. $data = request()->post();
  16. $validate = new \app\common\validate\User();
  17. $result = $validate->check($data,[],'add');
  18. if(true !== $result){
  19. $this->error = $validate->getError();
  20. return false;
  21. }
  22. $validate = new \app\common\validate\Worker();
  23. $result = $validate->check($data,[]);
  24. if(true !== $result){
  25. $this->error = $validate->getError();
  26. return false;
  27. }
  28. $idCard = new Idcard();
  29. if(!$idCard->isChinaIDCard($data['card'])){
  30. $this->error = '身份证格式错误';
  31. return false;
  32. }
  33. $userData = [
  34. 'id' => input('id/d',0),
  35. 'account' => input('account','','trim'),
  36. 'real_name' => input('real_name','','trim'),
  37. 'mobile' => input('mobile','','trim'),
  38. 'password' => input('password','','trim'),
  39. 'head_image' => input('head_image','','trim'),
  40. 'depId' => input('depId/d',0),
  41. 'rolesId' => input('rolesId/d',0),
  42. 'enable' => input('enable/d',1),
  43. ];
  44. Db::startTrans();
  45. try{
  46. $id = $userData['id'];
  47. unset($userData['id']);
  48. $depId = $userData['depId'];
  49. $rolesId = $userData['rolesId'];
  50. unset($userData['depId']);
  51. unset($userData['rolesId']);
  52. if($id > 0){
  53. if(!$userData['password']){
  54. unset($userData['password']);
  55. }
  56. $userData['update_time'] = date('Y-m-d H:i:s');
  57. $ret =Db::name('user')
  58. ->where(['id'=>$id])
  59. ->update($userData);
  60. if(!$ret){
  61. \exception('操作失败');
  62. }
  63. Db::name('user_dep')
  64. ->where('user_id',$id)
  65. ->setField('dep_id',$depId);
  66. Db::name('user_roles')
  67. ->where('user_id',$id)
  68. ->setField('roles_id',$rolesId);
  69. $workerData = [
  70. 'birthday'=>$idCard->getBirthday($data['card']),
  71. 'gender'=>$idCard->getChinaIDCardSex($data['card'])=='男'?1:2,
  72. 'card_imgs'=>!empty($data['card_imgs'])?implode(',',$data['card_imgs']):'' ,
  73. 'id_imgs'=>!empty($data['id_imgs'])?implode(',',$data['id_imgs']):'',
  74. 'work'=>$data['work']?$data['work']:null,
  75. 'content'=>$data['content'],
  76. 'recommend'=>$data['recommend'],
  77. 'card'=>$data['card'],
  78. 'update_time'=>getTime(),
  79. ];
  80. $res = $this->where('user_id',$id)
  81. ->update($workerData);
  82. if(!$res){
  83. \exception('护工信息更新失败');
  84. }
  85. }else{
  86. $userData['create_time'] = date('Y-m-d H:i:s');
  87. $userData['ip'] = request()->ip();
  88. $ret = Db::name('user')->insertGetId($userData);
  89. if(!$ret){
  90. \exception('操作失败');
  91. }
  92. Db::name('user_org')
  93. ->insert(['user_id'=>$ret,'org_id'=>$orgId]);
  94. Db::name('user_dep')
  95. ->insert(['user_id'=>$ret,'dep_id'=>$depId]);
  96. Db::name('user_roles')
  97. ->insert(['user_id'=>$ret,'roles_id'=>$rolesId]);
  98. }
  99. $workerData = [
  100. 'user_id'=>$ret,
  101. 'birthday'=>$idCard->getBirthday($data['card']),
  102. 'gender'=>$idCard->getChinaIDCardSex($data['card'])=='男'?1:2,
  103. 'card_imgs'=>!empty($data['card_imgs'])?implode(',',$data['card_imgs']):'' ,
  104. 'id_imgs'=>!empty($data['id_imgs'])?implode(',',$data['id_imgs']):'',
  105. 'work'=>$data['work']?$data['work']:null,
  106. 'content'=>$data['content'],
  107. 'recommend'=>$data['recommend'],
  108. 'card'=>$data['card'],
  109. ];
  110. $res = $this->save($workerData);
  111. if(!$res){
  112. \exception('护工信息更新失败');
  113. }
  114. Db::commit();
  115. return true;
  116. }catch (Exception $e){
  117. Db::rollback();
  118. $this->error = $e->getMessage();
  119. return false;
  120. }
  121. }
  122. //护工列表
  123. public function lists($orgId,$where,$page,$size){
  124. $map[] = ['o.org_id','=',$orgId];
  125. $map[] = ['u.del','=',0];
  126. if(isset($where['name']) && $where['name']!=''){
  127. $map[] = ['u.real_name','like','%'.$where['name'].'%'];
  128. }
  129. if(isset($where['enable']) && $where['enable']!=''){
  130. $map[] = ['u.enable','=',$where['enable']];
  131. }
  132. if(isset($where['recommend']) && $where['recommend']!=''){
  133. $map[] = ['a.recommend','=',$where['recommend']];
  134. }
  135. if(isset($where['gender']) && $where['gender']!=''){
  136. $map[] = ['a.gender','=',$where['gender']];
  137. }
  138. if(isset($where['balance']) && $where['balance']!=''){
  139. $map[] = ['a.balance','>',0];
  140. }
  141. $roles = Db::name('roles')
  142. ->where('parent_id',11)
  143. ->where('del',0)
  144. ->where('enable',1)
  145. ->column('id');
  146. if(empty($roles)){
  147. $result['zhZye'] = 0;
  148. $result['page'] = $page;
  149. $result['total'] = 0;
  150. $result['records'] = [];
  151. $result['rows'] = 0;
  152. return $result;
  153. }
  154. $map[] = ['r.roles_id','in',$roles];
  155. $offset = ($page-1)*$size;
  156. $list = $this->alias('a')
  157. ->join('user u','a.user_id=u.id')
  158. ->join('user_org o','a.user_id=o.user_id')
  159. ->join('user_roles r','a.user_id=r.user_id')
  160. ->field('u.real_name,u.enable,u.mobile,u.create_time,u.head_image,a.*')
  161. ->limit($offset,$size)
  162. ->where($map)
  163. ->order('a.id','desc')
  164. ->select();
  165. $count = $this->alias('a')
  166. ->join('user u','a.user_id=u.id')
  167. ->join('user_org o','a.user_id=o.user_id')
  168. ->join('user_roles r','a.user_id=r.user_id')
  169. ->field('u.real_name,u.mobile,u.create_time,u.head_image,a.*')
  170. ->where($map)
  171. ->count();
  172. $lists = $list?$list->toArray():[];
  173. foreach ($lists as $k=>$v){
  174. $lists[$k]['work'] = $v['work']?birthday($v['work']):0;
  175. $lists[$k]['content'] = $v['content']?$v['content']:'';
  176. $lists[$k]['birthday'] = $v['birthday']?$v['birthday']:'';
  177. $lists[$k]['card_imgs'] = $v['card_imgs']?$v['card_imgs']:'';
  178. $lists[$k]['id_imgs'] = $v['id_imgs']?$v['id_imgs']:'';
  179. $lists[$k]['update_time'] = $v['update_time']?$v['update_time']:'';
  180. $lists[$k]['card'] = $v['card']?$v['card']:'';
  181. $lists[$k]['age'] = $v['birthday']?birthday($v['birthday']):0;
  182. }
  183. $result['zhZye'] = $this->alias('a')
  184. ->join('user u','a.user_id=u.id')
  185. ->join('user_org o','a.user_id=o.user_id')
  186. ->join('user_roles r','a.user_id=r.user_id')
  187. ->field('u.real_name,u.mobile,u.create_time,u.head_image,a.*')
  188. ->where($map)
  189. ->sum('a.balance');;
  190. $totalPage = ceil($count/$size);
  191. $result['page'] = $page;
  192. $result['total'] = $totalPage;
  193. $result['records'] = $count;
  194. $result['rows'] = $lists;
  195. return $result;
  196. }
  197. public function apiLists($orgId,$where,$page,$size){
  198. $map[] = ['o.org_id','=',$orgId];
  199. $map[] = ['u.del','=',0];
  200. $map[] = ['u.enable','=',1];
  201. if(isset($where['name']) && $where['name']!=''){
  202. $map[] = ['u.real_name','like','%'.$where['name'].'%'];
  203. }
  204. if(isset($where['enable']) && $where['enable']!=''){
  205. $map[] = ['u.enable','=',$where['enable']];
  206. }
  207. if(isset($where['recommend']) && $where['recommend']!=''){
  208. $map[] = ['a.recommend','=',$where['recommend']];
  209. }
  210. if(isset($where['gender']) && $where['gender']!=''){
  211. $map[] = ['a.gender','=',$where['gender']];
  212. }
  213. if(isset($where['balance']) && $where['balance']!=''){
  214. $map[] = ['a.balance','>',0];
  215. }
  216. $roles = Db::name('roles')
  217. ->where('parent_id',11)
  218. ->where('del',0)
  219. ->where('enable',1)
  220. ->column('id');
  221. if(empty($roles)){
  222. $result['zhZye'] = 0;
  223. $result['page'] = $page;
  224. $result['total'] = 0;
  225. $result['records'] = [];
  226. $result['rows'] = 0;
  227. return $result;
  228. }
  229. $map[] = ['r.roles_id','in',$roles];
  230. $offset = ($page-1)*$size;
  231. $list = $this->alias('a')
  232. ->join('user u','a.user_id=u.id')
  233. ->join('user_org o','a.user_id=o.user_id')
  234. ->join('user_roles r','a.user_id=r.user_id')
  235. ->field('u.real_name,u.enable,u.mobile,u.create_time,u.head_image,a.*')
  236. ->limit($offset,$size)
  237. ->where($map)
  238. ->order('a.id','desc')
  239. ->select();
  240. $count = $this->alias('a')
  241. ->join('user u','a.user_id=u.id')
  242. ->join('user_org o','a.user_id=o.user_id')
  243. ->join('user_roles r','a.user_id=r.user_id')
  244. ->field('u.real_name,u.mobile,u.create_time,u.head_image,a.*')
  245. ->where($map)
  246. ->count();
  247. $lists = $list?$list->toArray():[];
  248. foreach ($lists as $k=>$v){
  249. $lists[$k]['work'] = $v['work']?birthday($v['work']):0;
  250. $lists[$k]['content'] = $v['content']?$v['content']:'';
  251. $lists[$k]['birthday'] = $v['birthday']?$v['birthday']:'';
  252. $lists[$k]['card_imgs'] = $v['card_imgs']?$v['card_imgs']:'';
  253. $lists[$k]['id_imgs'] = $v['id_imgs']?$v['id_imgs']:'';
  254. $lists[$k]['update_time'] = $v['update_time']?$v['update_time']:'';
  255. $lists[$k]['card'] = $v['card']?$v['card']:'';
  256. $lists[$k]['age'] = $v['birthday']?birthday($v['birthday']):0;
  257. }
  258. $result['zhZye'] = $this->alias('a')
  259. ->join('user u','a.user_id=u.id')
  260. ->join('user_org o','a.user_id=o.user_id')
  261. ->join('user_roles r','a.user_id=r.user_id')
  262. ->field('u.real_name,u.mobile,u.create_time,u.head_image,a.*')
  263. ->where($map)
  264. ->sum('a.balance');;
  265. $totalPage = ceil($count/$size);
  266. $result['page'] = $page;
  267. $result['total'] = $totalPage;
  268. $result['records'] = $count;
  269. $result['rows'] = $lists;
  270. return $result;
  271. }
  272. public function getUserInfo($id){
  273. $info = db('user')
  274. ->alias('u')
  275. ->join('user_dep ud','ud.user_id = u.id')
  276. ->join('user_roles ur','ur.user_id = u.id')
  277. ->join('worker a','u.id = a.user_id')
  278. ->field('u.account,u.enable,u.real_name,u.mobile,u.create_time,u.head_image,ud.dep_id,ur.roles_id,a.*')
  279. ->where('u.id',$id)->find();
  280. return $info;
  281. }
  282. public function getAllByOrg($org_id){
  283. $map[] = ['o.org_id','=',$org_id];
  284. $map[] = ['u.del','=',0];
  285. $map[] = ['u.enable','=',1];
  286. $roles = Db::name('roles')
  287. ->where('parent_id',11)
  288. ->where('del',0)
  289. ->where('enable',1)
  290. ->column('id');
  291. $map[] = ['r.roles_id','in',$roles];
  292. $lists = $this->alias('a')
  293. ->join('user u','a.user_id=u.id')
  294. ->join('user_org o','a.user_id=o.user_id')
  295. ->join('user_roles r','a.user_id=r.user_id')
  296. ->field('a.id,u.real_name as name,u.enable,u.mobile,u.create_time,u.head_image as img')
  297. ->where($map)
  298. ->order('a.id','desc')
  299. ->select();
  300. $lists = $lists?$lists->toArray():[];
  301. foreach ($lists as $k=>$v){
  302. $lists[$k]['count'] = Db::name('ph_todo')
  303. ->where('worker_id',$v['id'])
  304. ->where('status',1)->count();
  305. $lists[$k]['title'] = $v['name'].'('.$lists[$k]['count'].')';
  306. }
  307. return $lists;
  308. }
  309. //护工人数
  310. public function getAllByOrgCount($org_id,$map=[]){
  311. $map[] = ['o.org_id','=',$org_id];
  312. $map[] = ['u.enable','=',1];
  313. $map[] = ['u.del','=',0];
  314. $roles = Db::name('roles')
  315. ->where('parent_id',11)
  316. ->where('del',0)
  317. ->where('enable',1)
  318. ->column('id');
  319. $map[] = ['r.roles_id','in',$roles];
  320. $lists = $this->alias('a')
  321. ->join('user u','a.user_id=u.id')
  322. ->join('user_org o','a.user_id=o.user_id')
  323. ->join('user_roles r','a.user_id=r.user_id')
  324. ->field('a.id,u.real_name as name,u.enable,u.mobile,u.create_time,u.head_image as img')
  325. ->where($map)
  326. ->count();
  327. return $lists;
  328. }
  329. public function hotList($org_id){
  330. $map[] = ['o.org_id','=',$org_id];
  331. $map[] = ['u.del','=',0];
  332. $map[] = ['u.enable','=',1];
  333. $roles = Db::name('roles')
  334. ->where('parent_id',11)
  335. ->where('del',0)
  336. ->where('enable',1)
  337. ->column('id');
  338. $map[] = ['r.roles_id','in',$roles];
  339. $lists = $this->alias('a')
  340. ->join('user u','a.user_id=u.id')
  341. ->join('user_org o','a.user_id=o.user_id')
  342. ->join('user_roles r','a.user_id=r.user_id')
  343. ->field('a.id,a.birthday,a.work,u.real_name as name,u.enable,u.mobile,u.create_time,u.head_image as img')
  344. ->where($map)
  345. ->where('a.recommend',1)
  346. ->order('a.id','desc')
  347. ->select();
  348. $lists = $lists?$lists->toArray():[];
  349. foreach ($lists as $k=>$v){
  350. $lists[$k]['age'] = $v['birthday']?birthday($v['birthday']):0;
  351. $lists[$k]['worker'] = $v['work']?birthday($v['work']):0;;
  352. }
  353. return $lists;
  354. }
  355. public function getAddressName($id){
  356. $addressInfo = Db::name('address')
  357. ->where('id',$id)
  358. ->find();
  359. $name = $addressInfo['title'];
  360. if($addressInfo['pid'] > 0){
  361. $pInfo = Db::name('address')
  362. ->where('id',$addressInfo['pid'])
  363. ->value('title');
  364. $name = $pInfo.$name;
  365. }
  366. return $name;
  367. }
  368. //护工详情
  369. public function workerDetail($id,$orgId,$userId=0){
  370. $info = Db::name('worker')
  371. ->where('id',$id)
  372. ->find();
  373. if(empty($info)) HelpHander::error('护工信息不存在');
  374. $info['serviceList'] = $this->getService(explode(',',$info['services']));
  375. $info['age'] = birthday($info['birthday']);
  376. $info['work'] = strpos($info['work'],'-')!==false?birthday($info['work']):'';
  377. $info['serviceStatus'] =$this->checkServiceStatus($info['id'])?'服务中':'空闲中';
  378. $info['minPrice'] =$this->getWorkerMinPrice($info['id']);
  379. $info['maxPrice'] =$this->getWorkerMaxPrice($info['id']);
  380. $info['servicesNum'] =count(Db::name('orders')
  381. ->where('worker_id',$id)
  382. ->group('user_id')
  383. ->select()->toArray());
  384. $info['servicesDay'] =diffBetweenTwoDays($info['create_time'],date('Y-m-d H:i:s'));
  385. $info['commentNum'] = (new Comment())->getCommentNum($info['id']);
  386. $info['cateList'] = $this->getWorkerPrice($id,$orgId);
  387. if($userId > 0){
  388. $info['isCollect'] = (new Collect())->checkIsCollect($info['id'],$userId);//是否收藏该护工
  389. }
  390. $info['serviceInfo'] = implode('、',array_column($this->getService(explode(',',$info['services'])),'title'));
  391. return $info;
  392. }
  393. }