* @date 2020/6/19 17:08 * * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function createSpec() { // 接收数据 $params = $this->request->param(); // 数据校验 $valid = $this->validate($params,[ 'name|规格名称' => 'require', ]); (true !== $valid) && IResponse::failure($valid); // 1. 存在性 $model = model('common/GoodsSpec'); if ($spec = $model->where(['spec_name' => $params['name']])->find()) { //直接返回id return IResponse::success($spec); } // 不存在则新增 return IResponse::success($model->create([ 'spec_name' => $params['name'] ],true)); } /** * 新增规格属性 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/6/19 17:10 * * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function createSpecValue() { // 接收数据 $params = $this->request->param(); // 数据校验 $valid = $this->validate($params,[ 'spec_id|规格ID' => 'require', 'name|规格属性名称' => 'require', ]); (true !== $valid) && IResponse::failure($valid); // 1. 存在性 $model = model('common/GoodsSpecValue'); if ($value = $model->where(['spec_id' => $params['spec_id'],'value_name' => $params['name']])->find()) { //直接返回id return IResponse::success($value); } // 不存在则新增 return IResponse::success($model->create([ 'spec_id' => $params['spec_id'], 'value_name' => $params['name'] ],true)); } /** * 取得商品规格 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/6/19 17:00 * * @param $id * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getSkuById($id) { // 获取规格 $skus = model('common/GoodsSku') ->where(['goods_id' => $id]) ->select(); $spec = []; $values = []; $ret = []; // 取出这个产品的SPEC SPEC_VALUE foreach ($skus as $sku) { $ret['sku'][] = [ 'ids' => dejson($sku['spec_param']), 'price' => $sku['price'], 'instock_price' => $sku['instock_price'], 'stock' => $sku['stock'], ]; foreach (dejson($sku['spec_param']) as $sp) { foreach ($sp as $sid => $vid) { $spec[] = model('GoodsSpec')->where(['id' => $sid])->find(); $values[] = model('GoodsSpecValue')->where(['spec_id' => $sid, 'id' => $vid])->find(); } } } // 构造 foreach (array_unique($spec) as $s => $k) { $sub = []; foreach (array_unique($values) as $i => $v) { if ($v['spec_id'] == $k['id']) { $sub[] = [ 'id' => $v['id'], 'name' => $v['value_name'] ]; } } $ret['spec'][] = [ 'id' => $k['id'], 'name' => $k['spec_name'], 'sub' => $sub ]; } return IResponse::success($ret); } /** * 获取商品信息 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/10 12:15 * * @return mixed * @throws \Lettered\Support\Exceptions\FailedException * @throws \think\exception\DbException */ public function index() { $where = []; //组合搜索 !empty(input('name')) && $where[] = ['name', 'like', '%' . input('name') . '%']; !empty(input('seller_id')) && $where[] = ['seller_id', 'eq', input('seller_id')]; !empty(input('category_id')) && $where[] = ['category_id', 'eq', input('category_id')]; !empty(input('area')) && $where[] = ['area_id', 'eq', input('area')]; $product = model('common/Goods'); return IResponse::paginate($product->where($where) ->where(['seller_id' => $this->seller->id]) ->with(['category'])->order(['created_at' => 'desc']) ->paginate(input('limit'),false)); } /** * 新增产品 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/10 12:32 * * @return mixed */ public function save() { // 接收数据 $params = $this->request->param(); // 归属商家 $params['seller_id'] = $this->seller->id; // 数据校验 $valid = $this->validate($params,[ 'seller_id|归属商家' => 'require', 'cover_img|产品主图' => 'require', 'name|产品名称' => 'require|unique:Goods', 'sku|产品规格' => 'require', 'content|产品详情' => 'require', 'sku| 商品规格参数' => 'require' ],[ 'email.unique' => '重复产品名称!' ]); (true !== $valid) && IResponse::failure($valid); // 保存数据 $goodsId = model('common/Goods')->storeBy($params); Db::startTrans(); try { // sku处理 $insku = dejson($params['sku']); //库存 $stock = 0; foreach ($insku as &$sku){ $sku['goods_id'] = $goodsId; $sku['spec_param'] = enjson($sku['ids']); $sku['param'] = enjson($sku['param']); $stock += $sku['stock']; model('common/GoodsSku')::create($sku, true); } // 进货价 $iprice = array_column($insku, 'instock_price'); sort($iprice); // 价格 $price = array_column($insku, 'price'); sort($price); // 更新信息 model('common/Goods')->updateBy($goodsId, [ 'stock' => $stock, 'instock_price' => $iprice[0], 'price' => $price[0] ]); Db::commit(); }catch (\Exception $e){ Log::debug($e->getMessage()); Db::rollback(); } return $goodsId ? IResponse::success([],'新增产品成功'): IResponse::failure('新增产品异常'); } /** * 更新产品信息 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/10 12:32 * * @param $id * @return mixed * @throws \think\Exception * @throws \think\exception\PDOException */ public function update($id) { // 接收数据 $params = $this->request->param(); // 归属商家 $params['seller_id'] = $this->seller->id; // 查询 $goods = model('common/Goods')->findBy($id); // 是否更改状态操作 if (isset($params['status']) && $params['status'] != '') { $valid = $this->validate($params, [ 'status|配置状态' => 'require|integer' ]); }else { // 数据校验 $valid = $this->validate($params, [ 'seller_id|归属商家' => 'require', 'cover_img|产品主图' => 'require', 'name|产品名称' => 'require|unique:Goods', 'sku|产品规格' => 'require', 'content|产品详情' => 'require', 'sku| 商品规格参数' => 'require' ]); } // 错误返回 (true !== $valid) && IResponse::failure($valid); // 删除旧数据 //TODO 模型方法有bug,等待优化 $this->app->db()->name('goods_sku')->where(['goods_id' => $goods['id']])->delete(); // sku处理 $insku = dejson($params['sku']); $stock = 0; foreach ($insku as &$sku){ $sku['goods_id'] = $goods['id']; $sku['spec_param'] = enjson($sku['ids']); $sku['param'] = enjson($sku['param']); $stock += $sku['stock']; model('common/GoodsSku')::create($sku,true); } // 库存 $params['stock'] = $stock; // 进货价 $iprice = array_column($insku, 'instock_price'); sort($iprice); $params['instock_price'] = $iprice[0]; // 价格 $price = array_column($insku, 'price'); sort($price); $params['price'] = $price[0]; // 更新信息 $goods->updateBy($id, $params); return IResponse::success('更新产品信息成功'); } /** * 删除产品 * * @param $id * @return \think\response\Json */ public function delete($id) { model('common/Goods')->deleteBy($id); return IResponse::success([],'删除产品成功'); } /************ 分类 **********************/ public function categories() { $where = []; //组合搜索 $user = model('common/GoodsCategory'); return IResponse::paginate($user->where($where) ->paginate(input('limit'),false)); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/16 18:02 * * @return mixed */ public function plectron(){ // 收参数 $params = $this->request->param(); foreach (str2arr($params['ids']) as $id){ $user = model('common/GoodsOrder')->getBy($id); if ($this->request->isDelete()){ $user->deleteBy($id); } $user->allowField(true)->updateBy($id, $params); } return IResponse::success([],'操作成功'); } /** * 订单操作 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/16 18:02 * * @param $id * @return mixed */ public function actionOrder($id) { $param = $this->request->param(); $valid = $this->validate($param, [ 'action|操作状态' => 'require' ]); // 错误返回 (true !== $valid) && IResponse::failure($valid); $model = model('common/GoodsOrder'); switch ($param['action']){ case "ship": $msg = "发货"; // 状态以及时间 $model->updateBy($id,[ 'shipping_at' => time(), 'status' => 3 ]); break; case "confirm": $msg = "确认"; // 系统帮助用户自主确认订单 $model->updateBy($id,[ 'received_at' => time(), 'status' => 4 ]); break; case "delete": $msg = "删除"; $model->deleteBy($id); break; } return IResponse::success([],$msg . '成功'); } }