Order.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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\OrderChangeModel;
  15. use app\api\model\OrderModel;
  16. use app\api\model\OrderProductModel;
  17. use app\api\model\OrderRoomModel;
  18. use app\api\model\ProductModel;
  19. use app\api\model\RoomModel;
  20. use app\api\model\StaffModel;
  21. use app\api\model\TurnStaffModel;
  22. use app\api\model\UserModel;
  23. use app\api\model\WriteOffModel;
  24. use app\common\service\OrderService;
  25. use app\common\service\ServerService;
  26. use app\common\until\Until;
  27. use think\App;
  28. use think\Db;
  29. use think\Model;
  30. class Order extends BaseController {
  31. public function __construct(App $app) {
  32. parent::__construct($app);
  33. if ($this->userId == '123456789') {
  34. throw new ApiException(
  35. '网络繁忙'
  36. );
  37. }
  38. }
  39. /**
  40. * @OA\Get(path="/api/Order/index",
  41. * tags={"订单管理"},
  42. * summary="订单列表",
  43. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  44. * @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="ineger",default="1")),
  45. * @OA\Parameter(name="pageSize", in="query", description="页尺寸", @OA\Schema(type="integer",default="10")),
  46. * @OA\Parameter(name="orderStatus", in="query", description="订单状态 1未支付 2已支付 ", @OA\Schema(type="integer")),
  47. * @OA\Parameter(name="writeOffStatus", in="query", description="核销状态 1未核销 2已核销 ", @OA\Schema(type="integer")),
  48. * @OA\Parameter(name="discussStatus", in="query", description="评价状态 1未评价 2已评价 ", @OA\Schema(type="integer")),
  49. * @OA\Parameter(name="orderSn", in="query", description="订单号", @OA\Schema(type="string")),
  50. * @OA\Parameter(name="mobile", in="query", description="手机号", @OA\Schema(type="string")),
  51. * @OA\Parameter(name="storeId", in="query", description="门店id", @OA\Schema(type="integer")),
  52. * @OA\Parameter(name="appointmentTime", in="query", description="预约时间", @OA\Schema(type="2020-01-02,2021-12-30")),
  53. * @OA\Parameter(name="createTcreateTimeime", in="query", description="订单时间", @OA\Schema(type="2020-01-02,2021-12-30")),
  54. * @OA\Parameter(name="orderType", in="query", description="订单类型 1小程序下单 2后台增加 3点餐", @OA\Schema(type="string")),
  55. * @OA\RequestBody(
  56. * ),
  57. * @OA\Response(response="200", description="请求成功")
  58. * )
  59. */
  60. public function index() {
  61. $input = Until::getInput();
  62. $model = new OrderModel();
  63. $model->setPage($input['page'] ?? 1);
  64. $model->setPageSize($input['pageSize'] ?? 10);
  65. $where = [];
  66. $model::where([['status', '=', 1], ['order_type', '<>', 2], ['create_time', '<',
  67. date('Y-m-d H:i:s', strtotime('-15minutes'))]])
  68. ->update(['status' => OrderModel::IS_CLOSE]);
  69. if (!empty($input['orderSn'])) {
  70. $where[] = ['o.order_sn', 'like', "%{$input['orderSn']}%"];
  71. }
  72. if (!empty($input['mobile'])) {
  73. $where[] = ['o.mobile', 'like', "%{$input['mobile']}%"];
  74. }
  75. if (!empty($input['writeOffStatus'])) {
  76. if (empty($input['foodOrder'])) {
  77. $where[] = ['wo.write_off_status', '=', (int)$input['writeOffStatus']];
  78. } else if ($input['foodOrder'] == 2) {
  79. $model->setOp('write_off_status', 'left');
  80. }
  81. }
  82. if (!empty($input['orderStatus'])) {
  83. $where[] = ['o.status', '=', (int)$input['orderStatus']];
  84. }
  85. if (!empty($input['discussStatus'])) {
  86. $where[] = ['discussOrder.id', '=', null];
  87. }
  88. if (!empty($input['storeId'])) {
  89. $where[] = ['store.id', '=', $input['storeId']];
  90. }
  91. if (!empty($input['appointmentTime'])) {
  92. $data = explode(',', $input['appointmentTime']);
  93. $where[] = ['o.appointment_time', 'between', [$data[0], $data[1] . ' 23:59:59']];
  94. }
  95. if (!empty($input['createTime'])) {
  96. $data = explode(',', $input['createTime']);
  97. $where[] = ['o.create_time', 'between', [$data[0], $data[1] . ' 23:59:59']];
  98. }
  99. if (!empty($input['orderType'])) {
  100. $type = explode(',', $input['orderType']);
  101. $where[] = ['o.order_type', 'in', $type];
  102. }
  103. if (!$this->isAdmin()) {
  104. $where[] = ['o.user_id', '=', $this->userId];
  105. // if (empty($input['orderStatus'])) {
  106. // $where[] = ['o.status', '<>', OrderModel::IS_DELETE];
  107. // }
  108. }
  109. $model->setWhere($where);
  110. $data = $model->getOrderList();
  111. $orderIds = array_column($data['list'], 'id');
  112. $productList = (new OrderProductModel())::where([['order_id', 'in', $orderIds]])->select();
  113. $productList = Until::modelToArray($productList);
  114. $product = [];
  115. foreach ($productList as &$v) {
  116. unset($v['product_snap']);
  117. $product[$v['order_id']][] = $v;
  118. }
  119. $statusFilter = [1 => '未支付', 2 => '已支付', 3 => '已关闭', 4 => '已删除', 5 => '已完成'];
  120. foreach ($data['list'] as $k => &$one) {
  121. if ($one['status'] == OrderModel::IS_PAY) {
  122. if ($one['allocateStatus'] == 1 && ($one['write_off_status'] == 2 || $one['order_type'] == 2) && !empty($one['roomId'])) {
  123. $one['allocateOrderStatus'] = 2; // 可开始服务
  124. } elseif ($one['allocateStatus'] == 2) {
  125. $one['allocateOrderStatus'] = 3; //服务中 可结束服务
  126. } else {
  127. $one['allocateOrderStatus'] = 999; //未核销 或者未分配房间
  128. }
  129. } else {
  130. $one['allocateOrderStatus'] = 1; //未支付
  131. }
  132. if ($one['allocateStatus'] == 3) {
  133. $one['allocateOrderStatus'] = 4; //服务结束
  134. }
  135. // var_dump($one['allocateOrderStatus']);
  136. $one['productList'] = $product[$one['id']] ?? [];
  137. $one['statusText'] = $statusFilter[$one['status']];
  138. if ($one['status'] === OrderModel::IS_PAY) {
  139. if ($one['write_off_status'] == 2) {
  140. // $one['statusText'] = '待消费';
  141. } else {
  142. $one['statusText'] = '已支付';
  143. if ($one['allocateStatus'] == 3 && empty($one['discuss_id']) && !empty($input['discussStatus'])) {
  144. $one['statusText'] = '未评价';
  145. }
  146. }
  147. }
  148. }
  149. Until::output($data);
  150. }
  151. public function save() {
  152. $input = Until::getInput();
  153. $rule = [
  154. 'name|品牌名称' => 'require',
  155. 'groupId|集团id' => 'require',
  156. ];
  157. Until::check($rule, $input);
  158. $model = new BrandModel();
  159. if (!empty($input['id'])) {
  160. $id = (int)$input['id'];
  161. $model::where(['id' => $id])->update([
  162. 'brand_name' => $input['name'],
  163. 'group_id' => $input['groupId']
  164. ]);
  165. } else {
  166. $id = $model->insertGetId([
  167. 'brand_name' => $input['name'],
  168. 'group_id' => $input['groupId']
  169. ]);
  170. }
  171. $info = $model::get($id);
  172. Until::output(['info' => Until::modelToArray($info)]);
  173. }
  174. /**
  175. * @OA\GET(path="/api/Order/read",
  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\RequestBody(
  181. * ),
  182. * @OA\Response(response="200", description="请求成功")
  183. * )
  184. */
  185. public function read($id) {
  186. $model = new OrderModel();
  187. $where[] = ['o.id', '=', (int)$id];
  188. $model->setWhere($where);
  189. $info = $model->getOrderInfo();
  190. $productList = (new OrderProductModel())::where([['order_id', '=', $info['id']]])->select();
  191. $productList = Until::modelToArray($productList);
  192. foreach ($productList as &$v) {
  193. unset($v['product_snap']);
  194. }
  195. $info['productList'] = $productList;
  196. Until::output(['info' => $info]);
  197. }
  198. /**
  199. * @OA\GET(path="/api/Order/delete",
  200. * tags={"订单管理"},
  201. * summary="删除品牌信息",
  202. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  203. * @OA\Parameter(name="id", in="query", description="品牌id", @OA\Schema(type="ineger",default="1")),
  204. * @OA\Parameter(name="status", in="query", description="1正常 2删除", @OA\Schema(type="ineger",default="1")),
  205. * @OA\RequestBody(
  206. * ),
  207. * @OA\Response(response="200", description="请求成功")
  208. * )
  209. */
  210. public function delete($id, $status) {
  211. $model = new BrandModel();
  212. $where[] = ['id', '=', (int)$id];
  213. $data = ['status' => (int)$status];
  214. $isSuccess = $model::where($where)->update($data);
  215. Until::output(['isSuccess' => $isSuccess]);
  216. }
  217. /**
  218. * @OA\Post(path="/api/Order/createOrder",
  219. * tags={"订单管理"},
  220. * summary="创建订单信息",
  221. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  222. * @OA\RequestBody(
  223. * @OA\MediaType(
  224. * mediaType="multipart/form-data",
  225. * @OA\Schema(
  226. * @OA\Property(description="商品id", property="productId", type="integer", default="1"),
  227. * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
  228. * @OA\Property(description="职员id", property="staffId", type="integer", default="1"),
  229. * @OA\Property(description="预约时间", property="appointmentTime", type="string", default="2020-12-12 16:30"),
  230. * @OA\Property(description="预约结束时间", property="endTime", type="string", default="2020-12-12 16:30"),
  231. * @OA\Property(description="商品数量", property="num", type="integer", default="1"),
  232. * @OA\Property(description="手机号", property="mobile", type="string", default="15623655623"),
  233. * @OA\Property(description="姓名", property="name", type="string", default="夏铭"),
  234. * @OA\Property(description="支付方式", property="payType", type="string", default="1微信 2支付宝 3银行卡 4现金 "),
  235. * required={"productId","storeId","appointmentTime","num","mobile"})
  236. * )
  237. * ),
  238. * @OA\Response(response="200", description="请求成功")
  239. * )
  240. */
  241. public function createOrder() {
  242. $input = Until::getInput();
  243. $rule = [
  244. 'productId|商品id' => 'require',
  245. 'storeId|门店id' => 'require',
  246. 'appointmentTime|预约时间' => 'require',
  247. 'num|数量' => 'require',
  248. 'mobile|手机号' => 'require',
  249. 'staffId|技师' => 'require'
  250. ];
  251. Until::check($rule, $input);
  252. if (empty($input['endTime'])) {
  253. $input['endTime'] = date('Y-m-d H:i', strtotime($input['appointmentTime']) + 1800);
  254. }
  255. if (strtotime($input['appointmentTime']) >= strtotime($input['endTime'])) {
  256. throw new ApiException('结束时间必须大于开始时间');
  257. }
  258. $input['productId'] = (int)$input['productId'];
  259. if ($input['num'] < 1) {
  260. throw new ApiException('数量必须大于1');
  261. }
  262. if ($this->isAdmin() && !empty($input['name'])) {
  263. $model = new UserModel();
  264. $rs = $model::where(['mobile' => $input['mobile']])->find();
  265. if (empty($rs)) {
  266. $id = $model->insertGetId([
  267. 'name' => '',
  268. 'real_name' => $input['name'],
  269. 'avatar' => '',
  270. 'mobile' => $input['mobile'],
  271. 'join_type' => 2,
  272. ]);
  273. $input['userId'] = $id;
  274. } else {
  275. $model::where(['id' => $rs['id']])->update(['real_name' => $input['name']]);
  276. $input['userId'] = $rs['id'];
  277. }
  278. }
  279. $data = (new OrderService())->payOrder($input);
  280. Until::output($data);
  281. }
  282. /**
  283. * @OA\Post(path="/api/Order/createFoodOrder",
  284. * tags={"订单管理"},
  285. * summary="创建订单信息",
  286. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  287. * @OA\RequestBody(
  288. * @OA\MediaType(
  289. * mediaType="multipart/form-data",
  290. * @OA\Schema(
  291. * @OA\Property(description="购物车id", property="cartId", type="integer", default="1"),
  292. * @OA\Property(description="台桌id", property="tableId", type="integer", default="1"),
  293. * required={"cartId","tableId"})
  294. * )
  295. * ),
  296. * @OA\Response(response="200", description="请求成功")
  297. * )
  298. */
  299. public function createFoodOrder() {
  300. $input = Until::getInput();
  301. $rule = [
  302. 'cartId|商品id和数量' => 'require',
  303. 'tableId|桌台id' => 'require',
  304. ];
  305. Until::check($rule, $input);
  306. $data = (new OrderService())->payFoodOrder($input);
  307. Until::output($data);
  308. }
  309. /**
  310. * @OA\Post(path="/api/Order/assignStaff",
  311. * tags={"订单管理"},
  312. * summary="分配职员",
  313. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  314. * @OA\RequestBody(
  315. * @OA\MediaType(
  316. * mediaType="multipart/form-data",
  317. * @OA\Schema(
  318. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  319. * @OA\Property(description="职员id", property="staffId", type="integer", default="1"),
  320. * @OA\Property(description="是否轮班", property="isTurn", type="integer", default="1"),
  321. * required={"orderId"})
  322. * )
  323. * ),
  324. * @OA\Response(response="200", description="请求成功")
  325. * )
  326. */
  327. public function assignStaff() {
  328. $input = Until::getInput();
  329. $rule = [
  330. 'orderId|订单id' => 'require',
  331. // 'staffId|职员id' => 'require',
  332. ];
  333. Until::check($rule, $input);
  334. $model = new OrderModel();
  335. $orderInfo = $model::where(['id' => (int)$input['orderId']])->find();
  336. $allocateModel = new AllocateModel();
  337. $rs = $allocateModel::where(['order_id' => (int)$input['orderId']])->find();
  338. if (empty($rs)) {
  339. $allocateModel->insertGetId([
  340. 'order_id' => (int)$input['orderId'],
  341. 'staff_id' => $input['staffId'],
  342. 'assign_admin_id' => $this->adminId
  343. ]);
  344. } else {
  345. if ($rs['status'] == 2) {
  346. throw new ApiException('服务已开始,无法分配职员');
  347. } elseif ($rs['status'] == 3) {
  348. throw new ApiException('服务已结束,无法分配职员');
  349. }
  350. if (empty($input['staffId'])) {
  351. $where[] = ['s.status', '=', 1];
  352. $where[] = ['sto.id', '=', $orderInfo['store_id']];
  353. $turnInfo = (new StaffModel())->getTurnInfo();
  354. $input['staffId'] = $turnInfo['staffId'];
  355. }
  356. $allocateModel::where(['id' => $rs['id']])->update(['staff_id' => $input['staffId'], 'assign_admin_id' => $this->adminId]);
  357. }
  358. $model::where(['id' => (int)$input['orderId']])->update(['staff_id' => (int)$input['staffId']]);
  359. (new StaffModel())::where(['id' => $input['staffId']])->update(['server_status' => 2]);
  360. Until::output();
  361. }
  362. /**
  363. * @OA\Post(path="/api/Order/serverStart",
  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. * required={"orderId"})
  373. * )
  374. * ),
  375. * @OA\Response(response="200", description="请求成功")
  376. * )
  377. */
  378. public function serverStart() {
  379. $input = Until::getInput();
  380. $rule = [
  381. 'orderId|订单id' => 'require',
  382. ];
  383. Until::check($rule, $input);
  384. $ser = new ServerService();
  385. $ser->startService($input['orderId']);
  386. Until::output();
  387. }
  388. /**
  389. * @OA\Post(path="/api/Order/clockStart",
  390. * tags={"订单管理"},
  391. * summary="上钟",
  392. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  393. * @OA\RequestBody(
  394. * @OA\MediaType(
  395. * mediaType="multipart/form-data",
  396. * @OA\Schema(
  397. * @OA\Property(description="上钟器卡号(房间卡号)", property="clockCode", type="string", default="1"),
  398. * @OA\Property(description="工牌卡号", property="braceletCode", type="string", default="1"),
  399. * required={"clockCode","braceletCode"})
  400. * )
  401. * ),
  402. * @OA\Response(response="200", description="请求成功")
  403. * )
  404. */
  405. public function clockStart() {
  406. $input = Until::getInput();
  407. $rule = [
  408. 'clockCode|上钟器卡号' => 'require',
  409. 'braceletCode|手环卡号' => 'require',
  410. ];
  411. Until::check($rule, $input);
  412. $staffInfo = ServerService::getStaffByCode($input['braceletCode']);
  413. $roomInfo = ServerService::getRoomByCode($input['clockCode']);
  414. $allocateModel = new AllocateModel();
  415. $info = $allocateModel::where(['staff_id' => $staffInfo['id'], 'store_id' => $roomInfo['store_id']])->order('id', 'desc')->find();
  416. if (empty($info) || empty($info['order_id'])) {
  417. throw new ApiException('无此服务');
  418. }
  419. $ser = new ServerService();
  420. $ser->startService($info['order_id']);
  421. Until::output();
  422. }
  423. /**
  424. * @OA\Post(path="/api/Order/serverComplete",
  425. * tags={"订单管理"},
  426. * summary="服务完成",
  427. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  428. * @OA\RequestBody(
  429. * @OA\MediaType(
  430. * mediaType="multipart/form-data",
  431. * @OA\Schema(
  432. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  433. * required={"orderId"})
  434. * )
  435. * ),
  436. * @OA\Response(response="200", description="请求成功")
  437. * )
  438. */
  439. public function serverComplete() {
  440. $input = Until::getInput();
  441. $rule = [
  442. 'orderId|订单id' => 'require',
  443. ];
  444. Until::check($rule, $input);
  445. $model = new OrderModel();
  446. $allocateModel = new AllocateModel();
  447. $rs = $allocateModel::where(['order_id' => (int)$input['orderId']])->find();
  448. if (empty($rs)) {
  449. throw new ApiException('该服务还没分配职员');
  450. } else {
  451. if ($rs['status'] == 3) {
  452. throw new ApiException('该服务已完成');
  453. }
  454. }
  455. if (!empty($this->adminId)) {
  456. $opId = $this->adminId;
  457. $opType = 'admin';
  458. } else {
  459. $opId = $this->userId;
  460. $opType = 'xcx';
  461. }
  462. $allocateModel::where(['id' => $rs['id']])->update(['status' => 3, 'server_end_time' => date('Y-m-d H:i:s'), 'end_id' => $opId, 'op_type' => $opType]);
  463. $model::where(['id' => $input['orderId']])->update(['status' => 5]);
  464. $orderRoomRs = (new OrderRoomModel())::where(['order_id' => $input['orderId']])->find();
  465. (new RoomModel())::where(['id' => $orderRoomRs['room_id']])->update(['room_server_status' => 3]);
  466. (new StaffModel())::where(['id' => $rs['staff_id']])->update(['server_status' => 1]);
  467. Until::output();
  468. }
  469. /**
  470. * @OA\Post(path="/api/Order/payOrder",
  471. * tags={"订单管理"},
  472. * summary="手动支付",
  473. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  474. * @OA\RequestBody(
  475. * @OA\MediaType(
  476. * mediaType="multipart/form-data",
  477. * @OA\Schema(
  478. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  479. * @OA\Property(description="支付方式", property="payType", type="string", default="1微信 2支付宝 3银行卡 4现金 "),
  480. * @OA\Property(description="备注", property="remark", type="integer", default="1"),
  481. * required={"orderId","payType"})
  482. * )
  483. * ),
  484. * @OA\Response(response="200", description="请求成功")
  485. * )
  486. */
  487. public function payOrder() {
  488. $input = Until::getInput();
  489. $rule = [
  490. 'orderId|订单id' => 'require',
  491. 'payType|支付方式' => 'require',
  492. ];
  493. Until::check($rule, $input);
  494. $model = new OrderModel();
  495. $orderInfo = $model::where(['id' => (int)$input['orderId']])->find();
  496. if ($orderInfo === null) {
  497. throw new ApiException('无此订单');
  498. }
  499. if ($orderInfo['status'] === OrderModel::IS_PAY) {
  500. throw new ApiException('该订单已支付');
  501. }
  502. if ($orderInfo['order_type'] !== 2) {
  503. throw new ApiException('该订单已不是pad端下单');
  504. }
  505. $model::where(['id' => (int)$input['orderId']])->update([
  506. 'status' => OrderModel::IS_PAY,
  507. 'pay_type' => $input['payType'],
  508. 'remark' => $input['remark'],
  509. 'pay_time' => date('Y-m-d H:i:s')
  510. ]);
  511. Db::table('pay_record')->insertGetId(['order_id' => $input['orderId'], 'admin_id' => $this->adminId]);
  512. $code = random_int(10000, 99999);
  513. $wModel = new WriteOffModel();
  514. $wModel::where(['order_id' => $input['orderId']])->update([
  515. 'write_off_code' => $code,
  516. ]);
  517. Until::output([]);
  518. }
  519. public function notifyOrder() {
  520. $input = Until::getInput();
  521. $rule = [
  522. 'OrderNumber|订单号' => 'require',
  523. ];
  524. Until::check($rule, $input);
  525. if (md5('ef17f532-4661-b07c-5346-65dfa304c0d8' . $input['OrderNumber']) === $this->request->header('serchkey')) {
  526. (new OrderService())->notify($input['OrderNumber']);
  527. Until::output([]);
  528. }
  529. (new OrderService())->notify($input['OrderNumber']);
  530. Until::output(['decode' => md5('ef17f532-4661-b07c-5346-65dfa304c0d8' . $input['OrderNumber']),
  531. 'encode' => $this->request->header('serchkey')]);
  532. }
  533. public function changeCode() {
  534. $input = Until::getInput();
  535. $rule = [
  536. 'orderId|订单号' => 'require',
  537. ];
  538. Until::check($rule, $input);
  539. $ser = new OrderService();
  540. $code = $ser->changeCode();
  541. $wModel = new WriteOffModel();
  542. $wModel::where(['order_id' => $input['orderId'], 'write_off_status' => 1])->update([
  543. 'write_off_code' => $code,
  544. 'over_time' => date('Y-m-d H:i:s', time() + 2 * 60),
  545. ]);
  546. Until::output(['code' => $code]);
  547. }
  548. /**
  549. * @OA\Post(path="/api/Order/writeOffOrder",
  550. * tags={"订单管理"},
  551. * summary="核销订单",
  552. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  553. * @OA\RequestBody(
  554. * @OA\MediaType(
  555. * mediaType="multipart/form-data",
  556. * @OA\Schema(
  557. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  558. * @OA\Property(description="核销code", property="code", type="string", default="1"),
  559. * required={"orderId","code"})
  560. * )
  561. * ),
  562. * @OA\Response(response="200", description="请求成功")
  563. * )
  564. */
  565. public function writeOffOrder() {
  566. $input = Until::getInput();
  567. $rule = [
  568. 'orderId|订单id' => 'require',
  569. 'code|核销码' => 'require'
  570. ];
  571. Until::check($rule, $input);
  572. $rs = (new OrderModel())::where(['id' => $input['orderId']])->find();
  573. if ($rs['status'] != OrderModel::IS_PAY) {
  574. throw new ApiException('该订单未付款,不可核销');
  575. }
  576. $model = new WriteOffModel();
  577. $where = ['order_id' => $input['orderId'], 'write_off_code' => $input['code']];
  578. $writeOff = $model::where($where)->find();
  579. if ($writeOff === null) {
  580. throw new ApiException('核销码错误');
  581. }
  582. if ($writeOff['write_off_status'] == 2) {
  583. throw new ApiException('该订单已经核销了');
  584. }
  585. $userModel = new UserModel();
  586. $userModel::where([['id', '=', $rs['user_id']], ['first_store_id', '=', 0]])->update(['first_store_id' => $rs['store_id']]);
  587. $model::where($where)
  588. ->update([
  589. 'write_off_status' => 2,
  590. 'write_off_time' => date('Y-m-d H:i:s'),
  591. 'admin_id' => $this->adminId
  592. ]);
  593. Until::output();
  594. }
  595. /**
  596. * @OA\Post(path="/api/Order/closeOrder",
  597. * tags={"订单管理"},
  598. * summary="订单关闭",
  599. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  600. * @OA\Parameter(name="orderId", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
  601. * @OA\RequestBody(
  602. * ),
  603. * @OA\Response(response="200", description="请求成功")
  604. * )
  605. */
  606. public function closeOrder() {
  607. $input = Until::getInput();
  608. $model = new OrderModel();
  609. $model::where([
  610. 'user_id' => $this->userId,
  611. 'id' => (int)$input['orderId']
  612. ])->update(['status' => OrderModel::IS_CLOSE]);
  613. Until::output([]);
  614. }
  615. /**
  616. * @OA\Post(path="/api/Order/deleteOrder",
  617. * tags={"订单管理"},
  618. * summary="订单删除",
  619. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  620. * @OA\Parameter(name="orderId", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
  621. * @OA\RequestBody(
  622. * ),
  623. * @OA\Response(response="200", description="请求成功")
  624. * )
  625. */
  626. public function deleteOrder() {
  627. $input = Until::getInput();
  628. $model = new OrderModel();
  629. $model::where([
  630. 'user_id' => $this->userId,
  631. 'id' => (int)$input['orderId']
  632. ])->update(['status' => OrderModel::IS_DELETE]);
  633. Until::output([]);
  634. }
  635. public function payOrderAgain() {
  636. $input = Until::getInput();
  637. $rule = [
  638. 'orderId|订单id' => 'require',
  639. ];
  640. Until::check($rule, $input);
  641. $data = (new OrderService())->payAgain((int)$input['orderId']);
  642. Until::output($data);
  643. }
  644. /**
  645. * @OA\Post(path="/api/Order/updateCart",
  646. * tags={"订单管理"},
  647. * summary="加入购物车",
  648. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  649. * @OA\RequestBody(
  650. * @OA\MediaType(
  651. * mediaType="multipart/form-data",
  652. * @OA\Schema(
  653. * @OA\Property(description="商品id", property="productId", type="integer", default="1"),
  654. * @OA\Property(description="商品数量", property="num", type="integer", default="1"),
  655. * @OA\Property(description="购物车id", property="cartId", type="integer", default="1"),
  656. * @OA\Property(description="类型1增加 2减少", property="type", type="integer", default="1"),
  657. * required={"productId","num","cartId","type"})
  658. * )
  659. * ),
  660. * @OA\Response(response="200", description="请求成功")
  661. * )
  662. */
  663. public function updateCart() {
  664. $input = Until::getInput();
  665. $rule = [
  666. 'productId|订单id' => 'require',
  667. 'num|数量' => 'require',
  668. 'cartId|购物车id' => 'require',
  669. ];
  670. Until::check($rule, $input);
  671. $cartModel = new CartModel();
  672. $cartInfo = $cartModel::where([
  673. 'id' => $input['cartId']
  674. ])->find();
  675. if (empty($cartInfo)) {
  676. throw new ApiException('不存在的购物车id');
  677. }
  678. $cartProductModel = new CartProductModel();
  679. $rs = $cartProductModel::where(['product_id' => $input['productId'], 'cart_id' => $input['cartId']])->find();
  680. if (empty($rs)) {
  681. $cartProductModel->insertGetId([
  682. 'product_id' => $input['productId'],
  683. 'cart_id' => $input['cartId'],
  684. 'num' => $input['num'] ?? 1
  685. ]);
  686. } else {
  687. if ($input['type'] == 1) {
  688. $cartProductModel::where(['id' => $rs['id']])->inc('num', 1)->update();
  689. }
  690. if ($input['type'] == 2) {
  691. if ($rs['num'] == 1) {
  692. $cartProductModel::where(['id' => $rs['id']])->delete();
  693. }
  694. $cartProductModel::where(['id' => $rs['id']])->dec('num', 1)->update();
  695. }
  696. }
  697. $cartInfo = Until::modelToArray($cartInfo);
  698. $cartModel->setFields('c.*,cp.product_id,cp.num,p.product_name,p.current_price,p.product_img');
  699. $rs = $cartModel->getCartList();
  700. $cartInfo['totalNum'] = 0;
  701. $cartInfo['totalPrice'] = 0;
  702. foreach ($rs as $v) {
  703. $cartInfo['totalNum'] += $v['num'];
  704. $cartInfo['totalPrice'] += $v['num'] * $v['current_price'];
  705. }
  706. $cartInfo['id'] = $v['id'];
  707. $cartInfo['table_id'] = $v['table_id'];
  708. $cartInfo['store_id'] = $v['store_id'];
  709. $cartInfo['user_id'] = $v['user_id'];
  710. $cartInfo['create_time'] = $v['create_time'];
  711. $cartInfo['list'] = $cartInfo['totalNum'] == 0 ? [] : $rs;
  712. Until::output($cartInfo);
  713. }
  714. /**
  715. * @OA\Post(path="/api/Order/getCart",
  716. * tags={"订单管理"},
  717. * summary="获取购物车",
  718. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  719. * @OA\RequestBody(
  720. * @OA\MediaType(
  721. * mediaType="multipart/form-data",
  722. * @OA\Schema(
  723. * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
  724. * @OA\Property(description="桌台id", property="tableId", type="integer", default="1"),
  725. * required={"storeId","tableId"})
  726. * )
  727. * ),
  728. * @OA\Response(response="200", description="请求成功")
  729. * )
  730. */
  731. public function getCart() {
  732. $input = Until::getInput();
  733. $rule = [
  734. 'storeId|门店id' => 'require',
  735. 'tableId|桌台id' => 'require'
  736. ];
  737. Until::check($rule, $input);
  738. $model = new CartModel();
  739. try {
  740. $model->startTrans();
  741. $whereData = [
  742. 'table_id' => (int)$input['tableId'],
  743. 'store_id' => (int)$input['storeId'],
  744. 'user_id' => $this->userId,
  745. 'status' => CartModel::NORMAL
  746. ];
  747. $cartInfo = $model::where($whereData)->find();
  748. $cartInfo = Until::modelToArray($cartInfo);
  749. if (empty($cartInfo)) {
  750. $id = $model->insertGetId($whereData);
  751. }
  752. $model->setWhere([
  753. 'c.status' => CartModel::NORMAL,
  754. 'c.user_id' => $this->userId,
  755. 'c.table_id' => (int)$input['tableId'],
  756. 'c.store_id' => (int)$input['storeId']
  757. ]);
  758. $model->setFields('c.*,cp.product_id,cp.num,p.product_name,p.current_price,p.product_img');
  759. $rs = $model->getCartList();
  760. $model->commit();
  761. } catch (\Exception $e) {
  762. $model->rollback();
  763. throw new ApiException($e->getMessage());
  764. }
  765. $cartInfo['totalPrice'] = 0;
  766. $cartInfo['totalNum'] = 0;
  767. foreach ($rs as $v) {
  768. $cartInfo['totalNum'] += $v['num'];
  769. $cartInfo['totalPrice'] += $v['num'] * $v['current_price'];
  770. }
  771. $cartInfo['id'] = $v['id'];
  772. $cartInfo['table_id'] = $v['table_id'];
  773. $cartInfo['store_id'] = $v['store_id'];
  774. $cartInfo['user_id'] = $v['user_id'];
  775. $cartInfo['create_time'] = $v['create_time'];
  776. $cartInfo['totalPrice'] = number_format($cartInfo['totalPrice'], 2);
  777. $cartInfo['list'] = $cartInfo['totalNum'] == 0 ? [] : $rs;
  778. Until::output($cartInfo);
  779. }
  780. /**
  781. * @OA\Post(path="/api/Order/clearCart",
  782. * tags={"订单管理"},
  783. * summary="清空购物车",
  784. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  785. * @OA\RequestBody(
  786. * @OA\MediaType(
  787. * mediaType="multipart/form-data",
  788. * @OA\Schema(
  789. * @OA\Property(description="购物车id", property="cartId", type="integer", default="1"),
  790. * required={"cartId"})
  791. * )
  792. * ),
  793. * @OA\Response(response="200", description="请求成功")
  794. * )
  795. */
  796. public function clearCart() {
  797. $input = Until::getInput();
  798. $rule = [
  799. 'cartId|购物车id' => 'require',
  800. ];
  801. Until::check($rule, $input);
  802. $model = new CartModel();
  803. $model::where(['id' => $input['cartId']])->update(['status' => 0]);
  804. Until::output();
  805. }
  806. /**
  807. * @OA\Post(path="/api/Order/editOrder",
  808. * tags={"订单管理"},
  809. * summary="修改订单信息",
  810. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  811. * @OA\RequestBody(
  812. * @OA\MediaType(
  813. * mediaType="multipart/form-data",
  814. * @OA\Schema(
  815. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  816. * @OA\Property(description="订单金额", property="orderMoney", type="string", default="13.26"),
  817. * @OA\Property(description="技师", property="staffId", type="string", default="13.26"),
  818. * @OA\Property(description="旧服务项目id", property="oldProductId", type="string", default="13.26"),
  819. * @OA\Property(description="新服务项目id", property="newProductId", type="string", default="13.26"),
  820. * required={"orderId"})
  821. * )
  822. * ),
  823. * @OA\Response(response="200", description="请求成功")
  824. * )
  825. */
  826. public function editOrder() {
  827. $input = Until::getInput();
  828. $rule = [
  829. 'orderId|订单id' => 'require',
  830. ];
  831. Until::check($rule, $input);
  832. if (empty($input['orderMoney']) || empty($input['staffId']) || empty($input['oldProductId']) || empty($input['newProductId'])) {
  833. throw new ApiException('修改信息不能为空');
  834. }
  835. $log = new OrderChangeModel();
  836. $orderModel = new OrderModel();
  837. if (!empty($input['orderMoney'])) {
  838. try {
  839. $log->startTrans();
  840. $rs = $orderModel::where(['id' => $input['orderId']])->find();
  841. $log->insertGetId([
  842. 'order_id' => $input['orderId'],
  843. 'change' => 'orderMoney',
  844. 'remark' => "订单金额由{$rs['order_money']}修改为{$input['orderMoney']}",
  845. 'admin_id' => Until::$adminId,
  846. 'order_snap' => json_encode($rs)
  847. ]);
  848. $orderModel::where(['id' => $input['orderId']])->update(['order_money' => $input['orderMoney']]);
  849. $log->commit();
  850. }catch (\Exception $e) {
  851. $log->rollback();
  852. throw new ApiException($e->getMessage());
  853. }
  854. }
  855. if (!empty($input['staffId'])) {
  856. try {
  857. $log->startTrans();
  858. $rs = $orderModel::where(['id' => $input['orderId']])->find();
  859. $log->insertGetId([
  860. 'order_id' => $input['orderId'],
  861. 'change' => 'orderMoney',
  862. 'remark' => "订单由职员:{$rs['staffId']}修改为职员:{$input['staffId']}",
  863. 'admin_id' => Until::$adminId,
  864. 'order_snap' => json_encode($rs)
  865. ]);
  866. $orderModel::where(['id' => $input['orderId']])->update(['staff_id' => $input['staffId']]);
  867. (new AllocateModel())::where(['order_id' => $input['orderId']])->update(['staff_id' => $input['staffId']]);
  868. $log->commit();
  869. }catch (\Exception $e) {
  870. $log->rollback();
  871. throw new ApiException($e->getMessage());
  872. }
  873. }
  874. $data = (new OrderService())->payFoodOrder($input);
  875. Until::output($data);
  876. }
  877. }