Departuser.php 6.8 KB

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