Order.php 28 KB

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