You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
7.0 KiB
225 lines
7.0 KiB
<?php
|
|
|
|
namespace App\AdminSupplier\Controllers;
|
|
|
|
use App\AdminAgent\Lazys\DemandBiddingLazys;
|
|
use App\Models\AgentProduct;
|
|
use App\Models\DemandProduct;
|
|
use App\Models\Product;
|
|
use App\Traits\ResponseHelper;
|
|
use App\AdminAgent\Repositories\Demand;
|
|
use App\Models\DemandBidding;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Show;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use App\Traits\DemandTraits;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\URL;
|
|
|
|
class DemandController extends AdminController
|
|
{
|
|
|
|
use ResponseHelper;
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new Demand(['publisher','biddingUser']), function (Grid $grid) {
|
|
if (\request('self',0)) {
|
|
$grid->model()->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[1]]);
|
|
}
|
|
$grid->column('id')->sortable();
|
|
$grid->column('title');
|
|
$grid->column('detail','内容')->display('查看')->modal('详情',function ($modal) {
|
|
$modal->xl();
|
|
return $this->comment;
|
|
});
|
|
$grid->column('images','图片')->display(function ($image) {
|
|
return json_decode($image,true);
|
|
})->image();
|
|
$grid->column('deadline');
|
|
$grid->column('publisher.name','发布人');
|
|
$grid->column('publisher_type')->using(DemandTraits::$polymorphic);
|
|
$grid->column('biddingUser.name','中标人');
|
|
$grid->column('bidding_user_type','竞标用户类型')->using(DemandTraits::$polymorphic);
|
|
$grid->column('price');
|
|
$grid->column('stock');
|
|
$grid->column('state')->using(DemandTraits::$state)->dot(
|
|
[
|
|
1 => 'yellow',
|
|
2 => 'success',
|
|
3 => 'danger',
|
|
]
|
|
);
|
|
|
|
if (\request('self',0)) {
|
|
$grid->column('bidding','竞标明细')
|
|
->display(function (){
|
|
return '竞标数 : '. DemandBidding::query()->where('demand_id',$this->id)->count();
|
|
})
|
|
->modal('竞标明细',function () {
|
|
return DemandBiddingLazys::make(['demand_id' => $this->id]);
|
|
});
|
|
}else{
|
|
$grid->column('bidding','竞标')
|
|
->if(function (){
|
|
return $this->state == 1 && $this->bidding_user_type == DemandTraits::$col[1];
|
|
})
|
|
->then(function (Grid\Column $column) {
|
|
$column->append('<a class="btn btn-sm btn-primary" href="'.admin_url('/demand_bidding/create?demand_id='.$this->id).'">发起竞标</a>');
|
|
});
|
|
}
|
|
|
|
$grid->column('created_at')->sortable();
|
|
$grid->disableDeleteButton();
|
|
$grid->disableEditButton();
|
|
$grid->disableQuickEditButton();
|
|
$grid->disableViewButton();
|
|
$grid->disableActions();
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$filter->equal('id');
|
|
$filter->equal('bidding_user_type','竞标用户类型')->select(DemandTraits::$polymorphic);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new Demand(['publisher','biddingUser']), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('title');
|
|
$show->field('comment');
|
|
$show->field('images')->image();
|
|
$show->field('deadline');
|
|
$show->field('bidding_user_type','竞标用户类型')->using(DemandTraits::$polymorphic);
|
|
$show->field('price');
|
|
$show->field('stock');
|
|
$show->field('publisher_type')->using(DemandTraits::$polymorphic);
|
|
$show->field('publisher.name','发布人');
|
|
$show->field('state')->using(DemandTraits::$state)->dot(
|
|
[
|
|
1 => 'yellow',
|
|
2 => 'danger',
|
|
3 => 'success',
|
|
]
|
|
);;
|
|
$show->field('created_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new Demand(), function (Form $form) {
|
|
$form->disableEditingCheck();
|
|
$form->disableCreatingCheck();
|
|
$form->disableViewCheck();
|
|
$form->display('id');
|
|
$form->text('title');
|
|
$form->text('comment');
|
|
$form->multipleImage('images','图片')->limit(5)->saving(function ($path) {
|
|
return json_encode($path);
|
|
});
|
|
$form->hidden('deadline');
|
|
$form->select('bidding_user_type','竞标用户类型')
|
|
->when([0],function (Form $form){
|
|
$form->select('demand_product_id','产品')->options(function (){
|
|
return DemandProduct::query()->where('supplier_id',Admin::user()->id)->where('status',1)->pluck('title','id');
|
|
});
|
|
})
|
|
->options([
|
|
'代理商'
|
|
])
|
|
->default(0);
|
|
$form->decimal('price');
|
|
$form->number('stock');
|
|
$form->hidden('publisher_type');
|
|
$form->hidden('publisher_id');
|
|
$form->saving(function (Form $form) {
|
|
// 判断是否是新增操作
|
|
if ($form->isCreating()) {
|
|
//if ($form->bidding_user_type != 2) {
|
|
// $form->demand_product_id = 0;
|
|
//}
|
|
$form->bidding_user_type = DemandTraits::$col[$form->bidding_user_type];
|
|
//处理流拍时间
|
|
$form->deadline = now()->addDays(5);
|
|
//发布人身份
|
|
$form->publisher_type = DemandTraits::$col[1];
|
|
$form->publisher_id = Admin::user()->id;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
public function binding()
|
|
{
|
|
$demandBiddingId = request('demand_bidding_id',0);
|
|
$demandBidding = DemandBidding::find($demandBiddingId);
|
|
if (empty($demandBidding)) {
|
|
return false;
|
|
}
|
|
|
|
$demand = \App\Models\Demand::find($demandBidding->demand_id);
|
|
|
|
if (empty($demand)) {
|
|
return false;
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
|
|
$demandBidding->state = 1;
|
|
$demandBidding->save();
|
|
//改变订单状态
|
|
$demand->bidding_id = $demandBidding->id;
|
|
$demand->bidding_user_id = $demandBidding->bidding_user_id;
|
|
$demand->state = DemandTraits::$stateKey[1];
|
|
$demand->save();
|
|
//将产品绑给代理商
|
|
$demandProduct = DemandProduct::find($demand->demand_product_id);
|
|
$product = new Product();
|
|
|
|
$product->supplier_id = $demandProduct->supplier_id;
|
|
$product->category_id = $demandProduct->category_id;
|
|
$product->title = $demandProduct->title;
|
|
$product->price = $demandBidding->price;
|
|
$product->original_price = $demandProduct->original_price;
|
|
$product->pictures = $demandProduct->pictures;
|
|
$product->stock = $demand->stock;
|
|
$product->know = $demandProduct->know;
|
|
$product->content = $demandProduct->content;
|
|
$product->agent_id = $demandBidding->bidding_user_id;
|
|
$product->save();
|
|
|
|
$demand->product_id = $product->id;
|
|
$demand->save();
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
Log::error('选中竞标失败::'.$e->getTraceAsString());
|
|
DB::rollBack();
|
|
return $this->jsonFail(1001,'选中竞标失败,稍后重试或联系管理员!'.$e->getMessage());
|
|
}
|
|
return back();
|
|
}
|
|
}
|