初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,386 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\app\physical;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 商品分类管理
|
||||
*/
|
||||
class Category extends Backend
|
||||
{
|
||||
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\app\physical\Category;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
$list = $this->model
|
||||
->where($where)
|
||||
->where(['uniacid' => UNIACID])
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
$result = array("total" => $list->total(), "rows" => $list->items(), 'attr' => [
|
||||
'status' => $this->model->getStatusList()
|
||||
]);
|
||||
|
||||
return $this->success("获取成功", $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分类树
|
||||
*/
|
||||
public function tree()
|
||||
{
|
||||
$list = $this->model
|
||||
->where(['uniacid' => UNIACID, 'parent_id' => 0])
|
||||
->order('weigh', 'desc')
|
||||
->order('id', 'asc')
|
||||
->select();
|
||||
|
||||
$result = [];
|
||||
foreach ($list as $item) {
|
||||
$result[] = $this->getCategoryTree($item);
|
||||
}
|
||||
|
||||
return $this->success("获取成功", $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归获取分类树
|
||||
*/
|
||||
protected function getCategoryTree($category)
|
||||
{
|
||||
$data = $category->toArray();
|
||||
$children = $this->model
|
||||
->where(['uniacid' => UNIACID, 'parent_id' => $category['id']])
|
||||
->order('weigh', 'desc')
|
||||
->order('id', 'asc')
|
||||
->select();
|
||||
|
||||
if (!empty($children)) {
|
||||
$data['children'] = [];
|
||||
foreach ($children as $child) {
|
||||
$data['children'][] = $this->getCategoryTree($child);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
$params = $this->request->param('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
|
||||
$params['uniacid'] = UNIACID;
|
||||
$params['createtime'] = time();
|
||||
$params['updatetime'] = time();
|
||||
|
||||
$result = $this->model->allowField(true)->save($params);
|
||||
|
||||
if ($result !== false) {
|
||||
$this->success("添加成功");
|
||||
} else {
|
||||
$this->error(__('No rows were inserted'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$params = $this->request->param('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
|
||||
$params['updatetime'] = time();
|
||||
|
||||
$row = $this->model->where('id', $ids)->where('uniacid', UNIACID)->find();
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
|
||||
$result = $row->allowField(true)->save($params);
|
||||
|
||||
if ($result !== false) {
|
||||
$this->success("更新成功");
|
||||
} else {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if ($ids) {
|
||||
// 检查是否有子分类
|
||||
$hasChildren = $this->model->where('parent_id', 'in', $ids)->where('uniacid', UNIACID)->find();
|
||||
if ($hasChildren) {
|
||||
$this->error('该分类下存在子分类,请先删除子分类');
|
||||
}
|
||||
|
||||
$count = $this->model->where('id', 'in', $ids)->where('uniacid', UNIACID)->delete();
|
||||
if ($count) {
|
||||
$this->success("删除成功");
|
||||
} else {
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取状态列表
|
||||
*/
|
||||
public function getStatusList()
|
||||
{
|
||||
$this->success("获取成功", $this->model->getStatusList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量保存分类(前端完成所有操作后一次性提交)
|
||||
*
|
||||
* @param array original_ids 原始数据中的所有分类ID列表
|
||||
* @param array categories 完整的树形分类结构数据
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$originalIds = $this->request->param('original_ids/a', []);
|
||||
$categories = $this->request->param('categories/a', []);
|
||||
|
||||
if (empty($categories)) {
|
||||
$this->error('分类数据不能为空');
|
||||
}
|
||||
|
||||
// 提取当前提交的所有ID
|
||||
$currentIds = $this->extractAllIds($categories);
|
||||
|
||||
// 找出需要删除的ID(在original_ids中存在,但不在currentIds中)
|
||||
$toDelete = array_diff($originalIds, $currentIds);
|
||||
|
||||
// 分类处理数据,按层级分组
|
||||
$toAdd = [0 => [], 1 => [], 2 => []]; // 新增的数据,按level分组
|
||||
$toUpdate = [0 => [], 1 => [], 2 => []]; // 更新的数据,按level分组
|
||||
|
||||
$this->categorizeDataByLevel($categories, $toAdd, $toUpdate);
|
||||
|
||||
// 开始事务
|
||||
Db::startTrans();
|
||||
$result = null;
|
||||
$error = null;
|
||||
|
||||
try {
|
||||
$addedCount = 0;
|
||||
$updatedCount = 0;
|
||||
$deletedCount = 0;
|
||||
|
||||
// 1. 先删除(从叶子节点开始,避免外键约束问题)
|
||||
if (!empty($toDelete)) {
|
||||
// 按层级从深到浅排序,先删除子级
|
||||
$deleteList = $this->model
|
||||
->where('id', 'in', $toDelete)
|
||||
->where('uniacid', UNIACID)
|
||||
->order('id', 'desc')
|
||||
->select();
|
||||
|
||||
foreach ($deleteList as $item) {
|
||||
// 递归删除该分类及其所有子分类
|
||||
$deletedCount += $this->deleteCategoryRecursive($item['id']);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 更新现有数据(按层级顺序:先一级,再二级,最后三级)
|
||||
$idMapping = []; // 用于映射前端临时ID到真实ID
|
||||
|
||||
for ($level = 0; $level <= 2; $level++) {
|
||||
// 按weigh排序
|
||||
usort($toUpdate[$level], function ($a, $b) {
|
||||
return $a['weigh'] - $b['weigh'];
|
||||
});
|
||||
|
||||
foreach ($toUpdate[$level] as $item) {
|
||||
$id = $item['id'];
|
||||
unset($item['id']);
|
||||
unset($item['children']);
|
||||
unset($item['level']);
|
||||
|
||||
$item['updatetime'] = time();
|
||||
|
||||
$this->model->where('id', $id)->where('uniacid', UNIACID)->update($item);
|
||||
$updatedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 新增数据 - 按原始树形结构顺序保存(父子顺序)
|
||||
// 使用前端提交的parent_id来确保正确的层级关系
|
||||
$this->saveAddedCategories($categories, $idMapping, $addedCount);
|
||||
|
||||
Db::commit();
|
||||
|
||||
$result = [
|
||||
'added' => $addedCount,
|
||||
'updated' => $updatedCount,
|
||||
'deleted' => $deletedCount,
|
||||
'id_mapping' => $idMapping
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$error = $e->getMessage();
|
||||
}
|
||||
|
||||
if ($error) {
|
||||
$this->error('保存失败:' . $error);
|
||||
} else {
|
||||
$this->success('保存成功', $result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归删除分类及其子分类
|
||||
*/
|
||||
protected function deleteCategoryRecursive($categoryId)
|
||||
{
|
||||
$count = 0;
|
||||
|
||||
// 先删除子分类
|
||||
$children = $this->model
|
||||
->where('parent_id', $categoryId)
|
||||
->where('uniacid', UNIACID)
|
||||
->column('id');
|
||||
|
||||
foreach ($children as $childId) {
|
||||
$count += $this->deleteCategoryRecursive($childId);
|
||||
}
|
||||
|
||||
// 再删除当前分类
|
||||
$count += $this->model->where('id', $categoryId)->where('uniacid', UNIACID)->delete();
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从树形结构中提取所有ID
|
||||
*/
|
||||
protected function extractAllIds($categories)
|
||||
{
|
||||
$ids = [];
|
||||
foreach ($categories as $item) {
|
||||
$ids[] = abs($item['id']); // 取绝对值,因为新增的是负数
|
||||
if (!empty($item['children'])) {
|
||||
$ids = array_merge($ids, $this->extractAllIds($item['children']));
|
||||
}
|
||||
}
|
||||
return array_unique($ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据按层级分类为新增和更新
|
||||
*/
|
||||
protected function categorizeDataByLevel($categories, &$toAdd, &$toUpdate, $parentId = 0, $level = 0)
|
||||
{
|
||||
foreach ($categories as $item) {
|
||||
// 如果当前是新增分类(id < 0),则使用当前id作为子分类的parentId
|
||||
// 如果当前是已存在分类(id > 0),则使用当前id作为子分类的parentId
|
||||
$currentParentId = $item['id'];
|
||||
|
||||
$item['parent_id'] = $parentId;
|
||||
$item['level'] = $level;
|
||||
|
||||
if ($item['id'] < 0) {
|
||||
// 新增数据,按层级分组
|
||||
$toAdd[$level][] = $item;
|
||||
} else {
|
||||
// 更新数据,按层级分组
|
||||
$toUpdate[$level][] = $item;
|
||||
}
|
||||
|
||||
// 处理子分类
|
||||
if (!empty($item['children'])) {
|
||||
$this->categorizeDataByLevel($item['children'], $toAdd, $toUpdate, $currentParentId, $level + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归保存新增分类(按原始树形结构顺序:父子顺序)
|
||||
*/
|
||||
protected function saveAddedCategories($categories, &$idMapping, &$addedCount, $parentId = 0)
|
||||
{
|
||||
// 按weigh排序
|
||||
usort($categories, function ($a, $b) {
|
||||
return $a['weigh'] - $b['weigh'];
|
||||
});
|
||||
|
||||
foreach ($categories as $item) {
|
||||
// 如果id < 0,说明是新增分类
|
||||
// 如果id > 0,说明是已存在分类
|
||||
$isNew = $item['id'] < 0;
|
||||
|
||||
// 记录调试信息
|
||||
$debugMsg = sprintf("处理分类: id=%d, name=%s, isNew=%s, parentId=%d",
|
||||
$item['id'], $item['name'], $isNew ? 'true' : 'false', $parentId);
|
||||
trace('Category save: ' . $debugMsg, 'info');
|
||||
|
||||
if ($isNew) {
|
||||
$tempId = $item['id'];
|
||||
|
||||
$data = [
|
||||
'name' => $item['name'],
|
||||
'parent_id' => $parentId, // 强制使用递归传入的parentId
|
||||
'style' => $item['style'],
|
||||
'image' => isset($item['image']) ? $item['image'] : '',
|
||||
'description' => isset($item['description']) ? $item['description'] : '',
|
||||
'status' => isset($item['status']) ? intval($item['status']) : 1,
|
||||
'weigh' => isset($item['weigh']) ? $item['weigh'] : 0,
|
||||
'uniacid' => UNIACID,
|
||||
'createtime' => time(),
|
||||
'updatetime' => time(),
|
||||
];
|
||||
|
||||
trace('Category insert data: ' . json_encode($data), 'info');
|
||||
|
||||
$newId = $this->model->insertGetId($data);
|
||||
$idMapping[$tempId] = $newId;
|
||||
$addedCount++;
|
||||
|
||||
trace('Category inserted: newId=' . $newId . ', parentId=' . $parentId, 'info');
|
||||
|
||||
// 递归保存子分类,传入当前新创建的ID作为parent_id
|
||||
if (!empty($item['children'])) {
|
||||
$this->saveAddedCategories($item['children'], $idMapping, $addedCount, $newId);
|
||||
}
|
||||
} else {
|
||||
// 已存在的分类不需要在这里处理,更新操作在前面已处理
|
||||
// 但需要递归处理其子分类,传入当前分类的id作为parent_id
|
||||
if (!empty($item['children'])) {
|
||||
$this->saveAddedCategories($item['children'], $idMapping, $addedCount, $item['id']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\app\physical;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
|
||||
/**
|
||||
* 实物商品通用配置
|
||||
*/
|
||||
class Config extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* AttachmentGroup模型对象
|
||||
* @var \app\admin\model\attachment\AttachmentGroup
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
protected $configName = 'physical';
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
|
||||
\app\common\model\fill\Handle::check('app_config', 'physical');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置
|
||||
* @return mixed
|
||||
*/
|
||||
public function getConfig()
|
||||
{
|
||||
$config = \app\common\model\app\Config::getConfig($this->configName);
|
||||
|
||||
if (!isset($config['auto_confirm_days'])) {
|
||||
$config['auto_confirm_days'] = '7';
|
||||
}
|
||||
|
||||
if (!isset($config['show_sales'])) {
|
||||
$config['show_sales'] = '1';
|
||||
}
|
||||
|
||||
if (!isset($config['express_app_code'])) {
|
||||
$config['express_app_code'] = '';
|
||||
}
|
||||
|
||||
return $this->success("获取成功", [
|
||||
'config' => $config
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置配置
|
||||
* @return mixed
|
||||
*/
|
||||
public function setConfig()
|
||||
{
|
||||
$row = $this->request->post("row/a", [], 'trim');
|
||||
|
||||
$data = \app\common\model\app\Config::setConfig($this->configName, $row);
|
||||
return $this->success("保存成功", $data);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,452 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\app\physical;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\model\order\Order;
|
||||
use app\common\model\app\physical\OrderExpress;
|
||||
use app\common\model\app\physical\OrderAddress;
|
||||
use app\common\library\app\physical\express\Express;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 订单发货管理
|
||||
*/
|
||||
class Dispatch extends Backend
|
||||
{
|
||||
|
||||
protected $orderModel = null;
|
||||
protected $expressModel = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->orderModel = new Order;
|
||||
$this->expressModel = new OrderExpress;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发货操作
|
||||
*
|
||||
* @param string $action 发货行为(默认:confirm=确认发货, cancel=取消发货, change=修改发货信息)
|
||||
* @param int $order_id 订单id
|
||||
* @param int $order_express_id 发货单id
|
||||
* @param string $method 发货方式(input=手动发货, api=推送运单, upload=上传发货单)
|
||||
* @param array $express 物流信息
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function dispatch()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
|
||||
$action = $params['action'] ?? 'confirm';
|
||||
if (!in_array($action, ['confirm', 'cancel', 'change'])) {
|
||||
$this->error('发货参数错误');
|
||||
}
|
||||
|
||||
$orderId = $params['order_id'] ?? 0;
|
||||
if (!$orderId) {
|
||||
$this->error('订单ID不能为空');
|
||||
}
|
||||
|
||||
$order = $this->orderModel->where([
|
||||
'id' => $orderId,
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
|
||||
if (!$order) {
|
||||
$this->error('未找到订单');
|
||||
}
|
||||
|
||||
if ($order['order_type'] != 'physical') {
|
||||
$this->error('该订单不是实物商品订单');
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
case 'confirm':
|
||||
$result = $this->confirmDispatch($order, $params);
|
||||
$this->success('发货成功', $result);
|
||||
break;
|
||||
case 'cancel':
|
||||
$result = $this->cancelDispatch($order, $params);
|
||||
$this->success('取消发货成功');
|
||||
break;
|
||||
case 'change':
|
||||
$result = $this->changeDispatch($order, $params);
|
||||
$this->success('修改成功', $result);
|
||||
break;
|
||||
}
|
||||
|
||||
$this->error('操作失败');
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认发货
|
||||
*/
|
||||
private function confirmDispatch($order, $params)
|
||||
{
|
||||
$method = $params['method'] ?? 'input';
|
||||
if (!in_array($method, ['input', 'api', 'upload'])) {
|
||||
$this->error('请使用正确的发货方式');
|
||||
}
|
||||
|
||||
$orderStatus = \app\common\constant\order\Status::STATUS_UNSEND;
|
||||
if ($order['status'] != $orderStatus) {
|
||||
$this->error("该订单状态不允许发货");
|
||||
}
|
||||
|
||||
$express = $params['express'] ?? null;
|
||||
if (empty($express['name']) || empty($express['code']) || empty($express['no'])) {
|
||||
$this->error('请输入正确的快递信息');
|
||||
}
|
||||
|
||||
// 获取收货地址信息
|
||||
$address = OrderAddress::where([
|
||||
'order_id' => $order['id'],
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$orderExpress = $this->expressModel->create([
|
||||
'uniacid' => UNIACID,
|
||||
'user_id' => $order['user_id'],
|
||||
'order_id' => $order['id'],
|
||||
'method' => $method,
|
||||
'driver' => $express['driver'] ?? null,
|
||||
'express_name' => $express['name'],
|
||||
'express_code' => $express['code'],
|
||||
'express_no' => $express['no'],
|
||||
'sender_mobile' => $address ? $address['mobile'] : '',
|
||||
'status' => 'transport',
|
||||
'ext' => isset($express['ext']) ? json_encode($express['ext']) : null,
|
||||
'createtime' => time(),
|
||||
'updatetime' => time()
|
||||
]);
|
||||
|
||||
$order->status = \app\common\constant\order\Status::STATUS_UNRECEIVE;
|
||||
$order->save();
|
||||
|
||||
\app\common\model\order\Log::set($order['order_no'], "订单已发货,快递公司:{$express['name']},运单号:{$express['no']}");
|
||||
|
||||
Db::commit();
|
||||
|
||||
$this->syncExpressInfo($orderExpress, $order);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
return $express;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消发货
|
||||
*/
|
||||
private function cancelDispatch($order, $params)
|
||||
{
|
||||
$orderExpressId = $params['order_express_id'] ?? 0;
|
||||
if (!$orderExpressId) {
|
||||
$this->error('发货单ID不能为空');
|
||||
}
|
||||
|
||||
$orderExpress = $this->expressModel->where([
|
||||
'id' => $orderExpressId,
|
||||
'order_id' => $order['id'],
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
|
||||
if (!$orderExpress) {
|
||||
$this->error('未找到发货单');
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$orderExpress->delete();
|
||||
|
||||
$order->status = \app\common\constant\order\Status::STATUS_UNSEND;
|
||||
$order->save();
|
||||
|
||||
\app\common\model\order\Log::set($order['order_no'], "已取消发货");
|
||||
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改发货信息
|
||||
*/
|
||||
private function changeDispatch($order, $params)
|
||||
{
|
||||
$orderExpressId = $params['order_express_id'] ?? 0;
|
||||
if (!$orderExpressId) {
|
||||
$this->error('发货单ID不能为空');
|
||||
}
|
||||
|
||||
$orderExpress = $this->expressModel->where([
|
||||
'id' => $orderExpressId,
|
||||
'order_id' => $order['id'],
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
|
||||
if (!$orderExpress) {
|
||||
$this->error('未找到发货单');
|
||||
}
|
||||
|
||||
$express = $params['express'] ?? null;
|
||||
if (empty($express['name']) || empty($express['code']) || empty($express['no'])) {
|
||||
$this->error('请输入正确的快递信息');
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$orderExpress->save([
|
||||
'express_name' => $express['name'],
|
||||
'express_code' => $express['code'],
|
||||
'express_no' => $express['no'],
|
||||
'sender_mobile' => $express['sender_mobile'] ?? '',
|
||||
'method' => 'input',
|
||||
'status' => 'noinfo',
|
||||
'updatetime' => time()
|
||||
]);
|
||||
|
||||
// 删除旧的物流轨迹记录
|
||||
\app\common\model\app\physical\OrderExpressLog::where([
|
||||
'order_express_id' => $orderExpress['id'],
|
||||
'uniacid' => UNIACID
|
||||
])->delete();
|
||||
|
||||
\app\common\model\order\Log::set($order['order_no'], "修改发货信息,快递公司:{$express['name']},运单号:{$express['no']}");
|
||||
|
||||
Db::commit();
|
||||
|
||||
$this->syncExpressInfo($orderExpress, $order);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
return $express;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询物流信息
|
||||
*/
|
||||
public function query()
|
||||
{
|
||||
$orderExpressId = $this->request->param('order_express_id', 0);
|
||||
if (!$orderExpressId) {
|
||||
$this->error('发货单ID不能为空');
|
||||
}
|
||||
|
||||
$orderExpress = $this->expressModel->where([
|
||||
'id' => $orderExpressId,
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
|
||||
if (!$orderExpress) {
|
||||
$this->error('未找到发货单');
|
||||
}
|
||||
|
||||
$expressLib = new Express();
|
||||
$result = $expressLib->search([
|
||||
'order_id' => $orderExpress['order_id'],
|
||||
'express_code' => $orderExpress['express_code'],
|
||||
'express_no' => $orderExpress['express_no']
|
||||
], $orderExpress);
|
||||
|
||||
$this->success('查询成功', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新物流信息
|
||||
*/
|
||||
public function refresh()
|
||||
{
|
||||
$orderExpressId = $this->request->param('order_express_id', 0);
|
||||
if (!$orderExpressId) {
|
||||
$this->error('发货单ID不能为空');
|
||||
}
|
||||
|
||||
$orderExpress = $this->expressModel->where([
|
||||
'id' => $orderExpressId,
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
|
||||
if (!$orderExpress) {
|
||||
$this->error('未找到发货单');
|
||||
}
|
||||
|
||||
$order = $this->orderModel->where([
|
||||
'id' => $orderExpress['order_id'],
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
|
||||
if (!$order) {
|
||||
$this->error('未找到订单');
|
||||
}
|
||||
|
||||
try {
|
||||
$this->syncExpressInfo($orderExpress, $order);
|
||||
|
||||
$orderExpress = $this->expressModel->where([
|
||||
'id' => $orderExpressId,
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
} catch (\Exception $e) {
|
||||
$this->error('刷新失败:' . $e->getMessage());
|
||||
}
|
||||
$this->success('刷新成功', $orderExpress);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步物流信息
|
||||
*/
|
||||
private function syncExpressInfo($orderExpress, $order)
|
||||
{
|
||||
try {
|
||||
$physicalConfig = \app\common\model\app\Config::getConfig('physical');
|
||||
$aliyunConfig = [];
|
||||
$aliyunConfig['appcode'] = $physicalConfig['express_app_code'] ?? '';
|
||||
|
||||
$expressLib = new Express('aliyun', $aliyunConfig);
|
||||
|
||||
// 使用 Express 库的更新方法
|
||||
$expressLib->updateExpress($orderExpress);
|
||||
} catch (\Exception $e) {
|
||||
\think\Log::error('syncExpressInfo.Exception: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据订单号获取收货信息
|
||||
*/
|
||||
public function getAddress()
|
||||
{
|
||||
$orderNo = $this->request->param('order_no', '');
|
||||
if (empty($orderNo)) {
|
||||
$this->error('订单号不能为空');
|
||||
}
|
||||
|
||||
$order = $this->orderModel->where([
|
||||
'order_no' => $orderNo,
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
|
||||
if (!$order) {
|
||||
$this->error('未找到订单');
|
||||
}
|
||||
|
||||
if ($order['order_type'] != 'physical') {
|
||||
$this->error('该订单不是实物商品订单');
|
||||
}
|
||||
|
||||
$address = OrderAddress::where([
|
||||
'order_id' => $order['id'],
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
|
||||
if (!$address) {
|
||||
$this->error('未找到收货地址');
|
||||
}
|
||||
|
||||
$this->success('获取成功', $address);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据订单号编辑收货信息
|
||||
*/
|
||||
public function editAddress()
|
||||
{
|
||||
$orderNo = $this->request->param('order_no', '');
|
||||
if (empty($orderNo)) {
|
||||
$this->error('订单号不能为空');
|
||||
}
|
||||
|
||||
$order = $this->orderModel->where([
|
||||
'order_no' => $orderNo,
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
|
||||
if (!$order) {
|
||||
$this->error('未找到订单');
|
||||
}
|
||||
|
||||
if ($order['order_type'] != 'physical') {
|
||||
$this->error('该订单不是实物商品订单');
|
||||
}
|
||||
|
||||
$orderStatus = \app\common\constant\order\Status::STATUS_UNSEND;
|
||||
if ($order['status'] != $orderStatus) {
|
||||
$this->error('该订单状态不允许修改收货信息');
|
||||
}
|
||||
|
||||
$consignee = $this->request->param('consignee', '');
|
||||
$mobile = $this->request->param('mobile', '');
|
||||
$province = $this->request->param('province', '');
|
||||
$city = $this->request->param('city', '');
|
||||
$district = $this->request->param('district', '');
|
||||
$address = $this->request->param('address', '');
|
||||
|
||||
if (empty($consignee)) {
|
||||
$this->error('收货人不能为空');
|
||||
}
|
||||
if (empty($mobile)) {
|
||||
$this->error('联系电话不能为空');
|
||||
}
|
||||
if (empty($address)) {
|
||||
$this->error('详细地址不能为空');
|
||||
}
|
||||
|
||||
$orderAddress = OrderAddress::where([
|
||||
'order_id' => $order['id'],
|
||||
'uniacid' => UNIACID
|
||||
])->find();
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
if ($orderAddress) {
|
||||
$orderAddress->save([
|
||||
'consignee' => $consignee,
|
||||
'mobile' => $mobile,
|
||||
'province_name' => $province,
|
||||
'city_name' => $city,
|
||||
'district_name' => $district,
|
||||
'address' => $address,
|
||||
'updatetime' => time()
|
||||
]);
|
||||
} else {
|
||||
OrderAddress::create([
|
||||
'uniacid' => UNIACID,
|
||||
'order_id' => $order['id'],
|
||||
'user_id' => $order['user_id'],
|
||||
'consignee' => $consignee,
|
||||
'mobile' => $mobile,
|
||||
'province_name' => $province,
|
||||
'city_name' => $city,
|
||||
'district_name' => $district,
|
||||
'address' => $address,
|
||||
'createtime' => time(),
|
||||
'updatetime' => time()
|
||||
]);
|
||||
}
|
||||
|
||||
\app\common\model\order\Log::set($order['order_no'], "修改收货信息:{$consignee},{$mobile},{$province}{$city}{$district}{$address}");
|
||||
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error('修改失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->success('修改成功');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,875 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\app\physical;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 商品管理
|
||||
*/
|
||||
class Goods extends Backend
|
||||
{
|
||||
|
||||
protected $model = null;
|
||||
protected $skuModel = null;
|
||||
protected $skuPriceModel = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\app\physical\Goods;
|
||||
$this->skuModel = new \app\admin\model\app\physical\Sku;
|
||||
$this->skuPriceModel = new \app\admin\model\app\physical\SkuPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
$query = $this->model
|
||||
->where($where)
|
||||
->where([
|
||||
'uniacid'=>UNIACID
|
||||
]);
|
||||
|
||||
|
||||
// 处理分类搜索
|
||||
$hasCategoryId = $this->request->has('category_id');
|
||||
|
||||
if ($hasCategoryId) {
|
||||
$categoryIds = $this->request->post('category_id/a', []);
|
||||
|
||||
// 如果是空数组,返回空结果
|
||||
if (empty($categoryIds)) {
|
||||
$query->where('1=0');
|
||||
} else {
|
||||
// 获取所有分类及其子分类
|
||||
$allCategoryIds = [];
|
||||
foreach ($categoryIds as $categoryId) {
|
||||
$childIds = $this->getCategoryChildIds($categoryId);
|
||||
$allCategoryIds = array_merge($allCategoryIds, $childIds);
|
||||
}
|
||||
$allCategoryIds = array_unique($allCategoryIds);
|
||||
|
||||
$query->where(function ($q) use ($allCategoryIds) {
|
||||
foreach ($allCategoryIds as $cid) {
|
||||
$q->whereOrRaw("find_in_set($cid, category_id)");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$list = $query->order($sort, $order)->paginate($limit);
|
||||
|
||||
// 获取商品ID列表
|
||||
$goodsIds = [];
|
||||
foreach ($list as $item) {
|
||||
$goodsIds[] = $item['id'];
|
||||
}
|
||||
|
||||
// 批量获取SKU价格数据
|
||||
$skuPrices = [];
|
||||
if (!empty($goodsIds)) {
|
||||
$skuPriceList = $this->skuPriceModel
|
||||
->where('goods_id', 'in', $goodsIds)
|
||||
->where('uniacid', UNIACID)
|
||||
->select();
|
||||
|
||||
foreach ($skuPriceList as $skuPrice) {
|
||||
$skuPrices[$skuPrice['goods_id']][] = $skuPrice;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理列表数据
|
||||
$rows = [];
|
||||
foreach ($list as $item) {
|
||||
$item = $item->toArray();
|
||||
$goodsId = $item['id'];
|
||||
|
||||
// 获取该商品的SKU价格列表
|
||||
$goodsSkuPrices = $skuPrices[$goodsId] ?? [];
|
||||
|
||||
// 计算总库存(只计算status=1的规格)
|
||||
$totalStock = 0;
|
||||
foreach ($goodsSkuPrices as $skuPrice) {
|
||||
if (isset($skuPrice['status']) && $skuPrice['status'] == 1) {
|
||||
$totalStock += $skuPrice['stock'];
|
||||
}
|
||||
}
|
||||
$item['total_stock'] = $totalStock;
|
||||
|
||||
// SKU价格列表(用于编辑)
|
||||
$item['sku_price_list'] = $goodsSkuPrices;
|
||||
|
||||
// 价格范围(从商品表获取价格,如果为空则从SKU计算)
|
||||
$item['price'] = $item['price'] ?? 0;
|
||||
if (!empty($goodsSkuPrices)) {
|
||||
$prices = array_column($goodsSkuPrices, 'price');
|
||||
$item['price_min'] = min($prices);
|
||||
$item['price_max'] = max($prices);
|
||||
} else {
|
||||
$item['price_min'] = $item['price'];
|
||||
$item['price_max'] = $item['price'];
|
||||
}
|
||||
|
||||
$rows[] = $item;
|
||||
}
|
||||
|
||||
$result = array("total" => $list->total(), "rows" => $rows,'attr'=>[
|
||||
'status'=>$this->model->getStatusList(),
|
||||
'specType'=>$this->model->getSpecTypeList(),
|
||||
'isHidden'=>$this->model->getIsHiddenList()
|
||||
]);
|
||||
|
||||
return $this->success("获取成功",$result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分类及其所有子分类ID
|
||||
* @param int|array $categoryId 分类ID或分类ID数组
|
||||
* @return array
|
||||
*/
|
||||
protected function getCategoryChildIds($categoryId)
|
||||
{
|
||||
$categoryIds = [];
|
||||
|
||||
// 支持数组或单个ID
|
||||
$categoryIdArray = is_array($categoryId) ? $categoryId : [$categoryId];
|
||||
|
||||
foreach ($categoryIdArray as $cid) {
|
||||
$categoryIds[] = $cid;
|
||||
// 递归获取所有子分类
|
||||
$this->getChildCategoryIdsById($cid, $categoryIds);
|
||||
}
|
||||
|
||||
return array_unique($categoryIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归获取子分类ID
|
||||
* @param int $parentId
|
||||
* @param array $categoryIds
|
||||
*/
|
||||
protected function getChildCategoryIdsById($parentId, &$categoryIds)
|
||||
{
|
||||
$categoryModel = new \app\admin\model\app\physical\Category();
|
||||
|
||||
$children = $categoryModel
|
||||
->where('parent_id', $parentId)
|
||||
->where('uniacid', UNIACID)
|
||||
->column('id');
|
||||
|
||||
foreach ($children as $childId) {
|
||||
$categoryIds[] = $childId;
|
||||
$this->getChildCategoryIdsById($childId, $categoryIds);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归获取子分类ID(兼容旧代码)
|
||||
* @param object $model
|
||||
* @param int $parentId
|
||||
* @param array $categoryIds
|
||||
*/
|
||||
protected function getChildCategoryIds($model, $parentId, &$categoryIds)
|
||||
{
|
||||
$children = $model
|
||||
->where('parent_id', $parentId)
|
||||
->where('uniacid', UNIACID)
|
||||
->column('id');
|
||||
|
||||
foreach ($children as $childId) {
|
||||
$categoryIds[] = $childId;
|
||||
$this->getChildCategoryIds($model, $childId, $categoryIds);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 表单验证
|
||||
*/
|
||||
protected function validateGoods($params, $skuList = [], $specType = 'single')
|
||||
{
|
||||
// 验证发货时间
|
||||
if (!isset($params['delivery_time'])) {
|
||||
$this->error('发货时间不能为空');
|
||||
}
|
||||
|
||||
// 验证单次限购
|
||||
if (isset($params['single_limit'])) {
|
||||
if (!is_numeric($params['single_limit'])) {
|
||||
$this->error('单次限购数量必须是数字');
|
||||
}
|
||||
if ($params['single_limit'] < 0) {
|
||||
$this->error('单次限购数量不能小于0');
|
||||
}
|
||||
}
|
||||
|
||||
// 验证终身限购
|
||||
if (isset($params['lifetime_limit'])) {
|
||||
if (!is_numeric($params['lifetime_limit'])) {
|
||||
$this->error('终身限购数量必须是数字');
|
||||
}
|
||||
if ($params['lifetime_limit'] < 0) {
|
||||
$this->error('终身限购数量不能小于0');
|
||||
}
|
||||
}
|
||||
|
||||
// 多规格验证
|
||||
if ($specType == 'multi' && !empty($skuList)) {
|
||||
$skuPrices = $skuList['sku_prices'] ?? [];
|
||||
$skuNames = $skuList['sku_names'] ?? [];
|
||||
|
||||
// 验证每个规格都有规格值
|
||||
if (!empty($skuNames)) {
|
||||
foreach ($skuNames as $index => $skuName) {
|
||||
if (empty($skuName['name'])) {
|
||||
$this->error('规格名称不能为空');
|
||||
}
|
||||
if (empty($skuName['values']) || !is_array($skuName['values'])) {
|
||||
$this->error('规格【' . $skuName['name'] . '】必须有规格值');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 验证SKU价格
|
||||
if (!empty($skuPrices)) {
|
||||
foreach ($skuPrices as $index => $skuPrice) {
|
||||
if (!isset($skuPrice['price']) || $skuPrice['price'] === '' || $skuPrice['price'] === null) {
|
||||
$this->error('规格【' . ($skuPrice['sku_text'] ?? '第' . ($index + 1) . '个') . '】价格不能为空');
|
||||
}
|
||||
if (!is_numeric($skuPrice['price']) || $skuPrice['price'] < 0) {
|
||||
$this->error('规格【' . ($skuPrice['sku_text'] ?? '第' . ($index + 1) . '个') . '】价格必须大于等于0');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
$params = $this->request->param('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
|
||||
$specType = $params['spec_type'] ?? 'single';
|
||||
$skus = $params['skus'] ?? [];
|
||||
$skuList = $params['sku_list'] ?? [];
|
||||
unset($params['skus'], $params['sku_list']);
|
||||
|
||||
// 表单验证
|
||||
$this->validateGoods($params, $skuList, $specType);
|
||||
|
||||
// 处理轮播图
|
||||
$carousel = $params['carousel'] ?? [];
|
||||
if (is_array($carousel)) {
|
||||
if (count($carousel) > 10) {
|
||||
$this->error('轮播图最多只能上传10张');
|
||||
}
|
||||
$params['carousel'] = json_encode($carousel);
|
||||
}
|
||||
|
||||
// 处理分类ID
|
||||
if (isset($params['category_id']) && is_array($params['category_id'])) {
|
||||
$params['category_id'] = implode(',', $params['category_id']);
|
||||
}
|
||||
|
||||
$params['createtime'] = time();
|
||||
$params['updatetime'] = time();
|
||||
$params['uniacid'] = UNIACID;
|
||||
|
||||
// 设置限购字段默认值
|
||||
if (!isset($params['single_limit'])) {
|
||||
$params['single_limit'] = 0;
|
||||
}
|
||||
if (!isset($params['lifetime_limit'])) {
|
||||
$params['lifetime_limit'] = 0;
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$result = $this->model->allowField(true)->save($params);
|
||||
|
||||
if ($result && !empty($skuList)) {
|
||||
$this->saveSkuData($this->model->id, $skuList, $specType);
|
||||
|
||||
// 计算并更新商品价格
|
||||
$this->updateGoodsPrice($this->model->id, $specType);
|
||||
|
||||
// 计算并更新商品库存
|
||||
$this->updateGoodsStock($this->model->id, $specType);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
$this->success("添加成功");
|
||||
} else {
|
||||
$this->error(__('No rows were inserted'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$params = $this->request->param('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
|
||||
$specType = $params['spec_type'] ?? 'single';
|
||||
$skuList = $params['sku_list'] ?? [];
|
||||
unset($params['sku_list']);
|
||||
|
||||
// 表单验证
|
||||
$this->validateGoods($params, $skuList, $specType);
|
||||
|
||||
// 处理轮播图
|
||||
$carousel = $params['carousel'] ?? [];
|
||||
if (is_array($carousel)) {
|
||||
if (count($carousel) > 10) {
|
||||
$this->error('轮播图最多只能上传10张');
|
||||
}
|
||||
$params['carousel'] = json_encode($carousel);
|
||||
}
|
||||
|
||||
// 处理分类ID
|
||||
if (isset($params['category_id']) && is_array($params['category_id'])) {
|
||||
$params['category_id'] = implode(',', $params['category_id']);
|
||||
}
|
||||
|
||||
// 设置限购字段默认值
|
||||
if (!isset($params['single_limit'])) {
|
||||
$params['single_limit'] = 0;
|
||||
}
|
||||
if (!isset($params['lifetime_limit'])) {
|
||||
$params['lifetime_limit'] = 0;
|
||||
}
|
||||
|
||||
$params['updatetime'] = time();
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$row = $this->model->where('id', $ids)->where('uniacid', UNIACID)->find();
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$result = $row->allowField(true)->save($params);
|
||||
|
||||
if ($result && !empty($skuList)) {
|
||||
$this->saveSkuData($ids, $skuList, $specType);
|
||||
|
||||
// 计算并更新商品价格
|
||||
$this->updateGoodsPrice($ids, $specType);
|
||||
|
||||
// 计算并更新商品库存
|
||||
$this->updateGoodsStock($ids, $specType);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
$this->success("更新成功");
|
||||
} else {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成唯一SKU编码
|
||||
* 格式: SP + 13位随机字符
|
||||
*/
|
||||
protected function generateSkuSn()
|
||||
{
|
||||
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
do {
|
||||
$sn = 'SP';
|
||||
for ($i = 0; $i < 13; $i++) {
|
||||
$sn .= $chars[mt_rand(0, strlen($chars) - 1)];
|
||||
}
|
||||
// 检查是否已存在
|
||||
$exists = $this->skuPriceModel->where('sn', $sn)->find();
|
||||
} while ($exists);
|
||||
|
||||
return $sn;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存规格和SKU数据
|
||||
* @param int $goodsId 商品ID
|
||||
* @param array $skuData SKU数据
|
||||
* @param string $specType 规格类型:single=单规格,multi=多规格
|
||||
*/
|
||||
protected function saveSkuData($goodsId, $skuData, $specType = 'multi')
|
||||
{
|
||||
if ($specType == 'single') {
|
||||
// 单规格处理
|
||||
$this->saveSingleSku($goodsId, $skuData);
|
||||
} else {
|
||||
// 多规格处理
|
||||
$this->saveMultiSku($goodsId, $skuData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存单规格SKU
|
||||
*/
|
||||
protected function saveSingleSku($goodsId, $skuData)
|
||||
{
|
||||
$skuPrices = $skuData['sku_prices'] ?? [];
|
||||
|
||||
if (empty($skuPrices) || !isset($skuPrices[0])) {
|
||||
// 如果没有提供单规格数据,直接删除所有规格数据
|
||||
$this->skuModel->where('goods_id', $goodsId)->where('uniacid', UNIACID)->delete();
|
||||
$this->skuPriceModel->where('goods_id', $goodsId)->where('uniacid', UNIACID)->delete();
|
||||
return;
|
||||
}
|
||||
|
||||
$skuPrice = $skuPrices[0];
|
||||
|
||||
// 查询是否已存在SKU价格记录
|
||||
$existingSkuPrice = $this->skuPriceModel
|
||||
->where('goods_id', $goodsId)
|
||||
->where('uniacid', UNIACID)
|
||||
->order('id', 'asc')
|
||||
->find();
|
||||
|
||||
// 删除多余的规格项(多规格转单规格时)
|
||||
$this->skuModel->where('goods_id', $goodsId)->where('uniacid', UNIACID)->delete();
|
||||
|
||||
// 准备单规格数据
|
||||
$data = [
|
||||
'goods_id' => $goodsId,
|
||||
'uniacid' => UNIACID,
|
||||
'goods_sku_ids' => null,
|
||||
'goods_sku_text' => null,
|
||||
'image' => $skuPrice['image'] ?? '',
|
||||
'stock' => $skuPrice['stock'] ?? 0,
|
||||
'stock_warning' => $skuPrice['stock_warning'] ?? null,
|
||||
'sales' => $skuPrice['sales'] ?? 0,
|
||||
'sn' => $skuPrice['sn'] ?? '',
|
||||
'weight' => $skuPrice['weight'] ?? 0,
|
||||
'volume' => $skuPrice['volume'] ?? 0,
|
||||
'cost_price' => $skuPrice['cost_price'] ?? 0,
|
||||
'original_price' => $skuPrice['original_price'] ?? 0,
|
||||
'price' => $skuPrice['price'] ?? 0,
|
||||
'status' => $skuPrice['status'] ?? 1,
|
||||
'weigh' => $skuPrice['weigh'] ?? 0,
|
||||
'updatetime' => time()
|
||||
];
|
||||
|
||||
if ($existingSkuPrice) {
|
||||
// 更新现有记录
|
||||
// 删除该商品的其他SKU价格记录(防止多规格改单规格遗留数据)
|
||||
$this->skuPriceModel
|
||||
->where('goods_id', $goodsId)
|
||||
->where('uniacid', UNIACID)
|
||||
->where('id', '<>', $existingSkuPrice['id'])
|
||||
->delete();
|
||||
|
||||
// 更新记录(不更新销量,销量是累计的)
|
||||
unset($data['sales']);
|
||||
$this->skuPriceModel->where('id', $existingSkuPrice['id'])->update($data);
|
||||
} else {
|
||||
// 新增记录
|
||||
$data['createtime'] = time();
|
||||
|
||||
// 自动生成SKU编码(如果为空)
|
||||
if (empty($data['sn'])) {
|
||||
$data['sn'] = $this->generateSkuSn();
|
||||
}
|
||||
|
||||
// 先删除所有旧的SKU价格记录
|
||||
$this->skuPriceModel->where('goods_id', $goodsId)->where('uniacid', UNIACID)->delete();
|
||||
|
||||
$this->skuPriceModel->insert($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存多规格SKU
|
||||
*/
|
||||
protected function saveMultiSku($goodsId, $skuData)
|
||||
{
|
||||
// 先删除所有旧的规格数据
|
||||
$this->skuModel->where('goods_id', $goodsId)->where('uniacid', UNIACID)->delete();
|
||||
$this->skuPriceModel->where('goods_id', $goodsId)->where('uniacid', UNIACID)->delete();
|
||||
|
||||
if (empty($skuData)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$skuNames = $skuData['sku_names'] ?? [];
|
||||
$skuPrices = $skuData['sku_prices'] ?? [];
|
||||
|
||||
$skuIdMap = [];
|
||||
|
||||
// 保存规格名称和规格值
|
||||
if (!empty($skuNames)) {
|
||||
foreach ($skuNames as $skuName) {
|
||||
$specId = $this->skuModel->insertGetId([
|
||||
'goods_id' => $goodsId,
|
||||
'uniacid' => UNIACID,
|
||||
'name' => $skuName['name'] ?? '',
|
||||
'parent_id' => 0,
|
||||
'weigh' => $skuName['weigh'] ?? 0,
|
||||
'createtime' => time(),
|
||||
'updatetime' => time()
|
||||
]);
|
||||
|
||||
if (!empty($skuName['values'])) {
|
||||
foreach ($skuName['values'] as $index => $value) {
|
||||
$childId = $this->skuModel->insertGetId([
|
||||
'goods_id' => $goodsId,
|
||||
'uniacid' => UNIACID,
|
||||
'name' => $value,
|
||||
'parent_id' => $specId,
|
||||
'weigh' => $index,
|
||||
'createtime' => time(),
|
||||
'updatetime' => time()
|
||||
]);
|
||||
$skuIdMap[$skuName['name']][$value] = $childId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 保存SKU价格
|
||||
if (!empty($skuPrices)) {
|
||||
foreach ($skuPrices as $skuPrice) {
|
||||
$skuIds = $skuPrice['sku_ids'] ?? [];
|
||||
$skuText = $skuPrice['sku_text'] ?? '';
|
||||
|
||||
$skuIdStr = '';
|
||||
if (is_array($skuIds)) {
|
||||
$skuIdStr = implode(',', $skuIds);
|
||||
}
|
||||
|
||||
// 自动生成SKU编码(如果为空)
|
||||
$sn = $skuPrice['sn'] ?? '';
|
||||
if (empty($sn)) {
|
||||
$sn = $this->generateSkuSn();
|
||||
}
|
||||
|
||||
$this->skuPriceModel->insert([
|
||||
'goods_id' => $goodsId,
|
||||
'uniacid' => UNIACID,
|
||||
'goods_sku_ids' => $skuIdStr,
|
||||
'goods_sku_text' => $skuText,
|
||||
'image' => $skuPrice['image'] ?? '',
|
||||
'stock' => $skuPrice['stock'] ?? 0,
|
||||
'stock_warning' => $skuPrice['stock_warning'] ?? null,
|
||||
'sales' => $skuPrice['sales'] ?? 0,
|
||||
'sn' => $sn,
|
||||
'weight' => $skuPrice['weight'] ?? 0,
|
||||
'volume' => $skuPrice['volume'] ?? 0,
|
||||
'cost_price' => $skuPrice['cost_price'] ?? 0,
|
||||
'original_price' => $skuPrice['original_price'] ?? 0,
|
||||
'price' => $skuPrice['price'] ?? 0,
|
||||
'status' => $skuPrice['status'] ?? 1,
|
||||
'weigh' => $skuPrice['weigh'] ?? 0,
|
||||
'createtime' => time(),
|
||||
'updatetime' => time()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情
|
||||
*/
|
||||
public function detail($ids = null)
|
||||
{
|
||||
$row = $this->model->where('id', $ids)->where('uniacid', UNIACID)->find();
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
|
||||
// 处理分类ID
|
||||
if (isset($row['category_id']) && !empty($row['category_id'])) {
|
||||
$row['category_id'] = explode(',', $row['category_id']);
|
||||
// 转换为整数数组
|
||||
$row['category_id'] = array_map('intval', $row['category_id']);
|
||||
} else {
|
||||
$row['category_id'] = [];
|
||||
}
|
||||
|
||||
$specType = $row['spec_type'] ?? 'single';
|
||||
|
||||
if ($specType == 'single') {
|
||||
// 单规格商品,从sku_price表读取第一条记录
|
||||
$skuPrice = $this->skuPriceModel
|
||||
->where('goods_id', $ids)
|
||||
->where('uniacid', UNIACID)
|
||||
->order('id', 'asc')
|
||||
->find();
|
||||
|
||||
$row['sku_tree'] = [];
|
||||
|
||||
if ($skuPrice) {
|
||||
$row['sku_prices'] = [$skuPrice];
|
||||
} else {
|
||||
// 如果没有找到sku_price记录,返回空结构
|
||||
$row['sku_prices'] = [];
|
||||
}
|
||||
} else {
|
||||
// 多规格商品,从sku表读取数据
|
||||
$skuTree = $this->skuModel->getTree($ids);
|
||||
$skuPrices = $this->skuPriceModel->where('goods_id', $ids)->where('uniacid', UNIACID)->select();
|
||||
|
||||
$row['sku_tree'] = $skuTree;
|
||||
$row['sku_prices'] = $skuPrices;
|
||||
}
|
||||
|
||||
$this->success("获取成功", $row);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新商品价格
|
||||
* 单规格:取单一价格
|
||||
* 多规格:取最低价格
|
||||
* @param int $goodsId 商品ID
|
||||
* @param string $specType 规格类型:single=单规格,multi=多规格
|
||||
*/
|
||||
protected function updateGoodsPrice($goodsId, $specType = 'single')
|
||||
{
|
||||
$price = 0;
|
||||
|
||||
if ($specType == 'single') {
|
||||
// 单规格:取第一个SKU的价格
|
||||
$skuPrice = $this->skuPriceModel
|
||||
->where('goods_id', $goodsId)
|
||||
->where('uniacid', UNIACID)
|
||||
->order('id', 'asc')
|
||||
->find();
|
||||
|
||||
if ($skuPrice) {
|
||||
$price = $skuPrice['price'] ?? 0;
|
||||
}
|
||||
} else {
|
||||
// 多规格:取最低价格
|
||||
$minPrice = $this->skuPriceModel
|
||||
->where('goods_id', $goodsId)
|
||||
->where('uniacid', UNIACID)
|
||||
->min('price');
|
||||
|
||||
$price = $minPrice ?? 0;
|
||||
}
|
||||
|
||||
// 更新商品价格
|
||||
$this->model->where('id', $goodsId)->update([
|
||||
'price' => $price,
|
||||
'updatetime' => time()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新商品库存
|
||||
* 单规格:取单一库存(status=1)
|
||||
* 多规格:取所有规格库存之和(status=1)
|
||||
* @param int $goodsId 商品ID
|
||||
* @param string $specType 规格类型:single=单规格,multi=多规格
|
||||
*/
|
||||
protected function updateGoodsStock($goodsId, $specType = 'single')
|
||||
{
|
||||
if (empty($goodsId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stock = 0;
|
||||
|
||||
if ($specType == 'single') {
|
||||
$skuPrice = $this->skuPriceModel
|
||||
->where('goods_id', $goodsId)
|
||||
->where('uniacid', UNIACID)
|
||||
->where('status', 1)
|
||||
->order('id', 'asc')
|
||||
->find();
|
||||
|
||||
if ($skuPrice) {
|
||||
$stock = $skuPrice['stock'] ?? 0;
|
||||
}
|
||||
} else {
|
||||
$totalStock = $this->skuPriceModel
|
||||
->where('goods_id', $goodsId)
|
||||
->where('uniacid', UNIACID)
|
||||
->where('status', 1)
|
||||
->sum('stock');
|
||||
|
||||
$stock = $totalStock ?? 0;
|
||||
}
|
||||
|
||||
// 更新商品库存
|
||||
$this->model->where('id', $goodsId)->where('uniacid', UNIACID)->update([
|
||||
'stock' => $stock,
|
||||
'updatetime' => time()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if ($ids) {
|
||||
Db::startTrans();
|
||||
try {
|
||||
$count = $this->model->where('id', 'in', $ids)->where('uniacid', UNIACID)->delete();
|
||||
$this->skuModel->where('goods_id', 'in', $ids)->where('uniacid', UNIACID)->delete();
|
||||
$this->skuPriceModel->where('goods_id', 'in', $ids)->where('uniacid', UNIACID)->delete();
|
||||
Db::commit();
|
||||
} catch (PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($count) {
|
||||
$this->success("删除成功");
|
||||
} else {
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新
|
||||
*/
|
||||
public function multi($ids = "")
|
||||
{
|
||||
$ids = $ids ? $ids : $this->request->param("ids");
|
||||
if ($ids) {
|
||||
if ($this->request->has('params')) {
|
||||
parse_str($this->request->post("params"), $values);
|
||||
$values = $this->preExcludeFields($values);
|
||||
$values['updatetime'] = time();
|
||||
$result = $this->model->where('id', 'in', $ids)->where('uniacid', UNIACID)->update($values);
|
||||
if ($result) {
|
||||
$this->success("操作成功");
|
||||
} else {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新SKU价格和库存
|
||||
* 用于列表页快速编辑
|
||||
*/
|
||||
public function updateSkuPrice()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
$goodsId = $params['goods_id'] ?? 0;
|
||||
$skuPrices = $params['sku_prices'] ?? [];
|
||||
|
||||
if (empty($goodsId)) {
|
||||
$this->error('商品ID不能为空');
|
||||
}
|
||||
|
||||
if (empty($skuPrices) || !is_array($skuPrices)) {
|
||||
$this->error('SKU价格数据不能为空');
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
foreach ($skuPrices as $skuPrice) {
|
||||
$skuPriceId = $skuPrice['id'] ?? 0;
|
||||
if (empty($skuPriceId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$updateData = [];
|
||||
|
||||
// 更新价格
|
||||
if (isset($skuPrice['price'])) {
|
||||
$updateData['price'] = floatval($skuPrice['price']);
|
||||
}
|
||||
|
||||
// 更新库存(支持增减)
|
||||
if (isset($skuPrice['stock_change'])) {
|
||||
$stockChange = intval($skuPrice['stock_change']);
|
||||
if ($stockChange > 0) {
|
||||
// 增加库存
|
||||
$this->skuPriceModel->where('id', $skuPriceId)
|
||||
->where('uniacid', UNIACID)
|
||||
->setInc('stock', $stockChange);
|
||||
} elseif ($stockChange < 0) {
|
||||
// 减少库存
|
||||
$this->skuPriceModel->where('id', $skuPriceId)
|
||||
->where('uniacid', UNIACID)
|
||||
->setDec('stock', abs($stockChange));
|
||||
}
|
||||
}
|
||||
|
||||
// 直接设置库存
|
||||
if (isset($skuPrice['stock'])) {
|
||||
$updateData['stock'] = intval($skuPrice['stock']);
|
||||
}
|
||||
|
||||
// 更新其他字段
|
||||
if (!empty($updateData)) {
|
||||
$updateData['updatetime'] = time();
|
||||
$this->skuPriceModel->where('id', $skuPriceId)
|
||||
->where('uniacid', UNIACID)
|
||||
->update($updateData);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新商品价格
|
||||
$goods = $this->model->where('id', $goodsId)->where('uniacid', UNIACID)->find();
|
||||
if ($goods) {
|
||||
$specType = $goods['spec_type'] ?? 'single';
|
||||
$this->updateGoodsPrice($goodsId, $specType);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
$this->success('更新成功');
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取状态列表
|
||||
*/
|
||||
public function getStatusList()
|
||||
{
|
||||
$this->success("获取成功", $this->model->getStatusList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取规格类型列表
|
||||
*/
|
||||
public function getSpecTypeList()
|
||||
{
|
||||
$this->success("获取成功", $this->model->getSpecTypeList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取隐藏状态列表
|
||||
*/
|
||||
public function getIsHiddenList()
|
||||
{
|
||||
$this->success("获取成功", $this->model->getIsHiddenList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\app\physical;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 规格管理
|
||||
*/
|
||||
class Sku extends Backend
|
||||
{
|
||||
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\app\physical\Sku;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
|
||||
$goodsId = $this->request->param('goods_id');
|
||||
|
||||
if (!$goodsId) {
|
||||
$this->error('商品ID不能为空');
|
||||
}
|
||||
|
||||
$tree = $this->model->getTree($goodsId);
|
||||
|
||||
$this->success("获取成功", $tree);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
$params = $this->request->param('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
|
||||
$params['uniacid'] = UNIACID;
|
||||
$params['createtime'] = time();
|
||||
$params['updatetime'] = time();
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$result = $this->model->allowField(true)->save($params);
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
if ($result !== false) {
|
||||
$this->success("添加成功");
|
||||
} else {
|
||||
$this->error(__('No rows were inserted'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$params = $this->request->param('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
|
||||
$params['updatetime'] = time();
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$row = $this->model->where('id', $ids)->where('uniacid', UNIACID)->find();
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$result = $row->allowField(true)->save($params);
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
if ($result !== false) {
|
||||
$this->success("更新成功");
|
||||
} else {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if ($ids) {
|
||||
$count = $this->model->where('id', 'in', $ids)->where('uniacid', UNIACID)->delete();
|
||||
if ($count) {
|
||||
$this->success("删除成功");
|
||||
} else {
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新
|
||||
*/
|
||||
public function multi($ids = "")
|
||||
{
|
||||
$ids = $ids ? $ids : $this->request->param("ids");
|
||||
if ($ids) {
|
||||
if ($this->request->has('params')) {
|
||||
parse_str($this->request->post("params"), $values);
|
||||
$values = $this->preExcludeFields($values);
|
||||
$values['updatetime'] = time();
|
||||
$result = $this->model->where('id', 'in', $ids)->where('uniacid', UNIACID)->update($values);
|
||||
if ($result) {
|
||||
$this->success("操作成功");
|
||||
} else {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据商品ID获取规格树
|
||||
*/
|
||||
public function getTree()
|
||||
{
|
||||
$goodsId = $this->request->param('goods_id');
|
||||
if (!$goodsId) {
|
||||
$this->error('商品ID不能为空');
|
||||
}
|
||||
|
||||
$tree = $this->model->getTree($goodsId);
|
||||
$this->success("获取成功", $tree);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\app\physical;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* SKU价格管理
|
||||
*/
|
||||
class SkuPrice extends Backend
|
||||
{
|
||||
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\app\physical\SkuPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
|
||||
$goodsId = $this->request->param('goods_id');
|
||||
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
$query = $this->model
|
||||
->where($where)
|
||||
->where('uniacid', UNIACID)
|
||||
->order($sort, $order);
|
||||
|
||||
if ($goodsId) {
|
||||
$query->where(['goods_id' => $goodsId]);
|
||||
}
|
||||
|
||||
$list = $query->paginate($limit);
|
||||
|
||||
$result = array("total" => $list->total(), "rows" => $list->items(), 'attr'=>[
|
||||
'status'=>$this->model->getStatusList()
|
||||
]);
|
||||
|
||||
return $this->success("获取成功",$result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
$params = $this->request->param('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params['uniacid'] = UNIACID;
|
||||
$params['createtime'] = time();
|
||||
$params['updatetime'] = time();
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$result = $this->model->allowField(true)->save($params);
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
if ($result !== false) {
|
||||
$this->success("添加成功");
|
||||
} else {
|
||||
$this->error(__('No rows were inserted'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$params = $this->request->param('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
|
||||
$params['updatetime'] = time();
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$row = $this->model->where('id', $ids)->where('uniacid', UNIACID)->find();
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$result = $row->allowField(true)->save($params);
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
if ($result !== false) {
|
||||
$this->success("更新成功");
|
||||
} else {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情
|
||||
*/
|
||||
public function detail($ids = null)
|
||||
{
|
||||
$row = $this->model->where('id', $ids)->where('uniacid', UNIACID)->find();
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
|
||||
$this->success("获取成功", $row);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if ($ids) {
|
||||
$count = $this->model->where('id', 'in', $ids)->where('uniacid', UNIACID)->delete();
|
||||
if ($count) {
|
||||
$this->success("删除成功");
|
||||
} else {
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新
|
||||
*/
|
||||
public function multi($ids = "")
|
||||
{
|
||||
$ids = $ids ? $ids : $this->request->param("ids");
|
||||
if ($ids) {
|
||||
if ($this->request->has('params')) {
|
||||
parse_str($this->request->post("params"), $values);
|
||||
$values = $this->preExcludeFields($values);
|
||||
$values['updatetime'] = time();
|
||||
$result = $this->model->where('id', 'in', $ids)->where('uniacid', UNIACID)->update($values);
|
||||
if ($result) {
|
||||
$this->success("操作成功");
|
||||
} else {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据商品ID获取SKU列表
|
||||
*/
|
||||
public function getByGoodsId()
|
||||
{
|
||||
$goodsId = $this->request->param('goods_id');
|
||||
if (!$goodsId) {
|
||||
$this->error('商品ID不能为空');
|
||||
}
|
||||
|
||||
$list = $this->model->where('goods_id', $goodsId)->where('uniacid', UNIACID)->select();
|
||||
$this->success("获取成功", $list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新库存
|
||||
*/
|
||||
public function updateStock()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
$skus = $params['skus'] ?? [];
|
||||
|
||||
if (empty($skus)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'skus'));
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
foreach ($skus as $sku) {
|
||||
if (isset($sku['id']) && isset($sku['stock'])) {
|
||||
$this->model->where('id', $sku['id'])->update([
|
||||
'stock' => $sku['stock'],
|
||||
'updatetime' => time()
|
||||
]);
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
$this->success("更新成功");
|
||||
} catch (PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新价格
|
||||
*/
|
||||
public function updatePrice()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
$skus = $params['skus'] ?? [];
|
||||
|
||||
if (empty($skus)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'skus'));
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
foreach ($skus as $sku) {
|
||||
if (isset($sku['id'])) {
|
||||
$updateData = ['updatetime' => time()];
|
||||
if (isset($sku['price'])) {
|
||||
$updateData['price'] = $sku['price'];
|
||||
}
|
||||
if (isset($sku['original_price'])) {
|
||||
$updateData['original_price'] = $sku['original_price'];
|
||||
}
|
||||
if (isset($sku['cost_price'])) {
|
||||
$updateData['cost_price'] = $sku['cost_price'];
|
||||
}
|
||||
$this->model->where('id', $sku['id'])->update($updateData);
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
$this->success("更新成功");
|
||||
} catch (PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取状态列表
|
||||
*/
|
||||
public function getStatusList()
|
||||
{
|
||||
$this->success("获取成功", $this->model->getStatusList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user