$saas_user->id, 'is_delete' => 0, 'state' => 2]); if (!$courier) { return $this->asJson([ 'code' => 1, 'msg' => '当前用户不是骑手', ]); } if($type == 2){ $cacheV = WorkerOrderExt::cacheIgnoreOrderId($courier->id, $order_id); return $this->asJson([ 'code' => 0, 'msg' => 'success', ]); } $cacheK = 'DeliveryTypeSelf_OrderBind' . $order_id; $cacheV = cache()->get($cacheK); if($cacheV){ return $this->asJson([ 'code' => 1, 'msg' => '抢单失败0,请稍后再试~', ]); } cache()->set($cacheK, 1, 10); $rider_id = $courier->id; //骑手ID $now_num = DeliveryInfo::find()->alias('di') ->leftJoin(['o' => Order::tableName()], 'di.order_no = o.order_no') ->where(['o.trade_status' => Order::ORDER_FLOW_SEND, 'is_local' => 1, 'rider_id' => $rider_id]) ->count(); if ($courier->max_num != 0 && $now_num >= $courier->max_num) { return $this->asJson([ 'code' => 1, 'msg' => '当前配送订单已达到上限,请稍后再试', ]); } $t = \Yii::$app->db->beginTransaction(); try{ $order = Order::findOne($order_id); $delivery_info = DeliveryInfo::findOne(['order_no' => $order->order_no]); if (intval($delivery_info->is_store_delivery_type) === 1) { return $this->asJson([ 'code' => 1, 'msg' => '当前配送订单只能由商城配送员配送', ]); } $delivery_info->rider_id = $rider_id; $delivery_info->rider_name = $courier->real_name; $delivery_info->rider_mobile = $courier->mobile; $delivery_info->local_status = DeliveryInfo::LOCAL_STATUS_WAITING; if (!$delivery_info->save()) { throw new \Exception('delivery_info保存失败' . array_shift($delivery_info->getFirstErrors())); } $order->send_time = time(); $order->trade_status = Order::ORDER_FLOW_SEND; if (!$order->save()) { throw new \Exception('order保存失败' . array_shift($order->getFirstErrors())); } $t->commit(); return $this->asJson([ 'code' => 0, 'msg' => '操作成功', ]); } catch (\Exception $ex) { \Yii::error($ex); $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => '操作失败,' . $ex->getMessage(), ]); } } /** * 模块名:actionGetOrderList * 代码描述:获取订单列表 * 作者:WPing丶 * 请求方式:POST * 创建时间:2023/07/06 15:19:43 * @param int status 1=新任务,2=待取货,3=配送中 */ public function actionGetOrderList() { $status = post_params('status'); //1=新任务,2=待取货,3=配送中,4=历史订单(已完成) $start_time = post_params('start_time'); $end_time = post_params('end_time'); $lng = input_params('lng'); $lat = input_params('lat'); $saas_user = get_saas_user(); $courier = LocalDeliveryCourier::findOne(['saas_user_id' => $saas_user->id, 'is_delete' => 0, 'state' => 2]); if (!$courier) { return $this->asJson([ 'code' => 1, 'msg' => '当前用户不是骑手', ]); } $query = Order::find()->alias('o') ->leftJoin(['di' => DeliveryInfo::tableName()], 'o.order_no = di.order_no') ->leftJoin(['md' => Md::tableName()], 'o.md_id = md.id') ->where(['o.order_type' => [Order::ORDER_TYPE_STORE, 3], 'di.is_local' => 1]) ->select('o.*, di.id d_order_id, di.fee,di.rider_id,di.rider_name,di.rider_mobile,di.arrive_time,di.rush_time,di.confirm_time,di.serial_num, di.is_front_delivery')->orderBy('o.created_at DESC'); if (intval($courier->type) === 1) { $query->andWhere(['di.is_store_delivery_type' => 0]); } else { $query->andWhere(['di.is_store_delivery_type' => 1]); } switch ($status) { case 1: $query->andWhere([ 'o.trade_status' => Order::ORDER_FLOW_NO_SEND, 'o.is_delete' => Order::IS_DELETE_FALSE, 'di.local_status' => DeliveryInfo::LOCAL_STATUS_NO_SEND, 'di.rider_id' => 0, ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]); $courierArea = json_decode($courier['area'], true); // $courierCity = $courierArea[0]['city_id'] ?? 0; $courierDistrict = $courierArea[0]['district_id'] ?? 0; if(empty($courierDistrict)){ return $this->asJson([ 'code' => 1, 'msg' => '请先配置自己的常驻地区', ]); } $query->andWhere(['OR', ['o.district_id' => $courierDistrict], ['md.district' => $courierDistrict, 'o.is_offline' => 1, 'di.is_front_delivery' => 1]]); $query->andWhere(['<', 'o.delivery_time', time() + 3600 * 1.5]); break; case 2: $query->andWhere([ 'o.trade_status' => Order::ORDER_FLOW_SEND, 'o.is_delete' => Order::IS_DELETE_FALSE, 'di.local_status' => DeliveryInfo::LOCAL_STATUS_WAITING, 'di.rider_id' => $courier->id, ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]); break; case 3: $query->andWhere([ 'o.trade_status' => Order::ORDER_FLOW_SEND, 'o.is_delete' => Order::IS_DELETE_FALSE, 'di.local_status' => DeliveryInfo::LOCAL_STATUS_SENDING, 'di.rider_id' => $courier->id, ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]); break; case 4: $query->andWhere([ // 'o.trade_status' => Order::ORDER_FLOW_CONFIRM,//暂时注释 'o.is_delete' => Order::IS_DELETE_FALSE, 'di.local_status' => DeliveryInfo::LOCAL_STATUS_CONFIRM, 'di.rider_id' => $courier->id, ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]); break; default: return $this->asJson([ 'code' => 1, 'msg' => 'status参数错误' ]); break; } if ($start_time) { $query->andWhere(['>=', 'di.confirm_time', strtotime($start_time . ' 00:00:00')]); } if ($end_time) { $query->andWhere(['<=', 'di.confirm_time', strtotime($end_time . ' 23:59:59')]); } // $sql = $query->createCommand()->getRawSql(); // return $this->asJson([ // 'code' => 0, // 'msg' => 'success', // 'data' => $sql // ]); $setting = json_decode(Option::get(OptionSetting::LOCAL_DELIVERY_SETTING, 0, '', '{}')['value'], true); $list = pagination_make($query); foreach ($list['list'] as &$val) { $address_data = json::decode($val['address_data'], true); $store = Store::findOne($val['store_id']); $store_latitude = $store->latitude; $store_longitude = $store->longitude; $store_address = $store->address; $store_name = $val['store_id'] > 0 ? $store->name : ''; //判断是仓库订单 $is_front_delivery = intval($val['is_front_delivery']); if ($is_front_delivery) { $frontDeliveryOrder = FrontDeliveryOrder::findOne(['delivery_info_id' => $val['d_order_id']]); $front_agent_admin_id = $frontDeliveryOrder->front_agent_admin_id; $admin = Admin::findOne($front_agent_admin_id); if ($admin) { $store_latitude = $admin->lat ?: 0; $store_longitude = $admin->lng ?: 0; $store_address = $admin->address; $store_name = $admin->name; } if (intval($val['is_offline'])) { $mdInfo = Md::findOne($val['md_id']); if ($mdInfo) { $address_data = [ 'latitude' => $mdInfo->latitude, 'longitude' => $mdInfo->longitude ]; $val['address'] = $mdInfo->address; } } } $form = new OrderListForm(); $val['goods_list'] = $form->getOrderGoodsList($val['id']); $val['store_name'] = $store_name; $val['user_latitude'] = $address_data['latitude']; $val['user_longitude'] = $address_data['longitude']; $val['store_latitude'] = $store_latitude; $val['store_longitude'] = $store_longitude; $val['distance'] = Tools::getDistance($address_data['latitude'], $address_data['longitude'], $store_latitude, $store_longitude); $val['contact_tel'] = $store->contact_tel; $val['store_address'] = $store_address; if($lng){ $val['distance1'] = Tools::getDistance($store_latitude, $store_longitude, $lat, $lng); $val['distance2'] = Tools::getDistance($address_data['latitude'], $address_data['longitude'], $lat, $lng); }else{ $val['distance1'] = ''; $val['distance2'] = ''; } if($val['mch_id']){ $mch = \app\models\Mch::findOne($val['mch_id']); $val['store_name'] .= '-入驻商:' . $mch->name; $val['store_latitude'] = $mch->latitude; $val['store_longitude'] = $mch->longitude; $val['distance'] = Tools::getDistance($address_data['latitude'], $address_data['longitude'], $mch->latitude, $mch->longitude); $val['contact_tel'] = $mch->tel; $val['store_address'] = $mch->address; if($lng){ $val['distance1'] = Tools::getDistance($mch->latitude, $mch->longitude, $lat, $lng); } } $default_cost = 0; if(!empty($setting['default_cost']) && !empty($setting['default_cost']['value'])){ $default_cost = $setting['default_cost']['value']; } if($default_cost == -1){ $default_cost = $val['fee']; } $val['fee_cost'] = $default_cost; if (in_array($status, [2, 3])) { $delivery_info = DeliveryInfo::findOne($val['d_order_id']); if ($delivery_info) { if ($lat > 0) { $delivery_info->last_latitude = $lat; } if ($lng > 0) { $delivery_info->last_longitude = $lng; } $delivery_info->save(); } } } $data = [ 'q' => $query->createCommand()->getRawSql(), 'data' => $list['list'], 'pageNo' => $list['pageNo'], 'totalCount' => $list['totalCount'] ]; return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => $data ]); } /** * 模块名:actionCourierStatus * 代码描述:骑手上下线 * 作者:WPing丶 * 请求方式:POST * 创建时间:2023/07/06 18:20:19 * @param int status 0=离线 1=在线 */ public function actionCourierStatus() { $status = (int)post_params('status'); //0=离线 1=在线 if ($status != 0 && $status != 1) { return $this->asJson([ 'code' => 1, 'msg' => 'status参数错误', ]); } $saas_user = get_saas_user(); $courier = LocalDeliveryCourier::findOne(['saas_user_id' => $saas_user->id, 'is_delete' => 0, 'state' => 2]); if (!$courier) { return $this->asJson([ 'code' => 1, 'msg' => '当前用户不是骑手', ]); } if ($courier->status == $status) { $msg = $status == 0 ? '已离线,请勿重复操作' : '已上线,请勿重复操作'; return $this->asJson([ 'code' => 1, 'msg' => $msg, 'status' => $status ]); } if ($status == 0) { //离线时需要查询一下,是否有未配送订单 $count = Order::find()->alias('o') ->leftJoin(['di' => DeliveryInfo::tableName()], 'o.order_no = di.order_no') ->where(['o.order_type' => Order::ORDER_TYPE_STORE, 'di.is_local' => 1]) ->andWhere([ 'o.trade_status' => Order::ORDER_FLOW_SEND, 'o.is_delete' => Order::IS_DELETE_FALSE, 'di.rider_id' => $courier->id, ]) ->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]) ->andWhere([ 'or', ['di.local_status' => DeliveryInfo::LOCAL_STATUS_WAITING], ['di.local_status' => DeliveryInfo::LOCAL_STATUS_SENDING], ]) ->select('o.id,o.order_no,o.store_id,o.total_price,o.pay_price,o.name,o.mobile,o.address,o.created_at,o.trade_status,o.delivery_time,o.pay_time,o.address_data,di.fee,di.rider_id,di.rider_name,di.rider_mobile,di.serial_num') ->count(); if ($count > 0) { $courier->status = $status; if (!$courier->save()) { return $this->asJson([ 'code' => 1, 'msg' => $courier->errors[0], ]); } return $this->asJson([ 'code' => 0, 'msg' => '已下线停止接单,请继续完成未完成订单。', ]); } } $courier->status = $status; if (!$courier->save()) { return $this->asJson([ 'code' => 1, 'msg' => $courier->errors[0], ]); } return $this->asJson([ 'code' => 0, 'msg' => '操作成功', ]); } /** * 模块名:actionArriveStore * 代码描述:上报到店 * 作者:WPing丶 * 请求方式:POST * 创建时间:2023/07/06 19:13:17 * @param int id 订单ID */ public function actionArriveStore() { $t = \Yii::$app->db->beginTransaction(); try { $id = post_params('id'); //订单ID $saas_user = get_saas_user(); $courier = LocalDeliveryCourier::findOne(['saas_user_id' => $saas_user->id, 'is_delete' => 0, 'state' => 2]); if (!$courier) { throw new \Exception('当前用户不是骑手'); } $order = Order::findOne($id); $delivery_info = DeliveryInfo::findOne(['order_no' => $order->order_no]); if ($delivery_info->rider_id != $courier->id) { throw new \Exception('订单骑手不一致,请刷新列表'); } if ($delivery_info->local_status != DeliveryInfo::LOCAL_STATUS_WAITING) { throw new \Exception('订单状态发生变化,请刷新列表'); } $delivery_info->local_status = DeliveryInfo::LOCAL_STATUS_SENDING; $delivery_info->arrive_time = time(); if (!$delivery_info->save()) { throw new \Exception($delivery_info->errors[0]); } //如果是云仓订单发货操作(给供货商结算使用) $frontDeliveryOrder = FrontDeliveryOrder::findOne(['delivery_info_id' => $delivery_info->id]); if ($frontDeliveryOrder) { $supplier = new SupplierForm(); $supplier->send_type = 2; $supplier->order_id = (string)$frontDeliveryOrder->cloud_order_id; $result = $supplier->supplierPurchaseSend($frontDeliveryOrder->cloud_supplier_id); if ($result['code'] !== 0) { throw new \Exception($result['msg']); } } $t->commit(); return $this->asJson([ 'code' => 0, 'msg' => '取货成功', ]); } catch (\Exception $e) { $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage(), ]); } } /** * 模块名:actionOrderConfirm * 代码描述:订单完成 * 作者:WPing丶 * 请求方式:POST * 创建时间:2023/07/06 19:13:17 * @param int id 订单ID */ public function actionOrderConfirm() { $id = post_params('id'); //订单ID $saas_user = get_saas_user(); $courier = LocalDeliveryCourier::findOne(['saas_user_id' => $saas_user->id, 'is_delete' => 0, 'state' => 2]); if (!$courier) { return $this->asJson([ 'code' => 1, 'msg' => '当前用户不是骑手', ]); } $order = Order::findOne($id); $delivery_info = DeliveryInfo::findOne(['order_no' => $order->order_no]); if ($delivery_info->rider_id != $courier->id) { return $this->asJson([ 'code' => 1, 'msg' => '订单骑手不一致,请刷新列表', ]); } if ($delivery_info->local_status != DeliveryInfo::LOCAL_STATUS_SENDING) { return $this->asJson([ 'code' => 1, 'msg' => '订单状态发生变化,请刷新列表', ]); } $t = \Yii::$app->db->beginTransaction(); //开始事务 $delivery_info->local_status = DeliveryInfo::LOCAL_STATUS_CONFIRM; $delivery_info->confirm_time = time(); if (!$delivery_info->save()) { $t->rollBack(); //事务回滚 return $this->asJson([ 'code' => 1, 'msg' => $delivery_info->errors[0], ]); } $store_id = 0; if (intval($delivery_info->is_store_delivery_type)) { $store_id = $delivery_info->store_id; } $set = Option::get(OptionSetting::LOCAL_DELIVERY_SETTING, $store_id, OptionSetting::LOCAL_DELIVERY_GROUP_NAME, '{}')['value']; $local_setting = json_decode($set, true); if($local_setting['confirm_type'] && $local_setting['confirm_type']['value']){ if (intval($order->is_delivery)) { $order->trade_status = Order::ORDER_FLOW_CONFIRM; $order->confirm_time = time(); } else { $order->trade_status = Order::ORDER_FLOW_SEND; $order->send_time = time(); } if (!$order->save()) { $t->rollBack(); //事务回滚 return $this->asJson([ 'code' => 1, 'msg' => array_shift($order->getFirstErrors()), ]); } // /* 骑手完成订单后发放收入 */ if ($store_id > 0) { $local_setting['default_cost']['value'] = -1; } $amount = (float)$local_setting['default_cost']['value'] == -1 ? $delivery_info->fee : (float)$local_setting['default_cost']['value']; $log = LocalDeliveryLog::saveLog($saas_user->id, $amount, 1, 1, $id, "骑手配送收入:" . $amount . "元"); if (!$log) { $t->rollBack(); //事务回滚 return $this->asJson([ 'code' => 1, 'msg' => '发放骑手佣金报错', ]); } //如果是云仓订单收货操作(给供货商结算使用) // $frontDeliveryOrder = FrontDeliveryOrder::findOne(['delivery_info_id' => $delivery_info->id]); // if ($frontDeliveryOrder && intval($order->is_delivery)) { // $form = new MerchantForm(); // $data = $form->mchPurchaseOrderConfirm(0, $frontDeliveryOrder->store_id, $frontDeliveryOrder->cloud_order_id); // debug_log(json_encode($data)); // if ((int)$data['code'] !== 0) { // $t->rollBack(); //事务回滚 // return $this->asJson($data); // } // } } $t->commit(); //事务执行 return $this->asJson([ 'code' => 0, 'msg' => '订单已完成', ]); } /** * 模块名:actionSetting * 代码描述:骑手设置 * 作者:WPing丶 * 请求方式:POST * 创建时间:2023/07/06 20:38:45 * @param int max_num */ public function actionSetting() { $params = post_params(); //设置项 $saas_user = get_saas_user(); $courier = LocalDeliveryCourier::findOne(['saas_user_id' => $saas_user->id, 'is_delete' => 0, 'state' => 2]); if (!$courier) { return $this->asJson([ 'code' => 1, 'msg' => '当前用户不是骑手', ]); } if (array_key_exists('max_num', $params)) { $courier->max_num = $params['max_num']; } if (array_key_exists('work_time', $params)) { $courier->work_time = $params['work_time']; } if (array_key_exists('is_auto', $params)) { $courier->is_auto = $params['is_auto']; } // if (array_key_exists('store_id', $params)) { // $courier->store_id = $params['store_id']; // } if (array_key_exists('real_name', $params)) { $courier->real_name = $params['real_name']; } if (array_key_exists('real_code', $params)) { $courier->real_code = $params['real_code']; } if (array_key_exists('avatar', $params)) { $courier->avatar = $params['avatar']; } if (array_key_exists('area', $params)) { $courier->area = $params['area']; } if (!$courier->save()) { return $this->asJson([ 'code' => 1, 'msg' => $courier->errors[0], ]); } return $this->asJson([ 'code' => 0, 'msg' => '修改成功', ]); } /** * 模块名:actionTotalOrder * 代码描述:订单统计 * 作者:WPing丶 * 请求方式:GET * 创建时间:2023/07/06 15:19:43 * @param int start_time 时间范围 * @param int end_time 时间范围 */ public function actionTotalOrder() { $start_time = get_params('start_time', get_params('begin_time')); $end_time = get_params('end_time'); $saas_user = get_saas_user(); $courier = LocalDeliveryCourier::findOne(['saas_user_id' => $saas_user->id, 'is_delete' => 0, 'state' => 2]); if (!$courier) { return $this->asJson([ 'code' => 1, 'msg' => '当前用户不是骑手', ]); } $query = Order::find()->alias('o') ->leftJoin(['di' => DeliveryInfo::tableName()], 'o.order_no = di.order_no') ->where(['o.order_type' => Order::ORDER_TYPE_STORE, 'di.is_local' => 1]) ->select('o.id,o.order_no,o.store_id,o.total_price,o.pay_price,o.name,o.mobile,o.address,o.created_at,o.trade_status,o.delivery_time,o.pay_time,o.address_data,o.remark,di.fee,di.rider_id,di.rider_name,di.rider_mobile,di.arrive_time,di.rush_time,di.confirm_time,di.serial_num')->orderBy('o.created_at DESC'); if ($start_time) { $query->andWhere(['>=', 'di.confirm_time', strtotime($start_time . ' 00:00:00')]); } if ($end_time) { $query->andWhere(['<=', 'di.confirm_time', strtotime($end_time . ' 23:59:59')]); } //完成订单 $confirm_query = clone $query; $confirm_query->andWhere([ 'o.trade_status' => Order::ORDER_FLOW_CONFIRM, 'o.is_delete' => Order::IS_DELETE_FALSE, 'di.local_status' => DeliveryInfo::LOCAL_STATUS_CONFIRM, 'di.rider_id' => $courier->id, ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]); $list = pagination_make($confirm_query); $total_distance = 0; foreach ($list['list'] as &$val) { $form = new OrderListForm(); $val['goods_list'] = $form->getOrderGoodsList($val['id']); $store = Store::findOne($val['store_id']); $val['store_name'] = $val['store_id'] > 0 ? $store->name : ''; $address_data = json::decode($val['address_data'], true); $val['user_latitude'] = $address_data['latitude']; $val['user_longitude'] = $address_data['longitude']; $val['store_latitude'] = $store->latitude; $val['store_longitude'] = $store->longitude; $val['distance'] = Tools::getDistance($address_data['latitude'], $address_data['longitude'], $store->latitude, $store->longitude); $val['contact_tel'] = $store->contact_tel; $val['store_address'] = $store->address; //处理距离 $distance_str = $val['distance']; if (strpos($distance_str, 'km') !== false) { // 如果包含 'km' 则将字符串转换为浮点数并乘以 1000 $distance = (float) str_replace('km', '', $distance_str) * 1000; } elseif (strpos($distance_str, 'm') !== false) { // 如果包含 'm' 则将字符串转换为整数 $distance = (int) str_replace('m', '', $distance_str); } $distance = bcdiv($distance, 1000, 2); $total_distance += $distance; } $confirm_data = [ 'q' => $confirm_query->createCommand()->getRawSql(), 'data' => $list['list'], 'pageNo' => $list['pageNo'], 'totalCount' => $list['totalCount'] ]; //取消订单 $cancel_query = clone $query; $cancel_query->andWhere([ 'o.trade_status' => Order::ORDER_FLOW_CANCEL, 'o.is_delete' => Order::IS_DELETE_FALSE, 'di.rider_id' => $courier->id, ]); $list = pagination_make($cancel_query); foreach ($list['list'] as &$val) { $form = new OrderListForm(); $val['goods_list'] = $form->getOrderGoodsList($val['id']); $store = Store::findOne($val['store_id']); $val['store_name'] = $val['store_id'] > 0 ? $store->name : ''; $address_data = json::decode($val['address_data'], true); $val['user_latitude'] = $address_data['latitude']; $val['user_longitude'] = $address_data['longitude']; $val['store_latitude'] = $store->latitude; $val['store_longitude'] = $store->longitude; $val['distance'] = Tools::getDistance($address_data['latitude'], $address_data['longitude'], $store->latitude, $store->longitude); $val['contact_tel'] = $store->contact_tel; $val['store_address'] = $store->address; } $cancel_data = [ 'data' => $list['list'], 'pageNo' => $list['pageNo'], 'totalCount' => $list['totalCount'] ]; //统计 $total = [ 'confirm_num' => $confirm_data['totalCount'], //完成订单数 'cancel_num' => $cancel_data['totalCount'], //取消订单数 'total_distance' => bcdiv($total_distance, 1) ]; return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'confirm_list' => $confirm_data, //已完成订单列表 'cancel_list' => $cancel_data, //已取消订单列表 'total' => $total, ] ]); } /** * 模块名:actionCashInfo * 代码描述:提现基础设置 * 作者:WPing丶 * 请求方式:POST * 创建时间:2023/07/11 09:32:30 */ public function actionCashInfo() { $saas_user = get_saas_user(); $courier = LocalDeliveryCourier::findOne(['saas_user_id' => $saas_user->id, 'is_delete' => 0, 'state' => 2]); if (!$courier) { return $this->asJson([ 'code' => 1, 'msg' => '当前用户不是骑手', ]); } //今日收入 $today_money = LocalDeliveryLog::find()->where(['saas_user_id' => $saas_user->id, 'log_type' => 1])->andWhere(['>' ,'order_id', 0])->andWhere(['>', 'created_at', strtotime(date('Y-m-d'))])->select('sum(amount) as today_money')->asArray()->all(); //今日订单数 $today_order = LocalDeliveryLog::find()->where(['saas_user_id' => $saas_user->id, 'log_type' => 1])->andWhere(['>' ,'order_id', 0])->andWhere(['>', 'created_at', strtotime(date('Y-m-d'))])->count(); //今日提现 // $today_cash = LocalDeliveryLog::find()->where(['saas_user_id' => $saas_user->id, 'log_type' => 2])->andWhere(['>', 'created_at', strtotime(date('Y-m-d'))])->select('sum(amount) as today_cash')->asArray()->all(); $today_cash = LocalDeliveryCash::find()->where(['saas_user_id' => $saas_user->id])->andWhere(['<>' ,'status', 2])->andWhere(['>', 'created_at', strtotime(date('Y-m-d'))])->select('sum(price) as today_cash')->asArray()->all(); //同城配送设置相关 $values = Option::find()->where([ 'store_id' => 0, 'group' => OptionSetting::LOCAL_DELIVERY_GROUP_NAME, 'name' => OptionSetting::LOCAL_DELIVERY_SETTING ])->select('value')->one(); $local_setting = json_decode($values->value, true); //今日未到账收入(配送中) $query = Order::find()->alias('o') ->leftJoin(['di' => DeliveryInfo::tableName()], 'o.order_no = di.order_no') ->where(['o.order_type' => Order::ORDER_TYPE_STORE, 'di.is_local' => 1]) ->andWhere([ 'o.trade_status' => Order::ORDER_FLOW_SEND, 'o.is_delete' => Order::IS_DELETE_FALSE, 'di.local_status' => DeliveryInfo::LOCAL_STATUS_SENDING, 'di.rider_id' => $courier->id, ]) ->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]) ->andWhere(['>', 'di.created_at', strtotime(date('Y-m-d'))]); if ((float)$local_setting['default_cost']['value'] == -1) { $frozen_money = $query->select('sum(di.fee) as frozen_money')->asArray()->all()['frozen_money']; } else { $frozen_money = bcmul((float)$local_setting['default_cost']['value'], $query->count(), 2); } return $this->asJson([ 'code' => 0, 'data' => [ 'money' => $courier->money ?: 0, //账户余额 'total_money' => $courier->total_money ?: 0, //账户总收入 'today_money' => $today_money[0]['today_money'] ?: 0, //今日收入 'today_cash' => $today_cash[0]['today_cash'] ?: 0, //今日提现支出 'today_order' => $today_order ?: 0, //今日订单数 'frozen_money' => $frozen_money ?: 0, //未到账 'service_charge' => $local_setting['cash_service_charge']['value'] ?: 0, //提现手续费 'data' => LocalDeliveryLog::find()->alias('l')->where(['l.saas_user_id' => $saas_user->id])->orderBy('l.id DESC')->limit(3)->asArray()->all(), ], 'msg' => 'success', ]); } /** * 模块名:actionCashList * 代码描述:提现记录 * 作者:WPing丶 * 请求方式:POST * 创建时间:2023/07/11 11:04:47 * @param int id * @param string str * @param bool bool */ public function actionCashList() { $status = get_params('status'); $saas_user = get_saas_user(); $query = LocalDeliveryCash::find()->where(['saas_user_id' => $saas_user->id, 'is_delete' => 0]); if ($status > -1) { //状态,0:待审核,1:审核通过,2:审核驳回 $query->andWhere(['status' => $status]); } $query->orderBy('id DESC'); $pagination = pagination_make($query); $list = $pagination['list']; foreach ($list as &$value) { $value['created_at'] = $value['created_at'] > 0 ? date('Y-m-d H:i:s', $value['created_at']) : ''; $value['updated_at'] = $value['updated_at'] > 0 ? date('Y-m-d H:i:s', $value['updated_at']) : ''; $value['pay_time'] = $value['pay_time'] > 0 ? date('Y-m-d H:i:s', $value['pay_time']) : ''; } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'] ] ]); } /** * 模块名:actionCashApply * 代码描述:提现申请 * 作者:WPing丶 * 请求方式:POST * 创建时间:2023/07/11 15:13:16 * @param int id * @param string str * @param bool bool */ public function actionCashApply() { $saas_user = get_saas_user(); $courier = LocalDeliveryCourier::findOne(['saas_user_id' => $saas_user->id, 'is_delete' => 0, 'state' => 2]); if (!$courier) { return $this->asJson([ 'code' => 1, 'msg' => '当前用户不是骑手', ]); } $price = post_params('cash'); $type = post_params('type'); if (!$price || !in_array($type, ['alipay', 'bank_card', 'wechat'])) { return $this->asJson([ 'code' => 1, 'msg' => '参数非法', ]); } //商城余额是否充足 if ($price > $courier->money) { \Yii::error([__METHOD__, $price, $courier->money]); return $this->asJson([ 'code' => 1, 'msg' => '可提现金额不足' ]); } //同城配送设置相关 $values = Option::find()->where([ 'store_id' => 0, 'group' => OptionSetting::LOCAL_DELIVERY_GROUP_NAME, 'name' => OptionSetting::LOCAL_DELIVERY_SETTING ])->select('value')->one(); $local_setting = json_decode($values->value, true); $cash_max_day = $local_setting['cash_max_day']['value']; $min_money = $local_setting['min_money']['value']; //今日提现金额 $today_cash = LocalDeliveryCash::find() ->where(['<>', 'status', LocalDeliveryCash::CASH_STATUS_FAIL]) ->andWhere(['saas_user_id' => $saas_user->id]) ->andWhere(['>', 'created_at', strtotime(date('Y-m-d'))]) ->sum('price'); if ($cash_max_day > 0 && ($price > $cash_max_day || bcadd($price, (float)$today_cash, 2) > $cash_max_day)) { \Yii::error([__METHOD__, $price, $cash_max_day]); return $this->asJson([ 'code' => 1, 'msg' => '今日提现金额已超过每天提现最大额度' . $cash_max_day . '元' ]); } if ($price <= $min_money) { \Yii::error([__METHOD__, $price, $min_money]); return $this->asJson([ 'code' => 1, 'msg' => '提现金额不能小于' . $min_money . '元' ]); } $exit = LocalDeliveryCash::find()->andWhere(['=', 'status', 0])->andWhere(['saas_user_id' => $saas_user->id])->exists(); if ($exit) { \Yii::error([__METHOD__, $saas_user->id, $exit]); // return $this->asJson([ // 'code' => 1, // 'msg' => '尚有未完成的提现申请' // ]); } $t = \Yii::$app->db->beginTransaction(); $cash = new LocalDeliveryCash(); $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_LOCAL_CASH); $cash->is_delete = 0; $cash->status = 0; $cash->price = $price; $cash->created_at = time(); $cash->saas_user_id = $saas_user->id; $cash->store_id = $courier->store_id ?: 0; $cash->service_charge = bcmul(bcdiv($local_setting['cash_service_charge']['value'], 100, 4), $price, 2); $withdraw_method = json_decode($saas_user->withdraw_method, true); $withdraw_method = array_column($withdraw_method, null, 'type'); if ($type == 'wechat') { $cash->type = 0; $cash->name = $withdraw_method['wechat']['name']; $cash->mobile = $withdraw_method['wechat']['account']; } if ($type == 'alipay') { $cash->type = 1; $cash->name = $withdraw_method['alipay']['name']; $cash->mobile = $withdraw_method['alipay']['account']; } if ($type == 'bank_card') { $cash->type = 2; $cash->name = $withdraw_method['bank_card']['name']; $cash->mobile = $withdraw_method['bank_card']['account']; $cash->bank_name = $withdraw_method['bank_card']['bank']; } $cash->pay_time = 0; if ($cash->save()) { $subMoney = \app\models\LocalDeliveryCourier::subMoney($courier, $price, '配送员提现'); if (!$subMoney) { $t->rollBack(); \Yii::error([__METHOD__, $subMoney, $cash]); return $this->asJson([ 'code' => 1, 'msg' => '扣款失败', ]); } $t->commit(); return $this->asJson([ 'code' => 0, 'msg' => '申请成功' ]); } else { $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $cash->errors ]); } } /** * 模块名:actionAccountLogList * 代码描述:佣金明细 * 作者:WPing丶 * 请求方式:GET * 创建时间:2023/07/12 09:03:52 * @param int id * @param string str * @param bool bool */ public function actionAccountLogList() { $start_time = get_params('start_time'); $end_time = get_params('end_time'); $type = get_params('type'); $saas_user = get_saas_user(); $query = LocalDeliveryLog::find()->alias('l')->where(['l.saas_user_id' => $saas_user->id]); if ($start_time) { $query->andWhere(['>=', 'l.created_at', strtotime($start_time . ' 00:00:00')]); } if ($end_time) { $query->andWhere(['<=', 'l.created_at', strtotime($end_time . ' 23:59:59')]); } if ($type > 0) { $query->andWhere(['l.type' => $type]); } $query->orderBy('l.id DESC'); $pagination = pagination_make($query); $list = $pagination['list']; return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'] ], ]); } /** * 模块名:actionApplyCourier * 代码描述:骑手申请 * 作者:WPing丶 * 请求方式:GET * 创建时间:2023/07/13 17:31:20 * @param int id * @param string str * @param bool bool */ public function actionApplyCourier() { $saas_user = get_saas_user(); $params = post_params(); $courier = LocalDeliveryCourier::findOne(['is_delete' => 0, 'saas_user_id' => $saas_user->id]); if ($courier) { if ($courier->state == 1) { return $this->asJson([ 'code' => 1, 'msg' => '正在审核中,请耐心等待' ]); } elseif ($courier->state == 2) { return $this->asJson([ 'code' => 1, 'msg' => '审核已通过' ]); } } else { $courier = new LocalDeliveryCourier; } /* 参数验证begin */ if (!$params['real_name']) { return $this->asJson([ 'code' => 1, 'msg' => '请填写骑手姓名' ]); } if (!$params['mobile']) { return $this->asJson([ 'code' => 1, 'msg' => '请填写手机号' ]); } if (!$params['real_code']) { return $this->asJson([ 'code' => 1, 'msg' => '请填写身份证号' ]); } if (!$params['real_just_pic']) { return $this->asJson([ 'code' => 1, 'msg' => '请上传身份证正面照' ]); } if (!$params['real_back_pic']) { return $this->asJson([ 'code' => 1, 'msg' => '请上传身份证反面照' ]); } if (!$params['type']) { return $this->asJson([ 'code' => 1, 'msg' => '请选择骑手类型' ]); } if ($params['type'] == 2 && !$params['store_id']) { return $this->asJson([ 'code' => 1, 'msg' => '请选择所属店铺' ]); } /* end */ $courier->real_name = $params['real_name']; $courier->mobile = $params['mobile']; $courier->real_code = $params['real_code']; $courier->real_just_pic = $params['real_just_pic']; $courier->real_back_pic = $params['real_back_pic']; $courier->type = $params['type']; $courier->store_id = $params['store_id']; $courier->saas_user_id = $saas_user->id; $courier->work_time = '[{"begin_time":"00:00","end_time":"00:00"}]'; $courier->area = '[{"province_id":0,"city_id":0,"district_id":0,"town_id":0,"village_id":0}]'; $courier->state = 1; if (!$courier->save()) { return $this->asJson([ 'code' => 1, 'msg' => $courier->errors ]); } return $this->asJson([ 'code' => 0, 'msg' => '申请已提交' ]); } }