UserCollageServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. declare (strict_types=1);
  12. namespace app\services\activity\collage;
  13. use app\jobs\notice\PrintJob;
  14. use app\services\activity\table\TableQrcodeServices;
  15. use app\services\BaseServices;
  16. use app\dao\activity\collage\UserCollageDao;
  17. use app\services\message\NoticeService;
  18. use app\services\system\config\ConfigServices;
  19. use crmeb\services\printer\Printer;
  20. use think\exception\ValidateException;
  21. use app\services\other\CategoryServices;
  22. use crmeb\services\SystemConfigService;
  23. use crmeb\utils\Arr;
  24. use think\facade\Log;
  25. use app\services\user\UserServices;
  26. use app\services\user\UserBillServices;
  27. /**
  28. *
  29. * Class UserCollageServices
  30. * @package app\services\activity\collage
  31. * @mixin UserCollageDao
  32. */
  33. class UserCollageServices extends BaseServices
  34. {
  35. /**
  36. * UserCollageServices constructor.
  37. * @param UserCollageDao $dao
  38. */
  39. public function __construct(UserCollageDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * 验证拼单是否开启
  45. * @return bool
  46. */
  47. public function checkCollageStatus()
  48. {
  49. //门店是否开启
  50. if (!sys_config('store_func_status', 1)) {
  51. return false;
  52. }
  53. //桌码是否开启
  54. if (!sys_config('store_splicing_switch', 1)) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. /**
  60. * 验证桌码功能是否开启
  61. * @return bool
  62. */
  63. public function checkTabldeCodeStatus(int $store_id)
  64. {
  65. //门店是否开启
  66. if (!sys_config('store_func_status', 1)) {
  67. return false;
  68. }
  69. //桌码是否开启
  70. if (!store_config($store_id, 'store_code_switch', 1)) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. /**
  76. * 拼单、桌码状态
  77. * @param $id
  78. * @return array|\think\Model|null
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function collageStatus($id)
  84. {
  85. return $this->dao->get($id, ['status']);
  86. }
  87. /**
  88. * 记录发起拼单
  89. * @param int $uid
  90. * @param int $store_id
  91. * @param int $address_id
  92. * @param int $shipping_type
  93. * @return \crmeb\basic\BaseModel|\think\Model
  94. */
  95. public function setUserCollage(int $uid, int $store_id, int $address_id, int $shipping_type)
  96. {
  97. if ($this->dao->be(['uid' => $uid, 'type' => 9, 'store_id' => $store_id, 'address_id' => $address_id, 'shipping_type' => $shipping_type, 'status' => [0, 1]])) throw new ValidateException('您已在拼单中,不能再次拼单!');
  98. $data = [
  99. 'uid' => $uid,
  100. 'type' => 9,
  101. 'store_id' => $store_id,
  102. 'address_id' => $address_id,
  103. 'shipping_type' => $shipping_type,
  104. 'add_time' => time()
  105. ];
  106. $res = $this->dao->save($data);
  107. return $res;
  108. }
  109. /**
  110. * 获取拼单/桌码信息
  111. * @param array $where
  112. * @param string|null $field
  113. * @param array $with
  114. * @return array|\think\Model|null
  115. * @throws \think\db\exception\DataNotFoundException
  116. * @throws \think\db\exception\DbException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. */
  119. public function getUserCollage(array $where, ?string $field = '*', array $with = [])
  120. {
  121. return $this->dao->getOne($where, $field, $with);
  122. }
  123. /**
  124. * 修改拼单
  125. * @param int $id
  126. * @param array $where
  127. * @return mixed
  128. */
  129. public function userUpdate(int $id, array $where)
  130. {
  131. return $this->dao->update($id, $where);
  132. }
  133. /**
  134. * 记录桌码
  135. * @param int $uid
  136. * @param int $store_id
  137. * @param int $qrcode_id
  138. * @param int $number
  139. * @return bool|\crmeb\basic\BaseModel|\think\Model
  140. */
  141. public function setUserTableCode(int $uid, int $store_id, int $qrcode_id, int $number)
  142. {
  143. //1=>合并结账 2=>单独结账
  144. $store_checkout_method = store_config($store_id, 'store_checkout_method', 1);
  145. if ($store_checkout_method == 1) {
  146. $where = ['store_id' => $store_id, 'qrcode_id' => $qrcode_id, 'checkout_method' => $store_checkout_method, 'type' => 10, 'status' => [0, 1]];
  147. } else {
  148. $where = ['uid' => $uid, 'store_id' => $store_id, 'qrcode_id' => $qrcode_id, 'checkout_method' => $store_checkout_method, 'type' => 10, 'status' => [0, 1]];
  149. }
  150. $table = $this->dao->getOne($where);
  151. if ($table) return $table;
  152. $max = $this->dao->getMaxSerialNumber(['store_id' => $store_id, 'type' => 10]);
  153. $data = [
  154. 'uid' => $uid,
  155. 'type' => 10,
  156. 'store_id' => $store_id,
  157. 'checkout_method' => $store_checkout_method,
  158. 'qrcode_id' => $qrcode_id,
  159. 'number_diners' => $number,
  160. 'serial_number' => $max ? '00' . ($max + 1) : '001',
  161. 'add_time' => time()
  162. ];
  163. return $this->dao->save($data);
  164. }
  165. /**
  166. * 检查是否换桌 code 0 :换桌 1 :未换桌 2:无记录
  167. * @param int $uid
  168. * @param int $store_id
  169. * @param int $qrcode_id
  170. * @param int $store_checkout_method
  171. * @return array|int[]
  172. * @throws \think\db\exception\DataNotFoundException
  173. * @throws \think\db\exception\DbException
  174. * @throws \think\db\exception\ModelNotFoundException
  175. */
  176. public function isUserChangingTables(int $uid, int $store_id, int $qrcode_id, int $store_checkout_method)
  177. {
  178. $where = ['store_id' => $store_id, 'uid' => $uid, 'checkout_method' => $store_checkout_method, 'type' => 10, 'status' => [0, 1]];
  179. $whereTo = ['store_id' => $store_id, 'qrcode_id' => $qrcode_id, 'checkout_method' => $store_checkout_method, 'type' => 10, 'status' => [0, 1]];
  180. $table = $this->dao->getOne($where);
  181. $table1 = $this->dao->getOne($whereTo);
  182. if ($table) {
  183. if ($table['qrcode_id'] != $qrcode_id) {
  184. return ['code' => 0, 'tableId' => $table['id']];
  185. } else {
  186. return ['code' => 1, 'tableId' => $table['id']];
  187. }
  188. } else {
  189. if ($store_checkout_method == 1) {
  190. /** @var UserCollagePartakeServices $partakeService */
  191. $partakeService = app()->make(UserCollagePartakeServices::class);
  192. $partake_where = ['uid' => $uid, 'store_id' => $store_id, 'status' => 1];
  193. $partake = $partakeService->getMaxTime($partake_where);
  194. if (!$partake) {
  195. return ['code' => 2, 'tableId' => $table1 ? $table1['id'] : 0];
  196. }
  197. $table2 = $this->dao->get($partake['collate_code_id']);
  198. if (!$table2) {
  199. return ['code' => 2, 'tableId' => $table1 ? $table1['id'] : 0];
  200. }
  201. if (in_array($table2['status'], [0, 1])) {
  202. if ($table2['qrcode_id'] != $qrcode_id) {
  203. return ['code' => 0, 'tableId' => $table2['id']];
  204. } else {
  205. return ['code' => 1, 'tableId' => $table2['id']];
  206. }
  207. } else {
  208. return ['code' => 2, 'tableId' => $table1 ? $table1['id'] : 0];
  209. }
  210. } else {
  211. return ['code' => 1, 'tableId' => 0];
  212. }
  213. }
  214. }
  215. /**
  216. * 更换座位 处理原有商品
  217. * @param int $tableId
  218. * @param int $y_tableId
  219. * @return bool
  220. */
  221. public function userChangingTables(int $tableId, int $y_tableId)
  222. {
  223. /** @var UserCollagePartakeServices $partakeService */
  224. $partakeService = app()->make(UserCollagePartakeServices::class);
  225. $res = $partakeService->update(['collate_code_id' => $y_tableId], ['collate_code_id' => $tableId]);
  226. $table = $this->dao->get($y_tableId);
  227. /** @var TableQrcodeServices $qrcodeService */
  228. $qrcodeService = app()->make(TableQrcodeServices::class);
  229. $res1 = $qrcodeService->update($table['qrcode_id'], ['is_use' => 0, 'eat_number' => 0, 'order_time' => 0]);
  230. $res2 = $this->dao->update($tableId, ['number_diners' => $table['number_diners']]);
  231. $res3 = $this->dao->delete($y_tableId);
  232. return $res && $res1 && $res2 && $res3;
  233. }
  234. /**
  235. * 桌码订单
  236. * @param array $where
  237. * @return array
  238. * @throws \think\db\exception\DataNotFoundException
  239. * @throws \think\db\exception\DbException
  240. * @throws \think\db\exception\ModelNotFoundException
  241. */
  242. public function getStoreTableCodeList(array $where, array $field = ['*'])
  243. {
  244. [$page, $limit] = $this->getPageValue();
  245. $data = $this->dao->searchTableCodeList($where, $field, $page, $limit, ['qrcode', 'orderId']);
  246. $count = $this->dao->count($where);
  247. /** @var UserCollagePartakeServices $partakeService */
  248. $partakeService = app()->make(UserCollagePartakeServices::class);
  249. foreach ($data as $key => &$datum) {
  250. $datum['qrcode']['category'] = [];
  251. if (isset($datum['qrcode']['cate_id']) && $datum['qrcode']['cate_id']) {
  252. /** @var CategoryServices $categoryService */
  253. $categoryService = app()->make(CategoryServices::class);
  254. $datum['qrcode']['category'] = $categoryService->get((int)$datum['qrcode']['cate_id'], ['name']);
  255. } else {
  256. $datum['qrcode']['category'] = ['name' => '已删除'];
  257. $datum['qrcode']['table_number'] = '';
  258. }
  259. $dataPartake = $partakeService->getCashierTablePartakeProduct(['collate_code_id' => $datum['id'], 'status' => 1], '', ['productInfo']);
  260. $datum['cartList'] = $dataPartake['cart'] ?? [];
  261. $datum['sum_price'] = $dataPartake['sum_price'] ?? 0;
  262. $datum['cart_num'] = $dataPartake['cart_num'] ?? 0;
  263. }
  264. return compact('data', 'count');
  265. }
  266. /**
  267. * 确认下单
  268. * @param int $tableId
  269. * @param int $store_id
  270. * @return bool
  271. */
  272. public function userTablePlaceOrder(int $tableId, int $store_id)
  273. {
  274. $this->dao->update($tableId, ['status' => 1]);
  275. $print = store_config($store_id, 'store_printing_timing');
  276. if ($print && is_array($print) && in_array(1, $print)) {
  277. PrintJob::dispatch('tableDoJob', [$tableId, $store_id]);
  278. }
  279. return true;
  280. }
  281. /**
  282. * 桌码、拼单长期未操作取消桌码、拼单记录
  283. * @return bool|void
  284. * @throws \think\db\exception\DataNotFoundException
  285. * @throws \think\db\exception\DbException
  286. * @throws \think\db\exception\ModelNotFoundException
  287. */
  288. public function tableCodeNotOperating(int $type = 10)
  289. {
  290. $where = ['type' => $type, 'status' => [0, 1]];
  291. $list = $this->dao->searchTableCodeList($where);
  292. //系统预设取消订单时间段
  293. $keyValue = ['table_code_not_operating_time', 'collate_not_operating_time'];
  294. //获取配置
  295. $systemValue = SystemConfigService::more($keyValue);
  296. //格式化数据
  297. $systemValue = Arr::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  298. $table_code_not_operating_time = $systemValue['table_code_not_operating_time'];
  299. $collate_not_operating_time = $systemValue['collate_not_operating_time'];
  300. $not_operating_time = $type == 10 ? $table_code_not_operating_time : $collate_not_operating_time;
  301. /** @var UserCollagePartakeServices $partakeService */
  302. $partakeService = app()->make(UserCollagePartakeServices::class);
  303. $not_operating_time = (int)bcmul((string)$not_operating_time, '3600', 0);
  304. foreach ($list as $key => $item) {
  305. if ((strtotime($item['add_time']) + $not_operating_time) < time()) {
  306. try {
  307. $this->transaction(function () use ($item, $partakeService) {
  308. //修改记录状态
  309. $res = $this->dao->update($item['id'], ['status' => -1]);
  310. $res = $res && $partakeService->update(['collate_code_id' => $item['id']], ['status' => 0]);
  311. });
  312. } catch (\Throwable $e) {
  313. $msg = $type == 10 ? '桌码长期未操作取消桌码记录失败' : '拼单长期未操作取消拼单记录失败';
  314. Log::error($msg . ',失败原因:' . $e->getMessage(), $e->getTrace());
  315. }
  316. }
  317. }
  318. return true;
  319. }
  320. /**
  321. * 桌码打印订单
  322. * @param int $tableId
  323. * @param int $store_id
  324. * @return bool
  325. * @throws \think\db\exception\DataNotFoundException
  326. * @throws \think\db\exception\DbException
  327. * @throws \think\db\exception\ModelNotFoundException
  328. */
  329. public function tablePrint(int $tableId, int $store_id)
  330. {
  331. $table = $this->get($tableId);
  332. if($table['status']== -1){
  333. throw new ValidateException('桌码已取消');
  334. }
  335. /** @var UserCollagePartakeServices $partakeService */
  336. $partakeService = app()->make(UserCollagePartakeServices::class);
  337. $product = $partakeService->getCartInfoPrintProduct($table->toArray());
  338. if (!$product) {
  339. return true;
  340. }
  341. $partakeService->update(['collate_code_id' => $tableId], ['is_print' => 1]);
  342. /** @var ConfigServices $configServices */
  343. $configServices = app()->make(ConfigServices::class);
  344. [$switch, $name, $configData] = $configServices->getPrintingConfig(1, $store_id);
  345. if (!$switch) {
  346. throw new ValidateException('请先开启小票打印');
  347. }
  348. foreach ($configData as $value) {
  349. if (!$value) {
  350. throw new ValidateException('请先配置小票打印开发者');
  351. }
  352. }
  353. $printer = new Printer($name, $configData);
  354. $printer->setPrinterTableContent([
  355. 'name' => sys_config('site_name'),
  356. 'tableInfo' => is_object($table) ? $table->toArray() : $table,
  357. 'product' => $product
  358. ])->startPrinter();
  359. return true;
  360. }
  361. }