Departuser.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. class Departuser extends BaseController
  7. {
  8. public $post = "";
  9. public function __construct(App $app)
  10. {
  11. parent::__construct($app);
  12. $this->post = $this->request->post();
  13. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  14. if($token==""){
  15. return error_show(101,'token不能为空');
  16. }
  17. $effetc = VerifyTokens($token);
  18. if(!empty($effetc) && $effetc['code']!=0){
  19. return error_show($effetc['code'],$effetc['message']);
  20. }
  21. }
  22. /*列表*/
  23. public function list(){
  24. $page = isset($this->post['page']) && $this->post['page'] != "" ? intval($this->post['page']) : 1;
  25. $size = isset($this->post['size']) && $this->post['size'] != "" ? intval($this->post['size']) : 10;
  26. $us=[];
  27. $count = Db::name("depart_user")->where($us)->count();
  28. $page>=ceil($count/$size) ? $page=ceil($count/$size) :"";
  29. $list = Db::name("depart_user")->alias('b')->join('company_item a','a.id=b.itemid','left')
  30. ->field('a.name,b.itemid,b.uid,b.nickname')->where($us)->page($page,$size)->select();
  31. // $i=[];
  32. // foreach ($list as $p){
  33. // $tips = Db::name('depart_tips')->where(['is_del'=>0,'depart_uid'=>$p['uid']])->select();
  34. // $p['total_tips']=$tips;
  35. // $i[]=$p;
  36. // }
  37. return app_show(0, '信息获取成功', ['list' => $list, 'count' => $count]);
  38. }
  39. /*添加*/
  40. public function attend(){
  41. $uid = isset($this->post['uid']) && $this->post['uid'] !==""? trim($this->post['uid']):"";
  42. if($uid==""){
  43. return error_show(1004,"员工id不能为空");
  44. }
  45. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  46. $to= GetInfoById($token,['id'=>$uid]);
  47. if(empty($to)||$to['code']!=0){
  48. return app_show(1002,"员工信息不存在");
  49. }
  50. $nickname = isset($to['data']['nickname']) &&$to['data']['nickname']!="" ? $to['data']['nickname'] :"" ;
  51. $itemid = isset($this->post['itemid']) && $this->post['itemid'] !==""? trim($this->post['itemid']):"";
  52. if($itemid==""){
  53. return error_show(1004,"部门不能为空");
  54. }
  55. $repeat_itemid = Db::name("depart_user")->where(["is_del"=>0,"itemid"=>$itemid,"uid"=>$uid])->find();
  56. if(!empty($repeat_itemid)){
  57. return error_show(1004,"部门用户已存在");
  58. }
  59. $tid = Db::name('company_item')->where(['id'=>$itemid,'is_del'=>0])->find();
  60. //var_dump( Db::name('company_item')->getLastSql());
  61. if(empty($tid)){
  62. return error_show(1004,"部门不存在");
  63. }
  64. $status = isset($this->post['status']) && $this->post['status'] !==""? trim($this->post['status']) : "1";
  65. if($status==""){
  66. return error_show(0,"状态不能为空");
  67. }
  68. Db::startTrans();
  69. try{
  70. $data = [
  71. 'uid'=>$uid,'nickname'=>$nickname,'itemid'=>$itemid,
  72. 'is_del'=>0,'status'=>$status,
  73. 'addtime'=>date("Y-m-d H:i:s"),
  74. 'updatetime'=>date("Y-m-d H:i:s")
  75. ];
  76. $str=Db::name('depart_user')->insert($data);
  77. if($str){
  78. Db::commit();
  79. return error_show(0,"添加成功");
  80. }else{
  81. Db::rollback();
  82. return error_show(1002,"添加失败");
  83. }
  84. }catch (\Exception $e){
  85. return error_show(1003,$e->getMessage());
  86. }
  87. }
  88. /*编辑*/
  89. public function useredit(){
  90. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  91. $id = isset($this->post['id']) &&$this->post['id'] !==""? trim($this->post['id']):"";
  92. if($id ==""){
  93. return error_show(1004,"id不能为空");
  94. }
  95. $nid = Db::name('depart_user')->where(['id'=>$id,'is_del'=>0])->find();
  96. if(empty($nid)){
  97. return error_show(1004,"id不能为空");
  98. }
  99. $uid = isset($this->post['uid']) && $this->post['uid'] !==""? trim($this->post['uid']):"";
  100. if($uid ==""){
  101. return error_show(1004,"员工id不能为空");
  102. }
  103. $to= GetInfoById($token,['id'=>$uid]);
  104. if(empty($to)||$to['code']!=0){
  105. return app_show(1002,"员工信息不存在");
  106. }
  107. $nickname = isset($to['data']['nickname']) &&$to['data']['nickname']!="" ? $to['data']['nickname'] :"" ;
  108. $itemid = isset($this->post['itemid']) && $this->post['itemid'] !==""?($this->post)['itemid']: "";
  109. if($itemid==""){
  110. return error_show(1004,"部门id不能为空");
  111. }
  112. $repeat_itemid = Db::name("depart_user")->where(["is_del"=>0,"itemid"=>$itemid,"uid"=>$uid])->where('id','<>',$id)->find();
  113. if(!empty($repeat_itemid)){
  114. return error_show(1004,"部门用户已存在");
  115. }
  116. try{
  117. $d=[
  118. 'uid'=>$uid,'itemid'=>$itemid,'nickname'=>$nickname,
  119. 'is_del'=>0,'addtime'=>date("Y-m-d H:i:s"),
  120. 'updatetime'=>date("Y-m-d H:i:s"),
  121. ];
  122. $r = Db::name("depart_user")->where(["is_del"=>0,"id"=>$id])->save($d);
  123. // var_dump( Db::name("depart_user")->getLastSql());
  124. return $r ? app_show(0,"信息更新成功"): error_show(1003,"信息更新失败");
  125. }catch (\Exception $e){
  126. return error_show(1003,$e->getMessage());
  127. }
  128. }
  129. /*查询*/
  130. public function found(){
  131. $id = isset($this->post['id'])&& $this->post['id'] !==""? trim($this->post['id']) : "";
  132. if($id==""){
  133. return error_show(1004,'员工不能为空');
  134. }
  135. $fo = Db::name('depart_user')->alias('a')->join('company_item b',"b.id=a.id",'left')
  136. ->field('a.*,b.name')->where(['uid'=>$id,'a.is_del'=>0,'b.is_del'=>0])->find();
  137. // var_dump( Db::name('depart_user')->getLastSql());
  138. if(empty($fo)){
  139. return error_show(1004,"信息获取失败");
  140. }
  141. // $fi = Db::name('depart_tips')->field('year,month,total_tips')->where(['depart_uid'=>$id,'is_del'=>0])->select();
  142. // $fo['depart_user']=$fi;
  143. return app_show(0,"获取成功",$fo);
  144. }
  145. /*删除*/
  146. public function userdel(){
  147. $id = isset($this->post['id']) && $this->post['id'] !==""? trim($this->post['id']) :"";
  148. $de = Db::name('depart_user')->where(['is_del'=>0,'id'=>$id])->find();
  149. if($de==false){
  150. return error_show(1004,"员工信息不存在");
  151. }
  152. $de['is_del']=1;
  153. $de['updatetime']= date("Y-m-d H:i:s");
  154. $back = Db::name('depart_user')->save($de);
  155. // var_dump(Db::name('depart_user')->getLastSql());
  156. return $back ? app_show(0,"删除成功") :error_show(1004,"删除失败");
  157. }
  158. }