Cat.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\ChangeLog;
  5. use app\BaseController;
  6. use think\App;
  7. use think\Exception;
  8. use think\facade\Db;
  9. use think\facade\Validate;
  10. //分类
  11. class Cat extends Base
  12. {
  13. //树状列表
  14. public function list()
  15. {
  16. $param = $this->request->only(['pid' => 0, 'cat_name' => ''], 'post', 'trim');
  17. $where = [['is_del', '=', 0], ['pid', '=', $param['pid']]];
  18. if ($param['cat_name'] !== '') $where[] = ['cat_name', 'like', '%' . $param['cat_name'] . '%'];
  19. $data = Db::name('cat')->where($where)->order("id desc")->cursor();
  20. $vmp = [];
  21. foreach ($data as $sts) {
  22. $vmp[] = stro($sts);
  23. }
  24. return app_show(0, '获取成功', $vmp);
  25. }
  26. //列表(含规格和创建人部门)
  27. public function plist()
  28. {
  29. $param = $this->request->only(['page' => 1, 'size' => 10, 'cat_name' => '', 'pid' => '', 'status' => '', 'company_name' => ''], 'post', 'trim');
  30. $where = [['c.is_del', '=', 0]];
  31. if ($param['cat_name'] !== '') $where[] = ['c.cat_name', 'like', '%' . $param['cat_name'] . '%'];
  32. if ($param['pid'] !== '') $where[] = ['c.pid', '=', $param['pid']];
  33. if ($param['status'] !== '') $where[] = ['c.status', '=', $param['status']];
  34. if ($param['company_name'] !== '') $where[] = ['c.createrid', 'in', get_company_item_user_by_name($param['company_name'])];
  35. $count = Db::name('cat')->alias('c')->where($where)->count();
  36. $list = Db::name('cat')
  37. ->alias('c')
  38. ->where($where)
  39. ->page($param['page'], $param['size'])
  40. ->order(['c.id'=>'desc'])
  41. ->select()
  42. ->toArray();
  43. $var = [];
  44. $all_createrid = array_column($list,'createrid');
  45. $item = get_company_name_by_uid($all_createrid);
  46. foreach ($list as $value) {
  47. $temp = Db::name('cat_specs')
  48. ->field('a.specs_id,b.spec_name')
  49. ->alias('a')
  50. ->leftJoin('specs b', 'b.id=a.specs_id')
  51. ->where(['a.cat_id' => $value['id'], 'a.is_del' => 0])
  52. ->select()
  53. ->toArray();
  54. if (empty($temp)) {
  55. $temp = [];
  56. }
  57. $value['im'] = $temp;
  58. $value['company_name'] = $item[$value['createrid']]??'';
  59. $var[] = $value;
  60. }
  61. return app_show(0, '获取成功', ['list' => $var, 'count' => $count]);
  62. }
  63. //查看所有三级分类
  64. public function wlist()
  65. {
  66. $param = $this->request->only(['page' => 1, 'size' => 10, 'cat_name' => '', 'search' => '', 'pid' => '', 'status' => ''], 'post', 'trim');
  67. $where = [['is_del', '=', 0], ['level', '=', 3]];
  68. if ($param['cat_name'] !== '') $where[] = ['cat_name', 'like', '%' . $param['cat_name'] . '%'];
  69. if ($param['search'] !== '') $where[] = ['search', 'like', '%' . $param['search'] . '%'];
  70. if ($param['pid'] !== '') $where[] = ['pid', '=', $param['pid']];
  71. if ($param['status'] !== '') $where[] = ['status', '=', $param['status']];
  72. $count = Db::name('cat')
  73. ->where($where)
  74. ->count();
  75. $list = Db::name('cat')
  76. ->where($where)
  77. ->page($param['page'], $param['size'])
  78. ->select()
  79. ->toArray();
  80. $data = [];
  81. foreach ($list as $value) {
  82. $value['item'] = array_column(made($value['id'] ?? 0), 'id');
  83. // $value['is_gold'] = cat_is_gold($value['id']);;
  84. $data[] = $value;
  85. }
  86. return app_show(0, '获取成功', ['list' => $data, 'count' => $count]);
  87. }
  88. //列表
  89. public function title()
  90. {
  91. $param = $this->request->only(['cat_name' => '', 'pid' => '', 'status' => '',], 'post', 'trim');
  92. $where = [['is_del', '=', 0]];
  93. if ($param['cat_name'] !== '') $where[] = ['cat_name', 'like', '%' . $param['cat_name'] . '%'];
  94. if ($param['pid'] !== '') $where[] = ['pid', '=', $param['pid']];
  95. if ($param['status'] !== '') $where[] = ['status', '=', $param['status']];
  96. $list = Db::name('cat')
  97. ->where($where)
  98. ->select()
  99. ->toArray();
  100. return app_show(0, '获取成功', $list);
  101. }
  102. //列表
  103. public function tlist()
  104. {
  105. $param = $this->request->only(['page' => 1, 'size' => 10, 'cat_name' => '', 'status' => '', 'creater' => '', 'start' => '', 'end' => '', 'company_name' => ''], 'post', 'trim');
  106. $where = [['c.is_del', '=', 0]];
  107. if ($param['cat_name'] !== '') $where[] = ['c.cat_name', 'like', '%' . $param['cat_name'] . '%'];
  108. if ($param['status'] !== '') $where[] = ['c.status', '=', $param['status']];
  109. if ($param['creater'] !== '') $where[] = ['c.creater', 'like', '%' . $param['creater'] . '%'];
  110. if ($param['start'] !== '') $where[] = ['c.addtime', '>=', $param['start']];
  111. if ($param['end'] !== '') $where[] = ['c.addtime', '<=', $param['end']];
  112. if ($param['company_name'] !== '') $where[] = ['c.createrid', 'in', get_company_item_user_by_name($param['company_name'])];
  113. $count = Db::name('cat')
  114. ->alias('c')
  115. ->leftJoin('depart_user u', 'u.uid=c.createrid AND u.is_del=0')
  116. ->where($where)
  117. ->count();
  118. // $total = ceil($count / $size);
  119. // $page = $page >= $total ? $total : $page;
  120. $list = Db::name('cat')
  121. ->alias('c')
  122. ->where($where)
  123. ->page($param['page'], $param['size'])
  124. ->order("id desc")
  125. ->select()
  126. ->toArray();
  127. $var = [];
  128. $all_createrid = array_column($list,'createrid');
  129. $item = get_company_name_by_uid($all_createrid);
  130. foreach ($list as $value) {
  131. $temp = Db::name('cat_specs')
  132. ->field('a.specs_id,b.spec_name')
  133. ->alias('a')
  134. ->leftJoin('specs b', 'b.id=a.specs_id')
  135. ->where(['a.cat_id' => $value['id'], 'a.is_del' => 0])
  136. ->select()
  137. ->toArray();
  138. $value['im'] = $temp;
  139. $value['company_name'] = $item[$value['createrid']] ?? '';
  140. $var[] = $value;
  141. }
  142. return app_show(0, '获取成功', ['list' => $var, 'count' => $count]);
  143. }
  144. //创建
  145. public function create()
  146. {
  147. $param = $this->request->only(['cat_name', 'pid', 'cat_desc' => '', 'fund_code' => '', 'specs_id', 'weight' => 0, 'status' => 0], 'post', 'trim');
  148. $val = Validate::rule([
  149. 'cat_name|分类名称' => 'require|max:255',
  150. 'pid|父级id' => 'require|number|egt:0',
  151. 'specs_id' => 'require|array|max:100',
  152. 'weight|权重' => 'number|egt:0',
  153. 'status|状态' => 'number|in:0,1',
  154. ]);
  155. if (!$val->check($param)) return json_show(1004, $val->getError());
  156. $level = 1;
  157. if ($param['pid'] != 0) {
  158. $levl = Db::name('cat')
  159. ->field('id,level,search')
  160. ->where(['id' => $param['pid']])
  161. ->find();
  162. if (empty($levl)) return json_show(1002, '未找到父级数据');
  163. $level = $levl['level'] + 1;
  164. $temp = $levl['search'];
  165. }
  166. if ($level == 1) $search = $param['cat_name'];
  167. else $search = $temp . '-' . $param['cat_name'];
  168. $repeat_name = Db::name('cat')
  169. ->field('id')
  170. ->where(['is_del' => 0, 'cat_name' => $param['cat_name']])
  171. ->find();
  172. $int = isset($repeat_name['id']) && $repeat_name['id'] != 0 ? made($repeat_name['id']) : [];
  173. $im = array_column($int, 'name');
  174. $in = implode('/', $im);
  175. if (!empty($repeat_name)) {
  176. return json_show(1004, "分类名称已在{$in}存在");
  177. }
  178. // $specs_id = isset($this->post['specs_id']) && $this->post['specs_id'] !== '' ? $this->post['specs_id'] : [];
  179. // if (empty($specs_id)) {
  180. // return error_show(1002, '参数specs_id不能为空');
  181. // }
  182. // $token = isset($this->post['token']) && $this->post['token'] != '' ? trim($this->post['token']) : '';
  183. // if ($token == '') {
  184. // return error_show(105, '参数token不能为空');
  185. // }
  186. // $user = GetUserInfo($token);
  187. // if (empty($user) || $user['code'] != 0) {
  188. // return error_show(102, '创建人数据不存在');
  189. // }
  190. $createrid = $this->uid;//isset($user['data']['id']) ? $user['data']['id'] : '';
  191. $creater = $this->uname;//($user['data']['nickname']) ? $user['data']['nickname'] : '';
  192. // $weight = isset($this->post['weight']) && $this->post['weight'] !== '' ? intval($this->post['weight']) : '0';
  193. // $status = isset($this->post['status']) && $this->post['status'] !== '' ? intval($this->post['status']) : '0';
  194. Db::startTrans();
  195. try {
  196. $date = date('Y-m-d H:i:s');
  197. $data = [
  198. 'cat_name' => $param['cat_name'],
  199. 'pid' => $param['pid'],
  200. 'level' => $level,
  201. 'search' => $search,
  202. 'weight' => $param['weight'],
  203. 'status' => $param['status'],
  204. 'cat_desc' => $param['cat_desc'],
  205. 'fund_code' => $param['fund_code'],
  206. 'creater' => $creater,
  207. 'createrid' => $createrid,
  208. 'is_del' => 0,
  209. 'addtime' => $date,
  210. 'updatetime' => $date,
  211. ];
  212. $datainfo = Db::name('cat')->insertGetId($data);
  213. $vat = [];
  214. if ($datainfo > 0) {
  215. $stx = ['order_code' => $param['pid'], 'status' => $param['status'], 'action_remark' => '', 'action_type' => 'create'];
  216. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], $stx, 'sxd', $param['status'], $stx);
  217. $spc = [];
  218. $spc['cat_id'] = $datainfo;
  219. $spc['exam_status'] = 0;
  220. $spc['status'] = 0;
  221. $spc['creater'] = $creater;
  222. $spc['createrid'] = $createrid;
  223. $spc['is_del'] = 0;
  224. $spc['addtime'] = $date;
  225. $spc['updatetime'] = $date;
  226. $vpn = Db::name('cat_spec')->insertGetId($spc);
  227. if ($vpn == '') throw new Exception('新建失败');
  228. foreach ($param['specs_id'] as $value) {
  229. $item = [];
  230. $item['cat_id'] = $datainfo;
  231. $item['specs_id'] = $value;
  232. $item['is_del'] = 0;
  233. $item['addtime'] = $date;
  234. $vat[] = $item;
  235. }
  236. $vp = Db::name('cat_specs')->insertAll($vat);
  237. if ($vp == 0) throw new Exception('新建失败');
  238. $catinfo = ['cat_id' => $datainfo, 'apply_name' => $creater, 'apply_id' => $createrid, 'fund_code' => $param['fund_code'], 'status' => 0];
  239. $this->addPlat($catinfo);
  240. Db::commit();
  241. return json_show(0, '新建成功');
  242. } else {
  243. Db::rollback();
  244. return json_show(1002, '新建失败');
  245. }
  246. } catch (Exception $exception) {
  247. Db::rollback();
  248. return json_show(1005, $exception->getMessage());
  249. }
  250. }
  251. //编辑
  252. public function edit()
  253. {
  254. $id = isset($this->post['id']) && $this->post['id'] !== '' ? intval($this->post['id']) : '';
  255. $idinfo = Db::name('cat')->where(['id' => $id])->find();
  256. if ($idinfo == '') {
  257. return error_show(1002, '未找到商品数据');
  258. }
  259. $cat_name = isset($this->post['cat_name']) && $this->post['cat_name'] !== '' ? trim($this->post['cat_name']) : '';
  260. if ($cat_name == '') {
  261. return error_show(1002, '参数cat_name不能为空');
  262. }
  263. $pid = isset($this->post['pid']) && $this->post['pid'] !== '' ? intval($this->post['pid']) : '';
  264. if ($pid === '') {
  265. return error_show(1002, '参数pid不能为空');
  266. }
  267. $cat_desc = isset($this->post['cat_desc']) && $this->post['cat_desc'] !== '' ? trim($this->post['cat_desc']) : '';
  268. $fund_code = isset($this->post['fund_code']) && $this->post['fund_code'] !== '' ? trim($this->post['fund_code']) : '';
  269. $specs_id = isset($this->post['specs_id']) && $this->post['specs_id'] !== '' ? $this->post['specs_id'] : [];
  270. if (empty($specs_id)) {
  271. return error_show(1002, '参数specs_id不能为空');
  272. }
  273. // $token = isset($this->post['token']) && $this->post['token'] != '' ? trim($this->post['token']) : '';
  274. // if ($token == '') {
  275. // return error_show(105, '参数token不能为空');
  276. // }
  277. // $user = GetUserInfo($token);
  278. // if (empty($user) || $user['code'] != 0) {
  279. // return error_show(102, '创建人数据不存在');
  280. // }
  281. $level = 1;
  282. if ($pid !== 0) {
  283. $levl = Db::name('cat')->where(['id' => $pid])->find();
  284. if (empty($levl)) {
  285. return error_show(1002, '未找到父级数据');
  286. }
  287. $level = $levl['level'] + 1;
  288. $temp = $levl['search'];
  289. }
  290. if ($level == 1) {
  291. $search = $cat_name;
  292. } else {
  293. $search = $temp . '_' . $cat_name;
  294. }
  295. $createrid = $this->uid;//isset($user['data']['id']) ? $user['data']['id'] : '';
  296. $creater = $this->uname;//isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  297. //$level = isset($this->post['level']) && $this->post['level'] !=='' ? intval($this->post['level']) :'';
  298. $weight = isset($this->post['weight']) && $this->post['weight'] !== '' ? intval($this->post['weight']) : '0';
  299. $status = isset($this->post['status']) && $this->post['status'] !== '' ? intval($this->post['status']) : '0';
  300. Db::startTrans();
  301. try {
  302. $str = [
  303. 'id' => $id,
  304. 'cat_name' => $cat_name,
  305. 'pid' => $pid,
  306. 'level' => $level,
  307. 'weight' => $weight,
  308. // 'status'=>$status,
  309. 'cat_desc' => $cat_desc,
  310. 'is_del' => 0,
  311. 'fund_code' => $fund_code,
  312. 'updatetime' => date('Y-m-d H:i:s'),
  313. 'search' => $search
  314. ];
  315. $strinfo = Db::name('cat')->where(['id' => $id])->save($str);
  316. $temp = array_diff($str, $idinfo);
  317. $json = json_encode($temp, JSON_UNESCAPED_UNICODE);
  318. $jsp = json_encode($idinfo, JSON_UNESCAPED_UNICODE);
  319. if ($strinfo) {
  320. $order = ['order_code' => $id, 'status' => $status, 'action_remark' => '', 'action_type' => 'edit'];
  321. ActionLog::logAdd(['id'=>$createrid,'nickname'=>$creater], $order, 'sxd', $status, $order);
  322. ChangeLog::logAdd(7, $idinfo['id'], $jsp, $json, ['id'=>$createrid,'nickname'=>$creater]);
  323. $spc = [];
  324. $vp = Db::name('cat_spec')->where(['cat_id' => $idinfo['id'], 'is_del' => 0])->find();
  325. $str = '';
  326. if ($vp != false) {
  327. isset($vp['id']) && $vp['id'] !== '' ? $spc['id'] = $vp['id'] : '';
  328. $spc['cat_id'] = $idinfo['id'];
  329. $spc['exam_status'] = 0;
  330. $spc['status'] = 0;
  331. $spc['is_del'] = 0;
  332. $spc['updatetime'] = date('Y-m_d H:i:s');
  333. $str = $vp['id'];
  334. $vpn = Db::name('cat_spec')->save($spc);
  335. } else {
  336. $spc['cat_id'] = $idinfo['id'];
  337. $spc['exam_status'] = 0;
  338. $spc['status'] = 0;
  339. $spc['creater'] = $creater;
  340. $spc['createrid'] = $createrid;
  341. $spc['is_del'] = 0;
  342. $spc['addtime'] = date('Y-m-d H:i:d');
  343. $spc['updatetime'] = date('Y-m_d H:i:s');
  344. $vpn = Db::name('cat_spec')->insert($spc, true);
  345. $str = $vpn;
  346. }
  347. if ($vpn == false) {
  348. Db::rollback();
  349. return error_show(1002, '更新失败');
  350. }
  351. $db = Db::name('cat_specs')->where(['cat_id' => $idinfo['id'], 'is_del' => 0])->column('specs_id');
  352. if (empty($db)) {
  353. $db = [];
  354. }
  355. $sn = array_diff($specs_id, $db);
  356. $st = array_diff($db, $specs_id);
  357. if (!empty($st)) {
  358. $int['is_del'] = 1;
  359. $dn = Db::name('cat_specs')->where(['cat_id' => $idinfo['id'], 'specs_id' => $st, 'is_del' => 0])->save($int);
  360. if ($dn == false) {
  361. Db::rollback();
  362. return error_show(1002, '编辑失败');
  363. }
  364. }
  365. if (!empty($sn)) {
  366. $vat = [];
  367. foreach ($sn as $value) {
  368. $item = [];
  369. $item['cat_id'] = $idinfo['id'];
  370. $item['specs_id'] = $value;
  371. $item['is_del'] = 0;
  372. $item['addtime'] = date('Y-m-d H:i:s');
  373. $vat[] = $item;
  374. }
  375. $vp = Db::name('cat_specs')->insertAll($vat);
  376. if ($vp == 0) {
  377. Db::rollback();
  378. return error_show(1002, '编辑失败');
  379. }
  380. }
  381. $itn = sear($id);
  382. Db::commit();
  383. return error_show(0, '编辑成功');
  384. } else {
  385. Db::rollback();
  386. return error_show(1002, '编辑失败');
  387. }
  388. } catch (\Exception $e) {
  389. Db::rollback();
  390. return error_show(1005, $e->getMessage());
  391. }
  392. }
  393. //更新分类利率相关
  394. public function ratedit()
  395. {
  396. $id = isset($this->post['id']) && $this->post['id'] !== '' ? intval($this->post['id']) : '';
  397. $idinfo = Db::name('cat')->where(['id' => $id])->find();
  398. if ($idinfo == '') {
  399. return error_show(1002, '未找到商品数据');
  400. }
  401. $rate = isset($this->post['rate']) && $this->post['rate'] !== '' ? floatval($this->post['rate']) : '';
  402. $order_rate = isset($this->post['order_rate']) && $this->post['order_rate'] !== '' ? floatval($this->post['order_rate']) : '';
  403. $sale_rate = isset($this->post['sale_rate']) && $this->post['sale_rate'] !== '' ? floatval($this->post['sale_rate']) : '';
  404. $lower_rate = isset($this->post['lower_rate']) && $this->post['lower_rate'] !== '' ? floatval($this->post['lower_rate']) : '';
  405. $money_rate = isset($this->post['money_rate']) && $this->post['money_rate'] !== '' ? floatval($this->post['money_rate']) : '';
  406. $low_rate = isset($this->post['low_rate']) && $this->post['low_rate'] !== '' ? floatval($this->post['low_rate']) : '';
  407. if ($idinfo['pid'] == 0) {
  408. if ($rate === '') {
  409. return error_show(1003, '参数rate不能为空');
  410. }
  411. if ($order_rate === '') {
  412. return error_show(1003, '参数order_rate不能为空');
  413. }
  414. if ($sale_rate === '') {
  415. return error_show(1003, '参数sale_rate不能为空');
  416. }
  417. if ($lower_rate === '') {
  418. return error_show(1003, '参数lower_rate不能为空');
  419. }
  420. if ($money_rate == '') {
  421. return error_show(1003, '参数money_rate不能为空');
  422. }
  423. if ($low_rate == '') {
  424. return error_show(1003, '参数low_rate不能为空');
  425. }
  426. }
  427. $data = [
  428. 'id' => $id,
  429. 'rate' => $rate,
  430. 'lower_rate' => $lower_rate,
  431. 'order_rate' => $order_rate,
  432. 'sale_rate' => $sale_rate,
  433. 'money_rate' => $money_rate,
  434. 'low_rate' => $low_rate,
  435. 'is_del' => 0,
  436. 'updatetime' => date('Y-m-d H:i:s'),
  437. ];
  438. $strinfo = Db::name('cat')->where(['id' => $id])->save($data);
  439. if ($strinfo) {
  440. return error_show(0, '更新成功');
  441. } else {
  442. return error_show(1002, '更新失败');
  443. }
  444. }
  445. //详情
  446. public function info()
  447. {
  448. $id = $this->request->post('id/d', 0, 'trim');
  449. $idinfo = Db::name('cat')
  450. ->where(['id' => $id])
  451. ->append(['spec'])
  452. ->withAttr('spec', function ($val, $da) {
  453. return Db::name('cat_specs')
  454. ->alias('a')
  455. ->leftJoin('specs b', 'b.id=a.specs_id')
  456. ->where(['a.cat_id' => $da['id'], 'a.is_del' => 0])
  457. ->field('a.specs_id id,b.spec_name')
  458. ->select()
  459. ->toArray();
  460. })
  461. ->findOrEmpty();
  462. return $idinfo ? json_show(0, '获取成功', $idinfo) : json_show(1002, '未找到数据');
  463. }
  464. //启禁用
  465. public function status()
  466. {
  467. $id = isset($this->post['id']) && $this->post['id'] !== '' ? intval($this->post['id']) : '';
  468. if ($id === '') {
  469. return error_show(1002, '参数id不能为空');
  470. }
  471. $stn = Db::name('cat')->where(['id' => $id])->find();
  472. if (empty($stn)) {
  473. return error_show(1002, '未找到商品数据');
  474. }
  475. $status = isset($this->post['status']) && $this->post['status'] !== '' ? intval($this->post['status']) : '';
  476. if ($status === '') {
  477. return error_show(1002, '参数status不能为空');
  478. }
  479. if ($stn['level'] == 3) {
  480. if ($status == 1) {
  481. $can = made($stn['id']);
  482. $cat = array_column($can, 'id');
  483. } else {
  484. $cat = $stn['id'];
  485. }
  486. // return error_show(1002, '所在级别不能启用');
  487. }
  488. if ($stn['level'] == 2 || $stn['level'] == 1) {
  489. if ($status == 1) {
  490. return error_show(1002, '所在等级不能启用');
  491. } else {
  492. $db = Db::name('cat')->where(['pid' => $stn['id'], 'status' => 1, 'is_del' => 0])->count();
  493. if ($db == 0) {
  494. $cat = $stn['id'];
  495. } else {
  496. return error_show(1002, '子级分类未禁用');
  497. }
  498. }
  499. }
  500. // $pd= $stn['status'];
  501. $it = [];
  502. $it['status'] = $status;
  503. $it['updatetime'] = date('Y-m-d H:i:s');
  504. $str = Db::name('cat')->where(['id' => $cat, 'is_del' => 0])->save($it);
  505. if ($str) {
  506. $catinfo = ['cat_id' => $id, 'status' => $status];
  507. $this->addPlat($catinfo);
  508. if (!empty($cat)) {
  509. $catinfo = ['cat_id' => $cat, 'status' => $status];
  510. $this->addPlat($catinfo);
  511. }
  512. return error_show(0, '状态更新成功');
  513. } else {
  514. return error_show(1002, '状态更新失败');
  515. }
  516. }
  517. //删除
  518. public function delete()
  519. {
  520. $id = isset($this->post['id']) && $this->post['id'] !== '' ? intval($this->post['id']) : '';
  521. $info = Db::name('cat')->where(['is_del' => 0, 'id' => $id])->find();
  522. if ($info == false) {
  523. return error_show(1002, '未找到数据');
  524. }
  525. $supp = Db::name('cat')->update(['id' => $id, 'is_del' => 1, 'updatetime' => date('Y-m-d H:i:s')]);
  526. if ($supp) {
  527. return error_show(0, '删除成功');
  528. } else {
  529. return error_show(1002, '删除失败');
  530. }
  531. }
  532. //平台添加分类
  533. private function addPlat($cat)
  534. {
  535. $plat = Db::name('platform')
  536. ->field('id')
  537. ->where(['platform_type' => 1, 'is_del' => 0])
  538. ->select()
  539. ->toArray();
  540. if (empty($plat)) return false;
  541. $date = date('Y-m-d H:i:s');
  542. $isT = Db::name('cat_plat')
  543. ->where(['cat_id' => $cat['cat_id'], 'is_del' => 0])
  544. ->column('id', 'platform_id');
  545. foreach ($plat as $value) {
  546. $cat['platform_id'] = $value['id'];
  547. $cat['addtime'] = $date;
  548. $cat['updatetime'] = $date;
  549. if (isset($isT[$cat['platform_id']])) {
  550. $data = $cat;
  551. unset($data['cat_id']);
  552. unset($data['addtime']);
  553. Db::name('cat_plat')
  554. ->where('id', $isT[$cat['platform_id']])
  555. ->update($data);
  556. } else Db::name('cat_plat')->save($cat);
  557. }
  558. return true;
  559. }
  560. }