Newfill.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. class Newfill extends BaseController
  7. {
  8. public $post = "";
  9. public function __construct(App $app)
  10. {
  11. parent::__construct($app);
  12. $this->post = $this->request->post();
  13. }
  14. public function list()
  15. {
  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. $condition=[];
  19. $depart []=['is_del',"=",0];
  20. $condition[] = ['is_del',"=",0];
  21. $pid = isset($this->post['pid']) &&$this->post['pid']!=="" ?intval($this->post['pid']): "";
  22. if($pid!==""){
  23. $condition[]=["pid","=",$pid];
  24. }
  25. $name= isset($this->post['name'])&&$this->post['name']!=="" ? trim($this->post['name']):"";
  26. if($name!=""){
  27. $condition[]=["name","like","%{$this->post['name']}%"];
  28. }
  29. $nickname = isset($this->post['nickname']) && $this->post['nickname'] !==""? trim($this->post['nickname']) :"";
  30. if($nickname!=""){
  31. $is= Db::name("depart_user")->where([['nickname','like',"%{$nickname}%"]])->column('itemid');
  32. if(empty($is)){
  33. return error_show(1004,"未找到数据");
  34. }
  35. $condition[]=["id","in",$is];
  36. }
  37. if($pid==="" &&$name=="" &&$nickname==""){
  38. $condition[]=["pid","=",0];
  39. }
  40. $pidlist = Db::name("company_item")->where($condition)->column("pid");
  41. if(!empty($pidlist)){
  42. $depart[]=[["itemid","in",$pidlist]];
  43. }else{
  44. if($pid===""){
  45. $depart[]=['itemid','=',0];
  46. }else{
  47. $depart[]=['itemid','=',$pid];
  48. }
  49. }
  50. $item = Db::name("depart_user")->where($depart)->select();
  51. $list = Db::name("company_item")->where($condition)->select();
  52. return app_show(0,"获取成功",["depart"=>$list,"item"=>$item]);
  53. }
  54. /*状态*/
  55. public function stat(){
  56. $id = isset($this->post['id'])?intval($this->post['id']):"";
  57. if($id==""){
  58. return error_show(1004,"参数id 不能为空");
  59. }
  60. $s = Db::name('company_item')->where(["id"=>$id])->find();
  61. if(empty($s)){
  62. return error_show(1005,"未找到部门");
  63. }
  64. $var= $s['status'];
  65. $di = $s['status']==0? "1":"0";
  66. $s['status']=$di;
  67. $s['updatetime'] = date("Y-m-d H:i:s");
  68. // var_dump($s);
  69. $ti = Db::name('company_item')->save($s);
  70. $orde = ["order_code"=>$s['name'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  71. ActionLog::logAdd($this->post['token'],$orde,"company_item",$s['status'],$orde);
  72. return app_show(0,"更新成功");
  73. }
  74. /*职位*/
  75. public function userp(){
  76. $id = isset($this->post['id'])?intval($this->post['id']):"";
  77. if($id==""){
  78. return error_show(1004,"参数id不能为空");
  79. }
  80. $t = Db::name('depart_user')->where(["id"=>$id,"is_del"=>0])->find();
  81. if(empty($t)){
  82. return error_show(1005,"未找到用户");
  83. }
  84. $position = isset($this->post['position'])?intval($this->post['position']):"";
  85. if($position==""){
  86. return error_show(1004,"职位不能为空");
  87. }
  88. if($position==2){
  89. $f = Db::name('depart_user')->where(['itemid'=>$t['itemid'],"is_del"=>0,"position"=>2])->find();
  90. if(!empty($f)&& $f['id']!=$id){
  91. $f['position']=1;
  92. $f['updatetime']= date("Y-m-d H:i:s");
  93. $m= Db::name('depart_user')->save($f);
  94. if($m==false){
  95. Db::rollback();
  96. return error_show(1004,"负责人修改失败");
  97. }
  98. }
  99. }
  100. $t['position']=$position;
  101. $t['updatetime'] = date("Y-m-d H:i:s");
  102. //var_dump($t);
  103. $ti = Db::name('depart_user')->save($t);
  104. if($ti==true){
  105. Db::commit();
  106. return app_show(0,"修改成功");
  107. }else{
  108. Db::rollback();
  109. return error_show(1004,"修改失败");
  110. }
  111. }
  112. public function add(){
  113. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  114. if($token==""){
  115. return error_show(101,'token不能为空');
  116. }
  117. $effetc = VerifyTokens($token);
  118. if(!empty($effetc) && $effetc['code']!=0){
  119. return error_show($effetc['code'],$effetc['message']);
  120. }
  121. $item_name=isset($this->post['name']) && $this->post['name'] !==""? trim($this->post['name']) :"";
  122. if($item_name==""){
  123. return error_show(0,"部门名称不能为空");
  124. }
  125. $repeat_name = Db::name("company_item")->where(["is_del"=>0,"name"=>$item_name])->find();
  126. if(!empty($repeat_name)){
  127. return error_show(1004,"部门名称已存在");
  128. }
  129. $pid = isset($this->post['pid']) && $this->post['pid'] !==""? trim($this->post['pid']):"";
  130. if($pid===""){
  131. return error_show(1002,"父级id不能为空");
  132. }
  133. //$spid = Db::name('company_item')->where(['id'=>$pid,'is_del'=>0])->find();
  134. $spid=['depart_link'=>""];
  135. if($pid!=0){
  136. $spid = Db::name('company_item')->where(['id'=>$pid,'is_del'=>0])->find();
  137. if(empty($spid)){
  138. return error_show(1004,"父级数据不能为空");
  139. }
  140. }
  141. $level = isset($this->post['level']) && $this->post['level'] !==""? trim($this->post['level']):"1";
  142. $weight = isset($this->post['weight']) && $this->post['weight'] !==""? trim($this->post['weight']):"1";
  143. Db::startTrans();
  144. try{
  145. $data =[
  146. "name"=>$item_name, "pid"=>$pid, "level"=>$level, "weight"=>$weight,
  147. "is_del"=>0,
  148. "addtime"=>date("Y-m-d H:i:s"),
  149. "updatetime"=>date("Y-m-d H:i:s"),
  150. ];
  151. $t= Db::name("company_item")->insert($data,true);
  152. if($t>0){
  153. // $orde = ["order_code"=>$repeat_name['name'],"status"=>0,"action_remark"=>'',"action_type"=>"creat"];
  154. // ActionLog::logAdd($token,$orde,"company_item",0,$orde);
  155. $depart_link=$spid['depart_link']."{$t}-";
  156. $level =explode('-',$depart_link);
  157. $level = array_filter($level);
  158. $level= count($level);
  159. $i=['depart_link'=>$depart_link,'level'=>$level];
  160. $o=Db::name('company_item')->where(['id'=>$t])->update($i);
  161. if($o){
  162. // $stn = ["order_code"=>$repeat_name['name'],"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  163. // ActionLog::logAdd($token,$stn,"company_item",0,$stn);
  164. Db::commit();
  165. return error_show(0,"添加成功");
  166. }
  167. }
  168. Db::rollback();
  169. }catch (\Exception $e){
  170. Db::rollback();
  171. return error_show(1003,$e->getMessage());
  172. }
  173. }
  174. public function refresh(){
  175. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  176. if($token==""){
  177. return error_show(101,'token不能为空');
  178. }
  179. $effetc = VerifyTokens($token);
  180. if(!empty($effetc) && $effetc['code']!=0){
  181. return error_show($effetc['code'],$effetc['message']);
  182. }
  183. $id = isset($this->post['id'])?intval($this->post['id']):"";
  184. $items = Db::name("company_item")->where(['id'=>$id,'is_del'=>0])->find();
  185. if($items==false){
  186. return error_show(1003,"部门信息不存在");
  187. }
  188. $pid = isset($this->post['pid']) && $this->post['pid'] !==""? trim($this->post['pid']):"";
  189. if($pid==""){
  190. return error_show(1002,"父级id不能为空");
  191. }
  192. $spid=['depart_link'=>""];
  193. if($pid!=0){
  194. $spid = Db::name('company_item')->where(['id'=>$pid,'is_del'=>0])->find();
  195. if(!empty($spid)){
  196. return error_show(1004,"父级数据不能为空");
  197. }
  198. }
  199. $weight = isset($this->post['weight']) && $this->post['weight'] !==""? trim($this->post['weight']):"1";
  200. $itemname=isset($this->post['name'])? trim($this->post['name']):"";
  201. if($itemname==""){
  202. return error_show(1002,"部门名称不能为空");
  203. }
  204. $repeat_name = Db::name("company_item")->where(["is_del"=>0,"name"=>$itemname])->where('id','<>',$id)->find();
  205. //echo Db::name("company_item")->getLastSql();
  206. if(!empty($repeat_name)){
  207. return error_show(1004,"部门名称已存在");
  208. }
  209. $depart_link=$spid['depart_link']."{$id}-";
  210. $itemlevel =explode('-',$depart_link);
  211. $itemlevel = array_filter($itemlevel);
  212. $itemlevel= count($itemlevel);
  213. $item=[
  214. "id"=>$id,
  215. "name"=>$itemname,
  216. "updatetime"=>date("Y-m-d H:i:s"),
  217. "weight"=>$weight,"pid"=>$pid,
  218. 'depart_link'=>$depart_link,
  219. 'level'=>$itemlevel
  220. ];
  221. $re = Db::name("company_item")->save($item);
  222. // $stn = ["order_code"=>$repeat_name['name'],"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  223. // ActionLog::logAdd($token,$stn,"company_item",0,$stn);
  224. return $re ? app_show(0,"信息更新成功"): error_show(1003,"信息更新失败");
  225. }
  226. /*查询*/
  227. public function query(){
  228. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  229. if($token==""){
  230. return error_show(101,'token不能为空');
  231. }
  232. $effetc = VerifyTokens($token);
  233. if(!empty($effetc) && $effetc['code']!=0){
  234. return error_show($effetc['code'],$effetc['message']);
  235. }
  236. $dati = Db::name("company_item")->where(['pid'=>0,'is_del'=>0])->order("weight desc")->select();
  237. $k=[];
  238. foreach ($dati as $key=>$value){
  239. // $temp=[];
  240. //$temp= Db::name("company_item")->where(['pid'=>$value['id'],'is_del'=>0])->order("weight desc")->select();
  241. // $value['child'] =$temp;
  242. // $k[]=$value;
  243. $k[]=crea($value);
  244. }
  245. return app_show(0,"获取成功",$k);
  246. }
  247. public function itemdel(){
  248. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  249. if($token==""){
  250. return error_show(101,'token不能为空');
  251. }
  252. $effetc = VerifyTokens($token);
  253. if(!empty($effetc) && $effetc['code']!=0){
  254. return error_show($effetc['code'],$effetc['message']);
  255. }
  256. $id = isset($this->post['id']) ? intval($this->post['id']):"";
  257. $items = Db::name("company_item")->where(['is_del'=>0,'id'=>$id])->find();
  258. if($items==false){
  259. return error_show(1003,"部门信息不存在");
  260. }
  261. $items['is_del']=1;
  262. $items['updatetime']=date("Y-m-d H:i:s");
  263. $result = Db::name("company_item")->save($items);
  264. // $stn = ["order_code"=>$items['name'],"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  265. // ActionLog::logAdd($token,$stn,"company_item",0,$stn);
  266. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  267. }
  268. }