// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\store\controller; use app\common\exception\BaseException; use app\common\service\Export as ExportService; use app\store\model\Goods as GoodsModel; use app\store\model\GoodsSku; use app\store\model\GoodsSku as GoodsSkuModel; use app\store\model\member\MemberGoods; use app\store\model\user\Withdraw as WithdrawModel; use app\common\model\ActivityDiscount as ActivityDiscount; use app\common\enum\goods\SpecType as SpecTypeEnum; use app\store\model\groupbuylb\GroupBuyLbActivity; /** * 商品管理控制器 * Class Goods * @package app\store\controller */ class Goods extends Controller { /** * 商品列表 * @return array * @throws \think\db\exception\DbException */ public function list() { // 获取列表记录 $model = new GoodsModel; $params = $this->request->param(); $params['sortType'] = 'create_time'; $list = $model->getList($params); return $this->renderSuccess(compact('list')); } //单规格商品列表 public function listspec(){ //获取列表记录 $model = new GoodsModel; $params = $this->request->param(); $params['sortType'] = 'create_time'; $params['spec_type'] = SpecTypeEnum::SINGLE; $list = $model->getList($params); return $this->renderSuccess(compact('list')); } /** * 商品列表 * @return array * @throws \think\db\exception\DbException */ public function listdiscount() { $discount = new ActivityDiscount; $params = $this->request->param(); $activity_id = $params['activity_id']??0; $good_id_array = $discount->getActivityGoodIdArray($activity_id); // 获取列表记录 $model = new GoodsModel; $params['sortType'] = 'create_time'; $params['not_goods_id_arr'] = $good_id_array; $list = $model->getList($params); return $this->renderSuccess(compact('list')); } //裂变商品搜索 public function skuPickerGroupBuyList(){ $model = new GoodsSkuModel; $activity = new GroupBuyLbActivity; $params = $this->request->param(); $activity_id = $params['activity_id']??0; $good_id_array = $activity->getActivityGoodIdArray($activity_id); $params['not_goods_id_arr'] = $good_id_array; $list = $model->getPickerList($params); return $this->renderSuccess(compact('list')); } /** * 按商品SKU返回列表 * @return array * @throws \think\db\exception\DbException */ public function skuPickerList() { // 获取列表记录 $model = new GoodsSkuModel; $params = $this->request->param(); $list = $model->getPickerList($params); return $this->renderSuccess(compact('list')); } /** * 根据商品ID集获取列表记录 * @param array $goodsIds * @return array */ public function listByIds(array $goodsIds) { // 获取列表记录 $model = new GoodsModel; $list = $model->getListByIds($goodsIds); return $this->renderSuccess(compact('list')); } /** * 商品详情 * @param int $goodsId * @return array * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function detail(int $goodsId) { // 获取商品详情 $model = new GoodsModel; $goodsInfo = $model->getDetail($goodsId); return $this->renderSuccess(compact('goodsInfo')); } /** * 添加商品 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function add() { $model = new GoodsModel; if ($model->add($this->postForm())) { return $this->renderSuccess('添加成功'); } return $this->renderError($model->getError() ?: '添加失败'); } /** * 编辑商品 * @param int $goodsId * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function edit(int $goodsId) { // 商品详情 $model = GoodsModel::detail($goodsId); // 更新记录 if ($model->edit($this->postForm())) { return $this->renderSuccess('更新成功'); } return $this->renderError($model->getError() ?: '更新失败'); } /** * 修改商品状态(上下架) * @param array $goodsIds 商品id集 * @param bool $state 为true表示上架 * @return array */ public function state(array $goodsIds, bool $state) { $model = new GoodsModel; if (!$model->setStatus($goodsIds, $state)) { return $this->renderError($model->getError() ?: '操作失败'); } return $this->renderSuccess('操作成功'); } /** * 删除商品 * @param array $goodsIds * @return array */ public function delete(array $goodsIds) { $model = new GoodsModel; if (!$model->setDelete($goodsIds)) { return $this->renderError($model->getError() ?: '删除失败'); } return $this->renderSuccess('删除成功'); } /** * 根据商品id获取SKU列表 * @param int $goodsId * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function skulist(int $good_id) { // 商品SKU列表 $skulist = GoodsSkuModel::where("goods_id",$good_id)->select(); $list = []; foreach($skulist as $row){ $sku_name = $row->goods_props[0]['value']['name']??''; $goods_sku_id = $row->goods_sku_id; if(empty($sku_name)||empty($goods_sku_id)){ continue; } $new['good_sku_id'] = $goods_sku_id; $new['name'] = $sku_name; $list[] = $new; } return $this->renderSuccess(compact('list')); } //成本管理 public function manageCosts(){ $model = new GoodsModel; $list = $model->getCostsList($this->request->param()); return $this->renderSuccess(compact('list')); } public function editClearingPrices(array $skues){ //dd($skues); GoodsSku::updateClearingPrice($skues); return $this->renderSuccess('成功'); } public function editPlatformRate(array $rates){ //dd($skues); GoodsSku::updatePlatformRate($rates); return $this->renderSuccess('成功'); } /** * 导出成本管理列表功能 * @return array * @author: zjwhust * @Time: 2021/10/15 13:43 */ public function orderExport(){ $param = $this->request->param(); /* if(isset($param['goods_id']) && empty($param['goods_id'])){ return $this->renderError('请勾选订单商品后再导出'); }*/ $model = new GoodsModel(); $data = $model->listExport($param); $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls',true); return $this->renderSuccess($res,'导出成功'); } }