123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\ActionLog;
- use app\admin\model\ChangeLog;
- use app\BaseController;
- use think\App;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- //分类
- class Cat extends Base
- {
- //树状列表
- public function list()
- {
- $param = $this->request->only(['pid' => 0, 'cat_name' => ''], 'post', 'trim');
- $where = [['is_del', '=', 0], ['pid', '=', $param['pid']]];
- if ($param['cat_name'] !== '') $where[] = ['cat_name', 'like', '%' . $param['cat_name'] . '%'];
- $data = Db::name('cat')->where($where)->order("id desc")->cursor();
- $vmp = [];
- foreach ($data as $sts) {
- $vmp[] = stro($sts);
- }
- return app_show(0, '获取成功', $vmp);
- }
- //列表(含规格和创建人部门)
- public function plist()
- {
- $param = $this->request->only(['page' => 1, 'size' => 10, 'cat_name' => '', 'pid' => '', 'status' => '', 'company_name' => ''], 'post', 'trim');
- $where = [['c.is_del', '=', 0]];
- if ($param['cat_name'] !== '') $where[] = ['c.cat_name', 'like', '%' . $param['cat_name'] . '%'];
- if ($param['pid'] !== '') $where[] = ['c.pid', '=', $param['pid']];
- if ($param['status'] !== '') $where[] = ['c.status', '=', $param['status']];
- if ($param['company_name'] !== '') $where[] = ['c.createrid', 'in', get_company_item_user_by_name($param['company_name'])];
- $count = Db::name('cat')->alias('c')->where($where)->count();
- // $total = ceil($count / $size);
- // $page = $page >= $total ? $total : $page;
- $list = Db::name('cat')
- ->alias('c')
- ->where($where)
- ->page($param['page'], $param['size'])
- ->order(['c.id'=>'desc'])
- ->select()
- ->toArray();
- $var = [];
- $all_createrid = array_column($list,'createrid');
- $item = get_company_name_by_uid($all_createrid);
- foreach ($list as $value) {
- $temp = Db::name('cat_specs')
- ->field('a.specs_id,b.spec_name')
- ->alias('a')
- ->leftJoin('specs b', 'b.id=a.specs_id')
- ->where(['a.cat_id' => $value['id'], 'a.is_del' => 0])
- ->select()
- ->toArray();
- // $temp = Db::name('specs')
- // ->where(['id' => $info, 'is_del' => 0])
- // ->field('id as specid,spec_name')
- // ->select()
- // ->toArray();
- if (empty($temp)) {
- $temp = [];
- }
- $value['im'] = $temp;
- $value['company_name'] = $item[$value['createrid']]??'';
- $var[] = $value;
- }
- return app_show(0, '获取成功', ['list' => $var, 'count' => $count]);
- }
- //查看所有三级分类
- public function wlist()
- {
- $param = $this->request->only(['page' => 1, 'size' => 10, 'cat_name' => '', 'search' => '', 'pid' => '', 'status' => ''], 'post', 'trim');
- $where = [['is_del', '=', 0], ['level', '=', 3]];
- if ($param['cat_name'] !== '') $where[] = ['cat_name', 'like', '%' . $param['cat_name'] . '%'];
- if ($param['search'] !== '') $where[] = ['search', 'like', '%' . $param['search'] . '%'];
- if ($param['pid'] !== '') $where[] = ['pid', '=', $param['pid']];
- if ($param['status'] !== '') $where[] = ['status', '=', $param['status']];
- $count = Db::name('cat')
- ->where($where)
- ->count();
- $list = Db::name('cat')
- ->where($where)
- ->page($param['page'], $param['size'])
- ->select()
- ->toArray();
- $data = [];
- foreach ($list as $value) {
- $value['item'] = array_column(made($value['id'] ?? 0), 'id');
- $data[] = $value;
- }
- return app_show(0, '获取成功', ['list' => $data, 'count' => $count]);
- }
- //列表
- public function title()
- {
- $param = $this->request->only(['cat_name' => '', 'pid' => '', 'status' => '',], 'post', 'trim');
- $where = [['is_del', '=', 0]];
- if ($param['cat_name'] !== '') $where[] = ['cat_name', 'like', '%' . $param['cat_name'] . '%'];
- if ($param['pid'] !== '') $where[] = ['pid', '=', $param['pid']];
- if ($param['status'] !== '') $where[] = ['status', '=', $param['status']];
- $list = Db::name('cat')
- ->where($where)
- ->select()
- ->toArray();
- return app_show(0, '获取成功', $list);
- }
- //列表
- public function tlist()
- {
- $param = $this->request->only(['page' => 1, 'size' => 10, 'cat_name' => '', 'status' => '', 'creater' => '', 'start' => '', 'end' => '', 'company_name' => ''], 'post', 'trim');
- $where = [['c.is_del', '=', 0]];
- if ($param['cat_name'] !== '') $where[] = ['c.cat_name', 'like', '%' . $param['cat_name'] . '%'];
- if ($param['status'] !== '') $where[] = ['c.status', '=', $param['status']];
- if ($param['creater'] !== '') $where[] = ['c.creater', 'like', '%' . $param['creater'] . '%'];
- if ($param['start'] !== '') $where[] = ['c.addtime', '>=', $param['start']];
- if ($param['end'] !== '') $where[] = ['c.addtime', '<=', $param['end']];
- if ($param['company_name'] !== '') $where[] = ['c.createrid', 'in', get_company_item_user_by_name($param['company_name'])];
- $count = Db::name('cat')
- ->alias('c')
- ->leftJoin('depart_user u', 'u.uid=c.createrid AND u.is_del=0')
- ->where($where)
- ->count();
- // $total = ceil($count / $size);
- // $page = $page >= $total ? $total : $page;
- $list = Db::name('cat')
- ->alias('c')
- ->where($where)
- ->page($param['page'], $param['size'])
- ->order("id desc")
- ->select()
- ->toArray();
- $var = [];
- $all_createrid = array_column($list,'createrid');
- $item = get_company_name_by_uid($all_createrid);
- foreach ($list as $value) {
- $temp = Db::name('cat_specs')
- ->field('a.specs_id,b.spec_name')
- ->alias('a')
- ->leftJoin('specs b', 'b.id=a.specs_id')
- ->where(['a.cat_id' => $value['id'], 'a.is_del' => 0])
- ->select()
- ->toArray();
- $value['im'] = $temp;
- $value['company_name'] = $item[$value['createrid']] ?? '';
- $var[] = $value;
- }
- return app_show(0, '获取成功', ['list' => $var, 'count' => $count]);
- }
- //创建
- public function create()
- {
- $param = $this->request->only(['cat_name', 'pid', 'cat_desc' => '', 'fund_code' => '', 'specs_id', 'weight' => 0, 'status' => 0], 'post', 'trim');
- // $cat_name = isset($this->post['cat_name']) && $this->post['cat_name'] !== '' ? trim($this->post['cat_name']) : '';
- $val = Validate::rule([
- 'cat_name|分类名称' => 'require|max:255',
- 'pid|父级id' => 'require|number|egt:0',
- 'specs_id' => 'require|array|max:100',
- 'weight|权重' => 'number|egt:0',
- 'status|状态' => 'number|in:0,1',
- ]);
- if (!$val->check($param)) return json_show(1004, $val->getError());
- // if ($cat_name == '') {
- // return error_show(1002, '参数cat_name不能为空');
- // }
- // $pid = isset($this->post['pid']) && $this->post['pid'] !== '' ? intval($this->post['pid']) : '0';
- // if ($pid === '') {
- // return error_show(1002, '参数pid不能为空');
- // }
- // $cat_desc = isset($this->post['cat_desc']) && $this->post['cat_desc'] !== '' ? trim($this->post['cat_desc']) : '';
- // $fund_code = isset($this->post['fund_code']) && $this->post['fund_code'] !== '' ? trim($this->post['fund_code']) : '';
- $level = 1;
- if ($param['pid'] != 0) {
- $levl = Db::name('cat')
- ->field('id,level,search')
- ->where(['id' => $param['pid']])
- ->find();
- if (empty($levl)) return json_show(1002, '未找到父级数据');
- $level = $levl['level'] + 1;
- $temp = $levl['search'];
- }
- if ($level == 1) $search = $param['cat_name'];
- else $search = $temp . '-' . $param['cat_name'];
- $repeat_name = Db::name('cat')
- ->field('id')
- ->where(['is_del' => 0, 'cat_name' => $param['cat_name']])
- ->find();
- $int = isset($repeat_name['id']) && $repeat_name['id'] != 0 ? made($repeat_name['id']) : [];
- $im = array_column($int, 'name');
- $in = implode('/', $im);
- if (!empty($repeat_name)) {
- return json_show(1004, "分类名称已在{$in}存在");
- }
- // $specs_id = isset($this->post['specs_id']) && $this->post['specs_id'] !== '' ? $this->post['specs_id'] : [];
- // if (empty($specs_id)) {
- // return error_show(1002, '参数specs_id不能为空');
- // }
- // $token = isset($this->post['token']) && $this->post['token'] != '' ? trim($this->post['token']) : '';
- // if ($token == '') {
- // return error_show(105, '参数token不能为空');
- // }
- // $user = GetUserInfo($token);
- // if (empty($user) || $user['code'] != 0) {
- // return error_show(102, '创建人数据不存在');
- // }
- $createrid = $this->uid;//isset($user['data']['id']) ? $user['data']['id'] : '';
- $creater = $this->uname;//($user['data']['nickname']) ? $user['data']['nickname'] : '';
- // $weight = isset($this->post['weight']) && $this->post['weight'] !== '' ? intval($this->post['weight']) : '0';
- // $status = isset($this->post['status']) && $this->post['status'] !== '' ? intval($this->post['status']) : '0';
- Db::startTrans();
- try {
- $date = date('Y-m-d H:i:s');
- $data = [
- 'cat_name' => $param['cat_name'],
- 'pid' => $param['pid'],
- 'level' => $level,
- 'search' => $search,
- 'weight' => $param['weight'],
- 'status' => $param['status'],
- 'cat_desc' => $param['cat_desc'],
- 'fund_code' => $param['fund_code'],
- 'creater' => $creater,
- 'createrid' => $createrid,
- 'is_del' => 0,
- 'addtime' => $date,
- 'updatetime' => $date,
- ];
- $datainfo = Db::name('cat')->insertGetId($data);
- $vat = [];
- if ($datainfo > 0) {
- $stx = ['order_code' => $param['pid'], 'status' => $param['status'], 'action_remark' => '', 'action_type' => 'create'];
- ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], $stx, 'sxd', $param['status'], $stx);
- $spc = [];
- $spc['cat_id'] = $datainfo;
- $spc['exam_status'] = 0;
- $spc['status'] = 0;
- $spc['creater'] = $creater;
- $spc['createrid'] = $createrid;
- $spc['is_del'] = 0;
- $spc['addtime'] = $date;
- $spc['updatetime'] = $date;
- $vpn = Db::name('cat_spec')->insertGetId($spc);
- if ($vpn == '') throw new Exception('新建失败');
- foreach ($param['specs_id'] as $value) {
- $item = [];
- $item['cat_id'] = $datainfo;
- $item['specs_id'] = $value;
- $item['is_del'] = 0;
- $item['addtime'] = $date;
- $vat[] = $item;
- }
- $vp = Db::name('cat_specs')->insertAll($vat);
- if ($vp == 0) throw new Exception('新建失败');
- $catinfo = ['cat_id' => $datainfo, 'apply_name' => $creater, 'apply_id' => $createrid, 'fund_code' => $param['fund_code'], 'status' => 0];
- $this->addPlat($catinfo);
- Db::commit();
- return json_show(0, '新建成功');
- } else {
- Db::rollback();
- return json_show(1002, '新建失败');
- }
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1005, $exception->getMessage());
- }
- }
- //编辑
- public function edit()
- {
- $id = isset($this->post['id']) && $this->post['id'] !== '' ? intval($this->post['id']) : '';
- $idinfo = Db::name('cat')->where(['id' => $id])->find();
- if ($idinfo == '') {
- return error_show(1002, '未找到商品数据');
- }
- $cat_name = isset($this->post['cat_name']) && $this->post['cat_name'] !== '' ? trim($this->post['cat_name']) : '';
- if ($cat_name == '') {
- return error_show(1002, '参数cat_name不能为空');
- }
- $pid = isset($this->post['pid']) && $this->post['pid'] !== '' ? intval($this->post['pid']) : '';
- if ($pid === '') {
- return error_show(1002, '参数pid不能为空');
- }
- $cat_desc = isset($this->post['cat_desc']) && $this->post['cat_desc'] !== '' ? trim($this->post['cat_desc']) : '';
- $fund_code = isset($this->post['fund_code']) && $this->post['fund_code'] !== '' ? trim($this->post['fund_code']) : '';
- $specs_id = isset($this->post['specs_id']) && $this->post['specs_id'] !== '' ? $this->post['specs_id'] : [];
- if (empty($specs_id)) {
- return error_show(1002, '参数specs_id不能为空');
- }
- // $token = isset($this->post['token']) && $this->post['token'] != '' ? trim($this->post['token']) : '';
- // if ($token == '') {
- // return error_show(105, '参数token不能为空');
- // }
- // $user = GetUserInfo($token);
- // if (empty($user) || $user['code'] != 0) {
- // return error_show(102, '创建人数据不存在');
- // }
- $level = 1;
- if ($pid !== 0) {
- $levl = Db::name('cat')->where(['id' => $pid])->find();
- if (empty($levl)) {
- return error_show(1002, '未找到父级数据');
- }
- $level = $levl['level'] + 1;
- $temp = $levl['search'];
- }
- if ($level == 1) {
- $search = $cat_name;
- } else {
- $search = $temp . '_' . $cat_name;
- }
- $createrid = $this->uid;//isset($user['data']['id']) ? $user['data']['id'] : '';
- $creater = $this->uname;//isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
- //$level = isset($this->post['level']) && $this->post['level'] !=='' ? intval($this->post['level']) :'';
- $weight = isset($this->post['weight']) && $this->post['weight'] !== '' ? intval($this->post['weight']) : '0';
- $status = isset($this->post['status']) && $this->post['status'] !== '' ? intval($this->post['status']) : '0';
- Db::startTrans();
- try {
- $str = [
- 'id' => $id,
- 'cat_name' => $cat_name,
- 'pid' => $pid,
- 'level' => $level,
- 'weight' => $weight,
- // 'status'=>$status,
- 'cat_desc' => $cat_desc,
- 'is_del' => 0,
- 'fund_code' => $fund_code,
- 'updatetime' => date('Y-m-d H:i:s'),
- 'search' => $search
- ];
- $strinfo = Db::name('cat')->where(['id' => $id])->save($str);
- $temp = array_diff($str, $idinfo);
- $json = json_encode($temp, JSON_UNESCAPED_UNICODE);
- $jsp = json_encode($idinfo, JSON_UNESCAPED_UNICODE);
- if ($strinfo) {
- $order = ['order_code' => $id, 'status' => $status, 'action_remark' => '', 'action_type' => 'edit'];
- ActionLog::logAdd(['id'=>$createrid,'nickname'=>$creater], $order, 'sxd', $status, $order);
- ChangeLog::logAdd(7, $idinfo['id'], $jsp, $json, ['id'=>$createrid,'nickname'=>$creater]);
- $spc = [];
- $vp = Db::name('cat_spec')->where(['cat_id' => $idinfo['id'], 'is_del' => 0])->find();
- $str = '';
- if ($vp != false) {
- isset($vp['id']) && $vp['id'] !== '' ? $spc['id'] = $vp['id'] : '';
- $spc['cat_id'] = $idinfo['id'];
- $spc['exam_status'] = 0;
- $spc['status'] = 0;
- $spc['is_del'] = 0;
- $spc['updatetime'] = date('Y-m_d H:i:s');
- $str = $vp['id'];
- $vpn = Db::name('cat_spec')->save($spc);
- } else {
- $spc['cat_id'] = $idinfo['id'];
- $spc['exam_status'] = 0;
- $spc['status'] = 0;
- $spc['creater'] = $creater;
- $spc['createrid'] = $createrid;
- $spc['is_del'] = 0;
- $spc['addtime'] = date('Y-m-d H:i:d');
- $spc['updatetime'] = date('Y-m_d H:i:s');
- $vpn = Db::name('cat_spec')->insert($spc, true);
- $str = $vpn;
- }
- if ($vpn == false) {
- Db::rollback();
- return error_show(1002, '更新失败');
- }
- $db = Db::name('cat_specs')->where(['cat_id' => $idinfo['id'], 'is_del' => 0])->column('specs_id');
- if (empty($db)) {
- $db = [];
- }
- $sn = array_diff($specs_id, $db);
- $st = array_diff($db, $specs_id);
- if (!empty($st)) {
- $int['is_del'] = 1;
- $dn = Db::name('cat_specs')->where(['cat_id' => $idinfo['id'], 'specs_id' => $st, 'is_del' => 0])->save($int);
- if ($dn == false) {
- Db::rollback();
- return error_show(1002, '编辑失败');
- }
- }
- if (!empty($sn)) {
- $vat = [];
- foreach ($sn as $value) {
- $item = [];
- $item['cat_id'] = $idinfo['id'];
- $item['specs_id'] = $value;
- $item['is_del'] = 0;
- $item['addtime'] = date('Y-m-d H:i:s');
- $vat[] = $item;
- }
- $vp = Db::name('cat_specs')->insertAll($vat);
- if ($vp == 0) {
- Db::rollback();
- return error_show(1002, '编辑失败');
- }
- }
- $itn = sear($id);
- Db::commit();
- return error_show(0, '编辑成功');
- } else {
- Db::rollback();
- return error_show(1002, '编辑失败');
- }
- } catch (\Exception $e) {
- Db::rollback();
- return error_show(1005, $e->getMessage());
- }
- }
- //更新分类利率相关
- public function ratedit()
- {
- $id = isset($this->post['id']) && $this->post['id'] !== '' ? intval($this->post['id']) : '';
- $idinfo = Db::name('cat')->where(['id' => $id])->find();
- if ($idinfo == '') {
- return error_show(1002, '未找到商品数据');
- }
- $rate = isset($this->post['rate']) && $this->post['rate'] !== '' ? floatval($this->post['rate']) : '';
- $order_rate = isset($this->post['order_rate']) && $this->post['order_rate'] !== '' ? floatval($this->post['order_rate']) : '';
- $sale_rate = isset($this->post['sale_rate']) && $this->post['sale_rate'] !== '' ? floatval($this->post['sale_rate']) : '';
- $lower_rate = isset($this->post['lower_rate']) && $this->post['lower_rate'] !== '' ? floatval($this->post['lower_rate']) : '';
- $money_rate = isset($this->post['money_rate']) && $this->post['money_rate'] !== '' ? floatval($this->post['money_rate']) : '';
- $low_rate = isset($this->post['low_rate']) && $this->post['low_rate'] !== '' ? floatval($this->post['low_rate']) : '';
- if ($idinfo['pid'] == 0) {
- if ($rate === '') {
- return error_show(1003, '参数rate不能为空');
- }
- if ($order_rate === '') {
- return error_show(1003, '参数order_rate不能为空');
- }
- if ($sale_rate === '') {
- return error_show(1003, '参数sale_rate不能为空');
- }
- if ($lower_rate === '') {
- return error_show(1003, '参数lower_rate不能为空');
- }
- if ($money_rate == '') {
- return error_show(1003, '参数money_rate不能为空');
- }
- if ($low_rate == '') {
- return error_show(1003, '参数low_rate不能为空');
- }
- }
- $data = [
- 'id' => $id,
- 'rate' => $rate,
- 'lower_rate' => $lower_rate,
- 'order_rate' => $order_rate,
- 'sale_rate' => $sale_rate,
- 'money_rate' => $money_rate,
- 'low_rate' => $low_rate,
- 'is_del' => 0,
- 'updatetime' => date('Y-m-d H:i:s'),
- ];
- $strinfo = Db::name('cat')->where(['id' => $id])->save($data);
- if ($strinfo) {
- return error_show(0, '更新成功');
- } else {
- return error_show(1002, '更新失败');
- }
- }
- //详情
- public function info()
- {
- // $id = isset($this->post['id']) && $this->post['id'] !== '' ? intval($this->post['id']) : '';
- $id = $this->request->post('id/d', 0, 'trim');
- // if ($id == '') {
- // return error_show(1002, '参数id不能为空');
- // }
- // $idinfo = Db::name('cat')->where(['id' => $id])->find();
- // if ($idinfo == '') {
- // return error_show(1002, '未找到数据');
- // }
- $idinfo = Db::name('cat')
- ->where(['id' => $id])
- ->append(['spec'])
- ->withAttr('spec', function ($val, $da) {
- return Db::name('cat_specs')
- ->alias('a')
- ->leftJoin('specs b', 'b.id=a.specs_id')
- ->where(['a.cat_id' => $da['id'], 'a.is_del' => 0])
- ->field('a.specs_id id,b.spec_name')
- ->select()
- ->toArray();
- })
- ->findOrEmpty();
- // $info = Db::name('cat_specs')->where(['cat_id' => $idinfo['id'], 'is_del' => 0])->column('specs_id');
- // $temp = Db::name('specs')->where(['id' => $info, 'is_del' => 0])->field('id,spec_name')->select();
- //$idinfo['cat_id']=$info['cat_id'];
- // $idinfo['spec'] = $temp;
- // $idinfo['specs_id']=$info;
- return $idinfo ? json_show(0, '获取成功', $idinfo) : json_show(1002, '未找到数据');
- }
- //启禁用
- public function status()
- {
- $id = isset($this->post['id']) && $this->post['id'] !== '' ? intval($this->post['id']) : '';
- if ($id === '') {
- return error_show(1002, '参数id不能为空');
- }
- $stn = Db::name('cat')->where(['id' => $id])->find();
- if (empty($stn)) {
- return error_show(1002, '未找到商品数据');
- }
- $status = isset($this->post['status']) && $this->post['status'] !== '' ? intval($this->post['status']) : '';
- if ($status === '') {
- return error_show(1002, '参数status不能为空');
- }
- // if ($status == 1) {
- // if ($stn['rate'] === '') {
- // return error_show(1003, '单品修改下限未设置');
- // }
- // if ($stn['order_rate'] === '') {
- // return error_show(1003, '预算成本利率未设置');
- // }
- // if ($stn['sale_rate'] === '') {
- // return error_show(1003, '分类成本售价利率未设置');
- // }
- // if ($stn['lower_rate'] === '') {
- // return error_show(1003, '分类最低毛利率未设置');
- // }
- // if($stn['money_rate']==''){
- // return error_show(1003,'分类财务利率未设置');
- // }
- // if($stn['low_rate']==''){
- // return error_show(1003,'分类最低毛利未设置');
- // }
- // if($stn['fund_code']==''){
- // return error_show(1003,'分类核算码未设置');
- // }
- // }
- if ($stn['level'] == 3) {
- if ($status == 1) {
- $can = made($stn['id']);
- $cat = array_column($can, 'id');
- } else {
- $cat = $stn['id'];
- }
- // return error_show(1002, '所在级别不能启用');
- }
- if ($stn['level'] == 2 || $stn['level'] == 1) {
- if ($status == 1) {
- return error_show(1002, '所在等级不能启用');
- } else {
- $db = Db::name('cat')->where(['pid' => $stn['id'], 'status' => 1, 'is_del' => 0])->count();
- if ($db == 0) {
- $cat = $stn['id'];
- } else {
- return error_show(1002, '子级分类未禁用');
- }
- }
- }
- // $pd= $stn['status'];
- $it = [];
- $it['status'] = $status;
- $it['updatetime'] = date('Y-m-d H:i:s');
- $str = Db::name('cat')->where(['id' => $cat, 'is_del' => 0])->save($it);
- if ($str) {
- $catinfo = ['cat_id' => $id, 'status' => $status];
- $this->addPlat($catinfo);
- if (!empty($cat)) {
- $catinfo = ['cat_id' => $cat, 'status' => $status];
- $this->addPlat($catinfo);
- }
- return error_show(0, '状态更新成功');
- } else {
- return error_show(1002, '状态更新失败');
- }
- }
- //删除
- public function delete()
- {
- $id = isset($this->post['id']) && $this->post['id'] !== '' ? intval($this->post['id']) : '';
- $info = Db::name('cat')->where(['is_del' => 0, 'id' => $id])->find();
- if ($info == false) {
- return error_show(1002, '未找到数据');
- }
- $supp = Db::name('cat')->update(['id' => $id, 'is_del' => 1, 'updatetime' => date('Y-m-d H:i:s')]);
- if ($supp) {
- return error_show(0, '删除成功');
- } else {
- return error_show(1002, '删除失败');
- }
- }
- //平台添加分类
- private function addPlat($cat)
- {
- $plat = Db::name('platform')
- ->field('id')
- ->where(['platform_type' => 1, 'is_del' => 0])
- ->select()
- ->toArray();
- if (empty($plat)) return false;
- $date = date('Y-m-d H:i:s');
- $isT = Db::name('cat_plat')
- ->where(['cat_id' => $cat['cat_id'], 'is_del' => 0])
- ->column('id', 'platform_id');
- foreach ($plat as $value) {
- $cat['platform_id'] = $value['id'];
- $cat['addtime'] = $date;
- $cat['updatetime'] = $date;
- // $isT = Db::name('cat_plat')
- // ->where(['cat_id' => $cat['cat_id'], 'platform_id' => $cat['platform_id'], 'is_del' => 0])
- // ->column('cat_id');
- if (isset($isT[$cat['platform_id']])) {
- $data = $cat;
- unset($data['cat_id']);
- unset($data['addtime']);
- Db::name('cat_plat')
- ->where('id', $isT[$cat['platform_id']])
- ->update($data);
- } else Db::name('cat_plat')->save($cat);
- }
- return true;
- }
- }
|