Exclusive.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Exception;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. //泰康专属的一个列表
  8. class Exclusive extends Base
  9. {
  10. public function __construct(App $app)
  11. {
  12. parent::__construct($app);
  13. }
  14. public function list(){
  15. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']) :"1";
  16. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
  17. $where =[["is_del","=",0]];
  18. $cat_name=isset($this->post['name']) && $this->post['name'] !==""? trim($this->post['name']) :"";
  19. if($cat_name!==""){
  20. $where[]=['name',"like","%$cat_name%"];
  21. }
  22. $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
  23. if($pid!==""){
  24. $where[]=['pid',"=",$pid];
  25. }
  26. $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
  27. if($status!==""){
  28. $where[]=['status',"=",$status];
  29. }
  30. $count = Db::name("exclusive")->where($where)->count();
  31. $total = ceil($count / $size);
  32. $page = $page >= $total ? $total : $page;
  33. $list = Db::name('exclusive')->where($where)->page($page, $size)->select();
  34. return app_show(0, "获取成功", ['list' =>$list, 'count' => $count]);
  35. }
  36. public function add(){
  37. $name = isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  38. if($name==""){
  39. return error_show(1004,"参数name不能为空");
  40. }
  41. $rename = Db::name('exclusive')->where(['is_del' => 0, 'name' => $name])->find();
  42. if (!empty($rename)) {
  43. return error_show(1002, "专属类型名称已存在");
  44. }
  45. $pid =isset($this->post['pid'])&&$this->post['pid']!=="" ? intval($this->post['pid']):0;
  46. if($pid==""){
  47. return error_show(1002,"父级id不能为空");
  48. }
  49. $level=1;
  50. $search = $name;
  51. if($pid!==0){
  52. $parent= Db::name("exclusive")->where(["id"=>$pid,"is_del"=>0])->find();
  53. if($parent==false){
  54. return error_show(1003,"父级数据未找到");
  55. }
  56. $search=$parent['search']."/".$name;
  57. $level=$parent['level']+1;
  58. }
  59. $token = isset($this->post['token'])&&$this->post['token']!="" ? trim($this->post['token']):"";
  60. $user =GetUserInfo($token);
  61. if(empty($user)||$user['code']!=0){
  62. return error_show($user['code'],$user['msg']);
  63. }
  64. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  65. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  66. $data=[
  67. "name"=>$name,
  68. "pid"=>$pid,
  69. "level"=>$level,
  70. "search"=>$search,
  71. "createrid"=>$createrid,
  72. "creater"=>$creater,
  73. "status"=>0,
  74. "is_del"=>0,
  75. "addtime"=>date("Y-m-d H:i:s"),
  76. "updatetime"=>date("Y-m-d H:i:s")
  77. ];
  78. $in =Db::name("exclusive")->insert($data);
  79. if($in){
  80. return app_show(0,"新建成功");
  81. }else{
  82. return error_show(1003,"新建失败");
  83. }
  84. }
  85. //业务参数-专属类型管理的启用/禁用
  86. public function status()
  87. {
  88. // $id=isset($this->post['id'])&&$this->post['id']!=="" ? intval($this->post['id']):"";
  89. // if($id===""){
  90. // return error_show(1004,"参数id不能为空");
  91. // }
  92. // $exclusive=Db::name("exclusive")->where(["id"=>$id,"is_del"=>0])->find();
  93. // if($exclusive==false){
  94. // return error_show(1004,"未找到数据");
  95. // }
  96. // $msg = $exclusive['status']==0? "启用":"禁用";
  97. // $exclusive['status'] = $exclusive['status']==0?1:0;
  98. // $exclusive['updatetime'] =date("Y-m-d H:i:s");
  99. // $in =Db::name("exclusive")->save($exclusive);
  100. // if($in){
  101. // return app_show(0,"{$msg}成功");
  102. // }else{
  103. // return error_show(1004,"{$msg}失败");
  104. // }
  105. $param = $this->request->only(['id', 'status']);
  106. $val = Validate::rule([
  107. 'id' => 'require|number|gt:0',
  108. 'status' => 'require|number|in:0,1',
  109. ]);
  110. if (!$val->check($param)) return error_show(1004, $val->getError());
  111. Db::startTrans();
  112. try {
  113. $res = Db::name('exclusive')
  114. ->field('id,level,status,pid')
  115. ->where('id', $param['id'])
  116. ->find();
  117. if (empty($res)) throw new Exception('该专属类型不存在');
  118. //启用
  119. if ($param['status'] == 1) {
  120. //一级
  121. if ($res['level'] == 1) {
  122. //是否存在子级
  123. $total = Db::name('exclusive')
  124. ->where(['pid' => $param['id'], 'is_del' => 0])
  125. ->count('id');
  126. if ($total) {
  127. //有子级的话,子级有一个启用,即可启用
  128. $child_id = Db::name('exclusive')
  129. ->field('id')
  130. ->where(['pid' => $param['id'], 'is_del' => 0, 'status' => 1])
  131. ->find();
  132. if (empty($child_id)) throw new Exception('请先启用一个子级');
  133. }
  134. } else {
  135. //二级的话,顺便把它的上级也启用了
  136. Db::name('exclusive')
  137. ->where(['id' => $res['pid'], 'is_del' => 0, 'status' => 0])
  138. ->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  139. }
  140. } elseif ($param['status'] == 0) {
  141. //禁用
  142. if ($res['level'] == 1) {
  143. //是否存在子级
  144. $total = Db::name('exclusive')
  145. ->where(['pid' => $param['id'], 'is_del' => 0])
  146. ->count('id');
  147. if ($total) {
  148. //有子级的话,子级全部禁用,方可禁用
  149. $child_id = Db::name('exclusive')
  150. ->field('id')
  151. ->where(['pid' => $param['id'], 'is_del' => 0, 'status' => 1])
  152. ->find();
  153. if (!empty($child_id)) throw new Exception('请先禁用全部子级');
  154. }
  155. }else{
  156. //如果它的兄弟全部禁用的话,连同它的父级也禁用
  157. $normal_c = Db::name('exclusive')
  158. ->field('id')
  159. ->where(['pid' => $res['pid'], 'is_del' => 0, 'status' => 1])
  160. ->where('id', '<>', $param['id'])
  161. ->find();
  162. if (empty($normal_c)) {
  163. Db::name('exclusive')
  164. ->where(['id' => $res['pid'], 'status' => 1])
  165. ->update(['status' => 0, 'updatetime' => date('Y-m-d H:i:s')]);
  166. }
  167. }
  168. }
  169. //修改自己
  170. Db::name('exclusive')
  171. ->where(['id' => $param['id'], 'is_del' => 0])
  172. ->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  173. Db::commit();
  174. return error_show(0, "状态更新成功");
  175. } catch (\Exception $e) {
  176. Db::rollback();
  177. return error_show(1005, $e->getMessage());
  178. }
  179. // $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  180. // if($id===""){
  181. // return error_show(1002,"参数id不能为空");
  182. // }
  183. // $stn = Db::name('exclusive')->where(['id'=>$id])->find();
  184. // if(empty($stn)){
  185. // return error_show(1002,"未找到商品数据");
  186. // }
  187. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  188. // if($status===""){
  189. // return error_show(1002,"参数status不能为空");
  190. // }
  191. // if ($stn['level'] == 2) {
  192. // if ($status == 1) {
  193. // $can = mai($stn['id']);
  194. // $cat = array_column($can, 'id');//子级如果启用,连同它的父级一起启用
  195. // } else {
  196. // $cat = $stn['id'];
  197. // }
  198. // // return error_show(1002, "所在级别不能启用");
  199. // }
  200. // if ($stn['level'] == 1) {
  201. // if ($status == 1) {
  202. // return error_show(1002, "所在等级不能启用");
  203. // } else {
  204. // $db = Db::name('exclusive')->where(['pid' => $stn['id'], 'status' => 1, 'is_del' => 0])->count();
  205. // if ($db == 0) {
  206. // $cat = $stn['id'];
  207. // } else {
  208. // return error_show(1002, "子级分类未禁用");
  209. // }
  210. // }
  211. // }
  212. // // $pd= $stn['status'];
  213. // $it = [];
  214. // $it['status'] = $status;
  215. // $it['updatetime'] = date("Y-m-d H:i:s");
  216. // $str = Db::name('exclusive')->where(['id' => $cat, 'is_del' => 0])->save($it);
  217. // if ($str) {
  218. //// $order = ["order_code"=>$id,"status"=>$pd,"action_remark"=>'',"action_type"=>"edit"];
  219. //// ActionLog::logAdd($this->post['token'],$order,"sxd",$stn['status'],$order);
  220. // return error_show(0, "状态更新成功");
  221. // } else {
  222. // return error_show(1002, "状态更新失败");
  223. // }
  224. }
  225. public function query(){
  226. // $where =[["is_del","=",0]];
  227. // $cat_name=isset($this->post['name']) && $this->post['name'] !==""? trim($this->post['name']) :"";
  228. // if($cat_name!==""){
  229. // $where[]=['name',"like","%$cat_name%"];
  230. // }
  231. // $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
  232. // if($pid!==""){
  233. // $where[]=['pid',"=",$pid];
  234. // }
  235. // $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
  236. // if($status!==""){
  237. // $where[]=['status',"=",$status];
  238. // }
  239. // $level=isset($this->post['level']) && $this->post['level'] !==""? intval($this->post['level']) :"";
  240. // if($level!==""){
  241. // $where[]=['level',"=",$level];
  242. // }
  243. // $list = Db::name("exclusive")->where($where)->select();
  244. // return app_show(0,"获取成功",$list);
  245. $where=[["is_del","=",0]];
  246. $pid = isset($this->post['pid']) &&$this->post['pid']!=="" ?intval($this->post['pid']): "0";
  247. $where[]=["pid","=",$pid];
  248. $cat_name=isset($this->post['cat_name']) && $this->post['cat_name'] !==""? intval($this->post['cat_name']) :"";
  249. if($cat_name!==""){
  250. $where[]=['cat_name',"like","%$cat_name%"];
  251. }
  252. $data = Db::name("exclusive")->where($where)->select();
  253. $vmp = [];
  254. foreach ($data as $sts){
  255. $vmp[]=coco($sts);
  256. }
  257. return app_show(0,"获取成功",$vmp);
  258. }
  259. public function edit(){
  260. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  261. if($id==""){
  262. return error_show(1004,"参数id不能为空");
  263. }
  264. $info = Db::name('exclusive')->where(['id'=>$id,'is_del'=>0])->find();
  265. if($info==""){
  266. return error_show(1002,"未找到数据");
  267. }
  268. if($info['status']===1){
  269. return error_show(1004,"启用状态不可编辑");
  270. }
  271. $name = isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  272. if($name==""){
  273. return error_show(1004,"参数name不能为空");
  274. }
  275. $pid =isset($this->post['pid'])&&$this->post['pid']!=="" ? intval($this->post['pid']):0;
  276. if($pid==""){
  277. return error_show(1004,"参数pid不能为空");
  278. }
  279. $level=1;
  280. $search = $name;
  281. if($pid!==0){
  282. $parent= Db::name("exclusive")->where(["id"=>$pid,"is_del"=>0])->find();
  283. if($parent==false){
  284. return error_show(1003,"父级数据未找到");
  285. }
  286. $search=$parent['search']."/".$name;
  287. $level=$parent['level']+1;
  288. }
  289. $data=[
  290. "id"=>$id,
  291. "name"=>$name,
  292. "pid"=>$pid,
  293. "level"=>$level,
  294. "search"=>$search,
  295. "status"=>0,
  296. "is_del"=>0,
  297. "updatetime"=>date("Y-m-d H:i:s")
  298. ];
  299. $in =Db::name("exclusive")->save($data);
  300. if($in){
  301. return app_show(0,"编辑成功");
  302. }else{
  303. return error_show(1003,"编辑失败");
  304. }
  305. }
  306. public function delete(){
  307. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']) :"";
  308. $info = Db::name('exclusive')->where(["is_del"=>0,'id'=>$id])->find();
  309. if($info==false){
  310. return error_show(1002,"未找到数据");
  311. }
  312. $supp= Db::name('exclusive')->update(['id'=>$id,'is_del'=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  313. if($supp){
  314. return error_show(0,"删除成功");
  315. }else{
  316. return error_show(1002,"删除失败");
  317. }
  318. }
  319. }