|
|
|
@ -5,15 +5,12 @@ namespace App\AdminSupplier\Controllers; |
|
|
|
use App\AdminSupplier\Repositories\IndustryProduct; |
|
|
|
use App\Common\ProductStatus; |
|
|
|
use App\Models\Category; |
|
|
|
use App\Models\Supplier; |
|
|
|
use App\Models\SystemSetting; |
|
|
|
use Dcat\Admin\Admin; |
|
|
|
use Dcat\Admin\Form; |
|
|
|
use Dcat\Admin\Form\NestedForm; |
|
|
|
use Dcat\Admin\Grid; |
|
|
|
use Dcat\Admin\Show; |
|
|
|
use Dcat\Admin\Http\Controllers\AdminController; |
|
|
|
use Illuminate\Support\Facades\DB; |
|
|
|
|
|
|
|
class IndustryProductController extends AdminController |
|
|
|
{ |
|
|
|
@ -39,13 +36,19 @@ class IndustryProductController extends AdminController |
|
|
|
$grid->column('original_price'); |
|
|
|
$grid->column('stock'); |
|
|
|
$grid->column('sale'); |
|
|
|
$grid->column('status') |
|
|
|
/*->if(fn() => in_array($this->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE])) |
|
|
|
$grid->column('status')->help('切换开关可改变上下架状态') |
|
|
|
->if(fn() => in_array($this->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE])) |
|
|
|
->using([ProductStatus::SOLD_OUT => 0, ProductStatus::ON_SALE => 1]) |
|
|
|
->switch() |
|
|
|
->else()*/ |
|
|
|
->using(ProductStatus::array()); |
|
|
|
$grid->column('service_persons'); |
|
|
|
->else() |
|
|
|
->using(ProductStatus::array()) |
|
|
|
->dot([ |
|
|
|
ProductStatus::ON_SALE => 'success', |
|
|
|
ProductStatus::UNAUDITED => '', |
|
|
|
ProductStatus::REFUSE => 'danger', |
|
|
|
ProductStatus::SOLD_OUT => 'warning', |
|
|
|
], 'primary'); |
|
|
|
$grid->column('single_deposit'); |
|
|
|
$grid->column('min_sale'); |
|
|
|
$grid->column('created_at'); |
|
|
|
|
|
|
|
@ -85,7 +88,7 @@ class IndustryProductController extends AdminController |
|
|
|
$show->field('status')->using(ProductStatus::array()); |
|
|
|
$show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v)); |
|
|
|
$show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v)); |
|
|
|
$show->field('service_persons'); |
|
|
|
$show->field('single_deposit'); |
|
|
|
$show->field('min_sale'); |
|
|
|
$show->field('verify_mobile'); |
|
|
|
$show->field('created_at'); |
|
|
|
@ -102,10 +105,6 @@ class IndustryProductController extends AdminController |
|
|
|
{ |
|
|
|
Admin::user()->publish_type = json_decode(Admin::user()->publish_type, true); |
|
|
|
|
|
|
|
//注:这2个变量需要通过引用方式传值才可以
|
|
|
|
$change_deposit = 0; //变动的
|
|
|
|
$old_deposit = 0; //原来交易金数
|
|
|
|
|
|
|
|
return Form::make(new IndustryProduct(), function (Form $form) { |
|
|
|
$form->disableDeleteButton(); |
|
|
|
|
|
|
|
@ -116,7 +115,6 @@ class IndustryProductController extends AdminController |
|
|
|
$form->text('title')->required(); |
|
|
|
$form->currency('price')->required(); |
|
|
|
$form->currency('original_price')->required(); |
|
|
|
$form->number('service_persons')->min(0)->required(); |
|
|
|
$form->number('stock')->required(); |
|
|
|
$form->number('min_sale')->min(1)->required(); |
|
|
|
if ($form->isEditing() && in_array($form->model()->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE])) { |
|
|
|
@ -216,7 +214,7 @@ class IndustryProductController extends AdminController |
|
|
|
$form->text('extends.field_5_address', '地址'); |
|
|
|
$form->map('extends.field_5_latitude', 'extends.field_5_longitude', '位置'); |
|
|
|
}); |
|
|
|
})->saving(function (Form $form) use (&$change_deposit, &$old_deposit) { |
|
|
|
})->saving(function (Form $form) { |
|
|
|
//不允许编辑非自己数据
|
|
|
|
if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) { |
|
|
|
return $form->response()->error('数据不存在'); |
|
|
|
@ -230,39 +228,24 @@ class IndustryProductController extends AdminController |
|
|
|
$form->deleteInput('type'); |
|
|
|
} |
|
|
|
|
|
|
|
//处理交易金
|
|
|
|
$single = SystemSetting::val('single', 'price'); //单人头交易费
|
|
|
|
if (!$single) { |
|
|
|
return $form->response()->error('未设置单人头交易费,请联系管理员在后台设置'); |
|
|
|
} |
|
|
|
|
|
|
|
//用户可编辑的状态
|
|
|
|
$user_status = [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE]; |
|
|
|
|
|
|
|
//目前逻辑不考虑总后台修改单人头交易费的情况
|
|
|
|
if ($form->isCreating()) { |
|
|
|
$change_deposit = $single * $form->service_persons * $form->stock; |
|
|
|
|
|
|
|
$form->hidden(['status', 'supplier_id']); |
|
|
|
$form->supplier_id = Admin::user()->id; |
|
|
|
$form->status = ProductStatus::UNAUDITED; |
|
|
|
} |
|
|
|
//库存和单库存服务用户数变动时冻结或解冻相应的保证金
|
|
|
|
else if ($form->isEditing()) { |
|
|
|
if (!in_array($form->model()->status, $user_status) || !in_array($form->status, $user_status)) { |
|
|
|
$form->deleteInput('status'); |
|
|
|
//列表切换开关
|
|
|
|
if (in_array($form->model()->status, $user_status) && !is_null($form->status) && $form->status == 0) { |
|
|
|
$form->status = ProductStatus::SOLD_OUT; |
|
|
|
} |
|
|
|
|
|
|
|
//只处理原来状态是下架或上架用户可编辑的状态,否则后台未审核又多次编辑将出错
|
|
|
|
if (in_array($form->model()->status, $user_status)) { |
|
|
|
//原来的单库存服务用户数及库存计算出来的值,如果是未审核状态要返回去
|
|
|
|
$old_deposit = $single * $form->model()->service_persons * $form->model()->stock; |
|
|
|
|
|
|
|
//编辑库存或单库存服务用户数
|
|
|
|
if ($form->model()->stock != $form->stock || $form->model()->service_persons != $form->service_persons) { |
|
|
|
$new_deposit = $single * $form->service_persons * $form->stock; //新的单库存服务用户数及库存计算出来的值
|
|
|
|
$change_deposit = $new_deposit - $old_deposit; //saved里面获取的$form->model()->stock是不对的
|
|
|
|
} |
|
|
|
//非用户可编辑状态不允许修改
|
|
|
|
else if (!in_array($form->model()->status, $user_status) || !in_array($form->status, $user_status)) { |
|
|
|
$form->deleteInput('status'); |
|
|
|
} |
|
|
|
|
|
|
|
//因extends是数组,具有一定的特殊性,不能直接用$form->saved方法中的wasChanged判断是否修改,所以只能在这里做特殊处理
|
|
|
|
@ -280,38 +263,16 @@ class IndustryProductController extends AdminController |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//判断可用交易金是否充足
|
|
|
|
if (Admin::user()->deposit_normal < $change_deposit) { |
|
|
|
return $form->response()->error('当前可用交易金:' . Admin::user()->deposit_normal . ",你还需要充值{$change_deposit}交易金才能发布此产品"); |
|
|
|
} |
|
|
|
$form->deleteInput('single_deposit'); //交易金单价只能是总后台设置
|
|
|
|
|
|
|
|
//忽略字段
|
|
|
|
$form->ignore(['id', 'sale', 'created_at', 'updated_at', 'deleted_at']); |
|
|
|
$form->ignore(['id', 'sale', 'created_at', 'updated_at', 'deleted_at', 'single_deposit']); |
|
|
|
})->saved(function (Form $form, $result) use (&$change_deposit, &$old_deposit) { |
|
|
|
if ($form->isEditing()) { |
|
|
|
$supplier = Supplier::query()->find(Admin::user()->id); //不能使用Admin::user()修改,必须使用Supplier模型才能正确记录资金变动日志
|
|
|
|
|
|
|
|
//如果修改了库存和单库存服务用户数
|
|
|
|
$is_change = $form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content']); //有extends判断不准
|
|
|
|
if ($is_change) { |
|
|
|
DB::beginTransaction(); |
|
|
|
try { |
|
|
|
//将产品状态改为未审核
|
|
|
|
$form->model()->update(['status' => ProductStatus::UNAUDITED]); |
|
|
|
|
|
|
|
//如果改变的交易金不是0,原来冻结过的交易金先全部返还,待总后台审核通过之后再做扣除处理
|
|
|
|
$supplier->deposit_normal = $supplier->deposit_normal + $old_deposit; //正常交易金
|
|
|
|
$supplier->deposit_frozen = $supplier->deposit_frozen - $old_deposit; //冻结交易金
|
|
|
|
$supplier->save(); |
|
|
|
|
|
|
|
DB::commit(); |
|
|
|
} catch (\Exception $e) { |
|
|
|
DB::rollBack(); |
|
|
|
} |
|
|
|
} else if ($change_deposit != 0) { //修改库存或单库存服务用户数,但不修改关键信息,增减交易金,$change_deposit无论是正数还是负数都一样计算
|
|
|
|
$supplier->deposit_normal = $supplier->deposit_normal - $change_deposit; //正常交易金
|
|
|
|
$supplier->deposit_frozen = $supplier->deposit_frozen + $change_deposit; //冻结交易金
|
|
|
|
$supplier->save(); |
|
|
|
$form->model()->update(['status' => ProductStatus::UNAUDITED]); |
|
|
|
} |
|
|
|
} |
|
|
|
})->deleting(function (Form $form) { |
|
|
|
|