Order.php 32 KB

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