init
- 框架初始化 - 安装插件 - 修复PHP8.4报错
This commit is contained in:
247
application/admin/controller/shopro/goods/Comment.php
Normal file
247
application/admin/controller/shopro/goods/Comment.php
Normal file
@@ -0,0 +1,247 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\goods;
|
||||
|
||||
use think\Db;
|
||||
use app\admin\controller\shopro\Common;
|
||||
use app\admin\model\shopro\goods\Comment as CommentModel;
|
||||
use app\admin\model\shopro\data\FakeUser as FakeUserModel;
|
||||
|
||||
class Comment extends Common
|
||||
{
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new CommentModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品评价列表
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$comments = $this->model->sheepFilter()->with(['goods' => function ($query) {
|
||||
$query->field('id,image,title');
|
||||
}, 'order' => function ($query) {
|
||||
$query->removeOption('soft_delete');
|
||||
}, 'order_item'])->paginate($this->request->param('list_rows', 10));
|
||||
|
||||
$morphs = [
|
||||
'user' => \app\admin\model\shopro\user\User::class,
|
||||
'fake_user' => \app\admin\model\shopro\data\FakeUser::class
|
||||
];
|
||||
$comments = morph_to($comments, $morphs, ['user_type', 'user_id']);
|
||||
|
||||
$this->success('获取成功', null, $comments);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function detail($id)
|
||||
{
|
||||
$comment = $this->model->with(['admin', 'goods' => function ($query) {
|
||||
$query->field('id,image,title,price');
|
||||
}, 'order' => function ($query) {
|
||||
$query->removeOption('soft_delete');
|
||||
}, 'order_item'])->where('id', $id)->find();
|
||||
|
||||
if (!$comment) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
|
||||
$morphs = [
|
||||
'user' => \app\admin\model\shopro\user\User::class,
|
||||
'fake_user' => \app\admin\model\shopro\data\FakeUser::class
|
||||
];
|
||||
$comments = morph_to([$comment], $morphs, ['user_type', 'user_id']);
|
||||
|
||||
$this->success('获取成功', null, $comments->all()[0]);
|
||||
}
|
||||
|
||||
|
||||
public function add()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$params = $this->request->only([
|
||||
'goods_id', 'user_id', 'level', 'content', 'images', 'status'
|
||||
]);
|
||||
$params['user_type'] = 'fake_user';
|
||||
$this->svalidate($params, ".add");
|
||||
$fakeUser = FakeUserModel::find($params['user_id']);
|
||||
$params['user_nickname'] = $fakeUser ? $fakeUser->nickname : null;
|
||||
$params['user_avatar'] = $fakeUser ? $fakeUser->avatar : null;
|
||||
|
||||
Db::transaction(function () use ($params) {
|
||||
$this->model->save($params);
|
||||
});
|
||||
$this->success('保存成功');
|
||||
}
|
||||
|
||||
|
||||
public function edit($id = null)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch('add');
|
||||
}
|
||||
|
||||
$params = $this->request->only([
|
||||
'status'
|
||||
]);
|
||||
|
||||
$id = explode(',', $id);
|
||||
$list = $this->model->whereIn('id', $id)->select();
|
||||
$result = Db::transaction(function () use ($list, $params) {
|
||||
$count = 0;
|
||||
foreach ($list as $comment) {
|
||||
$comment->status = $params['status'] ?? 'hidden';
|
||||
$count += $comment->save();
|
||||
}
|
||||
|
||||
return $count;
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('更新成功', null, $result);
|
||||
} else {
|
||||
$this->error('更新失败,未改变任何记录');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function reply($id)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$params = $this->request->only([
|
||||
'content'
|
||||
]);
|
||||
$this->svalidate($params, '.reply');
|
||||
|
||||
$comment = $this->model->noReply()->find($id);
|
||||
if (!$comment) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
|
||||
$comment->reply_content = $params['content'];
|
||||
$comment->reply_time = time();
|
||||
$comment->admin_id = $this->auth->id;
|
||||
$comment->save();
|
||||
|
||||
$this->success('回复成功');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除商品评价
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if (empty($id)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'id'));
|
||||
}
|
||||
|
||||
$list = $this->model->where('id', 'in', $id)->select();
|
||||
Db::transaction(function () use ($list) {
|
||||
$count = 0;
|
||||
foreach ($list as $item) {
|
||||
$count += $item->delete();
|
||||
}
|
||||
|
||||
return $count;
|
||||
});
|
||||
|
||||
$this->success('删除成功');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 评价回收站
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function recyclebin()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$comments = $this->model->onlyTrashed()->sheepFilter()->paginate($this->request->param('list_rows', 10));
|
||||
|
||||
$this->success('获取成功', null, $comments);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 还原(支持批量)
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function restore($id = null)
|
||||
{
|
||||
if (empty($id)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'id'));
|
||||
}
|
||||
$items = $this->model->onlyTrashed()->where('id', 'in', $id)->select();
|
||||
Db::transaction(function () use ($items) {
|
||||
$count = 0;
|
||||
foreach ($items as $item) {
|
||||
$count += $item->restore();
|
||||
}
|
||||
|
||||
return $count;
|
||||
});
|
||||
|
||||
$this->success('还原成功');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 销毁(支持批量)
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function destroy($id = null)
|
||||
{
|
||||
if (empty($id)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'id'));
|
||||
}
|
||||
if ($id !== 'all') {
|
||||
$items = $this->model->onlyTrashed()->whereIn('id', $id)->select();
|
||||
} else {
|
||||
$items = $this->model->onlyTrashed()->select();
|
||||
}
|
||||
$result = Db::transaction(function () use ($items) {
|
||||
$count = 0;
|
||||
foreach ($items as $comment) {
|
||||
// 删除评价
|
||||
$count += $comment->delete(true);
|
||||
}
|
||||
return $count;
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('销毁成功', null, $result);
|
||||
}
|
||||
$this->error('销毁失败');
|
||||
}
|
||||
|
||||
}
|
||||
444
application/admin/controller/shopro/goods/Goods.php
Normal file
444
application/admin/controller/shopro/goods/Goods.php
Normal file
@@ -0,0 +1,444 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\goods;
|
||||
|
||||
use app\admin\controller\shopro\Common;
|
||||
use app\admin\model\shopro\goods\Goods as GoodsModel;
|
||||
use app\admin\model\shopro\goods\Sku as SkuModel;
|
||||
use app\admin\model\shopro\goods\SkuPrice as SkuPriceModel;
|
||||
use app\admin\model\shopro\goods\StockWarning as StockWarningModel;
|
||||
use app\admin\model\shopro\activity\Activity as ActivityModel;
|
||||
use app\admin\controller\shopro\traits\SkuPrice as SkuPriceTrait;
|
||||
use addons\shopro\traits\StockWarning as StockWarningTrait;
|
||||
use addons\shopro\service\goods\GoodsService;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 商品管理
|
||||
*/
|
||||
class Goods extends Common
|
||||
{
|
||||
|
||||
use SkuPriceTrait, StockWarningTrait;
|
||||
|
||||
protected $noNeedRight = ['getType', 'select', 'activitySelect'];
|
||||
|
||||
/**
|
||||
* 商品模型对象
|
||||
* @var \app\admin\model\shopro\goods\Goods
|
||||
*/
|
||||
protected $model = null;
|
||||
protected $activityModel = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new GoodsModel;
|
||||
$this->activityModel = new ActivityModel;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*
|
||||
* @return string|Json
|
||||
* @throws \think\Exception
|
||||
* @throws DbException
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$goodsTableName = $this->model->getQuery()->getTable();
|
||||
|
||||
$goods = $this->model->sheepFilter()->with(['max_sku_price']);
|
||||
|
||||
// 聚合库存 (包含下架的规格)
|
||||
$skuSql = SkuPriceModel::field('sum(stock) as stock, goods_id as sku_goods_id')->group('goods_id')->buildSql();
|
||||
$goods = $goods->join([$skuSql => 'sp'], $goodsTableName . '.id = sp.sku_goods_id', 'left')
|
||||
->field("$goodsTableName.*, sp.stock") // ,score.*
|
||||
->paginate($this->request->param('list_rows', 10))->each(function ($goods) {
|
||||
// 获取活动信息
|
||||
$goods->activities = $goods->activities;
|
||||
$goods->promos = $goods->promos;
|
||||
|
||||
$data_type = request()->param('data_type', ''); // 特殊 type 需要处理的数据
|
||||
if ($data_type == 'score_shop') {
|
||||
$goods->is_score_shop = $goods->is_score_shop;
|
||||
}
|
||||
});
|
||||
|
||||
$this->success('获取成功', null, $goods);
|
||||
}
|
||||
|
||||
|
||||
// 获取数据类型
|
||||
public function getType()
|
||||
{
|
||||
$activityTypes = $this->activityModel->typeList();
|
||||
$statusList = $this->model->statusList();
|
||||
|
||||
$result = [
|
||||
'activity_type' => $activityTypes,
|
||||
'status' => $statusList
|
||||
];
|
||||
|
||||
$data = [];
|
||||
foreach ($result as $key => $list) {
|
||||
$data[$key][] = ['name' => '全部', 'type' => 'all'];
|
||||
|
||||
foreach ($list as $k => $v) {
|
||||
$data[$key][] = [
|
||||
'name' => $v,
|
||||
'type' => $k
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$this->success('获取成功', null, $data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$params = $this->request->only([
|
||||
'type', 'title', 'subtitle', 'category_ids', 'image', 'images', 'image_wh', 'params',
|
||||
'original_price', 'price', 'is_sku', 'limit_type', 'limit_num', 'sales_show_type',
|
||||
'stock_show_type', 'show_sales', 'service_ids', 'dispatch_type', 'dispatch_id', 'is_offline', 'status', 'weigh',
|
||||
]); // likes, views, sales,
|
||||
$params['content'] = $this->request->param('content', '', null); // content 不经过全局过滤
|
||||
$this->svalidate($params, ".add");
|
||||
if (!$params['is_sku']) {
|
||||
// 校验单规格属性
|
||||
$sku_params = $this->request->only(['stock', 'stock_warning', 'sn', 'weight', 'cost_price', 'original_price', 'price']);
|
||||
$this->svalidate($sku_params, '.sku_params');
|
||||
}
|
||||
|
||||
$data = Db::transaction(function () use ($params) {
|
||||
$this->model->save($params);
|
||||
|
||||
$this->editSku($this->model, 'add');
|
||||
});
|
||||
$this->success('保存成功', null, $data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 详情
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function detail($id)
|
||||
{
|
||||
$goods = $this->model->where('id', $id)->find();
|
||||
if (!$goods) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$goods->category_ids_arr = $goods->category_ids_arr;
|
||||
|
||||
if ($goods->is_sku) {
|
||||
$goods->skus = $goods->skus;
|
||||
$goods->sku_prices = $goods->sku_prices;
|
||||
} else {
|
||||
// 将单规格的部分数据直接放到 row 上
|
||||
$goodsSkuPrice = SkuPriceModel::where('goods_id', $id)->order('id', 'asc')->find();
|
||||
|
||||
$goods->stock = $goodsSkuPrice->stock;
|
||||
$goods->sn = $goodsSkuPrice->sn;
|
||||
$goods->weight = $goodsSkuPrice->weight;
|
||||
$goods->stock_warning = $goodsSkuPrice->stock_warning;
|
||||
$goods->cost_price = $goodsSkuPrice->cost_price;
|
||||
}
|
||||
|
||||
$content = $goods['content'];
|
||||
$goods = $goods->toArray();
|
||||
$goods['content'] = $content;
|
||||
$this->success('保存成功', null, $goods);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 编辑(支持批量)
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch('add');
|
||||
}
|
||||
|
||||
$params = $this->request->only([
|
||||
'type', 'title', 'subtitle', 'image', 'images', 'image_wh', 'params',
|
||||
'original_price', 'price', 'is_sku', 'limit_type', 'limit_num', 'sales_show_type',
|
||||
'stock_show_type', 'show_sales', 'service_ids', 'dispatch_type', 'dispatch_id', 'is_offline', 'status', 'weigh',
|
||||
]); // likes, views, sales,
|
||||
$this->request->has('content') && $params['content'] = $this->request->param('content', '', null); // content 不经过全局过滤
|
||||
$this->svalidate($params);
|
||||
isset($params['is_sku']) && $params['category_ids'] = $this->request->param('category_ids', ''); // 分类不判空
|
||||
isset($params['is_sku']) && $params['params'] = $this->request->param('params/a', []); // 编辑如果没有传 params 赋值为空
|
||||
if (isset($params['is_sku']) && !$params['is_sku']) {
|
||||
// 校验单规格属性
|
||||
$sku_params = $this->request->only(['stock_warning', 'sn', 'weight', 'cost_price', 'original_price', 'price']);
|
||||
$this->svalidate($sku_params, 'sku_params');
|
||||
}
|
||||
|
||||
$id = explode(',', $id);
|
||||
|
||||
$items = $this->model->whereIn('id', $id)->select();
|
||||
Db::transaction(function () use ($items, $params) {
|
||||
foreach ($items as $goods) {
|
||||
$goods->save($params);
|
||||
|
||||
if (isset($params['is_sku'])) {
|
||||
// 编辑商品(如果没有 is_sku 就是批量编辑上下架等)
|
||||
$this->editSku($goods, 'edit');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$this->success('更新成功', null);
|
||||
}
|
||||
|
||||
|
||||
public function addStock($id)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$goods = $this->model->where('id', $id)->find();
|
||||
if (!$goods) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
if ($goods->is_sku) {
|
||||
// 多规格
|
||||
$skuPrices = $this->request->post('sku_prices/a', []);
|
||||
foreach ($skuPrices as $skuPrice) {
|
||||
if (isset($skuPrice['add_stock']) && $skuPrice['add_stock'] != 0 && $skuPrice['id']) {
|
||||
$skuPriceModel = SkuPriceModel::where('goods_id', $id)->order('id', 'asc')->find($skuPrice['id']);
|
||||
if ($skuPriceModel) {
|
||||
Db::transaction(function () use ($skuPriceModel, $skuPrice) {
|
||||
$this->addStockToSkuPrice($skuPriceModel, $skuPrice['add_stock'], 'goods');
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$add_stock = $this->request->param('add_stock', 0);
|
||||
$skuPriceModel = SkuPriceModel::where('goods_id', $id)->order('id', 'asc')->find();
|
||||
|
||||
if ($skuPriceModel) {
|
||||
Db::transaction(function () use ($skuPriceModel, $add_stock) {
|
||||
$this->addStockToSkuPrice($skuPriceModel, $add_stock, 'goods');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$this->success('补货成功');
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function select()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$type = $this->request->param('type', 'page');
|
||||
$goodsTableName = $this->model->getQuery()->getTable();
|
||||
|
||||
$goods = $this->model->sheepFilter()->with(['max_sku_price']);
|
||||
|
||||
// 聚合库存 (包含下架的规格)
|
||||
$skuSql = SkuPriceModel::field('sum(stock) as stock, goods_id as sku_goods_id')->group('goods_id')->buildSql();
|
||||
$goods = $goods->join([$skuSql => 'sp'], $goodsTableName . '.id = sp.sku_goods_id', 'left')
|
||||
->field("$goodsTableName.*, sp.stock"); // ,score.*
|
||||
|
||||
if ($type == 'select') {
|
||||
// 普通结果
|
||||
$goods = collection($goods->select());
|
||||
} else {
|
||||
// 分页结果
|
||||
$goods = $goods->paginate($this->request->param('list_rows', 10));
|
||||
}
|
||||
|
||||
$goods = $goods->each(function ($goods) {
|
||||
// 获取活动信息
|
||||
$goods->activities = $goods->activities;
|
||||
$goods->promos = $goods->promos;
|
||||
|
||||
$data_type = $this->request->param('data_type', ''); // 特殊 type 需要处理的数据
|
||||
if ($data_type == 'score_shop') {
|
||||
$goods->is_score_shop = $goods->is_score_shop;
|
||||
}
|
||||
});
|
||||
|
||||
$this->success('获取成功', null, $goods);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取指定活动相关商品
|
||||
*
|
||||
* @param Request $request
|
||||
* @return void
|
||||
*/
|
||||
public function activitySelect()
|
||||
{
|
||||
$activity_id = $this->request->param('activity_id');
|
||||
$need_buyers = $this->request->param('need_buyers', 0); // 需要查询哪些人在参与活动
|
||||
$activity = $this->activityModel->where('id', $activity_id)->find();
|
||||
if (!$activity) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
|
||||
|
||||
// 存一下,获取器获取指定活动的时候会用到
|
||||
foreach ($goodsIds as $id) {
|
||||
session('goods-activity_id:' . $id, $activity_id);
|
||||
}
|
||||
$service = new GoodsService(function ($goods) use ($need_buyers) {
|
||||
if ($need_buyers) {
|
||||
$goods->buyers = $goods->buyers;
|
||||
}
|
||||
$goods->activity = $goods->activity;
|
||||
return $goods;
|
||||
});
|
||||
|
||||
$goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->select();
|
||||
$goods = collection($goods)->toArray(); // 可以将里面的单个 model也转为数组
|
||||
foreach ($goods as &$gd) {
|
||||
unset($gd['new_sku_prices'], $gd['activity']);
|
||||
}
|
||||
$this->success('获取成功', null, $goods);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 删除(支持批量)
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if (empty($id)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'id'));
|
||||
}
|
||||
|
||||
$list = $this->model->where('id', 'in', $id)->select();
|
||||
$result = Db::transaction(function () use ($list) {
|
||||
$count = 0;
|
||||
foreach ($list as $item) {
|
||||
// 删除相关库存预警记录
|
||||
StockWarningModel::destroy(function ($query) use ($item) {
|
||||
$query->where('goods_id', $item->id);
|
||||
});
|
||||
$count += $item->delete();
|
||||
}
|
||||
|
||||
return $count;
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('删除成功', null, $result);
|
||||
} else {
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function recyclebin()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$goods = $this->model->onlyTrashed()->sheepFilter()->paginate($this->request->param('list_rows', 10));
|
||||
$this->success('获取成功', null, $goods);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 还原(支持批量)
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function restore($id = null)
|
||||
{
|
||||
if (empty($id)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'id'));
|
||||
}
|
||||
|
||||
$items = $this->model->onlyTrashed()->where('id', 'in', $id)->select();
|
||||
$result = Db::transaction(function () use ($items) {
|
||||
$count = 0;
|
||||
foreach ($items as $item) {
|
||||
$count += $item->restore();
|
||||
}
|
||||
|
||||
return $count;
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('还原成功', null, $result);
|
||||
} else {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 销毁(支持批量)
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function destroy($id = null)
|
||||
{
|
||||
if (empty($id)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'id'));
|
||||
}
|
||||
|
||||
if ($id !== 'all') {
|
||||
$items = $this->model->onlyTrashed()->whereIn('id', $id)->select();
|
||||
} else {
|
||||
$items = $this->model->onlyTrashed()->select();
|
||||
}
|
||||
$result = Db::transaction(function () use ($items) {
|
||||
$count = 0;
|
||||
foreach ($items as $goods) {
|
||||
// 删除商品相关的规格,规格记录
|
||||
SkuModel::where('goods_id', $goods->id)->delete();
|
||||
SkuPriceModel::where('goods_id', $goods->id)->delete();
|
||||
|
||||
// 删除商品
|
||||
$count += $goods->delete(true);
|
||||
}
|
||||
return $count;
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('销毁成功', null, $result);
|
||||
}
|
||||
$this->error('销毁失败');
|
||||
}
|
||||
}
|
||||
157
application/admin/controller/shopro/goods/Service.php
Normal file
157
application/admin/controller/shopro/goods/Service.php
Normal file
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\goods;
|
||||
|
||||
use app\admin\controller\shopro\Common;
|
||||
use think\Db;
|
||||
use app\admin\model\shopro\goods\Service as ServiceModel;
|
||||
|
||||
/**
|
||||
* 服务保障
|
||||
*/
|
||||
class Service extends Common
|
||||
{
|
||||
|
||||
protected $noNeedRight = ['select'];
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new ServiceModel;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 服务保障列表
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$services = $this->model->sheepFilter()->paginate(request()->param('list_rows', 10));
|
||||
|
||||
$this->success('获取成功', null, $services);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 添加服务保障
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$params = $this->request->only([
|
||||
'name', 'image', 'description'
|
||||
]);
|
||||
$this->svalidate($params, ".add");
|
||||
|
||||
Db::transaction(function () use ($params) {
|
||||
$this->model->save($params);
|
||||
});
|
||||
$this->success('保存成功');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 服务保障详情
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function detail($id)
|
||||
{
|
||||
$service = $this->model->where('id', $id)->find();
|
||||
|
||||
if (!$service) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
|
||||
$this->success('获取成功', null, $service);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 修改服务保障
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch('add');
|
||||
}
|
||||
|
||||
$params = $this->request->only([
|
||||
'name', 'image', 'description'
|
||||
]);
|
||||
$this->svalidate($params, ".edit");
|
||||
|
||||
$id = explode(',', $id);
|
||||
$list = $this->model->whereIn('id', $id)->select();
|
||||
$result = Db::transaction(function () use ($list, $params) {
|
||||
$count = 0;
|
||||
foreach ($list as $item) {
|
||||
$params['id'] = $item->id;
|
||||
$count += $item->save($params);
|
||||
}
|
||||
return $count;
|
||||
});
|
||||
if ($result) {
|
||||
$this->success('更新成功', null, $result);
|
||||
} else {
|
||||
$this->error('更新失败,未改变任何记录');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 删除服务标签
|
||||
*
|
||||
* @param string $id 要删除的服务保障列表
|
||||
* @return void
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if (empty($id)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'id'));
|
||||
}
|
||||
|
||||
$list = $this->model->where('id', 'in', $id)->select();
|
||||
Db::transaction(function () use ($list) {
|
||||
$count = 0;
|
||||
foreach ($list as $item) {
|
||||
$count += $item->delete();
|
||||
}
|
||||
|
||||
return $count;
|
||||
});
|
||||
|
||||
$this->success('删除成功');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取所有服务列表
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function select()
|
||||
{
|
||||
$services = $this->model->field('id, name')->select();
|
||||
|
||||
$this->success('获取成功', null, $services);
|
||||
}
|
||||
}
|
||||
57
application/admin/controller/shopro/goods/SkuPrice.php
Normal file
57
application/admin/controller/shopro/goods/SkuPrice.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\goods;
|
||||
|
||||
use app\admin\controller\shopro\Common;
|
||||
use think\Db;
|
||||
use app\admin\model\shopro\goods\SkuPrice as SkuPriceModel;
|
||||
|
||||
class SkuPrice extends Common
|
||||
{
|
||||
|
||||
protected $noNeedRight = ['index'];
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new SkuPriceModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* skuPrices列表
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$goods_id = $this->request->param('goods_id');
|
||||
$skuPrices = $this->model->where('goods_id', $goods_id)->select();
|
||||
|
||||
$this->success('获取成功', null, $skuPrices);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* skuPrices编辑
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
$params = $this->request->only([
|
||||
'status',
|
||||
]);
|
||||
|
||||
$id = explode(',', $id);
|
||||
$items = $this->model->whereIn('id', $id)->select();
|
||||
Db::transaction(function () use ($items, $params) {
|
||||
foreach ($items as $skuPrice) {
|
||||
$skuPrice->save($params);
|
||||
}
|
||||
});
|
||||
|
||||
$this->success('更新成功');
|
||||
}
|
||||
}
|
||||
48
application/admin/controller/shopro/goods/StockLog.php
Normal file
48
application/admin/controller/shopro/goods/StockLog.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\goods;
|
||||
|
||||
use app\admin\controller\shopro\Common;
|
||||
use think\Db;
|
||||
use app\admin\model\shopro\goods\SkuPrice as SkuPriceModel;
|
||||
use app\admin\model\shopro\goods\StockLog as StockLogModel;
|
||||
use addons\shopro\library\Operator;
|
||||
|
||||
/**
|
||||
* 补库存记录
|
||||
*/
|
||||
class StockLog extends Common
|
||||
{
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new StockLogModel;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 库存补货列表
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$skuPriceTableName = (new SkuPriceModel())->getQuery()->getTable();
|
||||
$stockLogs = $this->model->sheepFilter()->alias('g')->with(['goods' => function ($query) {
|
||||
$query->removeOption('soft_delete');
|
||||
}, 'oper'])
|
||||
->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
|
||||
->field('g.*,sp.stock as total_stock')
|
||||
->paginate($this->request->param('list_rows', 10))->toArray();
|
||||
// 解析操作人信息
|
||||
foreach ($stockLogs['data'] as &$log) {
|
||||
$log['oper'] = Operator::info('admin', $log['oper'] ?? null);
|
||||
}
|
||||
$this->success('获取成功', null, $stockLogs);
|
||||
}
|
||||
}
|
||||
120
application/admin/controller/shopro/goods/StockWarning.php
Normal file
120
application/admin/controller/shopro/goods/StockWarning.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\goods;
|
||||
|
||||
use app\admin\controller\shopro\Common;
|
||||
use think\Db;
|
||||
use app\admin\model\shopro\goods\SkuPrice as SkuPriceModel;
|
||||
use app\admin\model\shopro\goods\StockWarning as StockWarningModel;
|
||||
use addons\shopro\traits\StockWarning as StockWarningTrait;
|
||||
|
||||
/**
|
||||
* 库存预警
|
||||
*/
|
||||
class StockWarning extends Common
|
||||
{
|
||||
use StockWarningTrait;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new StockWarningModel;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 库存预警列表
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$skuPriceTableName = (new SkuPriceModel())->getQuery()->getTable();
|
||||
$stockWarnings = $this->model->sheepFilter()->alias('g')->with(['goods' => function ($query) {
|
||||
$query->removeOption('soft_delete');
|
||||
}])
|
||||
->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
|
||||
->field('g.*,sp.stock')
|
||||
->paginate($this->request->param('list_rows', 10));
|
||||
|
||||
$warning_total = $this->model->sheepFilter(false, function ($filters) {
|
||||
$filters['stock_type'] = 'no_enough';
|
||||
return $filters;
|
||||
})->alias('g')
|
||||
->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
|
||||
->field('g.*,sp.stock')
|
||||
->count();
|
||||
$over_total = $this->model->sheepFilter(false, function ($filters) {
|
||||
$filters['stock_type'] = 'over';
|
||||
return $filters;
|
||||
})->alias('g')
|
||||
->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
|
||||
->field('g.*,sp.stock')
|
||||
->count();
|
||||
|
||||
$result = [
|
||||
'rows' => $stockWarnings,
|
||||
'warning_total' => $warning_total,
|
||||
'over_total' => $over_total,
|
||||
];
|
||||
|
||||
$this->success('获取成功', null, $result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 补货
|
||||
*
|
||||
* @param [type] $ids
|
||||
* @param [type] $stock
|
||||
* @return void
|
||||
*/
|
||||
public function addStock ($id) {
|
||||
if ($this->request->isAjax()) {
|
||||
$params = $this->request->only(['stock']);
|
||||
$this->svalidate($params, ".add");
|
||||
|
||||
$stockWarning = $this->model->with(['sku_price'])->where('id', $id)->find();
|
||||
if (!$stockWarning) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
if (!$stockWarning->sku_price) {
|
||||
$this->error('库存规格不存在');
|
||||
}
|
||||
|
||||
Db::transaction(function () use ($stockWarning, $params) {
|
||||
// 补货
|
||||
$this->addStockToSkuPrice($stockWarning->sku_price, $params['stock'], 'stock_warning');
|
||||
});
|
||||
|
||||
$this->success('补货成功');
|
||||
}
|
||||
|
||||
return $this->view->fetch();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function recyclebin()
|
||||
{
|
||||
if ($this->request->isAjax()) {
|
||||
$skuPriceTableName = (new SkuPriceModel())->getQuery()->getTable();
|
||||
$stockWarnings = $this->model->onlyTrashed()->sheepFilter()->alias('g')->with(['goods' => function ($query) {
|
||||
$query->removeOption('soft_delete');
|
||||
}])
|
||||
->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
|
||||
->field('g.*,sp.stock')
|
||||
->paginate($this->request->param('list_rows', 10));
|
||||
|
||||
$this->success('获取成功', null, $stockWarnings);
|
||||
}
|
||||
|
||||
return $this->view->fetch();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user