Room.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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\DiscussModel;
  10. use app\api\model\GroupModel;
  11. use app\api\model\OrderRoomModel;
  12. use app\api\model\RoomModel;
  13. use app\common\until\Until;
  14. use think\Model;
  15. class Room extends BaseController {
  16. /**
  17. * @OA\Get(path="/api/Room/index",
  18. * tags={"房间管理"},
  19. * summary="房间列表",
  20. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  21. * @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="ineger",default="1")),
  22. * @OA\Parameter(name="pageSize", in="query", description="页尺寸", @OA\Schema(type="integer",default="10")),
  23. * @OA\Parameter(name="storeId", in="query", description="门店id", @OA\Schema(type="integer",default="1")),
  24. * @OA\Parameter(name="date", in="query", description="服务开始时间", @OA\Schema(type="string",default="2021-04-09")),
  25. * @OA\Parameter(name="status", in="query", description="1正常(待使用) 2使用中 3空闲 999禁用", @OA\Schema(type="integer",default="1")),
  26. * @OA\Parameter(name="name", in="query", description="名字或code", @OA\Schema(type="string",default="666")),
  27. * @OA\RequestBody(
  28. * ),
  29. * @OA\Response(response="200", description="请求成功")
  30. * )
  31. */
  32. public function index() {
  33. $input = Until::getInput();
  34. $model = new RoomModel();
  35. $model->setPage($input['page'] ?? 1);
  36. $model->setPageSize($input['pageSize'] ?? 10);
  37. $where = [];
  38. if (!empty($input['storeId'])) {
  39. $where[] = ['r.store_id', '=', (int)$input['storeId']];
  40. }
  41. if (!empty($input['status'])) {
  42. if ($input['status'] != 999) {
  43. $where[] = ['r.status', '=', 1];
  44. $where[] = ['r.room_server_status', '=', (int)$input['status']];
  45. }else{
  46. $where[] = ['r.status', '=', 2];
  47. }
  48. }
  49. if (!empty($input['name'])) {
  50. $where[] = ['r.room_name|r.room_code', 'like', "%{$input['name']}%"];
  51. }
  52. if ($this->isAdmin()) {
  53. $where[] = ['sr.admin_id', '=', $this->adminId];
  54. }
  55. $model->setWhere($where);
  56. $data = $model->getRoomList();
  57. Until::output($data);
  58. }
  59. /**
  60. * @OA\Post(path="/api/room/save",
  61. * tags={"房间管理"},
  62. * summary="保存房间信息",
  63. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  64. * @OA\RequestBody(
  65. * @OA\MediaType(
  66. * mediaType="multipart/form-data",
  67. * @OA\Schema(
  68. * @OA\Property(description="房间名称", property="roomName", type="integer", default="1"),
  69. * @OA\Property(description="房间代号", property="roomCode", type="integer", default="1"),
  70. * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
  71. * @OA\Property(description="状态", property="status", type="integer", default="1"),
  72. * @OA\Property(description="房间id", property="id", type="integer", default="1"),
  73. * required={"roomName","roomCode","storeId","status"})
  74. * )
  75. * ),
  76. * @OA\Response(response="200", description="请求成功")
  77. * )
  78. */
  79. public function save() {
  80. $input = Until::getInput();
  81. $rule = [
  82. 'roomName|房间名称' => 'require',
  83. 'roomCode|房间代号' => 'require',
  84. 'storeId|门店id' => 'require',
  85. 'status|状态' => 'require',
  86. // 'id|房间id' => 'require',
  87. ];
  88. Until::check($rule, $input);
  89. $model = new RoomModel();
  90. if (!empty($input['id'])) {
  91. $id = (int)$input['id'];
  92. $rs = $model::where([['store_id', '=', $input['storeId']], ['room_name', '=', $input['roomName']], ['id', '<>', $id]])->find();
  93. if ($rs !== null) {
  94. throw new ApiException('名称不能重复');
  95. }
  96. $rs = $model::where([['store_id', '=', $input['storeId']], ['room_code', '=', $input['roomCode']], ['id', '<>', $id]])->find();
  97. if ($rs !== null) {
  98. throw new ApiException('code不能重复');
  99. }
  100. $model::where(['id' => $id])->update([
  101. 'room_name' => $input['roomName'],
  102. 'store_id' => $input['storeId'],
  103. 'room_code' => $input['roomCode'],
  104. 'status' => $input['status'] ?? 1,
  105. ]);
  106. } else {
  107. $rs = $model::where([['store_id', '=', $input['storeId']], ['room_name', '=', $input['roomName']]])->find();
  108. if ($rs !== null) {
  109. throw new ApiException('名称不能重复');
  110. }
  111. $rs = $model::where([['store_id', '=', $input['storeId']], ['room_code', '=', $input['roomCode']]])->find();
  112. if ($rs !== null) {
  113. throw new ApiException('code不能重复');
  114. }
  115. $id = $model->insertGetId([
  116. 'room_name' => $input['roomName'],
  117. 'store_id' => $input['storeId'],
  118. 'room_code' => $input['roomCode'],
  119. 'status' => $input['status'] ?? 1,
  120. ]);
  121. }
  122. $where[] = ['r.id', '=', (int)$id];
  123. $model->setWhere($where);
  124. $info = $model->getRoomInfo();
  125. Until::output(['info' => $info]);
  126. }
  127. /**
  128. * @OA\Post(path="/api/room/updateStatus",
  129. * tags={"房间管理"},
  130. * summary="修改房间状态",
  131. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  132. * @OA\RequestBody(
  133. * @OA\MediaType(
  134. * mediaType="multipart/form-data",
  135. * @OA\Schema(
  136. * @OA\Property(description="房间id", property="id", type="integer", default="1"),
  137. * @OA\Property(description="状态", property="status", type="integer", default="1"),
  138. * required={"id","status"})
  139. * )
  140. * ),
  141. * @OA\Response(response="200", description="请求成功")
  142. * )
  143. */
  144. public function updateStatus() {
  145. $input = Until::getInput();
  146. $rule = [
  147. 'id|房间id' => 'require',
  148. 'status|状态' => 'require',
  149. ];
  150. Until::check($rule, $input);
  151. $model = new RoomModel();
  152. $model::where(['id' => $input['id']])->update(['status' => $input['status']]);
  153. Until::output();
  154. }
  155. /**
  156. * @OA\Post(path="/api/room/allocateRoom",
  157. * tags={"房间管理"},
  158. * summary="分配房间",
  159. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  160. * @OA\RequestBody(
  161. * @OA\MediaType(
  162. * mediaType="multipart/form-data",
  163. * @OA\Schema(
  164. * @OA\Property(description="房间id", property="id", type="integer", default="1"),
  165. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  166. * required={"id","orderId"})
  167. * )
  168. * ),
  169. * @OA\Response(response="200", description="请求成功")
  170. * )
  171. */
  172. public function allocateRoom() {
  173. $input = Until::getInput();
  174. $rule = [
  175. 'id|房间id' => 'require',
  176. 'orderId|订单id' => 'require',
  177. ];
  178. Until::check($rule, $input);
  179. $data = [
  180. 'order_id' => $input['orderId'],
  181. ];
  182. $model = new OrderRoomModel();
  183. $rs = $model::where($data)->find();
  184. if (empty($rs)) {
  185. $data['room_id'] = $input['id'];
  186. $model->insert($data);
  187. } else {
  188. $model::where(['order_id' => $input['orderId']])->update(['room_id' => $input['id']]);
  189. }
  190. (new RoomModel())::where(['id' => $input['id']])->update(['room_server_status' => 1]);
  191. Until::output();
  192. }
  193. /**
  194. * @OA\Post(path="/api/room/serverRoom",
  195. * tags={"房间管理"},
  196. * summary="房间报表",
  197. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  198. * @OA\RequestBody(
  199. * @OA\MediaType(
  200. * mediaType="multipart/form-data",
  201. * @OA\Schema(
  202. * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
  203. * @OA\Property(description="日期(默认今天)", property="date", type="string", default="2021-04-29"),
  204. * @OA\Property(description="状态 1未开始 2服务中 3已完成(多个逗号分隔)", property="status", type="integer", default="1"),
  205. * required={"storeId","status"})
  206. * )
  207. * ),
  208. * @OA\Response(response="200", description="请求成功")
  209. * )
  210. */
  211. public function serverRoom() {
  212. $input = Until::getInput();
  213. $rule = [
  214. 'storeId|门店id' => 'require',
  215. //'date|日期' => 'require',
  216. 'status|状态' => 'require',
  217. ];
  218. Until::check($rule, $input);
  219. $where = [];
  220. if (!empty($input['status'])) {
  221. $statusArr = explode(',', $input['status']);
  222. $where[] = ['as.status', 'in', $statusArr];
  223. }
  224. if (empty($input['date'])) {
  225. $input['date'] = date('Y-m-d');
  226. }
  227. $where[] = ['as.server_start_time', 'between', [$input['date'], date('Y-m-d', strtotime('+1 day', strtotime($input['date'])))]];
  228. $model = new OrderRoomModel();
  229. $model->setWhere($where);
  230. $rs = $model->getRoomList();
  231. if (empty($rs)) {
  232. Until::output([]);
  233. }
  234. $list = [];
  235. foreach ($rs as $v) {
  236. if ($v['asStatus'] == 2){
  237. $allocateOrderStatus = 3; //服务中
  238. }elseif ($v['asStatus'] == 3) {
  239. $allocateOrderStatus = 4; // 服务结束
  240. }else {
  241. $allocateOrderStatus = 2; //未开始
  242. }
  243. $productInfo = json_decode($v['product_snap'], true);
  244. if (empty($v['server_end_time'])) {
  245. $endTime = strtotime($v['server_start_time']) + $productInfo['time'];
  246. $time = $productInfo['time'];
  247. } else {
  248. $endTime = strtotime($v['server_end_time']);
  249. $time = (int)(($endTime - strtotime($v['server_start_time'])) / 60);
  250. }
  251. $list[$v['room_id']][] = [
  252. 'productName' => $productInfo['product_name'],
  253. 'start' => (int)((strtotime($v['server_start_time']) - strtotime($input['date'] . ' 08:00:00')) / 60),
  254. 'time' => $time,
  255. 'allocateOrderStatus' => $allocateOrderStatus,
  256. 'startTime' => date('H:i', strtotime($v['server_start_time'])),
  257. 'endTime' => date('H:i', $endTime),
  258. 'staffName' => $v['staff_name'],
  259. 'orderId' => $v['order_id'],
  260. 'name' => $v['name'],
  261. 'realName' => $v['real_name'],
  262. 'orderType' => $v['order_type'],
  263. ];
  264. }
  265. Until::output($list);
  266. }
  267. /**
  268. * @OA\Post(path="/api/room/freeRoom",
  269. * tags={"房间管理"},
  270. * summary="还未分配房间",
  271. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  272. * @OA\RequestBody(
  273. * @OA\MediaType(
  274. * mediaType="multipart/form-data",
  275. * @OA\Schema(
  276. * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
  277. * @OA\Property(description="日期(默认今天)", property="date", type="string", default="2021-04-29"),
  278. * required={"storeId"})
  279. * )
  280. * ),
  281. * @OA\Response(response="200", description="请求成功")
  282. * )
  283. */
  284. public function freeRoom() {
  285. $input = Until::getInput();
  286. $rule = [
  287. 'storeId|门店id' => 'require',
  288. //'date|日期' => 'require',
  289. ];
  290. Until::check($rule, $input);
  291. $where = [];
  292. if (empty($input['date'])) {
  293. $input['date'] = date('Y-m-d');
  294. }
  295. $where[] = ['o.status', '=', 2];
  296. $where[] = ['or.id', '=', null];
  297. $where[] = ['o.appointment_time', 'between', [$input['date'], date('Y-m-d', strtotime('+1 day', strtotime($input['date'])))]];
  298. $model = new OrderRoomModel();
  299. $model->setWhere($where);
  300. $rs = $model->getfreeRoomList();
  301. if (empty($rs)) {
  302. Until::output([]);
  303. }
  304. $list = $rs;
  305. $count = count($rs);
  306. Until::output(['count' => $count, 'list' => $list]);
  307. }
  308. }