feat(store): 添加价格变更日志功能

- 新增价格变更日志模型、控制器、列表、逻辑和验证器
- 在商品编辑时记录价格变更日志
- 实现价格变更日志的添加、编辑、删除和详情功能
This commit is contained in:
mkm 2025-01-10 17:24:37 +08:00
parent 52e7ac5e2b
commit dbb7b9e4c8
9 changed files with 423 additions and 3 deletions

View File

@ -237,10 +237,19 @@ class StoreProductLogic extends BaseLogic
$rose=bcmul($price_div, 100, 2);
}
$data['rose']=$rose;
StoreProduct::update($data, ['id' => $params['id']]);
$find=StoreProduct::where(['id' => $params['id']])->find();
if($find['purchase']!=$params['purchase']){
SqlChannelPriceLog($params['id'],21,$find['purchase'],$params['purchase'],);
}
if($find['cost']!=$params['cost']){
SqlChannelPriceLog($params['id'],4,$find['cost'],$params['cost'],);
}
if($find['price']!=$params['price']){
SqlChannelPriceLog($params['id'],22,$find['price'],$params['price'],);
}
$find->save($data);
// 修改活动专区商品
(new ActivityZoneLogic())->updateProduct($params['id'], $data);
// $dealCate = self::dealChangeCate($params['cate_id']);
//修改
StoreBranchProduct::where('product_id', $params['id'])->whereNotIn('store_id', [17, 18])->update([
@ -268,6 +277,7 @@ class StoreProductLogic extends BaseLogic
return true;
} catch (\Throwable $e) {
Db::rollback();
d($e);
throw new BusinessException('编辑商品失败' . $e->getMessage());
}
}

View File

@ -68,7 +68,7 @@ class IndexController extends BaseApiController
// }
// $a=StoreOrderCartInfo::where('store_id',2)->whereBetweenTime('create_time','2025-01-01','2025-01-8')->select()->toArray();
// d($a);
DemoLogic::test();
// DemoLogic::test();
d(1);
$arr = Db::name('ceshi_copy')->select();
foreach ($arr as $k => $v) {

View File

@ -0,0 +1,20 @@
<?php
namespace app\common\logic;
use app\common\model\change_price_log\ChangePriceLog;
class ChangePriceLogLogic extends BaseLogic
{
public function insert($product_id=0, $group_id=0, $before_price=0,$after_price=0):void
{
ChangePriceLog::create([
'product_id' => $product_id,
'group_id' => $group_id,
'before_price' => $before_price,
'after_price' => $after_price,
'create_time' => time()
]);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace app\common\model\change_price_log;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
/**
* 价格变更记录模型
* Class ChangePriceLog
* @package app\common\model\change_price_log
*/
class ChangePriceLog extends BaseModel
{
use SoftDelete;
protected $name = 'change_price_log';
protected $deleteTime = 'delete_time';
// 不生成该表的日志
public $doNotRecordLog = true;
}

View File

@ -5,6 +5,7 @@
*/
use app\common\logic\ChangeLogLogic;
use app\common\logic\ChangePriceLogLogic;
use app\common\service\FileService;
use support\Log;
@ -559,6 +560,16 @@ function SqlChannelLog($model='', $id=0, $nums=0,$pm=0,$url='',$admin_id=0):void
{
(new ChangeLogLogic())->insert($model, $id, $nums, $pm, $url,$admin_id);
}
/**
* 价格日志记录
* @param model 模型
* @param id 更新的模型组件id
* @param nums 更新的数量
*/
function SqlChannelPriceLog($product_id=0, $group_id=0, $before_price=0,$after_price=0):void
{
(new ChangePriceLogLogic())->insert($product_id, $group_id, $before_price, $after_price);
}
// if (!function_exists('getNewOrderSn')) {
// /**

View File

@ -0,0 +1,95 @@
<?php
namespace app\store\controller\change_price_log;
use app\store\lists\change_price_log\ChangePriceLogLists;
use app\store\logic\change_price_log\ChangePriceLogLogic;
use app\store\validate\change_price_log\ChangePriceLogValidate;
use app\store\controller\BaseAdminController;
/**
* 价格变更记录控制器
* Class ChangePriceLogController
* @package app\store\controller\change_price_log
*/
class ChangePriceLogController extends BaseAdminController
{
/**
* @notes 获取价格变更记录列表
* @return \think\response\Json
* @author admin
* @date 2025/01/10 15:54
*/
public function lists()
{
return $this->dataLists(new ChangePriceLogLists());
}
/**
* @notes 添加价格变更记录
* @return \think\response\Json
* @author admin
* @date 2025/01/10 15:54
*/
public function add()
{
$params = (new ChangePriceLogValidate())->post()->goCheck('add');
$result = ChangePriceLogLogic::add($params);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(ChangePriceLogLogic::getError());
}
/**
* @notes 编辑价格变更记录
* @return \think\response\Json
* @author admin
* @date 2025/01/10 15:54
*/
public function edit()
{
$params = (new ChangePriceLogValidate())->post()->goCheck('edit');
$result = ChangePriceLogLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(ChangePriceLogLogic::getError());
}
/**
* @notes 删除价格变更记录
* @return \think\response\Json
* @author admin
* @date 2025/01/10 15:54
*/
public function delete()
{
$params = (new ChangePriceLogValidate())->post()->goCheck('delete');
ChangePriceLogLogic::delete($params);
return $this->success('删除成功', [], 1, 1);
}
/**
* @notes 获取价格变更记录详情
* @return \think\response\Json
* @author admin
* @date 2025/01/10 15:54
*/
public function detail()
{
$params = (new ChangePriceLogValidate())->goCheck('detail');
$result = ChangePriceLogLogic::detail($params);
return $this->data($result);
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace app\store\lists\change_price_log;
use app\common\lists\BaseDataLists;
use app\common\model\change_price_log\ChangePriceLog;
use app\common\lists\ListsSearchInterface;
use app\common\model\store_product\StoreProduct;
/**
* 价格变更记录列表
* Class ChangePriceLogLists
* @package app\store\listschange_price_log
*/
class ChangePriceLogLists extends BaseDataLists implements ListsSearchInterface
{
public $ids;
/**
* @notes 设置搜索条件
* @return \string[][]
* @author admin
* @date 2025/01/10 15:54
*/
public function setSearch(): array
{
return [
'=' => ['product_id', 'group_id', 'before_price', 'after_price'],
];
}
/**
* @notes 获取价格变更记录列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author admin
* @date 2025/01/10 15:54
*/
public function lists(): array
{
if ($this->request->get('store_name')) {
$store_name = $this->request->get('store_name');
$ids = StoreProduct::where('store_name', 'like', '%' . $store_name . '%')->where('status',1)->column('id');
if ($ids) {
$this->searchWhere[] = ['product_id', 'in', $ids];
$this->ids = $ids;
} else {
return [];
}
}
return ChangePriceLog::where($this->searchWhere)
->field(['id', 'product_id', 'group_id', 'before_price', 'after_price'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()
->toArray();
}
/**
* @notes 获取价格变更记录数量
* @return int
* @author admin
* @date 2025/01/10 15:54
*/
public function count(): int
{
if($this->request->get('store_name')){
if($this->ids<=0){
return 0;
}
}
return ChangePriceLog::where($this->searchWhere)->count();
}
}

View File

@ -0,0 +1,99 @@
<?php
namespace app\store\logic\change_price_log;
use app\common\model\change_price_log\ChangePriceLog;
use app\common\logic\BaseLogic;
use support\exception\BusinessException;
use think\facade\Db;
/**
* 价格变更记录逻辑
* Class ChangePriceLogLogic
* @package app\store\logic\change_price_log
*/
class ChangePriceLogLogic extends BaseLogic
{
/**
* @notes 添加价格变更记录
* @param array $params
* @return bool
* @author admin
* @date 2025/01/10 15:54
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
ChangePriceLog::create([
'product_id' => $params['product_id'],
'group_id' => $params['group_id'],
'before_price' => $params['before_price'],
'after_price' => $params['after_price']
]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 编辑价格变更记录
* @param array $params
* @return bool
* @author admin
* @date 2025/01/10 15:54
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
ChangePriceLog::where('id', $params['id'])->update([
'product_id' => $params['product_id'],
'group_id' => $params['group_id'],
'before_price' => $params['before_price'],
'after_price' => $params['after_price']
]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 删除价格变更记录
* @param array $params
* @return bool
* @author admin
* @date 2025/01/10 15:54
*/
public static function delete(array $params): bool
{
return ChangePriceLog::destroy($params['id']);
}
/**
* @notes 获取价格变更记录详情
* @param $params
* @return array
* @author admin
* @date 2025/01/10 15:54
*/
public static function detail($params): array
{
return ChangePriceLog::findOrEmpty($params['id'])->toArray();
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace app\store\validate\change_price_log;
use app\common\validate\BaseValidate;
/**
* 价格变更记录验证器
* Class ChangePriceLogValidate
* @package app\store\validate\change_price_log
*/
class ChangePriceLogValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'id' => 'require',
];
/**
* 参数描述
* @var string[]
*/
protected $field = [
'id' => 'id',
];
/**
* @notes 添加场景
* @return ChangePriceLogValidate
* @author admin
* @date 2025/01/10 15:54
*/
public function sceneAdd()
{
return $this->remove('id', true);
}
/**
* @notes 编辑场景
* @return ChangePriceLogValidate
* @author admin
* @date 2025/01/10 15:54
*/
public function sceneEdit()
{
return $this->only(['id']);
}
/**
* @notes 删除场景
* @return ChangePriceLogValidate
* @author admin
* @date 2025/01/10 15:54
*/
public function sceneDelete()
{
return $this->only(['id']);
}
/**
* @notes 详情场景
* @return ChangePriceLogValidate
* @author admin
* @date 2025/01/10 15:54
*/
public function sceneDetail()
{
return $this->only(['id']);
}
}