AdminController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\controllers\v1\worker;
  8. use app\constants\OptionSetting;
  9. use app\models\Admin;
  10. use app\models\AgentGoodsBindGoods;
  11. use app\models\AgentGoodsInstallLog;
  12. use app\models\BrowseLog;
  13. use app\models\Favorite;
  14. use app\models\Goods;
  15. use app\models\Level;
  16. use app\models\Address;
  17. use app\models\MdStaff;
  18. use app\models\Option;
  19. use app\models\Order;
  20. use app\models\OrderDetail;
  21. use app\models\OrderForm;
  22. use app\models\OrderRefund;
  23. use app\models\SaasUser;
  24. use app\models\ShareHolder;
  25. use app\models\Store;
  26. use app\models\StoreShareMoney;
  27. use app\models\Supplier;
  28. use app\models\User;
  29. use app\models\UserCard;
  30. use app\models\UserCoupon;
  31. use app\models\VerifyCardSale;
  32. use app\models\VideoGoodsAuthor;
  33. use app\models\WechatConfig;
  34. use app\modules\admin\models\UserCenterForm;
  35. use app\modules\client\behaviors\Auth;
  36. use app\modules\client\controllers\BaseController;
  37. use app\modules\client\models\v1\CardListForm;
  38. use app\modules\client\models\v1\LevelPayDataForm;
  39. use app\modules\client\models\v1\OrderMemberForm;
  40. use app\modules\client\models\v1\QrcodeForm;
  41. use app\modules\client\models\v1\SaasUserForm;
  42. use app\modules\client\models\v1\UserForm;
  43. use app\modules\client\models\v1\AddressSaveForm;
  44. use app\modules\client\models\v1\AddressSetDefaultForm;
  45. use app\modules\client\models\v1\FavoriteAddForm;
  46. use app\modules\client\models\v1\FavoriteListForm;
  47. use app\modules\client\models\v1\TopicFavoriteForm;
  48. use app\modules\client\models\v1\TopicFavoriteListForm;
  49. use app\modules\client\models\v1\OrderListForm;
  50. use app\modules\client\models\v1\ShareForm;
  51. use app\utils\Sms;
  52. use app\utils\ShareQrcode;
  53. use app\utils\Wechat\Wechat;
  54. use EasyWeChat\Kernel\Exceptions\HttpException;
  55. use yii\base\BaseObject;
  56. use yii\data\Pagination;
  57. use yii\helpers\Json;
  58. use app\models\AccountLog;
  59. use app\models\Worker;
  60. use app\models\WorkerPic;
  61. use app\models\WorkerOrderExt;
  62. use app\models\WorkerGoods;
  63. use app\modules\client\models\v1\LoginForm;
  64. use app\modules\admin\models\worker\WorkerForm;
  65. use app\modules\client\models\v1\CashForm;
  66. use app\models\OrderComment;
  67. use app\models\WorkerSetting;
  68. use app\models\WorkerGoodsExt;
  69. use app\modules\client\models\v1\WorkerOrderForm;
  70. use app\modules\client\models\v1\worker\AdminForm;
  71. class AdminController extends BaseController
  72. {
  73. public $worker;
  74. public function behaviors()
  75. {
  76. return parent::behaviors();
  77. }
  78. public function beforeAction($action) {
  79. if (parent::beforeAction($action)) {
  80. try{
  81. if (input_params('worker_id')) {
  82. $where = [
  83. 'id' => input_params('worker_id')
  84. ];
  85. } else {
  86. $where = [
  87. 'user_id' => get_user_id()
  88. ];
  89. }
  90. $worker = Worker::findOne(array_merge($where, ['status' => [Worker::STATUS_VALID, Worker::STATUS_WAIT_AUDIT]]));
  91. if(!$worker){
  92. // throw new \Exception('用户不存在', 404);
  93. $this->asJson([
  94. 'user_id' => get_user_id(),
  95. 'action' => $action->id,
  96. 'code' => 0,
  97. 'msg' => 'ok',
  98. 'worker' => $worker,
  99. ]);
  100. return false;
  101. }
  102. // if($worker->status == Worker::STATUS_CLOSE){
  103. // $this->asJson([
  104. // 'user_id' => get_user_id(),
  105. // 'action' => $action->id,
  106. // 'code' => 1,
  107. // 'msg' => '已禁用',
  108. // ]);
  109. // return false;
  110. // }
  111. if($worker->status != Worker::STATUS_VALID){
  112. // throw new \Exception('用户状态异常', 401);
  113. $this->asJson([
  114. 'user_id' => get_user_id(),
  115. 'action' => $action->id,
  116. 'code' => 0,
  117. 'msg' => 'ok',
  118. 'worker' => $worker,
  119. ]);
  120. return false;
  121. }
  122. $this->worker = $worker;
  123. } catch (\Exception $ex) {
  124. $this->asJson([
  125. 'user_id' => get_user_id(),
  126. 'action' => $action->id,
  127. 'code' => 1,
  128. 'msg' => $ex->getMessage(),
  129. ]);
  130. return false;
  131. }
  132. return true;
  133. }
  134. return false;
  135. }
  136. /**
  137. * 用户中心
  138. * @return \yii\web\Response
  139. */
  140. public function actionIndex()
  141. {
  142. $level = $this->worker->levelInfo;
  143. $pics = $this->worker->pics;
  144. $this->worker->showBookTime();
  145. $data = [
  146. 'workerInfo' => $this->worker,
  147. 'levelName' => $level->name ?? '',
  148. 'pics' => $pics,
  149. ];
  150. $data['yesterday'] = [
  151. 'visited' => '待更新',
  152. 'orderCount' => '待更新',
  153. 'favCount' => '待更新',
  154. 'total_price' => '待更新',
  155. 'total_cash' => '待更新',
  156. 'is_price_yes' => '待更新',
  157. 'is_price_no' => '待更新',
  158. ];
  159. $data['favCount'] = (int)Favorite::find()->where(['worker_id' => $this->worker->id, 'is_delete' => 0])->count();
  160. $order_count = WorkerOrderExt::getWorkerOrderGroupStatus($this->worker->id);
  161. $data['order_count'] = $order_count;
  162. $data['orderCount'] = array_sum(array_column($order_count, 'cc'));
  163. $workerForm = new WorkerForm();
  164. $workerForm->store_id = get_store_id();
  165. $workerForm->worker_id = $this->worker->id;
  166. $workerProfitList = $workerForm->workerProfitList();
  167. $data['is_price_yes'] = sprintf('%.2f', $workerProfitList['sum_money'] - $workerProfitList['sumMoneyIsPriceNo']);
  168. $data['is_price_no'] = sprintf('%.2f', $workerProfitList['sumMoneyIsPriceNo']);
  169. $form = new ShareForm();
  170. $form->store_id = get_store_id();
  171. $form->user_id = $this->worker->user_id;
  172. $price = $form->getPrice();
  173. $data['total_price'] = $price['total_price'];
  174. $data['total_cash'] = $price['total_cash'];
  175. $res = [
  176. 'code' => 0,
  177. 'data' => $data,
  178. 'msg' => 'success'
  179. ];
  180. return $this->asJson($res);
  181. }
  182. public function actionOpenStatus()
  183. {
  184. $status = input_params('status');
  185. if ($status) {
  186. if (empty($this->worker->book_end_time)) {
  187. return $this->asJson([
  188. 'code' => 1,
  189. 'msg' => '操作失败!请设置服务时间。'
  190. ]);
  191. }
  192. if (date('Hi') <= $this->worker->book_start_time || date('Hi') >= $this->worker->book_end_time) {
  193. return $this->asJson([
  194. 'code' => 1,
  195. 'msg' => '操作失败!当前不在营业时间。'
  196. ]);
  197. }
  198. }
  199. $this->worker->open_status = $status ? 1 : 0;
  200. $save = $this->worker->save();
  201. if(!$save){
  202. return $this->asJson([
  203. 'code' => 1,
  204. 'msg' => '操作失败!' . array_shift($this->worker->getFirstErrors()),
  205. ]);
  206. }
  207. $res = [
  208. 'code' => 0,
  209. 'msg' => 'success'
  210. ];
  211. return $this->asJson($res);
  212. }
  213. public function actionUpdateInfo()
  214. {
  215. $keys = ['name', 'logo', 'desc', 'book_start_time','book_end_time','lat','lng','age','province_id','city_id','district_id','address'];
  216. $key = input_params('key');
  217. $val = input_params('val');
  218. if(is_string($key)){
  219. $key = [$key];
  220. $val = [$val];
  221. }
  222. foreach ($key as $k => $item) {
  223. if(!in_array($item, $keys)){
  224. return $this->asJson([
  225. 'code' => 1,
  226. 'msg' => '操作失败!参数错误',
  227. ]);
  228. }
  229. $this->worker->$item = $val[$k];
  230. }
  231. if(!$this->worker->validate()){
  232. return $this->asJson([
  233. 'code' => 1,
  234. 'msg' => '操作失败!' . array_shift($this->worker->getFirstErrors()),
  235. ]);
  236. }
  237. $save = $this->worker->save();
  238. if(!$save){
  239. return $this->asJson([
  240. 'code' => 1,
  241. 'msg' => '操作失败!' . array_shift($this->worker->getFirstErrors()),
  242. ]);
  243. }
  244. $res = [
  245. 'code' => 0,
  246. 'msg' => 'success'
  247. ];
  248. return $this->asJson($res);
  249. }
  250. public function actionUpdateTel()
  251. {
  252. $form = new LoginForm();
  253. $form->attributes = post_params();
  254. $form->store_id = get_store_id();
  255. $form->user = get_user();
  256. $verifySmsCode = $form->verifySmsCode(LoginForm::CACHE_KEY_BIND_PHONE);
  257. if ($verifySmsCode['code'] != 0) {
  258. return $this->asJson($verifySmsCode);
  259. }
  260. $this->worker->tel = $form->phone;
  261. $save = $this->worker->save();
  262. if(!$save){
  263. return $this->asJson([
  264. 'code' => 1,
  265. 'msg' => '操作失败!' . array_shift($this->worker->getFirstErrors()),
  266. ]);
  267. }
  268. $res = [
  269. 'code' => 0,
  270. 'msg' => 'success'
  271. ];
  272. return $this->asJson($res);
  273. }
  274. public function actionAddPics()
  275. {
  276. $pics = input_params('pics');
  277. WorkerPic::saveAll($this->worker->id, $pics);
  278. $res = [
  279. 'code' => 0,
  280. 'msg' => 'success',
  281. 'data' => $this->worker->pics,
  282. ];
  283. return $this->asJson($res);
  284. }
  285. public function actionDelPics()
  286. {
  287. $ids = input_params('ids');
  288. WorkerPic::del($this->worker->id, $ids);
  289. $res = [
  290. 'code' => 0,
  291. 'msg' => 'success',
  292. 'data' => $this->worker->pics,
  293. ];
  294. return $this->asJson($res);
  295. }
  296. //佣金信息
  297. public function actionProfitList()
  298. {
  299. $is_price_no = input_params('is_price_no', 0);
  300. $workerForm = new WorkerForm();
  301. $workerForm->store_id = get_store_id();
  302. $workerForm->worker_id = $this->worker->id;
  303. $workerProfitList = $workerForm->workerProfitList($is_price_no);
  304. $user = User::find()->where(['id' => $this->worker->user_id])->one();
  305. //获取分销佣金及提现
  306. $form = new ShareForm();
  307. $form->store_id = get_store_id();
  308. $form->user_id = get_user()->id;
  309. $sharePrice = $form->getPrice(\app\models\Cash::IS_CASH_TYPE_WORKER);
  310. $res = [
  311. 'code' => 0,
  312. 'msg' => 'success',
  313. 'data' => $workerProfitList,
  314. 'total_price' => $user->total_price,
  315. 'price' => $user->price,
  316. 'sharePrice' => $sharePrice,
  317. ];
  318. return $this->asJson($res);
  319. }
  320. /**
  321. * 申请提现
  322. */
  323. public function actionCashApply()
  324. {
  325. $form = new CashForm();
  326. $form->user_id = $this->worker->user_id;
  327. $form->store_id = get_store_id();
  328. $form->attributes = post_params();
  329. return $this->asJson($form->cashWorker());
  330. }
  331. /**
  332. * 关联产品
  333. */
  334. public function actionAddGoods()
  335. {
  336. $ids = input_params('ids');
  337. WorkerGoods::saveAll($this->worker->id, $ids);
  338. $res = [
  339. 'code' => 0,
  340. 'msg' => 'success',
  341. 'data' => $this->worker->goods,
  342. 'worker_id' => $this->worker->id,
  343. ];
  344. return $this->asJson($res);
  345. }
  346. public function actionDelGoods()
  347. {
  348. $ids = input_params('ids');
  349. WorkerGoods::del($this->worker->id, $ids);
  350. $res = [
  351. 'code' => 0,
  352. 'msg' => 'success',
  353. 'data' => $this->worker->goods,
  354. ];
  355. return $this->asJson($res);
  356. }
  357. //绑定商品列表
  358. public function actionGoodsList()
  359. {
  360. $type = input_params('type');
  361. $name = input_params('name');
  362. $cat_id = input_params('cat_id');
  363. $orderby = input_params('orderby');
  364. $begin = input_params('begin');
  365. $end = input_params('end');
  366. $store_id = get_store_id();
  367. $whereGoods = [
  368. 'g.store_id' => $store_id,
  369. 'g.product_type' => Goods::GOODS_TYPE_WORKER,
  370. 'g.is_delete' => 0,
  371. 'g.status' => 1,
  372. ];
  373. $queryGoods = Goods::find()->alias('g')->where($whereGoods);
  374. $queryGoodsBind = WorkerGoods::find()->alias('wg')->leftJoin(['g' => Goods::tableName()], 'g.id = wg.goods_id')->where([
  375. 'wg.status' => 1,
  376. 'wg.worker_id' => $this->worker->id,
  377. ])->andWhere($whereGoods);
  378. $queryGoodsBind->leftJoin(['wge' => WorkerGoodsExt::tableName()], 'g.id = wge.goods_id');
  379. $queryGoodsBind->andWhere(['OR', ['wge.bind_worker' => 0], ['wge.bind_worker' => 1, 'wg.public' => 1, 'wg.status' => 1]]);
  380. $queryGoods->leftJoin(['wge' => WorkerGoodsExt::tableName()], 'g.id = wge.goods_id');
  381. $queryGoods->leftJoin(['wg' => WorkerGoods::find()->where(['worker_id' => $this->worker->id])], 'g.id = wg.goods_id');
  382. $select = 'g.name, g.cover_pic, g.price, g.original_price, wge.*, wg.status is_bind';
  383. $queryGoods->select($select);
  384. $queryGoods->andWhere(['OR', ['wge.bind_worker' => 0], ['wge.bind_worker' => 1, 'wg.public' => 1, 'wg.status' => 1]]);
  385. if($name){
  386. $queryGoods->andWhere(['like', 'g.name', $name]);
  387. $queryGoodsBind->andWhere(['like', 'g.name', $name]);
  388. }
  389. if($begin){
  390. $queryGoods->andWhere(['>=', 'g.created_at', $begin]);
  391. $queryGoodsBind->andWhere(['>=', 'g.created_at', $begin]);
  392. }
  393. if($end){
  394. $queryGoods->andWhere(['<=', 'g.created_at', $end]);
  395. $queryGoodsBind->andWhere(['<=', 'g.created_at', $end]);
  396. }
  397. if($cat_id > 0){
  398. $queryGoods->andWhere(['wge.cat_id' => $cat_id]);
  399. $queryGoodsBind->andWhere(['wge.cat_id' => $cat_id]);
  400. }
  401. $queryOrderBy = 'g.id DESC';
  402. if($orderby == 1){
  403. $queryOrderBy = 'g.id DESC';
  404. }else if($orderby == 2){
  405. $queryOrderBy = 'g.id ASC';
  406. }else if($orderby == 3){
  407. $queryOrderBy = 'g.price DESC';
  408. }else if($orderby == 4){
  409. $queryOrderBy = 'g.price ASC';
  410. }
  411. $queryGoods->orderBy($queryOrderBy);
  412. $queryGoodsBind->orderBy($queryOrderBy);
  413. $queryGoods->groupBy('g.id');
  414. $queryGoodsBind->groupBy('g.id');
  415. if($type == 0){
  416. $data = pagination_make($queryGoods);
  417. }
  418. if($type == 1){
  419. $queryGoodsBind->select($select);
  420. $data = pagination_make($queryGoodsBind);
  421. }
  422. if($type == 2){
  423. $queryGoodsBindNo = clone $queryGoods;
  424. $queryUnBind = WorkerGoods::find()->where(['worker_id' => $this->worker->id, 'status' => 1])->select('goods_id');
  425. $queryGoodsBindNo->andWhere(['not in', 'g.id', $queryUnBind]);
  426. $data = pagination_make($queryGoodsBindNo);
  427. }
  428. $count = $queryGoods->count();
  429. $countGoodsBind = $queryGoodsBind->count();
  430. $countGoodsBindNo = $count > $countGoodsBind ? $count - $countGoodsBind : 0;
  431. $res = [
  432. 'code' => 0,
  433. 'msg' => 'success',
  434. 'data' => $data,
  435. 'count' => $count,
  436. 'countGoodsBind' => $countGoodsBind,
  437. 'countGoodsBindNo' => $countGoodsBindNo,
  438. 'q' => $queryGoods->createCommand()->getRawSql(),
  439. ];
  440. return $this->asJson($res);
  441. }
  442. //评价列表
  443. public function actionCommentList()
  444. {
  445. $adminForm = new AdminForm();
  446. $adminForm->store_id = get_store_id();
  447. $adminForm->worker_id = $this->worker->id;
  448. $adminForm->score = input_params('score');
  449. $adminForm->name = input_params('name');
  450. $adminForm->orderby = input_params('orderby');
  451. $adminForm->begin = input_params('begin');
  452. $adminForm->end = input_params('end');
  453. $res = $adminForm->commentList();
  454. return $this->asJson($res);
  455. }
  456. //待抢单列表
  457. public function actionWaitBindOrderList()
  458. {
  459. $store_id = get_store_id();
  460. $type = input_params('type', 1);
  461. $setting = WorkerSetting::getByStoreId($store_id);
  462. $orderSpace = $setting['order_space'];
  463. $sql = 'SELECT
  464. woe.order_id
  465. FROM
  466. cyy_worker_order_ext woe
  467. LEFT JOIN (
  468. SELECT
  469. count(id) num,
  470. order_id
  471. FROM
  472. cyy_order_detail
  473. WHERE
  474. goods_id IN (
  475. SELECT
  476. goods_id
  477. FROM
  478. cyy_worker_goods
  479. WHERE
  480. worker_id = :worker_id
  481. )
  482. GROUP BY
  483. order_id
  484. ) od ON od.order_id = woe.order_id
  485. LEFT JOIN (
  486. SELECT
  487. count(id) num2,
  488. order_id
  489. FROM
  490. cyy_order_detail
  491. GROUP BY
  492. order_id
  493. ) od2 ON od2.order_id = woe.order_id
  494. WHERE
  495. woe.status_ext = :status_ext
  496. AND od.num = od2.num2';
  497. $queryOrderId = \Yii::$app->db->createCommand($sql, [':worker_id' => $this->worker->id, ':status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND])->getRawSql();
  498. // $queryGoods = WorkerGoods::find()->where(['worker_id' => $this->worker->id, 'status' => 1])->select('goods_id');
  499. $queryOrder = WorkerOrderExt::find()->alias('woe')->leftJoin([
  500. 'od' => OrderDetail::tableName()
  501. ], 'od.order_id = woe.order_id')->leftJoin([
  502. 'o' => Order::tableName()
  503. ], 'o.id = woe.order_id')->leftJoin([
  504. 'wge' => WorkerGoodsExt::tableName()
  505. ], 'od.goods_id = wge.goods_id')->leftJoin([
  506. 'u' => User::tableName()
  507. ], 'o.user_id = u.id')->leftJoin([
  508. 'su' => SaasUser::tableName()
  509. ], 'su.mobile = u.binding')->where([
  510. 'woe.status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND,
  511. // 'od.goods_id' => $queryGoods,
  512. // 'o.id' => $queryOrderId,
  513. ])->andWhere('o.id in ('. $queryOrderId .')');
  514. $cacheV = WorkerOrderExt::cacheIgnoreOrderId($this->worker->id);
  515. $ignore_ids = [];
  516. if($cacheV){
  517. $ignore_ids = array_keys($cacheV);
  518. }
  519. if($type == 1){
  520. $queryOrder->andWhere(['not in', 'woe.order_id', $ignore_ids]);
  521. }else{
  522. $queryOrder->andWhere(['woe.order_id' => $ignore_ids]);
  523. }
  524. // $queryDis = 'ROUND(st_distance_sphere(point('. ($this->worker->lng ?? 0) .', '. ($this->worker->lat ?? 0) .'), point(woe.lng, woe.lat)), 2)';
  525. $latitude = $this->worker->lat ?: 0;
  526. $longitude = $this->worker->lng ?: 0;
  527. $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
  528. distance from cyy_worker_order_ext)"], 'woe_.id = woe.id');
  529. $queryOrder->andWhere(['<=', 'woe_.distance', $orderSpace]);
  530. $queryOrder->select($this->orderSelect());
  531. $queryOrder->addSelect(['woe_.distance dis']);
  532. $data = pagination_make($queryOrder);
  533. $this->formatList($data['list'], $ignore_ids);
  534. $res = [
  535. 'code' => 0,
  536. 'msg' => 'success',
  537. 'data' => $data,
  538. 'q' => $queryOrder->createCommand()->getRawSql(),
  539. ];
  540. return $this->asJson($res);
  541. }
  542. public function formatList(&$list, $ignore_ids) {
  543. foreach ($list as &$item) {
  544. $item['ignore'] = in_array($item['id'], $ignore_ids) ? 1 : 0;
  545. $goods_list = OrderDetail::find()->where(['order_id' => $item['id'], 'is_delete' => 0])->select('goods_id, goods_name, num, total_price, pic')->asArray()->all();
  546. foreach($goods_list as &$gitem){
  547. $WorkerGoodsExt = WorkerGoodsExt::find()->where(['goods_id' => $gitem['goods_id']])->select('desc')->one();
  548. $gitem['worker_goods_ext_desc'] = $WorkerGoodsExt['desc'];
  549. }
  550. $item['goods_list'] = $goods_list;
  551. $item['dis'] = self::distance($item['dis']);
  552. $item['order_form']['list'] = OrderForm::find()->where(['order_id' => $item['id']])->asArray()->all();
  553. foreach ($item['order_form']['list'] as &$form_item) {
  554. $form_item['default'] = $form_item['value'];
  555. if ($form_item['type'] == "checkbox" || $form_item['type'] == "radio") {
  556. $list = explode(',', $form_item['value']);
  557. $list_arr = [];
  558. foreach ($list as &$list_item) {
  559. $arr = [];
  560. $arr['name'] = $list_item;
  561. $arr['checked'] = true;
  562. array_push($list_arr, $arr);
  563. }
  564. $form_item['default_list'] = $list_arr;
  565. }
  566. $form_item['name'] = $form_item['key'];
  567. unset($form_item['key']);
  568. }
  569. }
  570. }
  571. //抢单
  572. public function actionOrderBind()
  573. {
  574. $order_id = input_params('order_id');
  575. $type = input_params('type', 1);
  576. if($type == 2){
  577. $cacheV = WorkerOrderExt::cacheIgnoreOrderId($this->worker->id, $order_id);
  578. return $this->asJson([
  579. 'code' => 0,
  580. 'msg' => 'success',
  581. ]);
  582. }
  583. $cacheK = 'WorkerOrderBind' . $order_id;
  584. $cacheV = cache()->get($cacheK);
  585. if($cacheV){
  586. return $this->asJson([
  587. 'code' => 1,
  588. 'msg' => '获取缓存锁失败',
  589. ]);
  590. }
  591. cache()->set($cacheK, 1, 10);
  592. $t = \Yii::$app->db->beginTransaction();
  593. try{
  594. $orderExt = WorkerOrderExt::findOne(['order_id' => $order_id, 'worker_id' => 0, 'status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND]);
  595. if(!$orderExt){
  596. throw new \Exception('参数错误');
  597. }
  598. $orderExt = WorkerOrderExt::find()->where('id = :id FOR UPDATE', [':id' => $orderExt->id])->one();
  599. if(!$orderExt){
  600. throw new \Exception('锁失败' . array_shift($orderExt->getFirstErrors()));
  601. }
  602. if($type == 1){
  603. $orderExt->worker_id = $this->worker->id;
  604. $orderExt->status_ext = WorkerOrderExt::STATUS_EXT_HAS_BIND;
  605. $orderExt->time_has_bind = time();
  606. $orderExt->is_hold = 1;
  607. }
  608. $save = $orderExt->save();
  609. if(!$save){
  610. throw new \Exception('ext保存失败' . array_shift($orderExt->getFirstErrors()));
  611. }
  612. $t->commit();
  613. return $this->asJson([
  614. 'code' => 0,
  615. 'msg' => 'success',
  616. ]);
  617. } catch (\Exception $ex) {
  618. $t->rollBack();
  619. return $this->asJson([
  620. 'code' => 1,
  621. 'msg' => '操作失败,' . $ex->getMessage(),
  622. ]);
  623. }
  624. }
  625. public function orderSelect() {
  626. 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';
  627. }
  628. //订单列表
  629. public function actionOrderList()
  630. {
  631. $order_id = input_params('order_id');
  632. $status_ext = input_params('status_ext');
  633. $cat_id = input_params('cat_id');
  634. $name = input_params('name');
  635. $orderby = input_params('orderby');
  636. $begin = input_params('begin');
  637. $end = input_params('end');
  638. $is_hold = input_params('is_hold');
  639. $queryOrder = WorkerOrderExt::find()->alias('woe')->leftJoin([
  640. 'od' => OrderDetail::tableName()
  641. ], 'od.order_id = woe.order_id')->leftJoin([
  642. 'o' => Order::tableName()
  643. ], 'o.id = woe.order_id')->leftJoin([
  644. 'wge' => WorkerGoodsExt::tableName()
  645. ], 'od.goods_id = wge.goods_id')->leftJoin([
  646. 'u' => User::tableName()
  647. ], 'o.user_id = u.id')->leftJoin([
  648. 'su' => SaasUser::tableName()
  649. ], 'su.mobile = u.binding');
  650. if($order_id){
  651. $queryOrder->andWhere(['woe.order_id' => $order_id]);
  652. $queryOrder->andWhere(['or', ['woe.status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND], ['woe.worker_id' => $this->worker->id]]);
  653. }else{
  654. $queryOrder->andWhere(['woe.worker_id' => $this->worker->id]);
  655. }
  656. if($status_ext){
  657. $queryOrder->andWhere(['status_ext' => $status_ext]);
  658. }
  659. if($is_hold){
  660. $queryOrder->andWhere(['woe.is_hold' => $is_hold]);
  661. }
  662. if($name){
  663. $queryOrder->andWhere(['like', 'od.goods_name', $name]);
  664. }
  665. if($begin){
  666. $queryOrder->andWhere(['>=', 'o.created_at', $begin]);
  667. }
  668. if($end){
  669. $queryOrder->andWhere(['<=', 'o.created_at', $end]);
  670. }
  671. if($cat_id > 0){
  672. $queryOrder->andWhere(['wge.cat_id' => $cat_id]);
  673. }
  674. $queryOrderBy = 'woe.id DESC';
  675. if($orderby == 1){
  676. $queryOrderBy = 'woe.id DESC';
  677. }else if($orderby == 2){
  678. $queryOrderBy = 'woe.id ASC';
  679. }else if($orderby == 3){
  680. $queryOrderBy = 'o.price DESC';
  681. }else if($orderby == 4){
  682. $queryOrderBy = 'o.price ASC';
  683. }
  684. $queryOrder->groupBy('o.id');
  685. $queryOrder->orderBy($queryOrderBy);
  686. $queryOrder->select($this->orderSelect());
  687. // $queryDis = 'ROUND(st_distance_sphere(point('. ($this->worker->lng ?? 0) .', '. ($this->worker->lat ?? 0) .'), point(woe.lng, woe.lat)), 2)';
  688. // $queryOrder->addSelect([$queryDis . ' dis']);
  689. $queryOrder->addSelect(['woe.dis']);
  690. $data = pagination_make($queryOrder);
  691. $cacheV = WorkerOrderExt::cacheIgnoreOrderId($this->worker->id);
  692. $ignore_ids = [];
  693. if($cacheV){
  694. $ignore_ids = array_keys($cacheV);
  695. }
  696. $this->formatList($data['list'], $ignore_ids);
  697. $order_count = WorkerOrderExt::getWorkerOrderGroupStatus($this->worker->id);
  698. $res = [
  699. 'code' => 0,
  700. 'msg' => 'success',
  701. 'data' => $data,
  702. 'order_count' => $order_count,
  703. 'orderCount' => array_sum(array_column($order_count, 'cc')),
  704. 'q' => $queryOrder->createCommand()->getRawSql(),
  705. ];
  706. return $this->asJson($res);
  707. }
  708. public static function distance($distance)
  709. {
  710. if ($distance == -1) {
  711. return -1;
  712. }
  713. if ($distance > 1000) {
  714. $distance = round($distance / 1000, 2) . 'km';
  715. } else {
  716. $distance = round($distance, 2);
  717. $distance .= 'm';
  718. }
  719. return $distance;
  720. }
  721. //订单操作
  722. public function actionOrderStatus()
  723. {
  724. $order_id = input_params('order_id');
  725. $type = input_params('type', 1);
  726. $cond = [
  727. 'order_id' => $order_id,
  728. 'worker_id' => $this->worker->id,
  729. ];
  730. $attr = [];
  731. if($type == 1){
  732. $cond = array_merge($cond, [
  733. 'status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND_OK,
  734. ]);
  735. $attr = [
  736. 'status_ext' => WorkerOrderExt::STATUS_EXT_HAS_BIND,
  737. 'time_has_bind' => time(),
  738. ];
  739. }
  740. if($type == 2){
  741. $cond = array_merge($cond, [
  742. 'status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND_OK,
  743. ]);
  744. $attr = [
  745. 'status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND,
  746. 'worker_id' => 0,
  747. ];
  748. }
  749. if($type == 3){
  750. $cond = array_merge($cond, [
  751. 'status_ext' => WorkerOrderExt::STATUS_EXT_HAS_BIND,
  752. ]);
  753. $attr = [
  754. 'status_ext' => WorkerOrderExt::STATUS_EXT_ON_ROAD,
  755. 'time_outgoing' => time(),
  756. ];
  757. }
  758. if($type == 4){
  759. $cond = array_merge($cond, [
  760. 'status_ext' => WorkerOrderExt::STATUS_EXT_ON_ROAD,
  761. ]);
  762. $attr = [
  763. 'status_ext' => WorkerOrderExt::STATUS_EXT_ARRIVE,
  764. 'time_reach' => time(),
  765. ];
  766. }
  767. if($type == 5){
  768. $cond = array_merge($cond, [
  769. 'status_ext' => WorkerOrderExt::STATUS_EXT_ARRIVE,
  770. ]);
  771. $attr = [
  772. 'status_ext' => WorkerOrderExt::STATUS_EXT_START,
  773. 'time_start_service' => time(),
  774. ];
  775. }
  776. if($type == 11){
  777. $cond = array_merge($cond, [
  778. 'status_ext' => WorkerOrderExt::STATUS_EXT_FINISH,
  779. ]);
  780. $attr = [
  781. 'worker_delete' => 1,
  782. ];
  783. }
  784. $orderExt = WorkerOrderExt::findOne($cond);
  785. if(!$orderExt){
  786. return $this->asJson([
  787. 'code' => 1,
  788. 'msg' => '参数错误',
  789. ]);
  790. }
  791. $orderExt->setAttributes($attr, false);
  792. $save = $orderExt->save();
  793. if(!$save){
  794. return $this->asJson([
  795. 'code' => 1,
  796. 'msg' => '操作失败,' . array_shift($orderExt->getFirstErrors()),
  797. ]);
  798. }
  799. return $this->asJson([
  800. 'code' => 0,
  801. 'msg' => 'success',
  802. ]);
  803. }
  804. //加钟
  805. public function actionOrderAddTime()
  806. {
  807. $order_id = input_params('order_id');
  808. $type = input_params('type', 1);
  809. $val = input_params('val');
  810. if(!$val || !$type || !$order_id || ($val <= 0)){
  811. return $this->asJson([
  812. 'code' => 1,
  813. 'msg' => '参数错误',
  814. ]);
  815. }
  816. if($type == 1){
  817. $fee = $val;
  818. }
  819. if($type == 2){
  820. $goodsCount = $val;
  821. }
  822. $orderAddTime = WorkerOrderForm::orderAddTime($this->worker->id, $order_id, $fee, $goodsCount);
  823. return $this->asJson($orderAddTime);
  824. }
  825. //尾款
  826. public function actionOrderFinishPay()
  827. {
  828. $order_id = input_params('order_id');
  829. $fee = input_params('fee');
  830. if(!$fee || !$order_id || ($fee <= 0)){
  831. return $this->asJson([
  832. 'code' => 1,
  833. 'msg' => '参数错误',
  834. ]);
  835. }
  836. $orderFinishPay = WorkerOrderForm::orderFinishPay($this->worker->id, $order_id, $fee);
  837. return $this->asJson($orderFinishPay);
  838. }
  839. }