Cat.php 27 KB

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