| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- /**
- * @file LocalDeliveryController
- * @editor Created by vscode
- * @author WPing丶
- * @date 2023/07/06
- * @time 11:56:13
- *
- * 备注:同城配送服务端相关接口
- */
- namespace app\modules\client\controllers\v1;
- use app\constants\OptionSetting;
- use app\models\DeliveryInfo;
- use app\models\District;
- use app\models\Lg;
- use app\models\LocalDeliveryCash;
- use app\models\LocalDeliveryCourier;
- use app\models\LocalDeliveryCourierComment;
- use app\models\LocalDeliveryCourierCommentExt;
- use app\models\LocalDeliveryCourierTag;
- use app\models\LocalDeliveryFreight;
- use app\models\LocalDeliveryLog;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\Store;
- use app\models\WorkerOrderExt;
- use app\modules\alliance\controllers\BaseController;
- use app\modules\admin\models\OrderListForm;
- use app\utils\OrderNo;
- use app\utils\Tools;
- use yii\helpers\Json;
- class LocalDeliveryController extends BaseController
- {
- //获取配送首页状态
- public function actionGetCourierInfo() {
- $saas_user = get_saas_user();
- $rider = LocalDeliveryCourier::find()->where(['saas_user_id' => $saas_user->id, 'is_delete' => 0, 'state' => 2])->asArray()->one();
- if(!empty($rider)) {
- if (intval($rider['store_id']) === intval(get_store_id())) {
- $area = Json::decode($rider['area'], true);
- $village = District::findOne($area[0]['village_id']);
- $town = District::findOne($area[0]['town_id']);
- $district = District::findOne($area[0]['district_id']);
- $city = District::findOne($area[0]['city_id']);
- $province = District::findOne($area[0]['province_id']);
- if ($village) {
- $rider['area_name'] = $province->name . '/' . $city->name . '/' . $district->name . '/' . $town->name . '/' . $village->name;
- } elseif ($town) {
- $rider['area_name'] = $province->name . '/' . $city->name . '/' . $district->name . '/' . $town->name;
- } elseif ($district) {
- $rider['area_name'] = $province->name . '/' . $city->name . '/' . $district->name;
- } else {
- $rider['area_name'] = '';
- }
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '非当前店铺骑手',
- 'data' => $rider,
- ]);
- }
- }else{
- $rider = LocalDeliveryCourier::find()->where(['saas_user_id' => $saas_user->id, 'is_delete' => 0, 'state' => 1, 'store_id' => get_store_id()])->asArray()->one();
- if($rider){
- $rider = [
- 'code' => 101,
- 'msg' => '正在审核,暂时无法操作',
- 'data' => $rider,
- ];
- } else {
- $rider = [
- 'code' => 100,
- 'msg' => '需要申请店铺骑手'
- ];
- }
- }
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'store')['value'];
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'pay', $local_type)['value'];
- $store_id = 0;
- if ($local_type === 'self_store') {
- $store_id = get_store_id();
- }
- //同城配送设置相关
- $values = Option::find()->where(['store_id' => $store_id,
- 'group' => OptionSetting::LOCAL_DELIVERY_GROUP_NAME, 'name' => OptionSetting::LOCAL_DELIVERY_SETTING])->select('value')->one();
- if($values){
- $local_setting = json_decode($values->value, true);
- }else{
- $local_setting = null;
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'courier' => $rider,
- 'local_setting' => $local_setting
- ]
- ]);
- }
- //抢单
- public function actionOrderBind()
- {
- $order_id = input_params('order_id');
- $type = input_params('type', 1);
- $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($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 (!$delivery_info) {
- $local_delivery_setting = Option::get(OptionSetting::LOCAL_DELIVERY_SETTING, get_store_id(), OptionSetting::LOCAL_DELIVERY_GROUP_NAME);
- $local_delivery_setting = json_decode($local_delivery_setting['value'], true);
- $default_time = time() + (($local_delivery_setting['default_time']['value'] ?? 0) * 60);
- $goods_list = OrderDetail::find()->where(['order_id' => $order_id, 'is_delete' => 0])
- ->select('num, total_price')->asArray()->all();
- foreach ($goods_list as &$goods_item) {
- $goods_item['price'] = $goods_item['total_price'] / $goods_item['num'];
- }
- $address_data = $order->address_data;
- $address_data = json_decode($address_data, true);
- $getFreight = LocalDeliveryFreight::getFreight(get_store_id(), [
- 'latitude' => $address_data['latitude'],
- 'longitude' => $address_data['longitude']
- ], $default_time, LocalDeliveryFreight::TYPE_STORE_LOCAL, $goods_list, LocalDeliveryFreight::IS_SCAN_YES);
- if($getFreight['code'] != 0){
- throw new \Exception($getFreight['msg']);
- }
- $delivery_info = new DeliveryInfo();
- $delivery_info->store_id = get_store_id();
- $delivery_info->order_no = $order->order_no;
- $delivery_info->fee = $getFreight['data'];
- $delivery_info->delivery_type = 0;
- $delivery_info->created_at = time();
- $delivery_info->is_local = DeliveryInfo::IS_LOCAL_YSE;
- $delivery_info->is_store_delivery_type = 1;
- $serial_num = DeliveryInfo::find()->where(['>','created_at',strtotime(date('Y-m-d'))])->orderBy('serial_num desc')->asArray()->one()['serial_num'];
- $delivery_info->serial_num = $serial_num > 0 ? $serial_num + 1 : 1;
- $delivery_info->save();
- }
- if (intval($delivery_info->is_store_delivery_type) === 0) {
- throw new \Exception('当前配送订单只能由平台配送员配送');
- }
- $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() . $ex->getLine(),
- ]);
- }
- }
- /**
- * 模块名: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');
- $order_no = input_params('order_no');
- $order_id = input_params('order_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' => '当前用户不是骑手',
- ]);
- }
- if ($order_id) {
- $order = Order::findOne($order_id);
- if ($order) {
- $order_no = $order->order_no;
- }
- }
- $query = Order::find()->alias('o')
- ->leftJoin(['di' => DeliveryInfo::tableName()], 'o.order_no = di.order_no')
- ->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')->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(['o.district_id' => $courierDistrict]);
- $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')]);
- }
- if ($order_no) {
- $query->andWhere(['di.order_no' => $order_no]);
- }
- // $sql = $query->createCommand()->getRawSql();
- // return $this->asJson([
- // 'code' => 0,
- // 'msg' => 'success',
- // 'data' => $sql
- // ]);
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'store')['value'];
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'pay', $local_type)['value'];
- $store_id = 0;
- if ($local_type === 'self_store') {
- $store_id = get_store_id();
- }
- debug_log($query->createCommand()->getRawSql(), 'courier.log');
- $setting = json_decode(Option::get(OptionSetting::LOCAL_DELIVERY_SETTING, $store_id, '', '{}')['value'], true);
- $list = pagination_make($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;
- 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 = [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ];
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data
- ]);
- }
- //扫码获取订单
- public function actionScanGetOrder() {
- $lng = input_params('lng');
- $lat = input_params('lat');
- $order_no = input_params('order_no');
- $order_id = input_params('order_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' => '当前用户不是骑手',
- ]);
- }
- if ($order_id) {
- $order = Order::findOne($order_id);
- if ($order) {
- $order_no = $order->order_no;
- }
- }
- $query = Order::find()
- ->where(['order_type' => Order::ORDER_TYPE_STORE])
- ->andWhere(['OR', ['trade_status' => Order::ORDER_FLOW_NO_SEND], ['trade_status' => [Order::ORDER_FLOW_DEFAULT, Order::ORDER_FLOW_NO_SEND], 'pay_type' => Order::PAY_TYPE_COD]])//增加扫码后可以抢其他类型(快递、自提、收银台等)的订单逻辑
- ->select('id, order_no, store_id, total_price, pay_price, name, mobile, address, created_at, trade_status, delivery_time, pay_time, address_data, remark')->orderBy('created_at DESC');
- if ($order_no) {
- $query->andWhere(['order_no' => $order_no]);
- }
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'store')['value'];
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'pay', $local_type)['value'];
- $store_id = get_store_id();
- $setting = json_decode(Option::get(OptionSetting::LOCAL_DELIVERY_SETTING, $store_id, '', '{}')['value'], true);
- $list = pagination_make($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'];
- $delivery_info = DeliveryInfo::findOne(['order_no' => $val['order_no']]);
- // 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
- $val['d_order_id'] = $delivery_info->id ?? 0;
- $val['fee'] = $delivery_info->fee ?? null;
- $val['rider_id'] = $delivery_info->rider_id ?? '';
- $val['rider_name'] = $delivery_info->rider_name ?? '';
- $val['rider_mobile'] = $delivery_info->rider_mobile ?? '';
- $val['arrive_time'] = $delivery_info->arrive_time ?? '';
- $val['rush_time'] = $delivery_info->rush_time ?? '';
- $val['confirm_time'] = $delivery_info->confirm_time ?? '';
- $val['serial_num'] = $delivery_info->serial_num ?? '';
- if ($val['delivery_time'] <= 0) {
- $val['delivery_time'] = $val['created_at'] + (($setting['default_time']['value'] ?? 0) * 60);
- }
- if (!isset($val['fee'])) {
- $local_delivery_setting = Option::get(OptionSetting::LOCAL_DELIVERY_SETTING, $store_id, OptionSetting::LOCAL_DELIVERY_GROUP_NAME);
- $local_delivery_setting = json_decode($local_delivery_setting['value'], true);
- $default_time = time() + (($local_delivery_setting['default_time']['value'] ?? 0) * 60);
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, $this->store_id, 'store')['value'];
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, $this->store_id, 'pay', $local_type)['value'];
- $type = 0;
-
- $goods_list = OrderDetail::find()->where(['order_id' => $val['id'], 'is_delete' => 0])
- ->select('num, total_price')->asArray()->all();
- foreach ($goods_list as &$goods_item) {
- $goods_item['price'] = $goods_item['total_price'] / $goods_item['num'];
- }
- $getFreight = LocalDeliveryFreight::getFreight($store_id, [
- 'latitude' => $address_data['latitude'],
- 'longitude' => $address_data['longitude']
- ], $default_time, LocalDeliveryFreight::TYPE_STORE_LOCAL, $goods_list, LocalDeliveryFreight::IS_SCAN_YES);
- if($getFreight['code'] != 0){
- return $getFreight;
- }
- $val['fee'] = $getFreight['data'];
- }
- $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'] = '';
- }
- $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;
- }
- $data = [
- '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()
- {
- $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_WAITING) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '订单状态发生变化,请刷新列表',
- ]);
- }
- $delivery_info->local_status = DeliveryInfo::LOCAL_STATUS_SENDING;
- $delivery_info->arrive_time = time();
- if (!$delivery_info->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $delivery_info->errors[0],
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '取货成功',
- ]);
- }
- /**
- * 模块名: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 = get_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']){
- $order->trade_status = Order::ORDER_FLOW_CONFIRM;
- $order->confirm_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' => '发放骑手佣金报错',
- ]);
- }
- }
- $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();
- //同城配送设置相关
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'store')['value'];
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'pay', $local_type);
- $store_id = $local_type['value'] === 'self_store' ? get_store_id() : 0;
- $values = Option::find()->where([
- 'store_id' => $store_id,
- 'group' => OptionSetting::LOCAL_DELIVERY_GROUP_NAME, 'name' => OptionSetting::LOCAL_DELIVERY_SETTING
- ])->select('value')->one();
- $local_setting = json_decode($values->value, true);
- if ($store_id > 0) {
- (float)$local_setting['default_cost']['value'] = -1;
- }
- //今日未到账收入(配送中)
- $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, //提现手续费
- 'pay_type_list' => $local_setting['pay_type']['value'],
- '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','lg'])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数非法',
- ]);
- }
- //商城余额是否充足
- if ($price > $courier->money) {
- \Yii::error([__METHOD__, $price, $courier->money]);
- return $this->asJson([
- 'code' => 1,
- 'msg' => '可提现金额不足'
- ]);
- }
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'store')['value'];
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'pay', $local_type)['value'];
- $store_id = 0;
- if ($local_type === 'self_store') {
- $store_id = get_store_id();
- }
- //同城配送设置相关
- $values = Option::find()->where([
- 'store_id' => $store_id,
- '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'];
- }
- if ($type == 'lg') {
- //灵工提现
- $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0,'status'=>1])->one();
- $cash->type = 4;
- $cash->name = $lg_info->name;
- $cash->mobile = $lg_info->mobile;
- }
- $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();
- $store_id = get_store_id();
- if (\Yii::$app->request->isGet) {
- $courier = LocalDeliveryCourier::findOne(['is_delete' => 0, 'saas_user_id' => $saas_user->id, 'store_id' => $store_id]);
- if (empty($courier)) {
- $courier = LocalDeliveryCourier::findOne(['is_delete' => 0, 'saas_user_id' => $saas_user->id]);
- if ($courier) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '当前账户已在其他店铺申请'
- ]);
- }
- }
- $area = json_decode($courier->area, true);
- if (!empty($area)) {
- $area = $area[0];
- $province = District::findOne($area['province_id'])->name ?: '';
- $city = District::findOne($area['city_id'])->name ?: '';
- $district = District::findOne($area['district_id'])->name ?: '';
- $area_name = [
- 'province' => $province,
- 'city' => $city,
- 'district' => $district
- ];
- $area = array_merge($area, $area_name);
- $courier->area = json_encode([$area], JSON_UNESCAPED_UNICODE);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '',
- 'data' => [
- 'real_name' => $courier->real_name ?: '',
- 'mobile' => $courier->mobile ?: '',
- 'real_code' => $courier->real_code ?: '',
- 'real_just_pic' => $courier->real_just_pic ?: '',
- 'real_back_pic' => $courier->real_back_pic ?: '',
- 'area' => $courier->area ?: '[{"province_id":0,"city_id":0,"district_id":0,"town_id":0,"village_id":0,"province":"","city":"","district":""}]',
- 'address' => $courier->address ?: '',
- 'state' => intval($courier->state) ?: 0
- ]
- ]);
- }
- $params = post_params();
- $courier = LocalDeliveryCourier::findOne(['is_delete' => 0, 'saas_user_id' => $saas_user->id, 'store_id' => $store_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 = LocalDeliveryCourier::findOne(['is_delete' => 0, 'saas_user_id' => $saas_user->id]);
- if ($courier) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '当前账户已在其他店铺申请'
- ]);
- }
- $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' => '请上传身份证反面照'
- ]);
- }
- /* end */
- $courier->store_id = get_store_id();
- $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 = 2;
- $courier->saas_user_id = $saas_user->id;
- $courier->work_time = '[{"begin_time":"00:00","end_time":"00:00"}]';
- $courier->area = $params['area'] ?: '[{"province_id":0,"city_id":0,"district_id":0,"town_id":0,"village_id":0}]';
- $courier->state = 1;
- $courier->address = $params['address'];
- $courier->avatar = $saas_user->avatar;
- if (!$courier->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $courier->errors
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '申请已提交'
- ]);
- }
- //订单骑手评价
- public function actionOrderRiverComment() {
- $order_no = post_params('order_no');
- $star = post_params('star');
- $tag_ids = post_params('tag_ids');
- $delivery_info = DeliveryInfo::findOne(['order_no' => $order_no, 'is_delete' => 0]);
- if (empty($delivery_info)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '订单查询失败'
- ]);
- }
- if ($delivery_info->local_status !== DeliveryInfo::LOCAL_STATUS_CONFIRM) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '订单未送达 不可操作'
- ]);
- }
- $courier = LocalDeliveryCourier::findOne($delivery_info->rider_id);
- if (empty($courier)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '骑手未找到'
- ]);
- }
- $courierComment = LocalDeliveryCourierComment::findOne(['d_order_id' => $delivery_info->id]);
- if (!empty($courierComment)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '已评价不可重复操作'
- ]);
- }
- if (!in_array($star, range(1, 5))) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '星级错误'
- ]);
- }
- $tag_ids = explode(',', $tag_ids);
- $new_tag_ids = [];
- foreach ($tag_ids as $tag_id) {
- $courierTag = LocalDeliveryCourierTag::findOne($tag_id);
- if ($courierTag) {
- $new_tag_ids[] = $tag_id;
- }
- }
- $t = \Yii::$app->db->beginTransaction();
- try {
- $courierComment = new LocalDeliveryCourierComment();
- $courierComment->d_order_id = $delivery_info->id;
- $courierComment->rider_id = $courier->id;
- $courierComment->store_id = get_store_id();
- $courierComment->star = $star;
- if (!$courierComment->save()) {
- throw new \Exception(json_encode($courierComment->errors, JSON_UNESCAPED_UNICODE));
- }
- foreach ($new_tag_ids as $new_tag_id) {
- $courierCommentTag = new LocalDeliveryCourierCommentExt();
- $courierCommentTag->comment_id = $courierComment->id;
- $courierCommentTag->tag_id = $new_tag_id;
- $courierCommentTag->rider_id = $courier->id;
- if (!$courierCommentTag->save()) {
- throw new \Exception(json_encode($courierCommentTag->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作成功'
- ]);
- } catch (\Exception $e) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ]);
- }
- }
- //订单骑手信息
- public function actionRiderInfo() {
- $rider_id = get_params('rider_id');
- $courier = LocalDeliveryCourier::findOne($rider_id);
- if (empty($courier)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '骑手不存在'
- ]);
- }
- $rider = [
- 'real_name' => $courier->real_name,
- 'avatar' => $courier->avatar,
- 'mobile' => $courier->mobile,
- 'pass_rate' => ($courier->pass_rate * 100) . "%",
- 'star' => ($courier->star * 100) . "%"
- ];
- //总里程
- $rider['distance'] = DeliveryInfo::find()->where(['rider_id' => $rider_id])->sum('distance') ?: '0.00';
- //标签
- $tags = LocalDeliveryCourierCommentExt::find()->where(['rider_id' => $rider_id, 'is_delete' => 0])
- ->select('tag_id, COUNT(tag_id) as num')->groupBy('tag_id')->asArray()->all();
- $rider['tags'] = [];
- foreach ($tags as $tag) {
- $courierTag = LocalDeliveryCourierTag::findOne($tag['tag_id']);
- if ($courierTag) {
- $rider['tags'][] = [
- 'tag' => $courierTag->tag,
- 'num' => $tag['num'],
- 'star' => $courierTag->star
- ];
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'rider_info' => $rider
- ]
- ]);
- }
- }
|