| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- namespace app\admin\controller\mall;
- use app\admin\logic\ShopGoodsLogic;
- use app\admin\logic\ShopGoodsSpecLogic;
- use app\admin\logic\ShopGoodsSpecTypeLogic;
- use app\admin\logic\ShopGoodsTypeLogic;
- use app\admin\logic\ShopHotKeywordsLogic;
- use app\admin\logic\ShopSupplierLogic;
- use app\admin\model\dao\ShopGoodsSpecRelation;
- use app\admin\model\dao\ShopGoodsSpecType;
- use app\common\model\ShopGoods as ShopGoods;
- use app\common\model\ShopGoodsSpec;
- use app\admin\traits\Curd;
- use app\common\command\Tree;
- use app\common\controller\AdminController;
- use EasyAdmin\annotation\ControllerAnnotation;
- use EasyAdmin\annotation\NodeAnotation;
- use think\App;
- use app\common\model\ShopGoodsSpecType as Spec;
- use think\facade\Db;
- use think\Model;
- /**
- * Class Goods
- * @package app\admin\controller\mall
- * @ControllerAnnotation(title="商城商品管理")
- */
- class Goods extends AdminController
- {
- use Curd;
- protected $relationSearch = true;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->model = new ShopGoods();
- }
- /**
- * @NodeAnotation(title="列表")
- */
- public function index()
- {
- if ($this->request->isAjax()) {
- if (input('selectFields')) {
- return $this->selectList();
- }
- list($page, $limit, $where) = $this->buildTableParames();
- list($count, $list) = ShopGoodsLogic::getList($page, $limit, $where);
- $data = [
- 'code' => 0,
- 'msg' => '',
- 'count' => $count,
- 'data' => $list,
- ];
- return json($data);
- }
- return $this->fetch();
- }
- public function goodsBase($id)
- {
- list($data, $hot_keywords, $shop_goods_type, $shop_supplier, $specAttrId) = ShopGoodsLogic::getGoodsBase($id);
- // var_dump($data);
- $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 = ShopGoodsSpecRelation::getArrListByGoodsId($goodsId);
- $specAttrId = ShopGoodsSpecType::getPidById($specRelation[0]['spec_id']);
- $specType = ShopGoodsSpecTypeLogic::getCacheList();
- $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 = ShopGoodsSpecLogic::getGoodsSpecByGoodId($goodsId);
- 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'])) {
- ShopGoods::singleSpecGoods($goods);
- } else {
- ShopGoods::manySpecGoods($goods);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('成功');
- }
- $hot_keywords = ShopHotKeywordsLogic::getCacheList();
- $shop_supplier = ShopSupplierLogic::getCacheList();
- $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'])) {
- 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();
- }
- /**
- * @NodeAnotation(title="入库")
- */
- public function stock($id)
- {
- $sku = ShopGoodsSpecLogic::getGoodsSpecByGoodId($id);
- $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();
- }
- }
|