| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\controllers\v1\worker;
- use app\constants\OptionSetting;
- use app\models\Admin;
- use app\models\AgentGoodsBindGoods;
- use app\models\AgentGoodsInstallLog;
- use app\models\BrowseLog;
- use app\models\Favorite;
- use app\models\Goods;
- use app\models\Level;
- use app\models\Address;
- use app\models\MdStaff;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\OrderForm;
- use app\models\OrderRefund;
- use app\models\SaasUser;
- use app\models\ShareHolder;
- use app\models\Store;
- use app\models\StoreShareMoney;
- use app\models\Supplier;
- use app\models\User;
- use app\models\UserCard;
- use app\models\UserCoupon;
- use app\models\VerifyCardSale;
- use app\models\VideoGoodsAuthor;
- use app\models\WechatConfig;
- use app\modules\admin\models\UserCenterForm;
- use app\modules\client\behaviors\Auth;
- use app\modules\client\controllers\BaseController;
- use app\modules\client\models\v1\CardListForm;
- use app\modules\client\models\v1\LevelPayDataForm;
- use app\modules\client\models\v1\OrderMemberForm;
- use app\modules\client\models\v1\QrcodeForm;
- use app\modules\client\models\v1\SaasUserForm;
- use app\modules\client\models\v1\UserForm;
- use app\modules\client\models\v1\AddressSaveForm;
- use app\modules\client\models\v1\AddressSetDefaultForm;
- use app\modules\client\models\v1\FavoriteAddForm;
- use app\modules\client\models\v1\FavoriteListForm;
- use app\modules\client\models\v1\TopicFavoriteForm;
- use app\modules\client\models\v1\TopicFavoriteListForm;
- use app\modules\client\models\v1\OrderListForm;
- use app\modules\client\models\v1\ShareForm;
- use app\utils\Sms;
- use app\utils\ShareQrcode;
- use app\utils\Wechat\Wechat;
- use EasyWeChat\Kernel\Exceptions\HttpException;
- use yii\base\BaseObject;
- use yii\data\Pagination;
- use yii\helpers\Json;
- use app\models\AccountLog;
- use app\models\Worker;
- use app\models\WorkerPic;
- use app\models\WorkerOrderExt;
- use app\models\WorkerGoods;
- use app\modules\client\models\v1\LoginForm;
- use app\modules\admin\models\worker\WorkerForm;
- use app\modules\client\models\v1\CashForm;
- use app\models\OrderComment;
- use app\models\WorkerSetting;
- use app\models\WorkerGoodsExt;
- use app\modules\client\models\v1\WorkerOrderForm;
- use app\modules\client\models\v1\worker\AdminForm;
- class AdminController extends BaseController
- {
- public $worker;
- public function behaviors()
- {
- return parent::behaviors();
- }
-
- public function beforeAction($action) {
- if (parent::beforeAction($action)) {
- try{
- if (input_params('worker_id')) {
- $where = [
- 'id' => input_params('worker_id')
- ];
- } else {
- $where = [
- 'user_id' => get_user_id()
- ];
- }
- $worker = Worker::findOne(array_merge($where, ['status' => [Worker::STATUS_VALID, Worker::STATUS_WAIT_AUDIT]]));
- if(!$worker){
- // throw new \Exception('用户不存在', 404);
- $this->asJson([
- 'user_id' => get_user_id(),
- 'action' => $action->id,
- 'code' => 0,
- 'msg' => 'ok',
- 'worker' => $worker,
- ]);
- return false;
- }
- // if($worker->status == Worker::STATUS_CLOSE){
- // $this->asJson([
- // 'user_id' => get_user_id(),
- // 'action' => $action->id,
- // 'code' => 1,
- // 'msg' => '已禁用',
- // ]);
- // return false;
- // }
- if($worker->status != Worker::STATUS_VALID){
- // throw new \Exception('用户状态异常', 401);
- $this->asJson([
- 'user_id' => get_user_id(),
- 'action' => $action->id,
- 'code' => 0,
- 'msg' => 'ok',
- 'worker' => $worker,
- ]);
- return false;
- }
- $this->worker = $worker;
- } catch (\Exception $ex) {
- $this->asJson([
- 'user_id' => get_user_id(),
- 'action' => $action->id,
- 'code' => 1,
- 'msg' => $ex->getMessage(),
- ]);
- return false;
- }
- return true;
- }
- return false;
- }
- /**
- * 用户中心
- * @return \yii\web\Response
- */
- public function actionIndex()
- {
- $level = $this->worker->levelInfo;
- $pics = $this->worker->pics;
- $this->worker->showBookTime();
- $data = [
- 'workerInfo' => $this->worker,
- 'levelName' => $level->name ?? '',
- 'pics' => $pics,
- ];
- $data['yesterday'] = [
- 'visited' => '待更新',
- 'orderCount' => '待更新',
- 'favCount' => '待更新',
- 'total_price' => '待更新',
- 'total_cash' => '待更新',
- 'is_price_yes' => '待更新',
- 'is_price_no' => '待更新',
- ];
- $data['favCount'] = (int)Favorite::find()->where(['worker_id' => $this->worker->id, 'is_delete' => 0])->count();
- $order_count = WorkerOrderExt::getWorkerOrderGroupStatus($this->worker->id);
- $data['order_count'] = $order_count;
- $data['orderCount'] = array_sum(array_column($order_count, 'cc'));
-
- $workerForm = new WorkerForm();
- $workerForm->store_id = get_store_id();
- $workerForm->worker_id = $this->worker->id;
- $workerProfitList = $workerForm->workerProfitList();
- $data['is_price_yes'] = sprintf('%.2f', $workerProfitList['sum_money'] - $workerProfitList['sumMoneyIsPriceNo']);
- $data['is_price_no'] = sprintf('%.2f', $workerProfitList['sumMoneyIsPriceNo']);
-
- $form = new ShareForm();
- $form->store_id = get_store_id();
- $form->user_id = $this->worker->user_id;
- $price = $form->getPrice();
- $data['total_price'] = $price['total_price'];
- $data['total_cash'] = $price['total_cash'];
-
- $res = [
- 'code' => 0,
- 'data' => $data,
- 'msg' => 'success'
- ];
- return $this->asJson($res);
- }
-
- public function actionOpenStatus()
- {
- $status = input_params('status');
- if ($status) {
- if (empty($this->worker->book_end_time)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败!请设置服务时间。'
- ]);
- }
- if (date('Hi') <= $this->worker->book_start_time || date('Hi') >= $this->worker->book_end_time) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败!当前不在营业时间。'
- ]);
- }
- }
- $this->worker->open_status = $status ? 1 : 0;
- $save = $this->worker->save();
- if(!$save){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败!' . array_shift($this->worker->getFirstErrors()),
- ]);
- }
- $res = [
- 'code' => 0,
- 'msg' => 'success'
- ];
- return $this->asJson($res);
- }
-
- public function actionUpdateInfo()
- {
- $keys = ['name', 'logo', 'desc', 'book_start_time','book_end_time','lat','lng','age','province_id','city_id','district_id','address'];
- $key = input_params('key');
- $val = input_params('val');
- if(is_string($key)){
- $key = [$key];
- $val = [$val];
- }
- foreach ($key as $k => $item) {
- if(!in_array($item, $keys)){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败!参数错误',
- ]);
- }
- $this->worker->$item = $val[$k];
- }
- if(!$this->worker->validate()){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败!' . array_shift($this->worker->getFirstErrors()),
- ]);
- }
- $save = $this->worker->save();
- if(!$save){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败!' . array_shift($this->worker->getFirstErrors()),
- ]);
- }
- $res = [
- 'code' => 0,
- 'msg' => 'success'
- ];
- return $this->asJson($res);
- }
-
- public function actionUpdateTel()
- {
- $form = new LoginForm();
- $form->attributes = post_params();
- $form->store_id = get_store_id();
- $form->user = get_user();
- $verifySmsCode = $form->verifySmsCode(LoginForm::CACHE_KEY_BIND_PHONE);
- if ($verifySmsCode['code'] != 0) {
- return $this->asJson($verifySmsCode);
- }
- $this->worker->tel = $form->phone;
- $save = $this->worker->save();
- if(!$save){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败!' . array_shift($this->worker->getFirstErrors()),
- ]);
- }
- $res = [
- 'code' => 0,
- 'msg' => 'success'
- ];
- return $this->asJson($res);
- }
-
- public function actionAddPics()
- {
- $pics = input_params('pics');
- WorkerPic::saveAll($this->worker->id, $pics);
- $res = [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $this->worker->pics,
- ];
- return $this->asJson($res);
- }
-
- public function actionDelPics()
- {
- $ids = input_params('ids');
- WorkerPic::del($this->worker->id, $ids);
- $res = [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $this->worker->pics,
- ];
- return $this->asJson($res);
- }
-
- //佣金信息
- public function actionProfitList()
- {
- $is_price_no = input_params('is_price_no', 0);
- $workerForm = new WorkerForm();
- $workerForm->store_id = get_store_id();
- $workerForm->worker_id = $this->worker->id;
- $workerProfitList = $workerForm->workerProfitList($is_price_no);
- $user = User::find()->where(['id' => $this->worker->user_id])->one();
- //获取分销佣金及提现
- $form = new ShareForm();
- $form->store_id = get_store_id();
- $form->user_id = get_user()->id;
- $sharePrice = $form->getPrice(\app\models\Cash::IS_CASH_TYPE_WORKER);
- $res = [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $workerProfitList,
- 'total_price' => $user->total_price,
- 'price' => $user->price,
- 'sharePrice' => $sharePrice,
- ];
- return $this->asJson($res);
- }
- /**
- * 申请提现
- */
- public function actionCashApply()
- {
- $form = new CashForm();
- $form->user_id = $this->worker->user_id;
- $form->store_id = get_store_id();
- $form->attributes = post_params();
- return $this->asJson($form->cashWorker());
- }
-
- /**
- * 关联产品
- */
- public function actionAddGoods()
- {
- $ids = input_params('ids');
- WorkerGoods::saveAll($this->worker->id, $ids);
- $res = [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $this->worker->goods,
- 'worker_id' => $this->worker->id,
- ];
- return $this->asJson($res);
- }
-
- public function actionDelGoods()
- {
- $ids = input_params('ids');
- WorkerGoods::del($this->worker->id, $ids);
- $res = [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $this->worker->goods,
- ];
- return $this->asJson($res);
- }
- //绑定商品列表
- public function actionGoodsList()
- {
- $type = input_params('type');
- $name = input_params('name');
- $cat_id = input_params('cat_id');
- $orderby = input_params('orderby');
- $begin = input_params('begin');
- $end = input_params('end');
- $store_id = get_store_id();
- $whereGoods = [
- 'g.store_id' => $store_id,
- 'g.product_type' => Goods::GOODS_TYPE_WORKER,
- 'g.is_delete' => 0,
- 'g.status' => 1,
- ];
- $queryGoods = Goods::find()->alias('g')->where($whereGoods);
- $queryGoodsBind = WorkerGoods::find()->alias('wg')->leftJoin(['g' => Goods::tableName()], 'g.id = wg.goods_id')->where([
- 'wg.status' => 1,
- 'wg.worker_id' => $this->worker->id,
- ])->andWhere($whereGoods);
- $queryGoodsBind->leftJoin(['wge' => WorkerGoodsExt::tableName()], 'g.id = wge.goods_id');
- $queryGoodsBind->andWhere(['OR', ['wge.bind_worker' => 0], ['wge.bind_worker' => 1, 'wg.public' => 1, 'wg.status' => 1]]);
- $queryGoods->leftJoin(['wge' => WorkerGoodsExt::tableName()], 'g.id = wge.goods_id');
- $queryGoods->leftJoin(['wg' => WorkerGoods::find()->where(['worker_id' => $this->worker->id])], 'g.id = wg.goods_id');
- $select = 'g.name, g.cover_pic, g.price, g.original_price, wge.*, wg.status is_bind';
- $queryGoods->select($select);
- $queryGoods->andWhere(['OR', ['wge.bind_worker' => 0], ['wge.bind_worker' => 1, 'wg.public' => 1, 'wg.status' => 1]]);
- if($name){
- $queryGoods->andWhere(['like', 'g.name', $name]);
- $queryGoodsBind->andWhere(['like', 'g.name', $name]);
- }
- if($begin){
- $queryGoods->andWhere(['>=', 'g.created_at', $begin]);
- $queryGoodsBind->andWhere(['>=', 'g.created_at', $begin]);
- }
- if($end){
- $queryGoods->andWhere(['<=', 'g.created_at', $end]);
- $queryGoodsBind->andWhere(['<=', 'g.created_at', $end]);
- }
- if($cat_id > 0){
- $queryGoods->andWhere(['wge.cat_id' => $cat_id]);
- $queryGoodsBind->andWhere(['wge.cat_id' => $cat_id]);
- }
- $queryOrderBy = 'g.id DESC';
- if($orderby == 1){
- $queryOrderBy = 'g.id DESC';
- }else if($orderby == 2){
- $queryOrderBy = 'g.id ASC';
- }else if($orderby == 3){
- $queryOrderBy = 'g.price DESC';
- }else if($orderby == 4){
- $queryOrderBy = 'g.price ASC';
- }
- $queryGoods->orderBy($queryOrderBy);
- $queryGoodsBind->orderBy($queryOrderBy);
- $queryGoods->groupBy('g.id');
- $queryGoodsBind->groupBy('g.id');
- if($type == 0){
- $data = pagination_make($queryGoods);
- }
- if($type == 1){
- $queryGoodsBind->select($select);
- $data = pagination_make($queryGoodsBind);
- }
- if($type == 2){
- $queryGoodsBindNo = clone $queryGoods;
- $queryUnBind = WorkerGoods::find()->where(['worker_id' => $this->worker->id, 'status' => 1])->select('goods_id');
- $queryGoodsBindNo->andWhere(['not in', 'g.id', $queryUnBind]);
- $data = pagination_make($queryGoodsBindNo);
- }
- $count = $queryGoods->count();
- $countGoodsBind = $queryGoodsBind->count();
- $countGoodsBindNo = $count > $countGoodsBind ? $count - $countGoodsBind : 0;
- $res = [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- 'count' => $count,
- 'countGoodsBind' => $countGoodsBind,
- 'countGoodsBindNo' => $countGoodsBindNo,
- 'q' => $queryGoods->createCommand()->getRawSql(),
- ];
- return $this->asJson($res);
- }
- //评价列表
- public function actionCommentList()
- {
- $adminForm = new AdminForm();
- $adminForm->store_id = get_store_id();
- $adminForm->worker_id = $this->worker->id;
- $adminForm->score = input_params('score');
- $adminForm->name = input_params('name');
- $adminForm->orderby = input_params('orderby');
- $adminForm->begin = input_params('begin');
- $adminForm->end = input_params('end');
- $res = $adminForm->commentList();
- return $this->asJson($res);
- }
- //待抢单列表
- public function actionWaitBindOrderList()
- {
- $store_id = get_store_id();
- $type = input_params('type', 1);
- $setting = WorkerSetting::getByStoreId($store_id);
- $orderSpace = $setting['order_space'];
- $sql = 'SELECT
- woe.order_id
- FROM
- cyy_worker_order_ext woe
- LEFT JOIN (
- SELECT
- count(id) num,
- order_id
- FROM
- cyy_order_detail
- WHERE
- goods_id IN (
- SELECT
- goods_id
- FROM
- cyy_worker_goods
- WHERE
- worker_id = :worker_id
- )
- GROUP BY
- order_id
- ) od ON od.order_id = woe.order_id
- LEFT JOIN (
- SELECT
- count(id) num2,
- order_id
- FROM
- cyy_order_detail
- GROUP BY
- order_id
- ) od2 ON od2.order_id = woe.order_id
- WHERE
- woe.status_ext = :status_ext
- AND od.num = od2.num2';
- $queryOrderId = \Yii::$app->db->createCommand($sql, [':worker_id' => $this->worker->id, ':status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND])->getRawSql();
- // $queryGoods = WorkerGoods::find()->where(['worker_id' => $this->worker->id, 'status' => 1])->select('goods_id');
- $queryOrder = WorkerOrderExt::find()->alias('woe')->leftJoin([
- 'od' => OrderDetail::tableName()
- ], 'od.order_id = woe.order_id')->leftJoin([
- 'o' => Order::tableName()
- ], 'o.id = woe.order_id')->leftJoin([
- 'wge' => WorkerGoodsExt::tableName()
- ], 'od.goods_id = wge.goods_id')->leftJoin([
- 'u' => User::tableName()
- ], 'o.user_id = u.id')->leftJoin([
- 'su' => SaasUser::tableName()
- ], 'su.mobile = u.binding')->where([
- 'woe.status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND,
- // 'od.goods_id' => $queryGoods,
- // 'o.id' => $queryOrderId,
- ])->andWhere('o.id in ('. $queryOrderId .')');
- $cacheV = WorkerOrderExt::cacheIgnoreOrderId($this->worker->id);
- $ignore_ids = [];
- if($cacheV){
- $ignore_ids = array_keys($cacheV);
- }
- if($type == 1){
- $queryOrder->andWhere(['not in', 'woe.order_id', $ignore_ids]);
- }else{
- $queryOrder->andWhere(['woe.order_id' => $ignore_ids]);
- }
- // $queryDis = 'ROUND(st_distance_sphere(point('. ($this->worker->lng ?? 0) .', '. ($this->worker->lat ?? 0) .'), point(woe.lng, woe.lat)), 2)';
- $latitude = $this->worker->lat ?: 0;
- $longitude = $this->worker->lng ?: 0;
- $queryOrder->leftJoin(['woe_' => "(select id, acos(cos({$latitude}*pi()/180 )*cos(lat*pi()/180)*cos({$longitude}*pi()/180 -lng*pi()/180)+sin({$latitude}*pi()/180 )*sin(lat*pi()/180))*6370996.81 as
- distance from cyy_worker_order_ext)"], 'woe_.id = woe.id');
- $queryOrder->andWhere(['<=', 'woe_.distance', $orderSpace]);
- $queryOrder->select($this->orderSelect());
- $queryOrder->addSelect(['woe_.distance dis']);
- $data = pagination_make($queryOrder);
- $this->formatList($data['list'], $ignore_ids);
- $res = [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- 'q' => $queryOrder->createCommand()->getRawSql(),
- ];
- return $this->asJson($res);
- }
- public function formatList(&$list, $ignore_ids) {
- foreach ($list as &$item) {
- $item['ignore'] = in_array($item['id'], $ignore_ids) ? 1 : 0;
- $goods_list = OrderDetail::find()->where(['order_id' => $item['id'], 'is_delete' => 0])->select('goods_id, goods_name, num, total_price, pic')->asArray()->all();
- foreach($goods_list as &$gitem){
- $WorkerGoodsExt = WorkerGoodsExt::find()->where(['goods_id' => $gitem['goods_id']])->select('desc')->one();
- $gitem['worker_goods_ext_desc'] = $WorkerGoodsExt['desc'];
- }
- $item['goods_list'] = $goods_list;
- $item['dis'] = self::distance($item['dis']);
- $item['order_form']['list'] = OrderForm::find()->where(['order_id' => $item['id']])->asArray()->all();
- foreach ($item['order_form']['list'] as &$form_item) {
- $form_item['default'] = $form_item['value'];
- if ($form_item['type'] == "checkbox" || $form_item['type'] == "radio") {
- $list = explode(',', $form_item['value']);
- $list_arr = [];
- foreach ($list as &$list_item) {
- $arr = [];
- $arr['name'] = $list_item;
- $arr['checked'] = true;
- array_push($list_arr, $arr);
- }
- $form_item['default_list'] = $list_arr;
- }
- $form_item['name'] = $form_item['key'];
- unset($form_item['key']);
- }
- }
- }
- //抢单
- public function actionOrderBind()
- {
- $order_id = input_params('order_id');
- $type = input_params('type', 1);
- if($type == 2){
- $cacheV = WorkerOrderExt::cacheIgnoreOrderId($this->worker->id, $order_id);
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- ]);
- }
- $cacheK = 'WorkerOrderBind' . $order_id;
- $cacheV = cache()->get($cacheK);
- if($cacheV){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '获取缓存锁失败',
- ]);
- }
- cache()->set($cacheK, 1, 10);
- $t = \Yii::$app->db->beginTransaction();
- try{
- $orderExt = WorkerOrderExt::findOne(['order_id' => $order_id, 'worker_id' => 0, 'status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND]);
- if(!$orderExt){
- throw new \Exception('参数错误');
- }
- $orderExt = WorkerOrderExt::find()->where('id = :id FOR UPDATE', [':id' => $orderExt->id])->one();
- if(!$orderExt){
- throw new \Exception('锁失败' . array_shift($orderExt->getFirstErrors()));
- }
- if($type == 1){
- $orderExt->worker_id = $this->worker->id;
- $orderExt->status_ext = WorkerOrderExt::STATUS_EXT_HAS_BIND;
- $orderExt->time_has_bind = time();
- $orderExt->is_hold = 1;
- }
- $save = $orderExt->save();
- if(!$save){
- throw new \Exception('ext保存失败' . array_shift($orderExt->getFirstErrors()));
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- ]);
- } catch (\Exception $ex) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败,' . $ex->getMessage(),
- ]);
- }
- }
- public function orderSelect() {
- return 'o.*, od.goods_name, od.num, od.total_price g_total_price, od.pic, wge.desc, woe.status_ext, su.name su_name, su.avatar su_avatar';
- }
- //订单列表
- public function actionOrderList()
- {
- $order_id = input_params('order_id');
- $status_ext = input_params('status_ext');
- $cat_id = input_params('cat_id');
- $name = input_params('name');
- $orderby = input_params('orderby');
- $begin = input_params('begin');
- $end = input_params('end');
- $is_hold = input_params('is_hold');
- $queryOrder = WorkerOrderExt::find()->alias('woe')->leftJoin([
- 'od' => OrderDetail::tableName()
- ], 'od.order_id = woe.order_id')->leftJoin([
- 'o' => Order::tableName()
- ], 'o.id = woe.order_id')->leftJoin([
- 'wge' => WorkerGoodsExt::tableName()
- ], 'od.goods_id = wge.goods_id')->leftJoin([
- 'u' => User::tableName()
- ], 'o.user_id = u.id')->leftJoin([
- 'su' => SaasUser::tableName()
- ], 'su.mobile = u.binding');
- if($order_id){
- $queryOrder->andWhere(['woe.order_id' => $order_id]);
- $queryOrder->andWhere(['or', ['woe.status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND], ['woe.worker_id' => $this->worker->id]]);
- }else{
- $queryOrder->andWhere(['woe.worker_id' => $this->worker->id]);
- }
- if($status_ext){
- $queryOrder->andWhere(['status_ext' => $status_ext]);
- }
- if($is_hold){
- $queryOrder->andWhere(['woe.is_hold' => $is_hold]);
- }
- if($name){
- $queryOrder->andWhere(['like', 'od.goods_name', $name]);
- }
- if($begin){
- $queryOrder->andWhere(['>=', 'o.created_at', $begin]);
- }
- if($end){
- $queryOrder->andWhere(['<=', 'o.created_at', $end]);
- }
- if($cat_id > 0){
- $queryOrder->andWhere(['wge.cat_id' => $cat_id]);
- }
- $queryOrderBy = 'woe.id DESC';
- if($orderby == 1){
- $queryOrderBy = 'woe.id DESC';
- }else if($orderby == 2){
- $queryOrderBy = 'woe.id ASC';
- }else if($orderby == 3){
- $queryOrderBy = 'o.price DESC';
- }else if($orderby == 4){
- $queryOrderBy = 'o.price ASC';
- }
- $queryOrder->groupBy('o.id');
- $queryOrder->orderBy($queryOrderBy);
- $queryOrder->select($this->orderSelect());
- // $queryDis = 'ROUND(st_distance_sphere(point('. ($this->worker->lng ?? 0) .', '. ($this->worker->lat ?? 0) .'), point(woe.lng, woe.lat)), 2)';
- // $queryOrder->addSelect([$queryDis . ' dis']);
- $queryOrder->addSelect(['woe.dis']);
- $data = pagination_make($queryOrder);
- $cacheV = WorkerOrderExt::cacheIgnoreOrderId($this->worker->id);
- $ignore_ids = [];
- if($cacheV){
- $ignore_ids = array_keys($cacheV);
- }
- $this->formatList($data['list'], $ignore_ids);
- $order_count = WorkerOrderExt::getWorkerOrderGroupStatus($this->worker->id);
- $res = [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- 'order_count' => $order_count,
- 'orderCount' => array_sum(array_column($order_count, 'cc')),
- 'q' => $queryOrder->createCommand()->getRawSql(),
- ];
- return $this->asJson($res);
- }
- public static function distance($distance)
- {
- if ($distance == -1) {
- return -1;
- }
- if ($distance > 1000) {
- $distance = round($distance / 1000, 2) . 'km';
- } else {
- $distance = round($distance, 2);
- $distance .= 'm';
- }
- return $distance;
- }
- //订单操作
- public function actionOrderStatus()
- {
- $order_id = input_params('order_id');
- $type = input_params('type', 1);
- $cond = [
- 'order_id' => $order_id,
- 'worker_id' => $this->worker->id,
- ];
- $attr = [];
- if($type == 1){
- $cond = array_merge($cond, [
- 'status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND_OK,
- ]);
- $attr = [
- 'status_ext' => WorkerOrderExt::STATUS_EXT_HAS_BIND,
- 'time_has_bind' => time(),
- ];
- }
- if($type == 2){
- $cond = array_merge($cond, [
- 'status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND_OK,
- ]);
- $attr = [
- 'status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND,
- 'worker_id' => 0,
- ];
- }
- if($type == 3){
- $cond = array_merge($cond, [
- 'status_ext' => WorkerOrderExt::STATUS_EXT_HAS_BIND,
- ]);
- $attr = [
- 'status_ext' => WorkerOrderExt::STATUS_EXT_ON_ROAD,
- 'time_outgoing' => time(),
- ];
- }
- if($type == 4){
- $cond = array_merge($cond, [
- 'status_ext' => WorkerOrderExt::STATUS_EXT_ON_ROAD,
- ]);
- $attr = [
- 'status_ext' => WorkerOrderExt::STATUS_EXT_ARRIVE,
- 'time_reach' => time(),
- ];
- }
- if($type == 5){
- $cond = array_merge($cond, [
- 'status_ext' => WorkerOrderExt::STATUS_EXT_ARRIVE,
- ]);
- $attr = [
- 'status_ext' => WorkerOrderExt::STATUS_EXT_START,
- 'time_start_service' => time(),
- ];
- }
- if($type == 11){
- $cond = array_merge($cond, [
- 'status_ext' => WorkerOrderExt::STATUS_EXT_FINISH,
- ]);
- $attr = [
- 'worker_delete' => 1,
- ];
- }
- $orderExt = WorkerOrderExt::findOne($cond);
- if(!$orderExt){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误',
- ]);
- }
- $orderExt->setAttributes($attr, false);
- $save = $orderExt->save();
- if(!$save){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败,' . array_shift($orderExt->getFirstErrors()),
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- ]);
- }
- //加钟
- public function actionOrderAddTime()
- {
- $order_id = input_params('order_id');
- $type = input_params('type', 1);
- $val = input_params('val');
- if(!$val || !$type || !$order_id || ($val <= 0)){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误',
- ]);
- }
- if($type == 1){
- $fee = $val;
- }
- if($type == 2){
- $goodsCount = $val;
- }
- $orderAddTime = WorkerOrderForm::orderAddTime($this->worker->id, $order_id, $fee, $goodsCount);
- return $this->asJson($orderAddTime);
- }
- //尾款
- public function actionOrderFinishPay()
- {
- $order_id = input_params('order_id');
- $fee = input_params('fee');
- if(!$fee || !$order_id || ($fee <= 0)){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误',
- ]);
- }
- $orderFinishPay = WorkerOrderForm::orderFinishPay($this->worker->id, $order_id, $fee);
- return $this->asJson($orderFinishPay);
- }
- }
|