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); } }