Order.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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], ['order_type','<>', 2] ,['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 => '已删除', 5 => '已完成'];
  114. foreach ($data['list'] as &$one) {
  115. if($one['status'] == 2) {
  116. if ($one['allocateStatus'] == 1 && ($one['write_off_status'] == 2 || $one['order_type'] == 2) && !empty($one['roomId'])) {
  117. $one['allocateOrderStatus'] = 2; // 可开始服务
  118. } elseif ($one['allocateStatus'] == 2) {
  119. $one['allocateOrderStatus'] = 3; //服务中 可结束服务
  120. }
  121. }else {
  122. $one['allocateOrderStatus'] = 1; //未支付
  123. }
  124. if ($one['allocateStatus'] == 3) {
  125. $one['allocateOrderStatus'] = 4; //服务结束
  126. }
  127. $one['productList'] = $product[$one['id']] ?? [];
  128. $one['statusText'] = $statusFilter[$one['status']];
  129. if ($one['status'] === OrderModel::IS_PAY) {
  130. if ($one['write_off_status'] == 1) {
  131. $one['statusText'] = '待消费';
  132. } else if ($one['write_off_status'] == 2) {
  133. if (empty($one['discuss_id']) && !empty($input['discussStatus'])) {
  134. $one['statusText'] = '未评价';
  135. }
  136. $one['statusText'] = '已消费';
  137. }
  138. }
  139. }
  140. Until::output($data);
  141. }
  142. public function save() {
  143. $input = Until::getInput();
  144. $rule = [
  145. 'name|品牌名称' => 'require',
  146. 'groupId|集团id' => 'require',
  147. ];
  148. Until::check($rule, $input);
  149. $model = new BrandModel();
  150. if (!empty($input['id'])) {
  151. $id = (int)$input['id'];
  152. $model::where(['id' => $id])->update([
  153. 'brand_name' => $input['name'],
  154. 'group_id' => $input['groupId']
  155. ]);
  156. } else {
  157. $id = $model->insertGetId([
  158. 'brand_name' => $input['name'],
  159. 'group_id' => $input['groupId']
  160. ]);
  161. }
  162. $info = $model::get($id);
  163. Until::output(['info' => Until::modelToArray($info)]);
  164. }
  165. /**
  166. * @OA\GET(path="/api/Order/read",
  167. * tags={"订单管理"},
  168. * summary="查看订单信息",
  169. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  170. * @OA\Parameter(name="id", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
  171. * @OA\RequestBody(
  172. * ),
  173. * @OA\Response(response="200", description="请求成功")
  174. * )
  175. */
  176. public function read($id) {
  177. $model = new OrderModel();
  178. $where[] = ['o.id', '=', (int)$id];
  179. $model->setWhere($where);
  180. $info = $model->getOrderInfo();
  181. $productList = (new OrderProductModel())::where([['order_id', '=', $info['id']]])->select();
  182. $productList = Until::modelToArray($productList);
  183. foreach ($productList as &$v) {
  184. unset($v['product_snap']);
  185. }
  186. $info['productList'] = $productList;
  187. Until::output(['info' => $info]);
  188. }
  189. /**
  190. * @OA\GET(path="/api/Order/delete",
  191. * tags={"订单管理"},
  192. * summary="删除品牌信息",
  193. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  194. * @OA\Parameter(name="id", in="query", description="品牌id", @OA\Schema(type="ineger",default="1")),
  195. * @OA\Parameter(name="status", in="query", description="1正常 2删除", @OA\Schema(type="ineger",default="1")),
  196. * @OA\RequestBody(
  197. * ),
  198. * @OA\Response(response="200", description="请求成功")
  199. * )
  200. */
  201. public function delete($id, $status) {
  202. $model = new BrandModel();
  203. $where[] = ['id', '=', (int)$id];
  204. $data = ['status' => (int)$status];
  205. $isSuccess = $model::where($where)->update($data);
  206. Until::output(['isSuccess' => $isSuccess]);
  207. }
  208. /**
  209. * @OA\Post(path="/api/Order/createOrder",
  210. * tags={"订单管理"},
  211. * summary="创建订单信息",
  212. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  213. * @OA\RequestBody(
  214. * @OA\MediaType(
  215. * mediaType="multipart/form-data",
  216. * @OA\Schema(
  217. * @OA\Property(description="商品id", property="productId", type="integer", default="1"),
  218. * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
  219. * @OA\Property(description="职员id", property="staffId", type="integer", default="1"),
  220. * @OA\Property(description="预约时间", property="appointmentTime", type="string", default="2020-12-12 16:30"),
  221. * @OA\Property(description="预约结束时间", property="endTime", type="string", default="2020-12-12 16:30"),
  222. * @OA\Property(description="商品数量", property="num", type="integer", default="1"),
  223. * @OA\Property(description="手机号", property="mobile", type="string", default="15623655623"),
  224. * @OA\Property(description="姓名", property="name", type="string", default="夏铭"),
  225. * @OA\Property(description="支付方式", property="payType", type="string", default="1微信 2支付宝 3银行卡 4现金 "),
  226. * required={"productId","storeId","appointmentTime","num","mobile"})
  227. * )
  228. * ),
  229. * @OA\Response(response="200", description="请求成功")
  230. * )
  231. */
  232. public function createOrder() {
  233. $input = Until::getInput();
  234. $rule = [
  235. 'productId|商品id' => 'require',
  236. 'storeId|门店id' => 'require',
  237. 'appointmentTime|预约时间' => 'require',
  238. 'num|数量' => 'require',
  239. 'mobile|手机号' => 'require',
  240. 'staffId|技师' => 'require'
  241. ];
  242. Until::check($rule, $input);
  243. if (empty($input['endTime'])) {
  244. $input['endTime'] = date('Y-m-d H:i', strtotime($input['appointmentTime']) + 1800);
  245. }
  246. if (strtotime($input['appointmentTime']) >= strtotime($input['endTime'])) {
  247. throw new ApiException('结束时间必须大于开始时间');
  248. }
  249. $input['productId'] = (int)$input['productId'];
  250. if ($input['num'] < 1) {
  251. throw new ApiException('数量必须大于1');
  252. }
  253. if ($this->isAdmin() && !empty($input['name'])) {
  254. $model = new UserModel();
  255. $rs = $model::where(['mobile' => $input['mobile']])->find();
  256. if (empty($rs)) {
  257. $id = $model->insertGetId([
  258. 'name' => '',
  259. 'real_name' => $input['name'],
  260. 'avatar' => '',
  261. 'mobile' => $input['mobile'],
  262. 'join_type' => 2,
  263. ]);
  264. $input['userId'] = $id;
  265. } else {
  266. $model::where(['id' => $rs['id']])->update(['real_name' => $input['name']]);
  267. $input['userId'] = $rs['id'];
  268. }
  269. }
  270. $data = (new OrderService())->payOrder($input);
  271. Until::output($data);
  272. }
  273. /**
  274. * @OA\Post(path="/api/Order/createFoodOrder",
  275. * tags={"订单管理"},
  276. * summary="创建订单信息",
  277. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  278. * @OA\RequestBody(
  279. * @OA\MediaType(
  280. * mediaType="multipart/form-data",
  281. * @OA\Schema(
  282. * @OA\Property(description="购物车id", property="cartId", type="integer", default="1"),
  283. * @OA\Property(description="台桌id", property="tableId", type="integer", default="1"),
  284. * required={"cartId","tableId"})
  285. * )
  286. * ),
  287. * @OA\Response(response="200", description="请求成功")
  288. * )
  289. */
  290. public function createFoodOrder() {
  291. $input = Until::getInput();
  292. $rule = [
  293. 'cartId|商品id和数量' => 'require',
  294. 'tableId|桌台id' => 'require',
  295. ];
  296. Until::check($rule, $input);
  297. $data = (new OrderService())->payFoodOrder($input);
  298. Until::output($data);
  299. }
  300. /**
  301. * @OA\Post(path="/api/Order/assignStaff",
  302. * tags={"订单管理"},
  303. * summary="分配职员",
  304. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  305. * @OA\RequestBody(
  306. * @OA\MediaType(
  307. * mediaType="multipart/form-data",
  308. * @OA\Schema(
  309. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  310. * @OA\Property(description="职员id", property="staffId", type="integer", default="1"),
  311. * required={"orderId","staffId"})
  312. * )
  313. * ),
  314. * @OA\Response(response="200", description="请求成功")
  315. * )
  316. */
  317. public function assignStaff() {
  318. $input = Until::getInput();
  319. $rule = [
  320. 'orderId|订单id' => 'require',
  321. 'staffId|职员id' => 'require',
  322. ];
  323. Until::check($rule, $input);
  324. $model = new OrderModel();
  325. $allocateModel = new AllocateModel();
  326. $rs = $allocateModel::where(['order_id' => (int)$input['orderId'], 'staff_id' => $input['staffId']])->find();
  327. if (empty($rs)) {
  328. $allocateModel->insertGetId([
  329. 'order_id' => (int)$input['orderId'],
  330. 'staff_id' => $input['staffId'],
  331. 'admin_id' => $this->adminId
  332. ]);
  333. } else {
  334. if ($rs['status'] == 2) {
  335. throw new ApiException('服务已开始,无法分配职员');
  336. }elseif ($rs['status'] == 3) {
  337. throw new ApiException('服务已结束,无法分配职员');
  338. }
  339. $allocateModel::where(['id' => $rs['id']])->update(['staff_id' => $input['staffId']]);
  340. }
  341. $model::where(['id' => (int)$input['orderId']])->update(['staff_id' => (int)$input['staffId']]);
  342. Until::output();
  343. }
  344. /**
  345. * @OA\Post(path="/api/Order/serverStart",
  346. * tags={"订单管理"},
  347. * summary="服务开始",
  348. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  349. * @OA\RequestBody(
  350. * @OA\MediaType(
  351. * mediaType="multipart/form-data",
  352. * @OA\Schema(
  353. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  354. * required={"orderId"})
  355. * )
  356. * ),
  357. * @OA\Response(response="200", description="请求成功")
  358. * )
  359. */
  360. public function serverStart() {
  361. $input = Until::getInput();
  362. $rule = [
  363. 'orderId|订单id' => 'require',
  364. ];
  365. Until::check($rule, $input);
  366. $allocateModel = new AllocateModel();
  367. $model = new OrderModel();
  368. $order = $model::where(['id' => $input['orderId']])->find();
  369. if (empty($order)) {
  370. throw new ApiException('无此订单');
  371. }
  372. if ($order['status'] != 2) {
  373. throw new ApiException('订单必须是支付状态');
  374. }
  375. $rs = $allocateModel::where(['order_id' => (int)$input['orderId']])->find();
  376. if (empty($rs)) {
  377. throw new ApiException('该服务还没分配职员');
  378. } else {
  379. if ($rs['status'] == 3) {
  380. throw new ApiException('该服务已完成');
  381. }
  382. if ($rs['status'] == 2) {
  383. throw new ApiException('该服务正在进行');
  384. }
  385. }
  386. $allocateModel::where(['id' => $rs['id']])->update(['status' => 2]);
  387. Until::output();
  388. }
  389. /**
  390. * @OA\Post(path="/api/Order/serverComplete",
  391. * tags={"订单管理"},
  392. * summary="服务完成",
  393. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  394. * @OA\RequestBody(
  395. * @OA\MediaType(
  396. * mediaType="multipart/form-data",
  397. * @OA\Schema(
  398. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  399. * required={"orderId"})
  400. * )
  401. * ),
  402. * @OA\Response(response="200", description="请求成功")
  403. * )
  404. */
  405. public function serverComplete() {
  406. $input = Until::getInput();
  407. $rule = [
  408. 'orderId|订单id' => 'require',
  409. ];
  410. Until::check($rule, $input);
  411. $model = new OrderModel();
  412. $allocateModel = new AllocateModel();
  413. $rs = $allocateModel::where(['order_id' => (int)$input['orderId']])->find();
  414. if (empty($rs)) {
  415. throw new ApiException('该服务还没分配职员');
  416. } else {
  417. if ($rs['status'] == 3) {
  418. throw new ApiException('该服务已完成');
  419. }
  420. }
  421. $allocateModel::where(['id' => $rs['id']])->update(['status' => 3]);
  422. $model::where(['id' => $input['orderId']])->update(['status' => 5]);
  423. Until::output();
  424. }
  425. /**
  426. * @OA\Post(path="/api/Order/payOrder",
  427. * tags={"订单管理"},
  428. * summary="手动支付",
  429. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  430. * @OA\RequestBody(
  431. * @OA\MediaType(
  432. * mediaType="multipart/form-data",
  433. * @OA\Schema(
  434. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  435. * @OA\Property(description="支付方式", property="payType", type="string", default="1微信 2支付宝 3银行卡 4现金 "),
  436. * @OA\Property(description="备注", property="remark", type="integer", default="1"),
  437. * required={"orderId","payType"})
  438. * )
  439. * ),
  440. * @OA\Response(response="200", description="请求成功")
  441. * )
  442. */
  443. public function payOrder() {
  444. $input = Until::getInput();
  445. $rule = [
  446. 'orderId|订单id' => 'require',
  447. 'payType|支付方式' => 'require',
  448. ];
  449. Until::check($rule, $input);
  450. $model = new OrderModel();
  451. $orderInfo = $model::where(['id' => (int)$input['orderId']])->find();
  452. if ($orderInfo === null) {
  453. throw new ApiException('无此订单');
  454. }
  455. if ($orderInfo['status'] === OrderModel::IS_PAY) {
  456. throw new ApiException('该订单已支付');
  457. }
  458. if ($orderInfo['order_type'] !== 2) {
  459. throw new ApiException('该订单已不是pad端下单');
  460. }
  461. $model::where(['id' => (int)$input['orderId']])->update([
  462. 'status' => OrderModel::IS_PAY,
  463. 'pay_type' => $input['payType'],
  464. 'remark' => $input['remark'],
  465. 'pay_time' => date('Y-m-d H:i:s')
  466. ]);
  467. Db::table('pay_record')->insertGetId(['order_id' => $input['orderId'], 'admin_id' => $this->adminId]);
  468. $code = random_int(10000, 99999);
  469. $wModel = new WriteOffModel();
  470. $wModel::where(['order_id' => $input['orderId']])->update([
  471. 'write_off_code' => $code,
  472. ]);
  473. Until::output([]);
  474. }
  475. public function notifyOrder() {
  476. $input = Until::getInput();
  477. $rule = [
  478. 'OrderNumber|订单号' => 'require',
  479. ];
  480. Until::check($rule, $input);
  481. if (md5('ef17f532-4661-b07c-5346-65dfa304c0d8' . $input['OrderNumber']) === $this->request->header('serchkey')) {
  482. (new OrderService())->notify($input['OrderNumber']);
  483. Until::output([]);
  484. }
  485. // (new OrderService())->notify($input['OrderNumber']);
  486. Until::output(['decode' => md5('ef17f532-4661-b07c-5346-65dfa304c0d8' . $input['OrderNumber']),
  487. 'encode' => $this->request->header('serchkey')]);
  488. }
  489. public function changeCode() {
  490. $input = Until::getInput();
  491. $rule = [
  492. 'orderId|订单号' => 'require',
  493. ];
  494. Until::check($rule, $input);
  495. $ser = new OrderService();
  496. $code = $ser->changeCode();
  497. $wModel = new WriteOffModel();
  498. $wModel::where(['order_id' => $input['orderId'], 'write_off_status' => 1])->update([
  499. 'write_off_code' => $code,
  500. 'over_time' => date('Y-m-d H:i:s', time() + 2 * 60),
  501. ]);
  502. Until::output(['code' => $code]);
  503. }
  504. /**
  505. * @OA\Post(path="/api/Order/writeOffOrder",
  506. * tags={"订单管理"},
  507. * summary="核销订单",
  508. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  509. * @OA\RequestBody(
  510. * @OA\MediaType(
  511. * mediaType="multipart/form-data",
  512. * @OA\Schema(
  513. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  514. * @OA\Property(description="核销code", property="code", type="string", default="1"),
  515. * required={"orderId","code"})
  516. * )
  517. * ),
  518. * @OA\Response(response="200", description="请求成功")
  519. * )
  520. */
  521. public function writeOffOrder() {
  522. $input = Until::getInput();
  523. $rule = [
  524. 'orderId|订单id' => 'require',
  525. 'code|核销码' => 'require'
  526. ];
  527. Until::check($rule, $input);
  528. $rs = (new OrderModel())::where(['id' => $input['orderId']])->find();
  529. if ($rs['status'] != OrderModel::IS_PAY) {
  530. throw new ApiException('该订单未付款,不可核销');
  531. }
  532. $model = new WriteOffModel();
  533. $where = ['order_id' => $input['orderId'], 'write_off_code' => $input['code']];
  534. $writeOff = $model::where($where)->find();
  535. if ($writeOff === null) {
  536. throw new ApiException('核销码错误');
  537. }
  538. if ($writeOff['write_off_status'] == 2) {
  539. throw new ApiException('该订单已经核销了');
  540. }
  541. $userModel = new UserModel();
  542. $userModel::where([['id', '=', $rs['user_id']], ['first_store_id', '=', 0]])->update(['first_store_id' => $rs['store_id']]);
  543. $model::where($where)
  544. ->update([
  545. 'write_off_status' => 2,
  546. 'write_off_time' => date('Y-m-d H:i:s'),
  547. 'admin_id' => $this->adminId
  548. ]);
  549. Until::output();
  550. }
  551. /**
  552. * @OA\Post(path="/api/Order/closeOrder",
  553. * tags={"订单管理"},
  554. * summary="订单关闭",
  555. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  556. * @OA\Parameter(name="orderId", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
  557. * @OA\RequestBody(
  558. * ),
  559. * @OA\Response(response="200", description="请求成功")
  560. * )
  561. */
  562. public function closeOrder() {
  563. $input = Until::getInput();
  564. $model = new OrderModel();
  565. $model::where([
  566. 'user_id' => $this->userId,
  567. 'id' => (int)$input['orderId']
  568. ])->update(['status' => OrderModel::IS_CLOSE]);
  569. Until::output([]);
  570. }
  571. /**
  572. * @OA\Post(path="/api/Order/deleteOrder",
  573. * tags={"订单管理"},
  574. * summary="订单删除",
  575. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  576. * @OA\Parameter(name="orderId", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
  577. * @OA\RequestBody(
  578. * ),
  579. * @OA\Response(response="200", description="请求成功")
  580. * )
  581. */
  582. public function deleteOrder() {
  583. $input = Until::getInput();
  584. $model = new OrderModel();
  585. $model::where([
  586. 'user_id' => $this->userId,
  587. 'id' => (int)$input['orderId']
  588. ])->update(['status' => OrderModel::IS_DELETE]);
  589. Until::output([]);
  590. }
  591. public function payOrderAgain() {
  592. $input = Until::getInput();
  593. $rule = [
  594. 'orderId|订单id' => 'require',
  595. ];
  596. Until::check($rule, $input);
  597. $data = (new OrderService())->payAgain((int)$input['orderId']);
  598. Until::output($data);
  599. }
  600. /**
  601. * @OA\Post(path="/api/Order/updateCart",
  602. * tags={"订单管理"},
  603. * summary="加入购物车",
  604. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  605. * @OA\RequestBody(
  606. * @OA\MediaType(
  607. * mediaType="multipart/form-data",
  608. * @OA\Schema(
  609. * @OA\Property(description="商品id", property="productId", type="integer", default="1"),
  610. * @OA\Property(description="商品数量", property="num", type="integer", default="1"),
  611. * @OA\Property(description="购物车id", property="cartId", type="integer", default="1"),
  612. * @OA\Property(description="类型1增加 2减少", property="type", type="integer", default="1"),
  613. * required={"productId","num","cartId","type"})
  614. * )
  615. * ),
  616. * @OA\Response(response="200", description="请求成功")
  617. * )
  618. */
  619. public function updateCart() {
  620. $input = Until::getInput();
  621. $rule = [
  622. 'productId|订单id' => 'require',
  623. 'num|数量' => 'require',
  624. 'cartId|购物车id' => 'require',
  625. ];
  626. Until::check($rule, $input);
  627. $cartModel = new CartModel();
  628. $cartInfo = $cartModel::where([
  629. 'id' => $input['cartId']
  630. ])->find();
  631. if (empty($cartInfo)) {
  632. throw new ApiException('不存在的购物车id');
  633. }
  634. $cartProductModel = new CartProductModel();
  635. $rs = $cartProductModel::where(['product_id' => $input['productId'], 'cart_id' => $input['cartId']])->find();
  636. if (empty($rs)) {
  637. $cartProductModel->insertGetId([
  638. 'product_id' => $input['productId'],
  639. 'cart_id' => $input['cartId'],
  640. 'num' => $input['num'] ?? 1
  641. ]);
  642. } else {
  643. if ($input['type'] == 1) {
  644. $cartProductModel::where(['id' => $rs['id']])->inc('num', 1)->update();
  645. }
  646. if ($input['type'] == 2) {
  647. if ($rs['num'] == 1) {
  648. $cartProductModel::where(['id' => $rs['id']])->delete();
  649. }
  650. $cartProductModel::where(['id' => $rs['id']])->dec('num', 1)->update();
  651. }
  652. }
  653. $cartInfo = Until::modelToArray($cartInfo);
  654. $cartModel->setFields('c.*,cp.product_id,cp.num,p.product_name,p.current_price,p.product_img');
  655. $rs = $cartModel->getCartList();
  656. $cartInfo['totalNum'] = 0;
  657. $cartInfo['totalPrice'] = 0;
  658. foreach ($rs as $v) {
  659. $cartInfo['totalNum'] += $v['num'];
  660. $cartInfo['totalPrice'] += $v['num'] * $v['current_price'];
  661. }
  662. $cartInfo['id'] = $v['id'];
  663. $cartInfo['table_id'] = $v['table_id'];
  664. $cartInfo['store_id'] = $v['store_id'];
  665. $cartInfo['user_id'] = $v['user_id'];
  666. $cartInfo['create_time'] = $v['create_time'];
  667. $cartInfo['list'] = $cartInfo['totalNum'] == 0 ? [] : $rs;
  668. Until::output($cartInfo);
  669. }
  670. /**
  671. * @OA\Post(path="/api/Order/getCart",
  672. * tags={"订单管理"},
  673. * summary="获取购物车",
  674. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  675. * @OA\RequestBody(
  676. * @OA\MediaType(
  677. * mediaType="multipart/form-data",
  678. * @OA\Schema(
  679. * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
  680. * @OA\Property(description="桌台id", property="tableId", type="integer", default="1"),
  681. * required={"storeId","tableId"})
  682. * )
  683. * ),
  684. * @OA\Response(response="200", description="请求成功")
  685. * )
  686. */
  687. public function getCart() {
  688. $input = Until::getInput();
  689. $rule = [
  690. 'storeId|门店id' => 'require',
  691. 'tableId|桌台id' => 'require'
  692. ];
  693. Until::check($rule, $input);
  694. $model = new CartModel();
  695. try {
  696. $model->startTrans();
  697. $whereData = [
  698. 'table_id' => (int)$input['tableId'],
  699. 'store_id' => (int)$input['storeId'],
  700. 'user_id' => $this->userId,
  701. 'status' => CartModel::NORMAL
  702. ];
  703. $cartInfo = $model::where($whereData)->find();
  704. $cartInfo = Until::modelToArray($cartInfo);
  705. if (empty($cartInfo)) {
  706. $id = $model->insertGetId($whereData);
  707. }
  708. $model->setWhere([
  709. 'c.status' => CartModel::NORMAL,
  710. 'c.user_id' => $this->userId,
  711. 'c.table_id' => (int)$input['tableId'],
  712. 'c.store_id' => (int)$input['storeId']
  713. ]);
  714. $model->setFields('c.*,cp.product_id,cp.num,p.product_name,p.current_price,p.product_img');
  715. $rs = $model->getCartList();
  716. $model->commit();
  717. } catch (\Exception $e) {
  718. $model->rollback();
  719. throw new ApiException($e->getMessage());
  720. }
  721. $cartInfo['totalPrice'] = 0;
  722. $cartInfo['totalNum'] = 0;
  723. foreach ($rs as $v) {
  724. $cartInfo['totalNum'] += $v['num'];
  725. $cartInfo['totalPrice'] += $v['num'] * $v['current_price'];
  726. }
  727. $cartInfo['id'] = $v['id'];
  728. $cartInfo['table_id'] = $v['table_id'];
  729. $cartInfo['store_id'] = $v['store_id'];
  730. $cartInfo['user_id'] = $v['user_id'];
  731. $cartInfo['create_time'] = $v['create_time'];
  732. $cartInfo['totalPrice'] = number_format($cartInfo['totalPrice'], 2);
  733. $cartInfo['list'] = $cartInfo['totalNum'] == 0 ? [] : $rs;
  734. Until::output($cartInfo);
  735. }
  736. /**
  737. * @OA\Post(path="/api/Order/clearCart",
  738. * tags={"订单管理"},
  739. * summary="清空购物车",
  740. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  741. * @OA\RequestBody(
  742. * @OA\MediaType(
  743. * mediaType="multipart/form-data",
  744. * @OA\Schema(
  745. * @OA\Property(description="购物车id", property="cartId", type="integer", default="1"),
  746. * required={"cartId"})
  747. * )
  748. * ),
  749. * @OA\Response(response="200", description="请求成功")
  750. * )
  751. */
  752. public function clearCart() {
  753. $input = Until::getInput();
  754. $rule = [
  755. 'cartId|购物车id' => 'require',
  756. ];
  757. Until::check($rule, $input);
  758. $model = new CartModel();
  759. $model::where(['id' => $input['cartId']])->update(['status' => 0]);
  760. Until::output();
  761. }
  762. }