Exclusive.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\facade\Db;
  5. class Exclusive extends Base
  6. {
  7. public function __construct(App $app)
  8. {
  9. parent::__construct($app);
  10. }
  11. public function list(){
  12. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']) :"1";
  13. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
  14. $where =[["is_del","=",0]];
  15. $cat_name=isset($this->post['name']) && $this->post['name'] !==""? trim($this->post['name']) :"";
  16. if($cat_name!==""){
  17. $where[]=['name',"like","%$cat_name%"];
  18. }
  19. $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
  20. if($pid!==""){
  21. $where[]=['pid',"=",$pid];
  22. }
  23. $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
  24. if($status!==""){
  25. $where[]=['status',"=",$status];
  26. }
  27. $count = Db::name("exclusive")->where($where)->count();
  28. $total = ceil($count / $size);
  29. $page = $page >= $total ? $total : $page;
  30. $list = Db::name('exclusive')->where($where)->page($page, $size)->select();
  31. return app_show(0, "获取成功", ['list' =>$list, 'count' => $count]);
  32. }
  33. public function add(){
  34. $name = isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  35. if($name==""){
  36. return error_show(1004,"参数name不能为空");
  37. }
  38. $rename = Db::name('exclusive')->where(['is_del' => 0, 'name' => $name])->find();
  39. if (!empty($rename)) {
  40. return error_show(1002, "专属类型名称已存在");
  41. }
  42. $pid =isset($this->post['pid'])&&$this->post['pid']!=="" ? intval($this->post['pid']):0;
  43. if($pid==""){
  44. return error_show(1002,"父级id不能为空");
  45. }
  46. $level=1;
  47. $search = $name;
  48. if($pid!==0){
  49. $parent= Db::name("exclusive")->where(["id"=>$pid,"is_del"=>0])->find();
  50. if($parent==false){
  51. return error_show(1003,"父级数据未找到");
  52. }
  53. $search=$parent['search']."/".$name;
  54. $level=$parent['level']+1;
  55. }
  56. $token = isset($this->post['token'])&&$this->post['token']!="" ? trim($this->post['token']):"";
  57. $user =GetUserInfo($token);
  58. if(empty($user)||$user['code']!=0){
  59. return error_show($user['code'],$user['msg']);
  60. }
  61. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  62. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  63. $data=[
  64. "name"=>$name,
  65. "pid"=>$pid,
  66. "level"=>$level,
  67. "search"=>$search,
  68. "createrid"=>$createrid,
  69. "creater"=>$creater,
  70. "status"=>0,
  71. "is_del"=>0,
  72. "addtime"=>date("Y-m-d H:i:s"),
  73. "updatetime"=>date("Y-m-d H:i:s")
  74. ];
  75. $in =Db::name("exclusive")->insert($data);
  76. if($in){
  77. return app_show(0,"新建成功");
  78. }else{
  79. return error_show(1003,"新建失败");
  80. }
  81. }
  82. public function status(){
  83. // $id=isset($this->post['id'])&&$this->post['id']!=="" ? intval($this->post['id']):"";
  84. // if($id===""){
  85. // return error_show(1004,"参数id不能为空");
  86. // }
  87. // $exclusive=Db::name("exclusive")->where(["id"=>$id,"is_del"=>0])->find();
  88. // if($exclusive==false){
  89. // return error_show(1004,"未找到数据");
  90. // }
  91. // $msg = $exclusive['status']==0? "启用":"禁用";
  92. // $exclusive['status'] = $exclusive['status']==0?1:0;
  93. // $exclusive['updatetime'] =date("Y-m-d H:i:s");
  94. // $in =Db::name("exclusive")->save($exclusive);
  95. // if($in){
  96. // return app_show(0,"{$msg}成功");
  97. // }else{
  98. // return error_show(1004,"{$msg}失败");
  99. // }
  100. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  101. if($id===""){
  102. return error_show(1002,"参数id不能为空");
  103. }
  104. $stn = Db::name('exclusive')->where(['id'=>$id])->find();
  105. if(empty($stn)){
  106. return error_show(1002,"未找到商品数据");
  107. }
  108. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  109. if($status===""){
  110. return error_show(1002,"参数status不能为空");
  111. }
  112. if ($stn['level']==2) {
  113. if($status==1){
  114. $can = mai($stn['id']);
  115. $cat=array_column($can,'id');
  116. }else{
  117. $cat =$stn['id'];
  118. }
  119. // return error_show(1002, "所在级别不能启用");
  120. }
  121. if($stn['level']==1){
  122. if($status==1){
  123. return error_show(1002,"所在等级不能启用");
  124. }else{
  125. $db= Db::name('exclusive')->where(['pid'=>$stn['id'],'status'=>1,'is_del'=>0])->count();
  126. if($db==0){
  127. $cat = $stn['id'];
  128. }else{
  129. return error_show(1002,"子级分类未禁用");
  130. }
  131. }
  132. }
  133. // $pd= $stn['status'];
  134. $it=[];
  135. $it['status']=$status;
  136. $it['updatetime']=date("Y-m-d H:i:s");
  137. $str = Db::name('exclusive')->where(['id'=>$cat,'is_del'=>0])->save($it);
  138. if($str){
  139. // $order = ["order_code"=>$id,"status"=>$pd,"action_remark"=>'',"action_type"=>"edit"];
  140. // ActionLog::logAdd($this->post['token'],$order,"sxd",$stn['status'],$order);
  141. return error_show(0,"状态更新成功");
  142. }else{
  143. return error_show(1002,"状态更新失败");
  144. }
  145. }
  146. public function query(){
  147. // $where =[["is_del","=",0]];
  148. // $cat_name=isset($this->post['name']) && $this->post['name'] !==""? trim($this->post['name']) :"";
  149. // if($cat_name!==""){
  150. // $where[]=['name',"like","%$cat_name%"];
  151. // }
  152. // $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
  153. // if($pid!==""){
  154. // $where[]=['pid',"=",$pid];
  155. // }
  156. // $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
  157. // if($status!==""){
  158. // $where[]=['status',"=",$status];
  159. // }
  160. // $level=isset($this->post['level']) && $this->post['level'] !==""? intval($this->post['level']) :"";
  161. // if($level!==""){
  162. // $where[]=['level',"=",$level];
  163. // }
  164. // $list = Db::name("exclusive")->where($where)->select();
  165. // return app_show(0,"获取成功",$list);
  166. $where=[["is_del","=",0]];
  167. $pid = isset($this->post['pid']) &&$this->post['pid']!=="" ?intval($this->post['pid']): "0";
  168. $where[]=["pid","=",$pid];
  169. $cat_name=isset($this->post['cat_name']) && $this->post['cat_name'] !==""? intval($this->post['cat_name']) :"";
  170. if($cat_name!==""){
  171. $where[]=['cat_name',"like","%$cat_name%"];
  172. }
  173. $data = Db::name("exclusive")->where($where)->select();
  174. $vmp = [];
  175. foreach ($data as $sts){
  176. $vmp[]=coco($sts);
  177. }
  178. return app_show(0,"获取成功",$vmp);
  179. }
  180. public function edit(){
  181. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  182. if($id==""){
  183. return error_show(1004,"参数id不能为空");
  184. }
  185. $info = Db::name('exclusive')->where(['id'=>$id,'is_del'=>0])->find();
  186. if($info==""){
  187. return error_show(1002,"未找到数据");
  188. }
  189. if($info['status']===1){
  190. return error_show(1004,"启用状态不可编辑");
  191. }
  192. $name = isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  193. if($name==""){
  194. return error_show(1004,"参数name不能为空");
  195. }
  196. $pid =isset($this->post['pid'])&&$this->post['pid']!=="" ? intval($this->post['pid']):0;
  197. if($pid==""){
  198. return error_show(1004,"参数pid不能为空");
  199. }
  200. $level=1;
  201. $search = $name;
  202. if($pid!==0){
  203. $parent= Db::name("exclusive")->where(["id"=>$pid,"is_del"=>0])->find();
  204. if($parent==false){
  205. return error_show(1003,"父级数据未找到");
  206. }
  207. $search=$parent['search']."/".$name;
  208. $level=$parent['level']+1;
  209. }
  210. $data=[
  211. "id"=>$id,
  212. "name"=>$name,
  213. "pid"=>$pid,
  214. "level"=>$level,
  215. "search"=>$search,
  216. "status"=>0,
  217. "is_del"=>0,
  218. "updatetime"=>date("Y-m-d H:i:s")
  219. ];
  220. $in =Db::name("exclusive")->save($data);
  221. if($in){
  222. return app_show(0,"编辑成功");
  223. }else{
  224. return error_show(1003,"编辑失败");
  225. }
  226. }
  227. public function delete(){
  228. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']) :"";
  229. $info = Db::name('exclusive')->where(["is_del"=>0,'id'=>$id])->find();
  230. if($info==false){
  231. return error_show(1002,"未找到数据");
  232. }
  233. $supp= Db::name('exclusive')->update(['id'=>$id,'is_del'=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  234. if($supp){
  235. return error_show(0,"删除成功");
  236. }else{
  237. return error_show(1002,"删除失败");
  238. }
  239. }
  240. }