189 lines
6.7 KiB
PHP
189 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\BiddingOrder;
|
|
use app\common\model\Photographer;
|
|
use app\common\model\Requirement;
|
|
use app\common\model\ServiceType;
|
|
use app\constant\ResponseCode;
|
|
use think\Exception;
|
|
|
|
class RequirementBiddingService
|
|
{
|
|
|
|
/**
|
|
* @desc 报价
|
|
* @param $request
|
|
* @return array
|
|
*/
|
|
public function bid($request)
|
|
{
|
|
try {
|
|
$data = $request->post();
|
|
$requirement = Requirement::where(['id' => $data['requirement_id']])->findOrEmpty();
|
|
if ($requirement->isEmpty()) {
|
|
throw new Exception('报价需求不存在');
|
|
}
|
|
if ($requirement->status != 1) {
|
|
throw new Exception('当前需求已不在报价阶段,无法报价');
|
|
}
|
|
$photographer = Photographer::where(['user_id' => $request->user->id])->findOrEmpty();
|
|
if ($photographer->isEmpty()) {
|
|
throw new Exception('未找到您的摄影师信息,不能报价');
|
|
}
|
|
$bidding_order = BiddingOrder::where(['user_id' => $photographer->user_id, 'requirement_id' => $requirement->id])->findOrEmpty();
|
|
if (!$bidding_order->isEmpty()) {
|
|
throw new Exception('你已存在报价记录,不能再次报价');
|
|
}
|
|
if (!isset($data['bidding_price']) || $data['bidding_price'] <= 0) {
|
|
throw new Exception('请输入正确的报价价格');
|
|
}
|
|
|
|
$res = BiddingOrder::create([
|
|
'user_id' => $request->user->id,
|
|
'photographer_id' => $photographer->id,
|
|
'requirement_id' => $requirement->id,
|
|
'requirement_user_id' => $requirement->user_id,
|
|
'requirement_detail' => json_encode($requirement->toArray()),
|
|
'requirement_price' => $requirement->price,
|
|
'bidding_price' => $data['bidding_price'],
|
|
]);
|
|
if (!$res) {
|
|
throw new Exception('报价失败');
|
|
}
|
|
|
|
return [
|
|
'code' => ResponseCode::SUCCESS,
|
|
'msg' => '报价成功',
|
|
];
|
|
|
|
} catch (Exception $e) {
|
|
return [
|
|
'code' => ResponseCode::FAIL,
|
|
'msg' => $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 获取用户需求的出价列表
|
|
* @param $request
|
|
* @return array
|
|
*/
|
|
public function getUserRequirementBiddingOrderList($request)
|
|
{
|
|
try {
|
|
$data = $request->get();
|
|
$bidding_order = BiddingOrder::where(['requirement_user_id' => $request->user->id, 'requirement_id' => $data['requirement_id']])->order('requirement_id desc,id desc');
|
|
|
|
$page = isset($data['page']) ? $data['page'] : 1;
|
|
$limit = isset($data['limit']) ? $data['limit'] : 10;
|
|
$total = $bidding_order->count();
|
|
$list = $bidding_order->with(['photographer'])
|
|
->field('id,user_id,photographer_id,requirement_id,requirement_price,bidding_price,bind_status,created_at')
|
|
->page($page, $limit)
|
|
->select();
|
|
|
|
foreach ($list as &$item) {
|
|
if ($item['photographer']) {
|
|
$service_type_name = '';
|
|
if($item['photographer']['service_type']){
|
|
$service_type_name = ServiceType::where([
|
|
'id'=>explode(',',$item['photographer']['service_type'])
|
|
])
|
|
->field('service_name')
|
|
->order('sort desc, id desc')
|
|
->select();
|
|
if($service_type_name){
|
|
$service_type_name = array_column($service_type_name->toArray(),'service_name');
|
|
}
|
|
}
|
|
$item['photographer']['service_type_name'] = $service_type_name;
|
|
}
|
|
}
|
|
|
|
$requirement = Requirement::where(['id' => $data['requirement_id']])->with('serviceType')->findOrEmpty();
|
|
return [
|
|
'code' => ResponseCode::SUCCESS,
|
|
'data' => [
|
|
'list' => $list,
|
|
'requirement' => $requirement,
|
|
'total' => $total,
|
|
'page' => $page
|
|
]
|
|
];
|
|
|
|
} catch (Exception $e) {
|
|
return [
|
|
'code' => ResponseCode::FAIL,
|
|
'msg' => $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 确定报价
|
|
* @param $request
|
|
* @return array|void
|
|
*/
|
|
public function confirmBid($request)
|
|
{
|
|
try {
|
|
$data = $request->post();
|
|
$bid_order = BiddingOrder::where(['id' => $data['bidding_order_id']])->findOrEmpty();
|
|
if ($bid_order->isEmpty()) {
|
|
throw new Exception('竞价订单不存在');
|
|
}
|
|
|
|
$requirement = Requirement::where(['id' => $bid_order->requirement_id])->findOrEmpty();
|
|
if ($requirement->isEmpty()) {
|
|
throw new Exception('需求不存在');
|
|
}
|
|
if ($requirement->status != 1) {
|
|
throw new Exception('需求不在出价中,无法确定报价');
|
|
}
|
|
if ($request->user->id != $bid_order->requirement_user_id) {
|
|
throw new Exception('非需求本人无法确定报价');
|
|
}
|
|
|
|
//计算定金
|
|
$ratio = RatioService::ratio();
|
|
$deposit_amount = 0;
|
|
if($requirement->is_need_deposit){
|
|
$deposit_amount = round($ratio * $bid_order->bidding_price / 100, 2);
|
|
}
|
|
|
|
//需求表
|
|
$requirement->save([
|
|
'bidding_order_id' => $bid_order->id,
|
|
'bid_win_user_id' => $bid_order->user_id,
|
|
'bid_win_photographer_id' => $bid_order->photographer_id,
|
|
'final_price' => $bid_order->bidding_price,
|
|
'deposit_amount' => $deposit_amount,
|
|
'status' => 2
|
|
]);
|
|
//竞价表
|
|
$bid_order->save([
|
|
'bind_status' => 1
|
|
]);
|
|
//更新其他竞价数据为【未中标】
|
|
BiddingOrder::where(['requirement_id' => $bid_order->requirement_id])->where('id', '<>', $bid_order->id)->save([
|
|
'bind_status' => 2
|
|
]);
|
|
|
|
return [
|
|
'code' => ResponseCode::SUCCESS,
|
|
'msg' => '确定出价完成,请尽快完成支付',
|
|
];
|
|
} catch (Exception $e) {
|
|
return [
|
|
'code' => ResponseCode::FAIL,
|
|
'msg' => $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
} |