model = new ShopGoods(); } /** * @NodeAnotation(title="列表") */ public function index () { if ($this->request->isAjax()) { if (input('selectFields')) { return $this->selectList(); } list($page, $limit, $where) = $this->buildTableParames(); // foreach ($where as $key=>$val){ // if ($val[0] == 'status'){ // $where[$key][0] = 'o.status'; // } // } // return $where; $count = $this->model ->withJoin(['supplier','cate'], 'LEFT') ->where($where) ->count(); $list = $this->model ->withJoin(['supplier','cate'], 'LEFT') ->where($where) ->page($page, $limit) ->order('goods_id desc') ->select()->toArray(); foreach ($list as $k => &$v) { $v['id'] = $v['goods_id']; // $v['supplier'] = Db::name('shop_supplier')->where(['id'=>$v['supplier']])->value('name'); $v['goods_type'] = Db::name('shop_goods_type')->where(['id'=>$v['goods_type']])->value('name'); } $data = [ 'code' => 0, 'msg' => '', 'count' => $count, 'data' => $list, ]; return json($data); } return $this->fetch(); } public function goodsBase ($id) { $data = $this->model->where(['goods_id' => $id])->findOrEmpty()->toArray(); if ($data['goods_img_banner']) { $data['goods_img_banner'] = implode('|', json_decode($data['goods_img_banner'], true)); } $data['goods_remark'] = htmlspecialchars_decode($data['goods_remark']); $specRelation = Db::name('shop_goods_spec_relation')->where(['goods_id' => $id])->select()->toArray(); $specAttrId = Db::name('shop_goods_spec_type')->where(['id' => $specRelation[0]['spec_id']])->value('pid'); if ($data['is_exist_many_spec'] == 0) { $data['defaultSku'] = Db::name('shop_goods_spec')->where(['goods_id' => $id])->find(); } $hot_keywords = Db::name('shop_hot_keywords')->select()->toArray(); $shop_supplier = Db::name('shop_supplier')->select()->toArray(); $shop_goods_type = Db::name('shop_goods_type')->field('id,name')->select()->toArray(); $this->assign('shop_goods_type', $shop_goods_type); $this->assign('box_type', system_box_typesetting()); $this->assign('hot_keywords', $hot_keywords); $this->assign('shop_supplier', $shop_supplier); $this->assign('specAttrId', $specAttrId); $this->assign('data', $data); } /** * @param $goodsId * @param $attriId * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * 获取商品规格,商品sku */ public function getGoodsSpecData ($goodsId, $attriId) { if ($this->request->isAjax()) { $specRelation = Db::name('shop_goods_spec_relation')->where(['goods_id' => $goodsId])->select()->toArray(); $specAttrId = Db::name('shop_goods_spec_type')->where(['id' => $specRelation[0]['spec_id']])->value('pid'); $specType = Db::name('shop_goods_spec_type')->select()->toArray(); $specType = app()->make(Tree::class)->DeepTree($specType, 'child'); $specTypeArr = []; foreach ($specType as $k => $v) { if ($v['id'] == $specAttrId) { if (isset($v['child'])) $specTypeArr = $v['child']; } } $specRelationArr = array_reduce($specRelation, function ($result, $value) { return array_merge($result, array_values(json_decode($value['spec_value'], true))); }, array()); $specRelationArr = array_column($specRelationArr, 'id'); foreach ($specTypeArr as $k => &$v) { if(isset($v['child'])) { foreach ($v['child'] as $ck => &$cv) { if (in_array($cv['id'], $specRelationArr)) { $cv['checked'] = true; } else { $cv['checked'] = false; } } } } $goodsSkuArr = []; $goodsSku = Db::name('shop_goods_spec')->where(['goods_id' => $goodsId])->select()->toArray(); foreach ($goodsSku as $sk => $sv) { $goodsSkuArr['skus[' . $sv['spec_ids'] . '][picture]'] = $sv['picture']; $goodsSkuArr['skus[' . $sv['spec_ids'] . '][price]'] = $sv['price']; $goodsSkuArr['skus[' . $sv['spec_ids'] . '][cost_price]'] = $sv['cost_price']; $goodsSkuArr['skus[' . $sv['spec_ids'] . '][weight]'] = $sv['weight']; $goodsSkuArr['skus[' . $sv['spec_ids'] . '][stock]'] = $sv['stock']; $goodsSkuArr['skus[' . $sv['spec_ids'] . '][rebate_score]'] = $sv['rebate_score']; } $data = [ 'code' => 0, 'msg' => '', 'spec' => $specTypeArr, 'sku' => $goodsSkuArr ]; return json($data); } } /** * @NodeAnotation(title="商品添加") */ public function add() { if ($this->request->isAjax()) { $goods = $this->request->post(); Db::startTrans(); try { if (!isset($goods['is_exist_many_spec'])) { $flag = ShopGoods::singleSpecGoods($goods); } else { $flag = ShopGoods::manySpecGoods($goods); } Db::commit(); }catch (\Exception $e){ Db::rollback(); $this->error($e->getMessage()); } $this->success('成功'); // return $flag ? json(['code' => 1, 'msg' => '发布成功']) : // json(['code' => 0, 'msg' => '发布失败']); } $hot_keywords = Db::name('shop_hot_keywords')->select()->toArray(); $shop_supplier = Db::name('shop_supplier')->select()->toArray(); $this->assign('hot_keywords', $hot_keywords); $this->assign('shop_supplier', $shop_supplier); return $this->fetch(); } /** * @NodeAnotation(title="商品编辑") */ public function edit ($id) { if ($this->request->isAjax()) { $goods = $this->request->post(); Db::startTrans(); try { if (!isset($goods['is_exist_many_spec'])) { ShopGoods::singleSpecGoods($goods); } else { ShopGoods::manySpecGoods($goods); } Db::commit(); }catch (\Exception $e){ Db::rollback(); $this->error($e->getMessage()); } $this->success('保存成功'); } $this->goodsBase($id); return $this->fetch(); } public function copy ($id) { if ($this->request->isAjax()) { $goods = $this->request->post(); Db::startTrans(); try { if (!isset($goods['is_exist_many_spec'])) { $flag = ShopGoods::singleSpecGoods($goods); } else { $flag = ShopGoods::manySpecGoods($goods); } Db::commit(); }catch (\Exception $e){ Db::rollback(); $this->error($e->getMessage()); } $this->success('保存成功'); // return $flag ? json(['code' => 1, 'msg' => '保存成功']) : // json(['code' => 0, 'msg' => '保存失败']); } $this->goodsBase($id); return $this->fetch(); } /** * @NodeAnotation(title="入库") */ public function stock ($id) { $sku = Db::name('shop_goods_spec')->where(['goods_id' => $id])->select()->toArray(); $this->assign('sku', $sku); if ($this->request->isAjax()) { $data = $this->request->post(); if (empty($data['goods_id'])) { return json(['code' => 0, 'msg' => '参数错误']); } unset($data['goods_id']); $stock = []; foreach ($data as $k => $v) { $stock[] = [ 'goods_spec_id' => explode('_', $k)[1], 'stock' => $v ]; } $ShopGoodsSpec = new ShopGoodsSpec(); $flag = $ShopGoodsSpec->saveAll($stock); return $flag ? json(['code' => 1, 'msg' => '保存成功']) : json(['code' => 0, 'msg' => '保存失败']); } return $this->fetch(); } }