UserCodeController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\v2\activity;
  12. use app\Request;
  13. use app\services\activity\collage\UserCollagePartakeServices;
  14. use app\services\activity\collage\UserCollageServices;
  15. use app\services\activity\table\TableQrcodeServices;
  16. use app\services\store\SystemStoreServices;
  17. use app\services\system\config\SystemConfigServices;
  18. /**
  19. *
  20. * Class UserCodeController
  21. * @package app\controller\api\v2\activity
  22. */
  23. class UserCodeController
  24. {
  25. protected $services;
  26. public function __construct(UserCollageServices $services)
  27. {
  28. $this->services = $services;
  29. }
  30. /**
  31. * 门店桌码配置
  32. * @param $store_id
  33. * @return \think\Response
  34. */
  35. public function getData($store_id)
  36. {
  37. if (!$store_id) return app('json')->fail('参数有误!');
  38. $configName = ['store_code_switch', 'store_checkout_method', 'store_number_diners_window'];
  39. /** @var SystemConfigServices $configServices */
  40. $configServices = app()->make(SystemConfigServices::class);
  41. $data = $configServices->getConfigAll($configName, 1, (int)$store_id);
  42. return app('json')->successful($data);
  43. }
  44. /**
  45. * 记录桌码
  46. * @param Request $request
  47. * @return \think\Response
  48. */
  49. public function setTableCode(Request $request)
  50. {
  51. [$store_id, $qrcode_id, $number] = $request->getMore([
  52. ['store_id', 0],
  53. ['qrcode_id', 0],
  54. ['number', 1],
  55. ], true);
  56. $uid = (int)$request->uid();
  57. if (!$store_id || !$qrcode_id) return app('json')->fail('参数有误!');
  58. if (!$this->services->checkTabldeCodeStatus((int)$store_id)) return app('json')->fail('门店或桌码未开启!');
  59. try {
  60. if ($number <= 0) $number = 1;
  61. /** @var TableQrcodeServices $qrcodeService */
  62. $qrcodeService = app()->make(TableQrcodeServices::class);
  63. $is_using = $qrcodeService->value(['id' => $qrcode_id], 'is_using');
  64. if (!$is_using) return app('json')->fail('桌码未启用');
  65. $res = $this->services->setUserTableCode($uid, $store_id, $qrcode_id, $number);
  66. $qrcodeService->update($qrcode_id, ['is_use' => 1, 'eat_number' => $number, 'order_time' => time()]);
  67. return app('json')->successful('ok', ['tableId' => $res->id]);
  68. } catch (\Throwable $e) {
  69. $msg = $e->getMessage();
  70. \think\facade\Log::error('桌码失败,原因:' . $msg . $e->getFile() . $e->getLine());
  71. return app('json')->fail('桌码失败');
  72. }
  73. }
  74. /**
  75. * 检查是否开启桌码记录 是否换桌
  76. * @param Request $request
  77. * @return \think\Response
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. */
  82. public function isUserTableCode(Request $request)
  83. {
  84. [$store_id, $qrcode_id] = $request->getMore([
  85. ['store_id', 0],
  86. ['qrcode_id', 0],
  87. ], true);
  88. $uid = (int)$request->uid();
  89. if (!$store_id || !$qrcode_id) return app('json')->fail('参数有误!');
  90. //1=>合并结账 2=>单独结账
  91. $store_checkout_method = store_config((int)$store_id, 'store_checkout_method', 1);
  92. $data = $this->services->isUserChangingTables($uid, $store_id, $qrcode_id, $store_checkout_method);
  93. return app('json')->successful($data);
  94. }
  95. /**处理换桌商品
  96. * @param Request $request
  97. * @return \think\Response
  98. */
  99. public function userChangingTables(Request $request)
  100. {
  101. [$tableId, $y_tableId] = $request->getMore([
  102. ['tableId', 0],
  103. ['y_tableId', 0],
  104. ], true);
  105. if (!$tableId || !$y_tableId) return app('json')->fail('参数有误!');
  106. $res = $this->services->userChangingTables($tableId, $y_tableId);
  107. if ($res) {
  108. return app('json')->successful('ok');
  109. } else {
  110. return app('json')->fail('换桌失败');
  111. }
  112. }
  113. /**
  114. * 获取桌码记录
  115. * @param Request $request
  116. * @return \think\Response
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function getTableCode(Request $request)
  122. {
  123. [$tableId] = $request->getMore([
  124. ['tableId', 0]
  125. ], true);
  126. if (!$tableId) return app('json')->fail('参数有误!');
  127. $where = ['id' => $tableId, 'type' => 10];
  128. $table = $this->services->getUserCollage($where);
  129. if (!$table) return app('json')->fail('桌码记录不存在');
  130. return app('json')->successful(['table' => $table]);
  131. }
  132. /**
  133. * 获取门店信息
  134. * @param Request $request
  135. * @return \think\Response
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. */
  140. public function getStoredata(Request $request)
  141. {
  142. [$store_id] = $request->getMore([
  143. ['store_id', 0],
  144. ], true);
  145. if (!$store_id) return app('json')->fail('参数有误!');
  146. /** @var SystemStoreServices $storeService */
  147. $storeService = app()->make(SystemStoreServices::class);
  148. $storeInfo = $storeService->getStoreInfo((int)$store_id);
  149. return app('json')->successful($storeInfo);
  150. }
  151. /**获取二维码信息
  152. * @param Request $request
  153. * @return \think\Response
  154. * @throws \think\db\exception\DataNotFoundException
  155. * @throws \think\db\exception\DbException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. */
  158. public function getTableCodeData(Request $request)
  159. {
  160. [$tableId] = $request->getMore([
  161. ['tableId', 0]
  162. ], true);
  163. if (!$tableId) return app('json')->fail('参数有误!');
  164. $where = ['id' => $tableId, 'type' => 10];
  165. $table = $this->services->getUserCollage($where, 'qrcode_id,serial_number,number_diners,status');
  166. if (!$table) return app('json')->fail('桌码记录不存在');
  167. if ($table['status'] == -1) return app('json')->fail('桌码已取消');
  168. $qrcode_id = $table['qrcode_id'];
  169. /** @var TableQrcodeServices $qrcodeService */
  170. $qrcodeService = app()->make(TableQrcodeServices::class);
  171. $Info = $qrcodeService->getQrcodeyInfo((int)$qrcode_id, ['category', 'storeName']);
  172. $Info['serial_number'] = $table['serial_number'];
  173. $Info['number_diners'] = $table['number_diners'];
  174. return app('json')->successful($Info);
  175. }
  176. /**
  177. * 购物车 统计 数量
  178. * @param Request $request
  179. * @return mixed
  180. */
  181. public function count(Request $request)
  182. {
  183. [$numType, $tableId, $store_id] = $request->getMore([
  184. ['numType', false],//购物车编号
  185. ['tableId', 0],
  186. ['store_id', 0]
  187. ], true);
  188. $uid = (int)$request->uid();
  189. if (!$tableId || !$store_id) return app('json')->fail('参数有误!');
  190. //1=>合并结账 2=>单独结账
  191. $store_checkout_method = store_config((int)$store_id, 'store_checkout_method', 1);
  192. $whereUid = ['uid' => $uid];
  193. $where = ['collate_code_id' => $tableId, 'store_id' => $store_id, 'status'=> 1];
  194. if ($store_checkout_method == 2) $where = $where + $whereUid;
  195. /** @var UserCollagePartakeServices $partakeService */
  196. $partakeService = app()->make(UserCollagePartakeServices::class);
  197. return app('json')->success('ok', $partakeService->getUserPartakeCount($where, (string)$numType, (int)$tableId, (int)$store_id));
  198. }
  199. /**
  200. * 获取购物车
  201. * @param Request $request
  202. * @return mixed
  203. */
  204. public function getCartList(Request $request)
  205. {
  206. $uid = (int)$request->uid();
  207. [$tableId, $store_id] = $request->getMore([
  208. ['tableId', 0],
  209. ['store_id', 0]
  210. ], true);
  211. if (!$tableId || !$store_id) return app('json')->fail('参数有误!');
  212. //1=>合并结账 2=>单独结账
  213. $store_checkout_method = store_config((int)$store_id, 'store_checkout_method', 1);
  214. $whereUid = ['uid' => $uid];
  215. $where = ['collate_code_id' => $tableId, 'store_id' => $store_id, 'status'=> 1];
  216. if ($store_checkout_method == 2) $where = $where + $whereUid;
  217. /** @var UserCollagePartakeServices $partakeService */
  218. $partakeService = app()->make(UserCollagePartakeServices::class);
  219. $valid = $partakeService->getTableCatePartakeList($uid, $where, (int)$store_id);
  220. return app('json')->successful($valid);
  221. }
  222. /**
  223. * 用户添加桌码商品
  224. * @param Request $request
  225. * @return \think\Response
  226. * @throws \think\db\exception\DataNotFoundException
  227. * @throws \think\db\exception\DbException
  228. * @throws \think\db\exception\ModelNotFoundException
  229. */
  230. public function addTableCodePartake(Request $request)
  231. {
  232. $where = $request->postMore([
  233. ['productId', 0],//普通商品编号
  234. [['cartNum', 'd'], 1], //购物车数量
  235. ['uniqueId', ''],//属性唯一值
  236. ['tableId', 0],//桌码ID
  237. ['storeId', 0],//门店ID
  238. ['isAdd', 1],//购物车数量加减 1 加 0 减
  239. ]);
  240. if (!$where['productId'] || !$where['storeId'] || !$where['tableId']) {
  241. return app('json')->fail('参数错误');
  242. }
  243. $uid = (int)$request->uid();
  244. $wheredata = ['id' => $where['tableId'], 'type' => 10];
  245. $table = $this->services->getUserCollage($wheredata);
  246. if ($table['store_id'] != $where['storeId']) return app('json')->fail('选择门店有误!');
  247. if ($table['status'] >= 2) return app('json')->fail('结算完成,不能在添加商品!');
  248. if ($table['status'] == -1) return app('json')->fail('桌码已取消');
  249. /** @var UserCollagePartakeServices $partakeService */
  250. $partakeService = app()->make(UserCollagePartakeServices::class);
  251. $res = $partakeService->addUserPartakeProduct($uid, (int)$where['productId'], $where['cartNum'], $where['uniqueId'], $where['tableId'], $where['storeId'], 10, $where['isAdd']);
  252. if ($res) {
  253. return app('json')->successful('ok');
  254. } else {
  255. return app('json')->fail('添加失败');
  256. }
  257. }
  258. /**
  259. * 用户清空购物车
  260. * @param Request $request
  261. * @return \think\Response
  262. */
  263. public function emptyTablePartake(Request $request)
  264. {
  265. [$tableId] = $request->getMore([
  266. ['tableId', 0]
  267. ], true);
  268. if (!$tableId) return app('json')->fail('参数有误!');
  269. /** @var UserCollagePartakeServices $partakeService */
  270. $partakeService = app()->make(UserCollagePartakeServices::class);
  271. $res = $partakeService->emptyUserTablePartake((int)$tableId);
  272. if ($res) {
  273. return app('json')->successful('ok');
  274. } else {
  275. return app('json')->fail('清空失败');
  276. }
  277. }
  278. /**确认下单
  279. * @param Request $request
  280. * @return \think\Response
  281. */
  282. public function userPlaceOrder(Request $request)
  283. {
  284. [$tableId, $storeId] = $request->getMore([
  285. ['tableId', 0],
  286. ['storeId', 0]
  287. ], true);
  288. if (!$tableId || !$storeId) return app('json')->fail('参数有误!');
  289. $table = $this->services->collageStatus($tableId);
  290. if ($table['status'] == -1) return app('json')->fail('桌码已取消');
  291. if ($table['status'] >= 2) return app('json')->fail('桌码已结算!');
  292. $this->services->userTablePlaceOrder((int)$tableId, (int)$storeId);
  293. return app('json')->successful('ok');
  294. }
  295. /**
  296. * 获取桌码数据
  297. * @param Request $request
  298. * @return \think\Response
  299. * @throws \think\db\exception\DataNotFoundException
  300. * @throws \think\db\exception\DbException
  301. * @throws \think\db\exception\ModelNotFoundException
  302. */
  303. public function getUserTableCodePartake(Request $request)
  304. {
  305. [$tableId] = $request->getMore([
  306. ['tableId', 0]
  307. ], true);
  308. if (!$tableId) return app('json')->fail('参数有误!');
  309. /** @var UserCollagePartakeServices $partakeService */
  310. $partakeService = app()->make(UserCollagePartakeServices::class);
  311. $cartList = $partakeService->getUserTablePartakeProduct($tableId);
  312. return app('json')->successful($cartList);
  313. }
  314. /**
  315. * 结算桌码商品
  316. * @param Request $request
  317. * @return \think\Response
  318. * @throws \think\db\exception\DataNotFoundException
  319. * @throws \think\db\exception\DbException
  320. * @throws \think\db\exception\ModelNotFoundException
  321. */
  322. public function userSettleAccountsCollage(Request $request)
  323. {
  324. [$tableId] = $request->getMore([
  325. ['tableId', 0],
  326. ], true);
  327. if (!$tableId) return app('json')->fail('参数有误!');
  328. $table = $this->services->collageStatus($tableId);
  329. if ($table['status'] == -1) return app('json')->fail('桌码已取消');
  330. if ($table['status'] >= 2) return app('json')->fail('桌码已完成结算');
  331. $uid = (int)$request->uid();
  332. /** @var UserCollagePartakeServices $partakeService */
  333. $partakeService = app()->make(UserCollagePartakeServices::class);
  334. $cartIds = $partakeService->allUserSettleAccountsTableCode((int)$tableId, $uid, 10);
  335. if ($cartIds) {
  336. return app('json')->successful('ok', ['cartIds' => $cartIds]);
  337. } else {
  338. return app('json')->fail('结算失败');
  339. }
  340. }
  341. /**用户删除桌码商品
  342. * @param Request $request
  343. * @return \think\Response
  344. */
  345. public function delUserTableCodePartake(Request $request)
  346. {
  347. $where = $request->postMore([
  348. ['tableId', 0],
  349. ['storeId', 0],
  350. ['productId', 0],//普通商品编号
  351. ['uniqueId', ''],//属性唯一值
  352. ]);
  353. if (!$where['tableId'] || !$where['storeId'] || !$where['productId'] || !$where['uniqueId']) return app('json')->fail('参数有误!');
  354. /** @var UserCollagePartakeServices $partakeService */
  355. $partakeService = app()->make(UserCollagePartakeServices::class);
  356. $res = $partakeService->delUserCatePartake((int)$where['tableId'], (int)$where['storeId'], (int)$where['productId'], $where['uniqueId']);
  357. if ($res) {
  358. return app('json')->successful('删除成功');
  359. } else {
  360. return app('json')->fail('删除失败');
  361. }
  362. }
  363. }