修改商品管理

This commit is contained in:
邹景立 2021-03-11 18:47:21 +08:00
parent a2ccffddb6
commit d8619e7bad
4 changed files with 21 additions and 23 deletions

View File

@ -30,7 +30,7 @@ class ShopGoods extends Controller
*/
protected function initialize()
{
$this->cateLevel = GoodsService::instance()->getCateLevel();
$this->cateLevel = GoodsService::instance()->getCateMax();
}
/**
@ -80,7 +80,7 @@ class ShopGoods extends Controller
{
$this->marks = GoodsService::instance()->getMarkData();
$this->cates = GoodsService::instance()->getCateData();
GoodsService::instance()->buildItemData($data, false);
GoodsService::instance()->buildData($data, false);
}
/**
@ -218,7 +218,7 @@ class ShopGoods extends Controller
if ($this->request->isGet()) {
$list = $this->app->db->name('ShopGoods')->where($map)->select()->toArray();
if (empty($list)) $this->error('无效的商品数据,请稍候再试!');
[$this->vo] = GoodsService::instance()->buildItemData($list);
[$this->vo] = GoodsService::instance()->buildData($list);
$this->fetch();
} else {
[$data, $post, $batch] = [[], $this->request->post(), CodeExtend::uniqidDate(12, 'B')];

View File

@ -30,7 +30,7 @@ class ShopGoodsCate extends Controller
*/
protected function initialize()
{
$this->cateLevel = GoodsService::instance()->getCateLevel();
$this->cateLevel = GoodsService::instance()->getCateMax();
}
/**

View File

@ -45,11 +45,10 @@ class Goods extends Controller
'num_read' => $this->app->db->raw('num_read+1'),
]);
}
$map = ['deleted' => 0, 'status' => 1];
$query = $this->_query('ShopGoods')->like('name,mark')->equal('code,cate');
$result = $query->where($map)->order('sort desc,id desc')->page(true, false, false, 10);
GoodsService::instance()->buildItemData($result['list']);
$this->success('获取商品成功', $result);
$query = $this->_query('ShopGoods')->like('name,cateids,marks,payment')->equal('code');
$result = $query->where(['deleted' => 0, 'status' => 1])->order('sort desc,id desc')->page(true, false, false, 10);
if (count($result['list']) > 0) GoodsService::instance()->buildData($result['list']);
$this->success('获取商品数据', $result);
}
/**

View File

@ -90,7 +90,7 @@ class GoodsService extends Service
}
$cates[$id]['ids'] = array_reverse($cates[$id]['ids']);
$cates[$id]['names'] = array_reverse($cates[$id]['names']);
if ($simple && count($cates[$id]['names']) !== $this->getCateLevel()) {
if ($simple && count($cates[$id]['names']) !== $this->getCateMax()) {
unset($cates[$key]);
}
}
@ -108,6 +108,15 @@ class GoodsService extends Service
return $query->where($map)->order('sort desc,id desc')->column('name');
}
/**
* 最大分类级别
* @return integer
*/
public function getCateMax(): int
{
return 3;
}
/**
* 商品数据绑定
* @param array $data 商品主数据
@ -117,29 +126,19 @@ class GoodsService extends Service
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function buildItemData(array &$data = [], $simple = true): array
public function buildData(array &$data = [], $simple = true): array
{
$cates = $this->getCateData();
$codes = array_unique(array_column($data, 'code'));
[$cates, $codes] = [$this->getCateData(), array_unique(array_column($data, 'code'))];
$marks = $this->app->db->name('ShopGoodsMark')->where(['status' => 1])->column('name');
$items = $this->app->db->name('ShopGoodsItem')->whereIn('goods_code', $codes)->where(['status' => 1])->select()->toArray();
foreach ($data as &$vo) {
[$vo['marks'], $vo['cateids'], $vo['cateinfo']] = [str2arr($vo['marks'], ',', $marks), str2arr($vo['cateids']), []];
foreach ($cates as $cate) if (in_array($cate['id'], $vo['cateids'])) $vo['cateinfo'] = $cate;
[$vo['slider'], $vo['specs'], $vo['items']] = [str2arr($vo['slider'], '|'), json_decode($vo['data_specs'], true), []];
foreach ($cates as $cate) if (in_array($cate['id'], $vo['cateids'])) $vo['cateinfo'] = $cate;
foreach ($items as $item) if ($item['goods_code'] === $vo['code']) $vo['items'][] = $item;
if ($simple) unset($vo['marks'], $vo['sort'], $vo['status'], $vo['deleted'], $vo['data_items'], $vo['data_specs'], $vo['cateinfo']['parent']);
}
return $data;
}
/**
* 最大分类级别
* @return integer
*/
public function getCateLevel(): int
{
return 3;
}
}