|
@@ -11,13 +11,16 @@
|
|
|
|
|
|
namespace App\Services\Api;
|
|
|
|
|
|
+use App\Models\AccountLogModel;
|
|
|
use App\Models\AccountModel;
|
|
|
use App\Models\GoodsModel;
|
|
|
use App\Models\MechanicModel;
|
|
|
use App\Models\MemberCouponModel;
|
|
|
use App\Models\MemberModel;
|
|
|
+use App\Models\MerchantModel;
|
|
|
use App\Models\OrderGoodsModel;
|
|
|
use App\Models\OrderModel;
|
|
|
+use App\Models\PaymentModel;
|
|
|
use App\Models\ScoreGoodsModel;
|
|
|
use App\Services\BaseService;
|
|
|
use App\Services\ConfigService;
|
|
@@ -69,7 +72,6 @@ class OrderService extends BaseService
|
|
|
$where = ['a.mark' => 1];
|
|
|
$shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
|
|
|
$parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
|
|
|
- $type = isset($params['type']) ? $params['type'] : 0;
|
|
|
if ($shopId > 0) {
|
|
|
$where['a.shop_id'] = $shopId;
|
|
|
}
|
|
@@ -120,7 +122,7 @@ class OrderService extends BaseService
|
|
|
if ($list) {
|
|
|
$refundStatusArr = [1=>'退款中',2=>'已退款',3=>'退款失败'];
|
|
|
$statusArr = [1=>'待付款',2=>'待发货',3=>'待收货',4=>'已完成',5=>'已退款'];
|
|
|
- $serviceStatusArr = [1=>'待付款',2=>'待接单',3=>'待收货',4=>'已完成',5=>'已退款'];
|
|
|
+ $serviceStatusArr = [1=>'待付款',2=>'待接单',3=>'待服务',4=>'已完成',5=>'已退款'];
|
|
|
foreach ($list['data'] as &$item) {
|
|
|
$item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
|
|
|
$item['pay_time'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i:s') : '';
|
|
@@ -142,6 +144,7 @@ class OrderService extends BaseService
|
|
|
if($refundStatus){
|
|
|
$item['refund_status_text'] = isset($refundStatusArr[$refundStatus])? $refundStatusArr[$refundStatus] : '';
|
|
|
}
|
|
|
+
|
|
|
$goods = isset($item['goods'])? $item['goods'] : [];
|
|
|
if($goods){
|
|
|
foreach ($goods as &$v){
|
|
@@ -165,6 +168,252 @@ class OrderService extends BaseService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 购买店内商品/结算
|
|
|
+ * @param $userId
|
|
|
+ * @param $params
|
|
|
+ * @return array|false
|
|
|
+ */
|
|
|
+ public function buy($userId, $params)
|
|
|
+ {
|
|
|
+ $goodsId = isset($params['goods_id'])? intval($params['goods_id']) : 0;
|
|
|
+ $sourceId = isset($params['source_id'])? intval($params['source_id']) : 0;
|
|
|
+ $payType = isset($params['pay_type'])? intval($params['pay_type']) : 0;
|
|
|
+ $deliveryType = isset($params['delivery_type'])? intval($params['delivery_type']) : 0;
|
|
|
+ $type = isset($params['type'])? intval($params['type']) : 0;
|
|
|
+ $couponId = isset($params['coupon_id'])? intval($params['coupon_id']) : 0;
|
|
|
+ $num = isset($params['num'])? intval($params['num']) : 0;
|
|
|
+ $day = isset($params['day'])? trim($params['day']) : '';
|
|
|
+ $time = isset($params['time_text'])? trim($params['time_text']) : '';
|
|
|
+ $address = isset($params['address'])? trim($params['address']) : '';
|
|
|
+ $province = isset($params['province'])? trim($params['province']) : '';
|
|
|
+ $city = isset($params['city'])? trim($params['city']) : '';
|
|
|
+ $district = isset($params['district'])? trim($params['district']) : '';
|
|
|
+ $realname = isset($params['realname'])? trim($params['realname']) : '';
|
|
|
+ $mobile = isset($params['mobile'])? trim($params['mobile']) : '';
|
|
|
+ if(empty($goodsId) || empty($sourceId) || $num<=0 || $deliveryType<=0 || $payType<=0 || $type != 2){
|
|
|
+ $this->error = 2501;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证收货地址
|
|
|
+ if($deliveryType == 2 && (empty($address) || empty($city) || empty($mobile) || empty($realname))){
|
|
|
+ $this->error = 2601;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 服务信息
|
|
|
+ $goodsInfo = GoodsModel::where(['id'=> $goodsId,'mark'=>1,'status'=>2])
|
|
|
+ ->select(['id as goods_id','goods_name','cate_id','merch_id','type','price','unit','sku','attr','weight','discount','service_fee','thumb','status'])
|
|
|
+ ->first();
|
|
|
+ $goodsInfo = $goodsInfo? $goodsInfo->toArray() : [];
|
|
|
+ $merchId = isset($goodsInfo['merch_id'])? intval($goodsInfo['merch_id']) : 0;
|
|
|
+ $price = isset($goodsInfo['price'])? floatval($goodsInfo['price']) : 0;
|
|
|
+ $discount = isset($goodsInfo['discount'])? floatval($goodsInfo['discount']) : 0;
|
|
|
+ $serviceFee = isset($goodsInfo['service_fee'])? floatval($goodsInfo['service_fee']) : 0;
|
|
|
+ if(empty($goodsInfo) || $merchId<=0){
|
|
|
+ $this->error = 2542;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证是否存在该技师已支付未接单订单
|
|
|
+ if($this->model->where(['source_id'=>$sourceId,'user_id'=> $userId,'type'=>2,'mark'=>1])->whereIn('status',[2,3])->value('id')){
|
|
|
+ $this->error = 2612;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证技师
|
|
|
+ $mechanicInfo = MechanicModel::where(['id'=> $sourceId,'mark'=>1,'status'=>2])
|
|
|
+ ->select(['id','code','nickname','status','services'])
|
|
|
+ ->first();
|
|
|
+ $mechanicInfo = $mechanicInfo? $mechanicInfo->toArray() : [];
|
|
|
+ $services = isset($mechanicInfo['services']) && $mechanicInfo['services']? explode(',', $mechanicInfo['services']) : [];
|
|
|
+ if(empty($mechanicInfo)){
|
|
|
+ $this->error = 2602;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 技师服务项目
|
|
|
+ if(empty($services) || !in_array($goodsId, $services)){
|
|
|
+ $this->error = 2603;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 技师接单状态
|
|
|
+ if($this->model->where(['source_id'=> $sourceId,'type'=>2,'mark'=>1,'status'=>3])->value('id')){
|
|
|
+ $this->error = 2604;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $userInfo = MemberModel::where(['id'=> $userId,'mark'=> 1,'status'=> 1])
|
|
|
+ ->select(['id','openid','nickname','city','balance','parent_id','parents'])
|
|
|
+ ->first();
|
|
|
+ $userInfo = $userInfo? $userInfo->toArray() : [];
|
|
|
+ $userCity = isset($userInfo['city'])? $userInfo['city'] : '';
|
|
|
+ $balance = isset($userInfo['balance'])? floatval($userInfo['balance']) : 0.00;
|
|
|
+ if(empty($userInfo)){
|
|
|
+ $this->error ='2017';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算价格
|
|
|
+ $payTotal = $total = moneyFormat($num * $price, 2);
|
|
|
+ if($discount>0 && $discount<100){
|
|
|
+ $payTotal = moneyFormat($payTotal * $discount/10, 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 优惠券
|
|
|
+ $couponPrice = 0;
|
|
|
+ if($couponId > 0){
|
|
|
+ $payTotal = MemberCouponService::make()->countTotalByCoupon($userId, $couponId, $payTotal, $merchId, $couponPrice);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 余额支付验证
|
|
|
+ if($payType == 30 && $balance< ($payTotal + $serviceFee)){
|
|
|
+ $this->error ='1029';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 收货地址
|
|
|
+ $addressData =[];
|
|
|
+ if($province){
|
|
|
+ $addressData[] = $province;
|
|
|
+ }
|
|
|
+ if($city){
|
|
|
+ $addressData[] = $city;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($district) {
|
|
|
+ $addressData[] = $district;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($address){
|
|
|
+ $addressData[] = $address;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 订单数据
|
|
|
+ $addressText = $addressData? implode(' ', $addressData) : '';
|
|
|
+ $orderNo = get_order_num('DY');
|
|
|
+ $order = [
|
|
|
+ 'user_id'=> $userId,
|
|
|
+ 'order_no'=> $orderNo,
|
|
|
+ 'merch_id'=> $merchId,
|
|
|
+ 'type'=> 2,
|
|
|
+ 'source_id'=> $sourceId,
|
|
|
+ 'price'=> $goodsInfo['price'],
|
|
|
+ 'num'=> $num,
|
|
|
+ 'city'=> $city? $city : $userCity,
|
|
|
+ 'appoint_time'=> $day.' '.$time,
|
|
|
+ 'real_name'=> $realname,
|
|
|
+ 'mobile'=> $mobile,
|
|
|
+ 'address'=> $addressText,
|
|
|
+ 'total'=> $total,
|
|
|
+ 'pay_type'=> $payType,
|
|
|
+ 'pay_money'=> moneyFormat($payTotal+$serviceFee, 2),
|
|
|
+ 'delivery_type'=> $deliveryType,
|
|
|
+ 'coupon_id'=> $couponId,
|
|
|
+ 'coupon_price'=> $couponPrice,
|
|
|
+ 'postage'=> $serviceFee,
|
|
|
+ 'create_time'=> time(),
|
|
|
+ 'update_time'=> time(),
|
|
|
+ 'remark'=> isset($params['remark'])? trim($params['remark']) : '',
|
|
|
+ 'status'=> 1,
|
|
|
+ 'mark'=> 1,
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 订单商品
|
|
|
+ $orderGoods = $goodsInfo;
|
|
|
+ $orderGoods['order_no'] = $orderNo;
|
|
|
+ $orderGoods['total'] = moneyFormat($goodsInfo['price'] * $num, 2);
|
|
|
+ $orderGoods['num'] = $num;
|
|
|
+ $orderGoods['create_time'] = time();
|
|
|
+ $orderGoods['update_time'] = time();
|
|
|
+ $orderGoods['status'] = 1;
|
|
|
+ $orderGoods['mark'] = 1;
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ // 创建订单
|
|
|
+ if(!$orderId = $this->model->insertGetId($order)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2605;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写入订单商品
|
|
|
+ if(!OrderGoodsModel::insertGetId($orderGoods)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2606;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 支付处理
|
|
|
+ $payment = [];
|
|
|
+ $payStatus = 2;
|
|
|
+ switch($payType){
|
|
|
+ case 30: // 余额支付
|
|
|
+ if(PaymentService::make()->ballancePay($userInfo, $order)){
|
|
|
+ $payStatus = 1;
|
|
|
+ }else{
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = PaymentService::make()->getError();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 20: // 支付宝
|
|
|
+ $payment = PaymentService::make()->aliPay($userInfo, $order);
|
|
|
+ if(empty($payment)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = PaymentService::make()->getError();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 10: // 微信支付
|
|
|
+ $payment = PaymentService::make()->wechatPay($userInfo, $order);
|
|
|
+ if(empty($payment)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = PaymentService::make()->getError();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ $this->error = 1030;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新优惠券状态
|
|
|
+ if($couponId && !MemberCouponModel::where(['id'=> $couponId,'mark'=>1])->update(['status'=> 2,'update_time'=>time()])){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2613;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 已支付
|
|
|
+ DB::commit();
|
|
|
+ if($payStatus == 1){
|
|
|
+ // 更新订单状态
|
|
|
+ $updateData = ['status'=>2,'pay_status'=> 1,'pay_time'=>date('Y-m-d H:i:s'),'update_time'=>time()];
|
|
|
+ if(!$this->model->where(['id'=> $orderId,'mark'=>1])->update($updateData)){
|
|
|
+ $this->error = 2609;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 2607;
|
|
|
+ return [
|
|
|
+ 'id'=> $orderId,
|
|
|
+ 'total'=> $payTotal,
|
|
|
+ 'pay_type'=> $payType,
|
|
|
+ ];
|
|
|
+ }else{
|
|
|
+ $this->error = 2608;
|
|
|
+ return [
|
|
|
+ 'id'=> $orderId,
|
|
|
+ 'payment'=> $payment,
|
|
|
+ 'total'=> $payTotal,
|
|
|
+ 'pay_type'=> $payType,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 购买服务
|
|
|
* @param $userId
|
|
|
* @param $params
|
|
@@ -410,7 +659,6 @@ class OrderService extends BaseService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 详情
|
|
|
* @param $id
|
|
@@ -418,7 +666,8 @@ class OrderService extends BaseService
|
|
|
*/
|
|
|
public function getInfo($id, $userId=0)
|
|
|
{
|
|
|
- $info = $this->model->with(['merchant','goods'])->from('orders as a')
|
|
|
+ $info = $this->model->with(['merchant','goods'])
|
|
|
+ ->from('orders as a')
|
|
|
->leftJoin('member as b', 'b.id', '=', 'a.user_id')
|
|
|
->where(['a.id' => $id, 'a.mark' => 1])
|
|
|
->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile'])
|
|
@@ -455,93 +704,538 @@ class OrderService extends BaseService
|
|
|
return $info;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 申请退款
|
|
|
+ * @param $userId
|
|
|
+ * @param $params
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function refund($userId, $params)
|
|
|
+ {
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0; // 商家
|
|
|
+ $mechId = isset($params['mech_id']) ? $params['mech_id'] : 0; //技师
|
|
|
+ $refundRemark = isset($params['remark']) ? $params['remark'] : '';
|
|
|
+ $info = $this->model->where(['id' => $id, 'mark' => 1])
|
|
|
+ ->select(['id','user_id','merch_id','source_id','pay_money','coupon_id','status','refund_status','pay_time'])
|
|
|
+ ->first();
|
|
|
+ $status = isset($info['status']) ? $info['status'] : 0;
|
|
|
+ $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
|
|
|
+ $orderMerchId = isset($info['merch_id']) ? $info['merch_id'] : 0;
|
|
|
+ $orderMechId = isset($info['source_id']) ? $info['source_id'] : 0;
|
|
|
+ $refundStatus = isset($info['refund_status']) ? $info['refund_status'] : 0;
|
|
|
+ if (!$id || empty($info)) {
|
|
|
+ $this->error = 2656;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 非法操作
|
|
|
+ if($merchId != $orderMerchId && $userId != $orderUserId && $mechId != $orderMechId){
|
|
|
+ $this->error = 2667;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!in_array($status, [2,3])){
|
|
|
+ $this->error = 2657;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($refundStatus>0){
|
|
|
+ $this->error = 2664;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理
|
|
|
+ if(!$this->model->where(['id'=> $id,'mark'=>1])->update(['status'=> 5,'refund_status'=>1,'refund_remark'=> $refundRemark,'update_time'=>time()])){
|
|
|
+ $this->error = 2665;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 2666;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
- * 已发货
|
|
|
+ * 申请退款取消
|
|
|
+ * @param $userId
|
|
|
+ * @param $params
|
|
|
* @return bool
|
|
|
*/
|
|
|
- public function deliver()
|
|
|
+ public function refundCancel($userId, $params)
|
|
|
+ {
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0; // 商家
|
|
|
+ $mechId = isset($params['mech_id']) ? $params['mech_id'] : 0; //技师
|
|
|
+ $info = $this->model->where(['id' => $id, 'mark' => 1])
|
|
|
+ ->select(['id','user_id','merch_id','source_id','pay_money','reception_at','coupon_id','status','refund_status','pay_time'])
|
|
|
+ ->first();
|
|
|
+ $status = isset($info['status']) ? $info['status'] : 0;
|
|
|
+ $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
|
|
|
+ $orderMerchId = isset($info['merch_id']) ? $info['merch_id'] : 0;
|
|
|
+ $orderMechId = isset($info['source_id']) ? $info['source_id'] : 0;
|
|
|
+ $refundStatus = isset($info['refund_status']) ? $info['refund_status'] : 0;
|
|
|
+ if (!$id || empty($info)) {
|
|
|
+ $this->error = 2656;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 非法操作
|
|
|
+ if($merchId != $orderMerchId && $userId != $orderUserId && $mechId != $orderMechId){
|
|
|
+ $this->error = 2667;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($status != 5){
|
|
|
+ $this->error = 2668;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($refundStatus<=0){
|
|
|
+ $this->error = 2669;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理
|
|
|
+ $updateData = ['status'=> $info['reception_at']? 3:2,'refund_status'=>0,'refund_remark'=> '','update_time'=>time()];
|
|
|
+ if(!$this->model->where(['id'=> $id,'mark'=>1])->update($updateData)){
|
|
|
+ $this->error = 2670;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 2671;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 申请退款审核
|
|
|
+ * @param $userId
|
|
|
+ * @param $params
|
|
|
+ * @return bool
|
|
|
+ * @throws \Yansongda\Pay\Exception\ContainerException
|
|
|
+ * @throws \Yansongda\Pay\Exception\InvalidParamsException
|
|
|
+ * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
|
|
|
+ */
|
|
|
+ public function refundConfirm($userId, $params)
|
|
|
+ {
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $checkStatus = isset($params['status']) ? $params['status'] : 0;
|
|
|
+ $remark = isset($params['remark']) ? $params['remark'] : '';
|
|
|
+ $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0; // 商家
|
|
|
+ $info = $this->model->with(['goods'])
|
|
|
+ ->where(['id' => $id, 'mark' => 1])
|
|
|
+ ->select(['id','user_id','merch_id','source_id','type','pay_money','order_no','pay_type','reception_at','coupon_id','status','refund_status','pay_time'])
|
|
|
+ ->first();
|
|
|
+ $status = isset($info['status']) ? $info['status'] : 0;
|
|
|
+ $orderMerchId = isset($info['merch_id']) ? $info['merch_id'] : 0;
|
|
|
+ $orderType = isset($info['type']) ? $info['type'] : 0;
|
|
|
+ $goods = isset($info['goods']) ? $info['goods'] : [];
|
|
|
+ $refundStatus = isset($info['refund_status']) ? $info['refund_status'] : 0;
|
|
|
+ if (!$id || empty($info)) {
|
|
|
+ $this->error = 2656;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 非法操作
|
|
|
+ if($merchId != $orderMerchId){
|
|
|
+ $this->error = 2667;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($status != 5){
|
|
|
+ $this->error = 2668;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($refundStatus<=0){
|
|
|
+ $this->error = 2669;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!in_array($checkStatus, [2,3])){
|
|
|
+ $this->error = 2639;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 支付信息
|
|
|
+ $orderNo = isset($info['order_no'])? $info['order_no'] : '';
|
|
|
+ $paymentInfo = PaymentModel::where(['order_no'=> $orderNo,'status'=>1,'mark'=>1])->first();
|
|
|
+ $totalFee = isset($paymentInfo['total_fee'])? $paymentInfo['total_fee'] : 0;
|
|
|
+ if(empty($paymentInfo) || $totalFee<=0){
|
|
|
+ $this->error = 2637;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $order = [
|
|
|
+ 'total_fee' => $totalFee,
|
|
|
+ 'type' => isset($info['type'])? $info['type'] : 2,
|
|
|
+ 'pay_type' => isset($info['pay_type'])? $info['pay_type'] : 0,
|
|
|
+ 'out_trade_no' => isset($paymentInfo['out_trade_no'])? $paymentInfo['out_trade_no'] : '',
|
|
|
+ 'transaction_id' => isset($paymentInfo['transaction_id'])? $paymentInfo['transaction_id'] : '',
|
|
|
+ 'order_no' => $orderNo
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 退款打款请求
|
|
|
+ DB::beginTransaction();
|
|
|
+ // 审核成功,打款
|
|
|
+ if($checkStatus == 2){
|
|
|
+ if(!PaymentService::make()->refund($order, 20)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2638;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理
|
|
|
+ $updateData = ['status'=> 5,'refund_status'=> $checkStatus,'refund_result'=> $checkStatus==2? '商家审核退款成功' : $remark,'update_time'=>time()];
|
|
|
+ if(!$this->model->where(['id'=> $id,'mark'=>1])->update($updateData)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2676;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 若退款成功,商品库存返还
|
|
|
+ if($goods && $checkStatus == 2 && in_array($orderType, [1,6])){
|
|
|
+ foreach($goods as $item){
|
|
|
+ $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
|
|
|
+ $goodsNum = isset($item['num'])? $item['num'] : 0;
|
|
|
+ if($goodsId && $goodsNum && !GoodsModel::where(['id'=> $goodsId])->increment('stock', $goodsNum)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2680;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ $this->error = 2677;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发货
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function delivery()
|
|
|
{
|
|
|
$params = request()->all();
|
|
|
$id = isset($params['id']) ? $params['id'] : 0;
|
|
|
$express = isset($params['express']) ? $params['express'] : '';
|
|
|
$expressNo = isset($params['express_no']) ? $params['express_no'] : '';
|
|
|
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
|
|
|
+ $info = $this->model->where(['id' => $id, 'mark' => 1])
|
|
|
+ ->select(['id','user_id','merch_id','source_id','pay_money','reception_at','coupon_id','status','refund_status','pay_time'])
|
|
|
+ ->first();
|
|
|
$status = isset($info['status']) ? $info['status'] : 0;
|
|
|
if (!$id || empty($info)) {
|
|
|
- $this->error = 2081;
|
|
|
+ $this->error = 2636;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if (!in_array($status, [2, 3])) {
|
|
|
- $this->error = 2090;
|
|
|
+ if ($status != 2) {
|
|
|
+ $this->error = 2668;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if ($this->model->where(['id' => $id])->update(['status' => 4, 'express' => $express, 'express_no' => $expressNo, 'update_time' => time()])) {
|
|
|
- $this->error = 2094;
|
|
|
+ if ($this->model->where(['id' => $id])->update(['status' => 3, 'express' => $express, 'express_no' => $expressNo, 'update_time' => time()])) {
|
|
|
+ $this->error = 2672;
|
|
|
return true;
|
|
|
} else {
|
|
|
- $this->error = 2095;
|
|
|
+ $this->error = 2673;
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 已收货
|
|
|
+ * 接单
|
|
|
* @return bool
|
|
|
*/
|
|
|
- public function receive()
|
|
|
+ public function picker()
|
|
|
{
|
|
|
$params = request()->all();
|
|
|
$id = isset($params['id']) ? $params['id'] : 0;
|
|
|
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
|
|
|
+ $mechId = isset($params['mech_id']) ? intval($params['mech_id']) : 0;
|
|
|
+ $info = $this->model->where(['id' => $id, 'mark' => 1])
|
|
|
+ ->select(['id','user_id','merch_id','source_id','pay_money','reception_at','coupon_id','status','refund_status','pay_time'])
|
|
|
+ ->first();
|
|
|
$status = isset($info['status']) ? $info['status'] : 0;
|
|
|
+ $orderMechId = isset($info['source_id']) ? $info['source_id'] : 0;
|
|
|
if (!$id || empty($info)) {
|
|
|
- $this->error = 2081;
|
|
|
+ $this->error = 2636;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if (!in_array($status, [3, 4, 5])) {
|
|
|
- $this->error = 2082;
|
|
|
+ if($mechId != $orderMechId){
|
|
|
+ $this->error = 2667;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if ($this->model->where(['id' => $id])->update(['status' => 5, 'update_time' => time()])) {
|
|
|
- $this->error = 2083;
|
|
|
+ if ($status != 2) {
|
|
|
+ $this->error = 2668;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理
|
|
|
+ if ($this->model->where(['id' => $id])->update(['status' => 3, 'update_time' => time()])) {
|
|
|
+ $this->error = 2674;
|
|
|
return true;
|
|
|
} else {
|
|
|
- $this->error = 2084;
|
|
|
+ $this->error = 2675;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 接单/上钟
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function inService()
|
|
|
+ {
|
|
|
+ $params = request()->all();
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $mechId = isset($params['mech_id']) ? intval($params['mech_id']) : 0;
|
|
|
+ $info = $this->model->where(['id' => $id, 'mark' => 1])
|
|
|
+ ->select(['id','user_id','merch_id','source_id','pay_money','reception_at','coupon_id','status','is_service','refund_status','pay_time'])
|
|
|
+ ->first();
|
|
|
+ $status = isset($info['status']) ? $info['status'] : 0;
|
|
|
+ $isService = isset($info['is_service']) ? $info['is_service'] : 0;
|
|
|
+ $orderMechId = isset($info['source_id']) ? $info['source_id'] : 0;
|
|
|
+ if (!$id || empty($info)) {
|
|
|
+ $this->error = 2636;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($mechId != $orderMechId){
|
|
|
+ $this->error = 2667;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($status != 3 || $isService == 1) {
|
|
|
+ $this->error = 2668;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理
|
|
|
+ if ($this->model->where(['id' => $id])->update(['is_service' => 1, 'update_time' => time()])) {
|
|
|
+ $this->error = 1003;
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ $this->error = 1002;
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取区域订单统计
|
|
|
- * @param $city
|
|
|
- * @return array|mixed
|
|
|
+ * 收货/已完成
|
|
|
+ * @return bool
|
|
|
*/
|
|
|
- public function getCountsByCity($city)
|
|
|
+ public function complete($userId, $params)
|
|
|
{
|
|
|
- $cacheKey = "caches:orders:counts:city_" . md5($city);
|
|
|
- $datas = RedisService::get($cacheKey);
|
|
|
- if ($datas) {
|
|
|
- return $datas;
|
|
|
- }
|
|
|
-
|
|
|
- $datas = ['count' => 0, 'total' => '0.00'];
|
|
|
- if ($city) {
|
|
|
- $count = $this->model->where(['city' => $city, 'status' => 4, 'mark' => 1])
|
|
|
- ->whereIn('type', [1, 2])
|
|
|
- ->count('id');
|
|
|
- $total = $this->model->where(['city' => $city, 'status' => 4, 'mark' => 1])
|
|
|
- ->whereIn('type', [1, 2])
|
|
|
- ->sum('pay_money');
|
|
|
- if ($count > 0) {
|
|
|
- $datas = ['count' => intval($count), 'total' => floatval($total)];
|
|
|
- RedisService::set($cacheKey, $datas, rand(5, 10));
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
|
|
|
+ $info = $this->model->with(['goods'])
|
|
|
+ ->where(['id' => $id, 'mark' => 1])
|
|
|
+ ->select(['id','user_id','merch_id','source_id','city','pay_money','type','reception_at','coupon_id','status','is_service','refund_status','pay_time'])
|
|
|
+ ->first();
|
|
|
+ $status = isset($info['status']) ? $info['status'] : 0;
|
|
|
+ $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
|
|
|
+ $orderMerchId = isset($info['merch_id']) ? $info['merch_id'] : 0;
|
|
|
+ $orderMechId = isset($info['source_id']) ? $info['source_id'] : 0;
|
|
|
+ $orderType = isset($info['type']) ? $info['type'] : 0;
|
|
|
+ $payMoney = isset($info['pay_money']) ? $info['pay_money'] : 0;
|
|
|
+ $goods = isset($info['goods'])? $info['goods'] : [];
|
|
|
+ if (!$id || empty($info)) {
|
|
|
+ $this->error = 2636;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 非法操作
|
|
|
+ if($merchId != $orderMerchId && $orderUserId != $userId){
|
|
|
+ $this->error = 2667;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 订单状态
|
|
|
+ if ($status != 3) {
|
|
|
+ $this->error = 2668;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 限制操作订单类型
|
|
|
+ if(!in_array($orderType, [1,2,6])){
|
|
|
+ $this->error = 2681;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO 确认数据处理
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ // 更新订单状态
|
|
|
+ if (!$this->model->where(['id' => $id])->update(['status' => 4, 'update_time' => time()])) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2678;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新商品/服务销量
|
|
|
+ if($goods){
|
|
|
+ foreach($goods as $item){
|
|
|
+ $num = isset($item['num'])? $item['num'] : 0;
|
|
|
+ if($num>0 && !GoodsModel::where(['id'=> $item['goods_id'],'mark'=>1])->update([
|
|
|
+ 'sales'=> DB::raw("sales + {$num}"),'update_time'=> time()
|
|
|
+ ])){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2679;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 服务订单统计处理
|
|
|
+ if($orderType == 2){
|
|
|
+ // 更新技师服务订单,接单统计数据
|
|
|
+ $serviceCount = $this->model->where(['user_id'=> $orderUserId,'source_id'=> $orderMechId,'type'=>2,'status'=>4,'mark'=>1])->count('id');
|
|
|
+ $updateData = ['order_total'=> DB::raw("order_total + 1"),'update_time'=>time()];
|
|
|
+ if($serviceCount<=1){
|
|
|
+ $updateData['service_num'] = DB::raw("service_num + 1");
|
|
|
+ }
|
|
|
+ if($orderMechId && !MechanicModel::where(['id'=>$orderMechId,'mark'=> 1])->update($updateData)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2682;
|
|
|
+ return false;
|
|
|
}
|
|
|
+
|
|
|
+ // 更新商家服务订单,统计数据
|
|
|
+ $updateData = ['service_order_num'=> DB::raw("service_order_num + 1"),'service_order_total'=> DB::raw("service_order_total + {$payMoney}"),'update_time'=>time()];
|
|
|
+ if($orderMechId && !MerchantModel::where(['id'=>$orderMerchId,'mark'=> 1])->update($updateData)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2682;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 店内订单结算
|
|
|
+ $balance = MerchantModel::where(['id'=> $orderMerchId,'mark'=>1])->value('balance');
|
|
|
+ if ($orderType == 6){
|
|
|
+ // 商家入账
|
|
|
+ $settleRate = ConfigService::make()->getConfigByCode('merch_settle_rate');
|
|
|
+ $settleRate = $settleRate>0? min(100, $settleRate) : 100;
|
|
|
+ $settleMoney = moneyFormat($payMoney * $settleRate/100, 2);
|
|
|
+ $updateData = ['balance'=> DB::raw("balance + {$settleMoney}"),'update_time'=>time()];
|
|
|
+ if($settleMoney>0 && !MerchantModel::where(['id'=> $orderMerchId,'mark'=>1])->update($updateData)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2683;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 流水记录
|
|
|
+ $log = [
|
|
|
+ 'user_id'=> 0,
|
|
|
+ 'merch_id'=> $orderMerchId,
|
|
|
+ 'source_uid'=> $orderUserId,
|
|
|
+ 'source_order_no'=> isset($info['order_no'])? $info['order_no'] :'',
|
|
|
+ 'type'=> 3,
|
|
|
+ 'coin_type'=> 2,
|
|
|
+ 'money'=> $settleMoney,
|
|
|
+ 'balance'=>$balance? $balance : 0.00,
|
|
|
+ 'create_time'=> time(),
|
|
|
+ 'update_time'=> time(),
|
|
|
+ 'remark'=> '商家店内订单收入',
|
|
|
+ 'status'=>1,
|
|
|
+ 'mark'=>1
|
|
|
+ ];
|
|
|
+ if(!AccountLogModel::insertGetId($log)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2641;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 平台抽成入账,财务统计
|
|
|
+ $financeMoney = floatval($payMoney - $settleMoney);
|
|
|
+ if($financeMoney>0){
|
|
|
+ FinanceService::make()->saveLog(0,$financeMoney, 1, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 服务订单结算
|
|
|
+ else if(in_array($orderType, [1,2])){
|
|
|
+
|
|
|
+ // 计算服务订单分销佣金和代理佣金
|
|
|
+ if(!$result = FinanceService::make()->settleOrder($info, $userId)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = FinanceService::make()->getError();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 商家入账
|
|
|
+ $settleMoney = isset($result['merch_money'])? $result['merch_money'] : 0;
|
|
|
+ $updateData = ['balance'=> DB::raw("balance + {$settleMoney}"),'update_time'=>time()];
|
|
|
+ if($settleMoney>0 && !MerchantModel::where(['id'=> $orderMerchId,'mark'=>1])->update($updateData)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2683;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $log = [
|
|
|
+ 'user_id'=> 0,
|
|
|
+ 'merch_id'=> $orderMerchId,
|
|
|
+ 'source_uid'=> $orderUserId,
|
|
|
+ 'source_order_no'=> isset($info['order_no'])? $info['order_no'] :'',
|
|
|
+ 'type'=> 3,
|
|
|
+ 'coin_type'=> 2,
|
|
|
+ 'money'=> $settleMoney,
|
|
|
+ 'balance'=>$balance? $balance : 0.00,
|
|
|
+ 'create_time'=> time(),
|
|
|
+ 'update_time'=> time(),
|
|
|
+ 'remark'=> '商家服务订单收入',
|
|
|
+ 'status'=>1,
|
|
|
+ 'mark'=>1
|
|
|
+ ];
|
|
|
+ if(!AccountLogModel::insertGetId($log)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2641;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 平台入账,财务统计
|
|
|
+ $financeMoney = isset($result['platform_money'])? $result['platform_money'] : 0;
|
|
|
+ if($financeMoney>0){
|
|
|
+ FinanceService::make()->saveLog(0,$financeMoney, 1, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ $this->error = 1002;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 评论
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function comment()
|
|
|
+ {
|
|
|
+ $params = request()->all();
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
|
|
|
+ $status = isset($info['status']) ? $info['status'] : 0;
|
|
|
+ if (!$id || empty($info)) {
|
|
|
+ $this->error = 2081;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!in_array($status, [3, 4, 5])) {
|
|
|
+ $this->error = 2082;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->model->where(['id' => $id])->update(['status' => 5, 'update_time' => time()])) {
|
|
|
+ $this->error = 2083;
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ $this->error = 2084;
|
|
|
+ return false;
|
|
|
}
|
|
|
- return $datas;
|
|
|
}
|
|
|
}
|