Order.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/12/16 23:06
  5. */
  6. namespace app\api\controller;
  7. use app\api\BaseController;
  8. use app\api\exception\ApiException;
  9. use app\api\model\BrandModel;
  10. use app\api\model\CartProductModel;
  11. use app\api\model\CartModel;
  12. use app\api\model\GroupModel;
  13. use app\api\model\OrderModel;
  14. use app\api\model\OrderProductModel;
  15. use app\api\model\ProductModel;
  16. use app\api\model\UserModel;
  17. use app\api\model\WriteOffModel;
  18. use app\common\service\OrderService;
  19. use app\common\until\Until;
  20. use think\App;
  21. use think\Db;
  22. class Order extends BaseController {
  23. public function __construct(App $app) {
  24. parent::__construct($app);
  25. if ($this->userId == '123456789') {
  26. throw new ApiException(
  27. '网络繁忙'
  28. );
  29. }
  30. }
  31. /**
  32. * @OA\Get(path="/api/Order/index",
  33. * tags={"订单管理"},
  34. * summary="订单列表",
  35. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  36. * @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="ineger",default="1")),
  37. * @OA\Parameter(name="pageSize", in="query", description="页尺寸", @OA\Schema(type="integer",default="10")),
  38. * @OA\Parameter(name="orderStatus", in="query", description="订单状态 1未支付 2已支付 ", @OA\Schema(type="integer")),
  39. * @OA\Parameter(name="writeOffStatus", in="query", description="核销状态 1未核销 2已核销 ", @OA\Schema(type="integer")),
  40. * @OA\Parameter(name="discussStatus", in="query", description="评价状态 1未评价 2已评价 ", @OA\Schema(type="integer")),
  41. * @OA\Parameter(name="orderSn", in="query", description="订单号", @OA\Schema(type="string")),
  42. * @OA\Parameter(name="mobile", in="query", description="手机号", @OA\Schema(type="string")),
  43. * @OA\Parameter(name="storeId", in="query", description="门店id", @OA\Schema(type="integer")),
  44. * @OA\Parameter(name="appointmentTime", in="query", description="预约时间", @OA\Schema(type="2020-01-02,2021-12-30")),
  45. * @OA\Parameter(name="createTime", in="query", description="订单时间", @OA\Schema(type="2020-01-02,2021-12-30")),
  46. * @OA\Parameter(name="orderType", in="query", description="订单类型 1小程序下单 2后台增加", @OA\Schema(type="string")),
  47. * @OA\RequestBody(
  48. * ),
  49. * @OA\Response(response="200", description="请求成功")
  50. * )
  51. */
  52. public function index() {
  53. $input = request()->get();
  54. $model = new OrderModel();
  55. $model->setPage($input['page'] ?? 1);
  56. $model->setPageSize($input['pageSize'] ?? 10);
  57. $where = [];
  58. $model::where([['status', '=', 1], ['create_time', '<',
  59. date('Y-m-d H:i:s', strtotime('-15minutes'))]])
  60. ->update(['status' => OrderModel::IS_CLOSE]);
  61. if (!empty($input['orderSn'])) {
  62. $where[] = ['o.order_sn', 'like', "%{$input['orderSn']}%"];
  63. }
  64. if (!empty($input['mobile'])) {
  65. $where[] = ['o.mobile', 'like', "%{$input['mobile']}%"];
  66. }
  67. if (!empty($input['writeOffStatus'])) {
  68. if (empty($input['foodOrder'])) {
  69. $where[] = ['wo.write_off_status', '=', (int)$input['writeOffStatus']];
  70. }else if ($input['foodOrder'] == 2){
  71. $model->setOp('write_off_status', 'left');
  72. }
  73. }
  74. if (!empty($input['orderStatus'])) {
  75. $where[] = ['o.status', '=', (int)$input['orderStatus']];
  76. }
  77. if (!empty($input['discussStatus'])) {
  78. $where[] = ['discussOrder.id', '=', null];
  79. }
  80. if (!empty($input['storeId'])) {
  81. $where[] = ['store.id', '=', $input['storeId']];
  82. }
  83. if (!empty($input['appointmentTime'])) {
  84. $data = explode(',', $input['appointmentTime']);
  85. $where[] = ['o.appointment_time', 'between', [$data[0], $data[1] . ' 23:59:59']];
  86. }
  87. if (!empty($input['createTime'])) {
  88. $data = explode(',', $input['createTime']);
  89. $where[] = ['o.create_time', 'between', [$data[0], $data[1] . ' 23:59:59']];
  90. }
  91. if (!empty($input['orderType'])) {
  92. $where[] = ['o.order_type', '=', $input['orderType']];
  93. }
  94. if (!$this->isAdmin()) {
  95. $where[] = ['o.user_id', '=', $this->userId];
  96. if (empty($input['orderStatus'])) {
  97. $where[] = ['o.status', '<>', OrderModel::IS_DELETE];
  98. }
  99. }
  100. $model->setWhere($where);
  101. $data = $model->getOrderList();
  102. $orderIds = array_column($data['list'], 'id');
  103. $productList = (new OrderProductModel())::where([['order_id', 'in', $orderIds]])->select();
  104. $productList = Until::modelToArray($productList);
  105. $product = [];
  106. foreach ($productList as &$v) {
  107. unset($v['product_snap']);
  108. $product[$v['order_id']][] = $v;
  109. }
  110. $statusFilter = [1 => '未支付', 2 => '已支付', 3 => '已关闭'];
  111. foreach ($data['list'] as &$one) {
  112. $one['productList'] = $product[$one['id']] ?? [];
  113. $one['statusText'] = $statusFilter[$one['status']];
  114. if ($one['status'] === OrderModel::IS_PAY) {
  115. if ($one['write_off_status'] == 1) {
  116. $one['statusText'] = '待消费';
  117. } else if ($one['write_off_status'] == 2) {
  118. if (empty($one['discuss_id']) && !empty($input['discussStatus'])) {
  119. $one['statusText'] = '未评价';
  120. }
  121. $one['statusText'] = '已消费';
  122. }
  123. }
  124. }
  125. Until::output($data);
  126. }
  127. public function save() {
  128. $input = Until::getInput();
  129. $rule = [
  130. 'name|品牌名称' => 'require',
  131. 'groupId|集团id' => 'require',
  132. ];
  133. Until::check($rule, $input);
  134. $model = new BrandModel();
  135. if (!empty($input['id'])) {
  136. $id = (int)$input['id'];
  137. $model::where(['id' => $id])->update([
  138. 'brand_name' => $input['name'],
  139. 'group_id' => $input['groupId']
  140. ]);
  141. } else {
  142. $id = $model->insertGetId([
  143. 'brand_name' => $input['name'],
  144. 'group_id' => $input['groupId']
  145. ]);
  146. }
  147. $info = $model::get($id);
  148. Until::output(['info' => Until::modelToArray($info)]);
  149. }
  150. /**
  151. * @OA\GET(path="/api/Order/read",
  152. * tags={"订单管理"},
  153. * summary="查看订单信息",
  154. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  155. * @OA\Parameter(name="id", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
  156. * @OA\RequestBody(
  157. * ),
  158. * @OA\Response(response="200", description="请求成功")
  159. * )
  160. */
  161. public function read($id) {
  162. $model = new OrderModel();
  163. $where[] = ['o.id', '=', (int)$id];
  164. $model->setWhere($where);
  165. $info = $model->getOrderInfo();
  166. $productList = (new OrderProductModel())::where([['order_id', '=', $info['id']]])->select();
  167. $productList = Until::modelToArray($productList);
  168. foreach ($productList as &$v) {
  169. unset($v['product_snap']);
  170. }
  171. $info['productList'] = $productList;
  172. Until::output(['info' => $info]);
  173. }
  174. /**
  175. * @OA\GET(path="/api/Order/delete",
  176. * tags={"订单管理"},
  177. * summary="删除品牌信息",
  178. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  179. * @OA\Parameter(name="id", in="query", description="品牌id", @OA\Schema(type="ineger",default="1")),
  180. * @OA\Parameter(name="status", in="query", description="1正常 2删除", @OA\Schema(type="ineger",default="1")),
  181. * @OA\RequestBody(
  182. * ),
  183. * @OA\Response(response="200", description="请求成功")
  184. * )
  185. */
  186. public function delete($id, $status) {
  187. $model = new BrandModel();
  188. $where[] = ['id', '=', (int)$id];
  189. $data = ['status' => (int)$status];
  190. $isSuccess = $model::where($where)->update($data);
  191. Until::output(['isSuccess' => $isSuccess]);
  192. }
  193. /**
  194. * @OA\Post(path="/api/Order/createOrder",
  195. * tags={"订单管理"},
  196. * summary="创建订单信息",
  197. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  198. * @OA\RequestBody(
  199. * @OA\MediaType(
  200. * mediaType="multipart/form-data",
  201. * @OA\Schema(
  202. * @OA\Property(description="商品id", property="productId", type="integer", default="1"),
  203. * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
  204. * @OA\Property(description="预约时间", property="appointmentTime", type="string", default="2020-12-12 16:30"),
  205. * @OA\Property(description="预约结束时间", property="endTime", type="string", default="2020-12-12 16:30"),
  206. * @OA\Property(description="商品数量", property="num", type="integer", default="1"),
  207. * @OA\Property(description="手机号", property="mobile", type="string", default="15623655623"),
  208. * required={"productId","storeId","appointmentTime","num","mobile"})
  209. * )
  210. * ),
  211. * @OA\Response(response="200", description="请求成功")
  212. * )
  213. */
  214. public function createOrder() {
  215. $input = Until::getInput();
  216. $rule = [
  217. 'productId|商品id' => 'require',
  218. 'storeId|门店id' => 'require',
  219. 'appointmentTime|预约时间' => 'require',
  220. 'num|数量' => 'require',
  221. 'mobile|手机号' => 'require'
  222. ];
  223. Until::check($rule, $input);
  224. if (empty($input['endTime'])) {
  225. $input['endTime'] = date('Y-m-d H:i', strtotime($input['appointmentTime']) + 1800);
  226. }
  227. if (strtotime($input['appointmentTime']) >= strtotime($input['endTime'])) {
  228. throw new ApiException('结束时间必须大于开始时间');
  229. }
  230. $input['productId'] = (int)$input['productId'];
  231. if ($input['num'] < 1) {
  232. throw new ApiException('数量必须大于1');
  233. }
  234. $data = (new OrderService())->payOrder($input);
  235. Until::output($data);
  236. }
  237. /**
  238. * @OA\Post(path="/api/Order/createFoodOrder",
  239. * tags={"订单管理"},
  240. * summary="创建订单信息",
  241. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  242. * @OA\RequestBody(
  243. * @OA\MediaType(
  244. * mediaType="multipart/form-data",
  245. * @OA\Schema(
  246. * @OA\Property(description="购物车id", property="cartId", type="integer", default="1"),
  247. * @OA\Property(description="台桌id", property="tableId", type="integer", default="1"),
  248. * required={"cartId","tableId"})
  249. * )
  250. * ),
  251. * @OA\Response(response="200", description="请求成功")
  252. * )
  253. */
  254. public function createFoodOrder() {
  255. $input = Until::getInput();
  256. $rule = [
  257. 'cartId|商品id和数量' => 'require',
  258. 'tableId|桌台id' => 'require',
  259. ];
  260. Until::check($rule, $input);
  261. $data = (new OrderService())->payFoodOrder($input);
  262. Until::output($data);
  263. }
  264. /**
  265. * @OA\Post(path="/api/Order/assignStaff",
  266. * tags={"订单管理"},
  267. * summary="分配职员",
  268. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  269. * @OA\RequestBody(
  270. * @OA\MediaType(
  271. * mediaType="multipart/form-data",
  272. * @OA\Schema(
  273. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  274. * @OA\Property(description="职员id", property="staffId", type="integer", default="1"),
  275. * required={"orderId","staffId"})
  276. * )
  277. * ),
  278. * @OA\Response(response="200", description="请求成功")
  279. * )
  280. */
  281. public function assignStaff() {
  282. $input = Until::getInput();
  283. $rule = [
  284. 'orderId|订单id' => 'require',
  285. 'staffId|职员id' => 'require',
  286. ];
  287. Until::check($rule, $input);
  288. $model = new OrderModel();
  289. $model::where(['id' => (int)$input['orderId']])->update(['staff_id' => (int)$input['staffId']]);
  290. Until::output([]);
  291. }
  292. /**
  293. * @OA\Post(path="/api/Order/payOrder",
  294. * tags={"订单管理"},
  295. * summary="手动支付",
  296. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  297. * @OA\RequestBody(
  298. * @OA\MediaType(
  299. * mediaType="multipart/form-data",
  300. * @OA\Schema(
  301. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  302. * required={"orderId"})
  303. * )
  304. * ),
  305. * @OA\Response(response="200", description="请求成功")
  306. * )
  307. */
  308. public function payOrder() {
  309. $input = Until::getInput();
  310. $rule = [
  311. 'orderId|订单id' => 'require',
  312. ];
  313. Until::check($rule, $input);
  314. $model = new OrderModel();
  315. $orderInfo = $model::where(['id' => (int)$input['orderId']])->find();
  316. if ($orderInfo === null) {
  317. throw new ApiException('无此订单');
  318. }
  319. if ($orderInfo['status'] === OrderModel::IS_PAY) {
  320. throw new ApiException('该订单已支付');
  321. }
  322. $model::where(['id' => (int)$input['orderId']])->update([
  323. 'status' => OrderModel::IS_PAY,
  324. 'pay_time' => date('Y-m-d H:i:s')
  325. ]);
  326. $code = random_int(10000, 99999);
  327. $wModel = new WriteOffModel();
  328. $wModel::where(['order_id' => $input['orderId']])->update([
  329. 'write_off_code' => $code,
  330. ]);
  331. Until::output([]);
  332. }
  333. public function notifyOrder() {
  334. $input = Until::getInput();
  335. $rule = [
  336. 'OrderNumber|订单号' => 'require',
  337. ];
  338. Until::check($rule, $input);
  339. if (md5('ef17f532-4661-b07c-5346-65dfa304c0d8' . $input['OrderNumber']) === $this->request->header('serchkey')) {
  340. (new OrderService())->notify($input['OrderNumber']);
  341. Until::output([]);
  342. }
  343. // (new OrderService())->notify($input['OrderNumber']);
  344. Until::output(['decode' => md5('ef17f532-4661-b07c-5346-65dfa304c0d8' . $input['OrderNumber']),
  345. 'encode' => $this->request->header('serchkey')]);
  346. }
  347. public function changeCode() {
  348. $input = Until::getInput();
  349. $rule = [
  350. 'orderId|订单号' => 'require',
  351. ];
  352. Until::check($rule, $input);
  353. $ser = new OrderService();
  354. $code = $ser->changeCode();
  355. $wModel = new WriteOffModel();
  356. $wModel::where(['order_id' => $input['orderId'], 'write_off_status' => 1])->update([
  357. 'write_off_code' => $code,
  358. 'over_time' => date('Y-m-d H:i:s', time() + 2 * 60),
  359. ]);
  360. Until::output(['code' => $code]);
  361. }
  362. /**
  363. * @OA\Post(path="/api/Order/writeOffOrder",
  364. * tags={"订单管理"},
  365. * summary="核销订单",
  366. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  367. * @OA\RequestBody(
  368. * @OA\MediaType(
  369. * mediaType="multipart/form-data",
  370. * @OA\Schema(
  371. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  372. * @OA\Property(description="核销code", property="code", type="string", default="1"),
  373. * required={"orderId","code"})
  374. * )
  375. * ),
  376. * @OA\Response(response="200", description="请求成功")
  377. * )
  378. */
  379. public function writeOffOrder() {
  380. $input = Until::getInput();
  381. $rule = [
  382. 'orderId|订单id' => 'require',
  383. 'code|核销码' => 'require'
  384. ];
  385. Until::check($rule, $input);
  386. $rs = (new OrderModel())::where(['id' => $input['orderId']])->find();
  387. if ($rs['status'] != OrderModel::IS_PAY) {
  388. throw new ApiException('该订单未付款,不可核销');
  389. }
  390. $model = new WriteOffModel();
  391. $where = ['order_id' => $input['orderId'], 'write_off_code' => $input['code']];
  392. $writeOff = $model::where($where)->find();
  393. if ($writeOff === null) {
  394. throw new ApiException('核销码错误');
  395. }
  396. if ($writeOff['write_off_status'] == 2) {
  397. throw new ApiException('该订单已经核销了');
  398. }
  399. $userModel = new UserModel();
  400. $userModel::where([['id', '=', $rs['user_id']], ['first_store_id', '=', 0]])->update(['first_store_id' => $rs['store_id']]);
  401. $model::where($where)
  402. ->update([
  403. 'write_off_status' => 2,
  404. 'write_off_time' => date('Y-m-d H:i:s'),
  405. 'admin_id' => $this->adminId
  406. ]);
  407. Until::output();
  408. }
  409. /**
  410. * @OA\Post(path="/api/Order/closeOrder",
  411. * tags={"订单管理"},
  412. * summary="订单关闭",
  413. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  414. * @OA\Parameter(name="orderId", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
  415. * @OA\RequestBody(
  416. * ),
  417. * @OA\Response(response="200", description="请求成功")
  418. * )
  419. */
  420. public function closeOrder() {
  421. $input = request()->get();
  422. $model = new OrderModel();
  423. $model::where([
  424. 'user_id' => $this->userId,
  425. 'id' => (int)$input['orderId']
  426. ])->update(['status' => OrderModel::IS_CLOSE]);
  427. Until::output([]);
  428. }
  429. /**
  430. * @OA\Post(path="/api/Order/deleteOrder",
  431. * tags={"订单管理"},
  432. * summary="订单删除",
  433. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  434. * @OA\Parameter(name="orderId", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
  435. * @OA\RequestBody(
  436. * ),
  437. * @OA\Response(response="200", description="请求成功")
  438. * )
  439. */
  440. public function deleteOrder() {
  441. $input = request()->get();
  442. $model = new OrderModel();
  443. $model::where([
  444. 'user_id' => $this->userId,
  445. 'id' => (int)$input['orderId']
  446. ])->update(['status' => OrderModel::IS_DELETE]);
  447. Until::output([]);
  448. }
  449. public function payOrderAgain() {
  450. $input = Until::getInput();
  451. $rule = [
  452. 'orderId|订单id' => 'require',
  453. ];
  454. Until::check($rule, $input);
  455. $data = (new OrderService())->payAgain((int)$input['orderId']);
  456. Until::output($data);
  457. }
  458. /**
  459. * @OA\Post(path="/api/Order/updateCart",
  460. * tags={"订单管理"},
  461. * summary="加入购物车",
  462. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  463. * @OA\RequestBody(
  464. * @OA\MediaType(
  465. * mediaType="multipart/form-data",
  466. * @OA\Schema(
  467. * @OA\Property(description="商品id", property="productId", type="integer", default="1"),
  468. * @OA\Property(description="商品数量", property="num", type="integer", default="1"),
  469. * @OA\Property(description="购物车id", property="cartId", type="integer", default="1"),
  470. * @OA\Property(description="类型1增加 2减少", property="type", type="integer", default="1"),
  471. * required={"productId","num","cartId","type"})
  472. * )
  473. * ),
  474. * @OA\Response(response="200", description="请求成功")
  475. * )
  476. */
  477. public function updateCart() {
  478. $input = Until::getInput();
  479. $rule = [
  480. 'productId|订单id' => 'require',
  481. 'num|数量' => 'require',
  482. 'cartId|购物车id' => 'require',
  483. ];
  484. Until::check($rule, $input);
  485. $cartModel = new CartModel();
  486. $cartInfo = $cartModel::where([
  487. 'id' => $input['cartId']
  488. ])->find();
  489. if (empty($cartInfo)) {
  490. throw new ApiException('不存在的购物车id');
  491. }
  492. $cartProductModel = new CartProductModel();
  493. $rs = $cartProductModel::where(['product_id' => $input['productId'], 'cart_id' => $input['cartId']])->find();
  494. if (empty($rs)) {
  495. $cartProductModel->insertGetId([
  496. 'product_id' => $input['productId'],
  497. 'cart_id' => $input['cartId'],
  498. 'num' => $input['num'] ?? 1
  499. ]);
  500. }else {
  501. if ($input['type'] == 1) {
  502. $cartProductModel::where(['id' => $rs['id']])->inc('num', 1)->update();
  503. }
  504. if ($input['type'] == 2) {
  505. if ($rs['num'] == 1) {
  506. $cartProductModel::where(['id' => $rs['id']])->delete();
  507. }
  508. $cartProductModel::where(['id' => $rs['id']])->dec('num', 1)->update();
  509. }
  510. }
  511. $cartInfo = Until::modelToArray($cartInfo);
  512. $cartModel->setFields('c.*,cp.product_id,cp.num,p.product_name,p.current_price,p.product_img');
  513. $rs = $cartModel->getCartList();
  514. $cartInfo['totalNum'] = 0;
  515. $cartInfo['totalPrice'] = 0;
  516. foreach ($rs as $v) {
  517. $cartInfo['totalNum'] += $v['num'];
  518. $cartInfo['totalPrice'] += $v['num'] * $v['current_price'];
  519. }
  520. $cartInfo['id'] = $v['id'];
  521. $cartInfo['table_id'] = $v['table_id'];
  522. $cartInfo['store_id'] = $v['store_id'];
  523. $cartInfo['user_id'] = $v['user_id'];
  524. $cartInfo['create_time'] = $v['create_time'];
  525. $cartInfo['list'] = $cartInfo['totalNum'] == 0 ? [] : $rs;
  526. Until::output($cartInfo);
  527. }
  528. /**
  529. * @OA\Post(path="/api/Order/getCart",
  530. * tags={"订单管理"},
  531. * summary="获取购物车",
  532. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  533. * @OA\RequestBody(
  534. * @OA\MediaType(
  535. * mediaType="multipart/form-data",
  536. * @OA\Schema(
  537. * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
  538. * @OA\Property(description="桌台id", property="tableId", type="integer", default="1"),
  539. * required={"storeId","tableId"})
  540. * )
  541. * ),
  542. * @OA\Response(response="200", description="请求成功")
  543. * )
  544. */
  545. public function getCart() {
  546. $input = Until::getInput();
  547. $rule = [
  548. 'storeId|门店id' => 'require',
  549. 'tableId|桌台id' => 'require'
  550. ];
  551. Until::check($rule, $input);
  552. $model = new CartModel();
  553. try {
  554. $model->startTrans();
  555. $whereData = [
  556. 'table_id' => (int)$input['tableId'],
  557. 'store_id' => (int)$input['storeId'],
  558. 'user_id' => $this->userId,
  559. 'status' => CartModel::NORMAL
  560. ];
  561. $cartInfo = $model::where($whereData)->find();
  562. $cartInfo = Until::modelToArray($cartInfo);
  563. if (empty($cartInfo)) {
  564. $id = $model->insertGetId($whereData);
  565. }
  566. $model->setWhere([
  567. 'c.status' => CartModel::NORMAL,
  568. 'c.user_id' => $this->userId,
  569. 'c.table_id' => (int)$input['tableId'],
  570. 'c.store_id' => (int)$input['storeId']
  571. ]);
  572. $model->setFields('c.*,cp.product_id,cp.num,p.product_name,p.current_price,p.product_img');
  573. $rs = $model->getCartList();
  574. $model->commit();
  575. }catch (\Exception $e) {
  576. $model->rollback();
  577. throw new ApiException($e->getMessage());
  578. }
  579. $cartInfo['totalPrice'] = 0;
  580. $cartInfo['totalNum'] = 0;
  581. foreach ($rs as $v) {
  582. $cartInfo['totalNum'] += $v['num'];
  583. $cartInfo['totalPrice'] += $v['num'] * $v['current_price'];
  584. }
  585. $cartInfo['id'] = $v['id'];
  586. $cartInfo['table_id'] = $v['table_id'];
  587. $cartInfo['store_id'] = $v['store_id'];
  588. $cartInfo['user_id'] = $v['user_id'];
  589. $cartInfo['create_time'] = $v['create_time'];
  590. $cartInfo['totalPrice'] = number_format($cartInfo['totalPrice'], 2);
  591. $cartInfo['list'] = $cartInfo['totalNum'] == 0 ? [] : $rs;
  592. Until::output($cartInfo);
  593. }
  594. /**
  595. * @OA\Post(path="/api/Order/clearCart",
  596. * tags={"订单管理"},
  597. * summary="清空购物车",
  598. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  599. * @OA\RequestBody(
  600. * @OA\MediaType(
  601. * mediaType="multipart/form-data",
  602. * @OA\Schema(
  603. * @OA\Property(description="购物车id", property="cartId", type="integer", default="1"),
  604. * required={"cartId"})
  605. * )
  606. * ),
  607. * @OA\Response(response="200", description="请求成功")
  608. * )
  609. */
  610. public function clearCart() {
  611. $input = Until::getInput();
  612. $rule = [
  613. 'cartId|购物车id' => 'require',
  614. ];
  615. Until::check($rule, $input);
  616. $model = new CartModel();
  617. $model::where(['id' => $input['cartId']])->update(['status' => 0]);
  618. Until::output();
  619. }
  620. }