Compare commits

...

1 Commits

Author SHA1 Message Date
3e8a8751fa 初始化临期打折、委托销售 2023-06-26 14:48:55 +08:00
6 changed files with 258 additions and 2 deletions

View File

@ -0,0 +1,38 @@
<?php
namespace app\common\model\store\activity;
use app\common\model\BaseModel;
class BusinessActivity extends BaseModel
{
const STATUS_WAIT = 0; //待(供应商/平台)接受
const STATUS_ACCEPT = 1; //(供应商/平台)已接受
const STATUS_CONFIRM = 11; //镇街店铺完成确认
const STATUS_REFUSE = 22; //(供应商/平台)拒绝接受
const STATUS_REFUSE_CONFIRM = 12; //镇街店铺拒绝确认
const STATUS_BREAK = 3; //已终止(到保质期/到委托周期)
const TYPE_ENTRUST = 1; //委托
const TYPE_DISCOUNT = 2; //临期折扣
public static function tablePk(): string
{
return 'id';
}
public static function tableName(): string
{
return 'business_activity';
}
public function isEntrust() {
return $this->type == self::TYPE_ENTRUST;
}
public function isDiscount() {
return $this->type == self::TYPE_DISCOUNT;
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace app\common\repositories\store\activity;
use app\common\model\store\activity\BusinessActivity;
abstract class ActivityRepository
{
const TYPE_MAP = [
BusinessActivity::TYPE_ENTRUST => 'entrust',
BusinessActivity::TYPE_DISCOUNT => 'discount',
];
/**
* @param $type
* @return ActivityRepository
* @throws \Exception
*/
public static function instance($type)
{
$type = self::TYPE_MAP[$type];
$class = 'app\common\repositories\store\activity\\' . ucfirst($type);
if (!class_exists($class)) {
throw new \Exception('请求类型错误');
}
return new $class();
}
abstract public function save($param);
abstract public function status($id, $status);
}

View File

@ -0,0 +1,34 @@
<?php
namespace app\common\repositories\store\activity;
use app\common\model\store\activity\BusinessActivity;
use app\common\repositories\store\product\ProductRepository;
class Discount extends ActivityRepository
{
public function save($param)
{
/** @var ProductRepository $productRepo */
$productRepo = app()->make(ProductRepository::class);
$product = $productRepo->get($param['product_id']);
if (empty($product)) {
throw new \Exception('商品不存在');
}
$data = [
'type' => BusinessActivity::TYPE_DISCOUNT,
'product_id' => $param['product_id'],
'to_mer_id' => 0,
'mer_id' => $param['mer_id'],
'price' => $param['price'],
'number' => $param['number'],
'other' => json_encode($param['other'] ?? []),
'update_time' => date('Y-m-d H:i:s'),
];
if (!BusinessActivity::create($data)) {
throw new \Exception('保存失败');
}
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace app\common\repositories\store\activity;
use app\common\dao\system\merchant\MerchantDao;
use app\common\model\store\activity\BusinessActivity;
use app\common\model\system\merchant\Merchant;
use app\common\repositories\store\product\ProductRepository;
use think\facade\Db;
class Entrust extends ActivityRepository
{
public function save($param)
{
/** @var ProductRepository $productRepo */
$productRepo = app()->make(ProductRepository::class);
$product = $productRepo->get($param['product_id']);
if (empty($product)) {
throw new \Exception('商品不存在');
}
/** @var MerchantDao $merchantDao */
$merchantDao = app()->make(MerchantDao::class);
$supplier = $merchantDao->apiGetOne($param['to_mer_id']);
if (empty($supplier) || $supplier['type_id'] != Merchant::TypeSupplyChain) {
throw new \Exception('供应商不存在');
}
if ($product->mer_id == $param['to_mer_id']) {
throw new \Exception('不能委托给自己');
}
$data = [
'type' => BusinessActivity::TYPE_ENTRUST,
'product_id' => $param['product_id'],
'to_mer_id' => $param['to_mer_id'],
'mer_id' => $product['mer_id'],
'price' => $param['price'],
'number' => $param['number'],
'other' => json_encode($param['other'] ?? []),
'update_time' => date('Y-m-d H:i:s'),
];
if (!BusinessActivity::create($data)) {
throw new \Exception('保存失败');
}
}
public function status($id, $status)
{
Db::startTrans();
try {
$data = BusinessActivity::getInstance()->find($id);
if (empty($data)) {
throw new \Exception('数据不存在');
}
if ($data['status'] != BusinessActivity::STATUS_WAIT) {
throw new \Exception('当前状态不支持操作');
}
$data->status = $status;
if (!$data->save()) {
throw new \Exception('操作失败');
}
Db::commit();
} catch (\Exception $e) {
Db::rollback();
throw new \Exception($e->getMessage());
}
}
}

View File

@ -0,0 +1,78 @@
<?php
namespace app\controller\api\server;
use app\common\model\store\activity\BusinessActivity;
use app\common\repositories\store\activity\ActivityRepository;
use crmeb\basic\BaseController;
class Activity extends BaseController
{
/**
* 提交活动申请
* @return mixed
* @throws \Exception
*/
public function create()
{
$param = $this->request->param();
$param['mer_id'] = $param['merId'];
$instance = ActivityRepository::instance($param['type']);
$instance->save($param);
return app('json')->success('提交成功');
}
/**
* 接受活动申请
* @return mixed
* @throws \Exception
*/
public function accept()
{
$param = $this->request->param();
$instance = ActivityRepository::instance($param['type']);
$instance->status($param['id'], BusinessActivity::STATUS_ACCEPT);
return app('json')->success('操作成功');
}
/**
* 拒绝活动申请
* @return mixed
* @throws \Exception
*/
public function refuse()
{
$param = $this->request->param();
$instance = ActivityRepository::instance($param['type']);
$instance->status($param['id'], BusinessActivity::STATUS_REFUSE);
return app('json')->success('操作成功');
}
/**
* 确认活动申请
* @return mixed
* @throws \Exception
*/
public function confirm()
{
$param = $this->request->param();
$instance = ActivityRepository::instance($param['type']);
$instance->status($param['id'], BusinessActivity::STATUS_CONFIRM);
return app('json')->success('操作成功');
}
/**
* 拒绝确认活动申请
* @return mixed
* @throws \Exception
*/
public function refuseConfirm()
{
$param = $this->request->param();
$instance = ActivityRepository::instance($param['type']);
$instance->status($param['id'], BusinessActivity::STATUS_REFUSE_CONFIRM);
return app('json')->success('操作成功');
}
}

View File

@ -287,8 +287,12 @@ Route::group('api/', function () {
Route::get('attr/detail/:id', 'StoreProductAttrTemplate/detail');
Route::get('attr/list', 'StoreProductAttrTemplate/getlist');
//活动
Route::post('activity/create', 'Activity/create');
Route::post('activity/accept', 'Activity/accept');
Route::post('activity/refuse', 'Activity/refuse');
Route::post('activity/confirm', 'Activity/confirm');
Route::post('activity/refuseConfirm', 'Activity/refuseConfirm');
})->prefix('api.server.')->middleware(\app\common\middleware\MerchantServerMiddleware::class,1);
//管理员订单