Order.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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\BrandModel;
  10. use app\api\model\GroupModel;
  11. use app\api\model\OrderModel;
  12. use app\api\model\ProductModel;
  13. use app\api\model\WriteOffModel;
  14. use app\common\service\OrderService;
  15. use app\common\until\Until;
  16. use think\App;
  17. use think\Db;
  18. class Order extends BaseController {
  19. public function __construct(App $app) {
  20. parent::__construct($app);
  21. if ($this->userId == '123456789') {
  22. throw new ApiException(
  23. '网络繁忙'
  24. );
  25. }
  26. }
  27. /**
  28. * @OA\Get(path="/api/Order/index",
  29. * tags={"订单管理"},
  30. * summary="订单列表",
  31. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  32. * @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="ineger",default="1")),
  33. * @OA\Parameter(name="pageSize", in="query", description="页尺寸", @OA\Schema(type="integer",default="10")),
  34. * @OA\Parameter(name="orderStatus", in="query", description="订单状态 1未支付 2已支付 ", @OA\Schema(type="integer")),
  35. * @OA\Parameter(name="writeOffStatus", in="query", description="核销状态 1未核销 2已核销 ", @OA\Schema(type="integer")),
  36. * @OA\Parameter(name="discussStatus", in="query", description="评价状态 1未评价 2已评价 ", @OA\Schema(type="integer")),
  37. * @OA\Parameter(name="orderSn", in="query", description="订单号", @OA\Schema(type="string")),
  38. * @OA\Parameter(name="mobile", in="query", description="手机号", @OA\Schema(type="string")),
  39. * @OA\Parameter(name="storeId", in="query", description="门店id", @OA\Schema(type="integer")),
  40. * @OA\Parameter(name="appointmentTime", in="query", description="预约时间", @OA\Schema(type="2020-01-02,2021-12-30")),
  41. * @OA\Parameter(name="createTime", in="query", description="订单时间", @OA\Schema(type="2020-01-02,2021-12-30")),
  42. * @OA\Parameter(name="orderType", in="query", description="订单类型 1小程序下单 2后台增加", @OA\Schema(type="string")),
  43. * @OA\RequestBody(
  44. * ),
  45. * @OA\Response(response="200", description="请求成功")
  46. * )
  47. */
  48. public function index() {
  49. $input = request()->get();
  50. $model = new OrderModel();
  51. $model->setPage($input['page'] ?? 1);
  52. $model->setPageSize($input['pageSize'] ?? 10);
  53. $where = [];
  54. Db::table('order')->where([['status', '=', 1], ['create_time', '<',
  55. date('Y-m-d H:i:s',strtotime('-15minutes'))]])
  56. ->update(['status' => OrderModel::IS_CLOSE]);
  57. if (!empty($input['orderSn'])) {
  58. $where[] = ['o.order_sn', 'like', "%{$input['orderSn']}%"];
  59. }
  60. if (!empty($input['mobile'])) {
  61. $where[] = ['o.mobile', 'like', "%{$input['mobile']}%"];
  62. }
  63. if (!empty($input['writeOffStatus'])) {
  64. $where[] = ['wo.write_off_status', '=', (int)$input['writeOffStatus']];
  65. }
  66. if (!empty($input['orderStatus'])) {
  67. $where[] = ['o.status', '=', (int)$input['orderStatus']];
  68. }
  69. if (!empty($input['discussStatus'])) {
  70. $where[] = ['discussOrder.id','=',null];
  71. }
  72. if (!empty($input['storeId'])) {
  73. $where[] = ['store.id', '=', $input['storeId']];
  74. }
  75. if (!empty($input['appointmentTime'])) {
  76. $data = explode(',', $input['appointmentTime']);
  77. $where[] = ['o.appointment_time', 'between', [$data[0], $data[1] . ' 23:59:59']];
  78. }
  79. if (!empty($input['createTime'])) {
  80. $data = explode(',', $input['createTime']);
  81. $where[] = ['o.create_time', 'between', [$data[0], $data[1] . ' 23:59:59']];
  82. }
  83. if (!empty($input['orderType'])) {
  84. $where[] = ['store.order_type', '=', $input['orderType']];
  85. }
  86. if (!$this->isAdmin()) {
  87. $where[] = ['o.user_id', '=', $this->userId];
  88. if (empty($input['orderStatus'])){
  89. $where[] = ['o.status', '<>', OrderModel::IS_DELETE];
  90. }
  91. }
  92. $model->setWhere($where);
  93. $data = $model->getOrderList();
  94. $statusFilter = [1=>'未支付',2=>'已支付',3=>'已关闭'];
  95. foreach ($data['list'] as &$one){
  96. $one['statusText'] = $statusFilter[$one['status']];
  97. if($one['status'] === OrderModel::IS_PAY) {
  98. if ($one['write_off_status'] == 1) {
  99. $one['statusText'] = '待消费';
  100. }else if ($one['write_off_status'] == 2) {
  101. if (empty($one['discuss_id']) && !empty($input['discussStatus'])){
  102. $one['statusText'] = '未评价';
  103. }
  104. $one['statusText'] = '已消费';
  105. }
  106. }
  107. }
  108. Until::output($data);
  109. }
  110. public function save() {
  111. $input = Until::getInput();
  112. $rule = [
  113. 'name|品牌名称' => 'require',
  114. 'groupId|集团id' => 'require',
  115. ];
  116. Until::check($rule, $input);
  117. $model = new BrandModel();
  118. if (!empty($input['id'])) {
  119. $id = (int)$input['id'];
  120. $model::where(['id' => $id])->update([
  121. 'brand_name' => $input['name'],
  122. 'group_id' => $input['groupId']
  123. ]);
  124. } else {
  125. $id = $model->insertGetId([
  126. 'brand_name' => $input['name'],
  127. 'group_id' => $input['groupId']
  128. ]);
  129. }
  130. $info = $model::get($id);
  131. Until::output(['info' => Until::modelToArray($info)]);
  132. }
  133. /**
  134. * @OA\GET(path="/api/Order/read",
  135. * tags={"订单管理"},
  136. * summary="查看订单信息",
  137. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  138. * @OA\Parameter(name="id", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
  139. * @OA\RequestBody(
  140. * ),
  141. * @OA\Response(response="200", description="请求成功")
  142. * )
  143. */
  144. public function read($id) {
  145. $model = new OrderModel();
  146. $where[] = ['o.id', '=', (int)$id];
  147. $model->setWhere($where);
  148. $info = $model->getOrderInfo();
  149. Until::output(['info' => $info]);
  150. }
  151. /**
  152. * @OA\GET(path="/api/Order/delete",
  153. * tags={"订单管理"},
  154. * summary="删除品牌信息",
  155. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  156. * @OA\Parameter(name="id", in="query", description="品牌id", @OA\Schema(type="ineger",default="1")),
  157. * @OA\Parameter(name="status", in="query", description="1正常 2删除", @OA\Schema(type="ineger",default="1")),
  158. * @OA\RequestBody(
  159. * ),
  160. * @OA\Response(response="200", description="请求成功")
  161. * )
  162. */
  163. public function delete($id, $status) {
  164. $model = new BrandModel();
  165. $where[] = ['id', '=', (int)$id];
  166. $data = ['status' => (int)$status];
  167. $isSuccess = $model::where($where)->update($data);
  168. Until::output(['isSuccess' => $isSuccess]);
  169. }
  170. /**
  171. * @OA\Post(path="/api/Order/createOrder",
  172. * tags={"订单管理"},
  173. * summary="创建订单信息",
  174. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  175. * @OA\RequestBody(
  176. * @OA\MediaType(
  177. * mediaType="multipart/form-data",
  178. * @OA\Schema(
  179. * @OA\Property(description="商品id", property="productId", type="integer", default="1"),
  180. * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
  181. * @OA\Property(description="预约时间", property="appointmentTime", type="string", default="2020-12-12 16:30"),
  182. * @OA\Property(description="预约结束时间", property="endTime", type="string", default="2020-12-12 16:30"),
  183. * @OA\Property(description="商品数量", property="num", type="integer", default="1"),
  184. * @OA\Property(description="手机号", property="mobile", type="string", default="15623655623"),
  185. * required={"productId","storeId","appointmentTime","num","mobile"})
  186. * )
  187. * ),
  188. * @OA\Response(response="200", description="请求成功")
  189. * )
  190. */
  191. public function createOrder() {
  192. $input = Until::getInput();
  193. $rule = [
  194. 'productId|商品id' => 'require',
  195. 'storeId|门店id' => 'require',
  196. 'appointmentTime|预约时间' => 'require',
  197. 'num|数量' => 'require',
  198. 'mobile|手机号' => 'require'
  199. ];
  200. Until::check($rule, $input);
  201. if (empty($input['endTime'])) {
  202. $input['endTime'] = date('Y-m-d H:i',strtotime($input['appointmentTime']) + 1800);
  203. }
  204. if (strtotime($input['appointmentTime']) >= strtotime($input['endTime'])) {
  205. throw new ApiException('结束时间必须大于开始时间');
  206. }
  207. $input['productId'] = (int)$input['productId'];
  208. if ($input['num'] < 1) {
  209. throw new ApiException('数量必须大于1');
  210. }
  211. $data = (new OrderService())->payOrder($input);
  212. Until::output($data);
  213. }
  214. /**
  215. * @OA\Post(path="/api/Order/assignStaff",
  216. * tags={"订单管理"},
  217. * summary="分配职员",
  218. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  219. * @OA\RequestBody(
  220. * @OA\MediaType(
  221. * mediaType="multipart/form-data",
  222. * @OA\Schema(
  223. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  224. * @OA\Property(description="职员id", property="staffId", type="integer", default="1"),
  225. * required={"orderId","staffId"})
  226. * )
  227. * ),
  228. * @OA\Response(response="200", description="请求成功")
  229. * )
  230. */
  231. public function assignStaff() {
  232. $input = Until::getInput();
  233. $rule = [
  234. 'orderId|订单id' => 'require',
  235. 'staffId|职员id' => 'require',
  236. ];
  237. Until::check($rule, $input);
  238. $model = new OrderModel();
  239. $model::where(['id' => (int)$input['orderId']])->update(['staff_id' => (int)$input['staffId']]);
  240. Until::output([]);
  241. }
  242. /**
  243. * @OA\Post(path="/api/Order/payOrder",
  244. * tags={"订单管理"},
  245. * summary="手动支付",
  246. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  247. * @OA\RequestBody(
  248. * @OA\MediaType(
  249. * mediaType="multipart/form-data",
  250. * @OA\Schema(
  251. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  252. * required={"orderId"})
  253. * )
  254. * ),
  255. * @OA\Response(response="200", description="请求成功")
  256. * )
  257. */
  258. public function payOrder() {
  259. $input = Until::getInput();
  260. $rule = [
  261. 'orderId|订单id' => 'require',
  262. ];
  263. Until::check($rule, $input);
  264. $model = new OrderModel();
  265. $orderInfo = $model::where(['id' => (int)$input['orderId']])->find();
  266. if ($orderInfo === null) {
  267. throw new ApiException('无此订单');
  268. }
  269. if ($orderInfo['status'] === OrderModel::IS_PAY) {
  270. throw new ApiException('该订单已支付');
  271. }
  272. $model::where(['id' => (int)$input['orderId']])->update([
  273. 'status' => OrderModel::IS_PAY,
  274. 'pay_time' => date('Y-m-d H:i:s')
  275. ]);
  276. $code = random_int(10000, 99999);
  277. $wModel = new WriteOffModel();
  278. $wModel::where(['order_id' => $input['orderId']])->update([
  279. 'write_off_code' => $code,
  280. ]);
  281. Until::output([]);
  282. }
  283. public function notifyOrder() {
  284. $input = Until::getInput();
  285. $rule = [
  286. 'OrderNumber|订单号' => 'require',
  287. ];
  288. Until::check($rule, $input);
  289. if (md5('ef17f532-4661-b07c-5346-65dfa304c0d8'.$input['OrderNumber']) === $this->request->header('serchkey')){
  290. (new OrderService())->notify($input['OrderNumber']);
  291. Until::output([]);
  292. }
  293. (new OrderService())->notify($input['OrderNumber']);
  294. Until::output(['decode' => md5('ef17f532-4661-b07c-5346-65dfa304c0d8'.$input['OrderNumber']),
  295. 'encode' => $this->request->header('serchkey')]);
  296. }
  297. public function changeCode() {
  298. $input = Until::getInput();
  299. $rule = [
  300. 'orderId|订单号' => 'require',
  301. ];
  302. Until::check($rule, $input);
  303. $ser = new OrderService();
  304. $code = $ser->changeCode();
  305. $wModel = new WriteOffModel();
  306. $wModel::where(['order_id' => $input['orderId']])->update([
  307. 'write_off_code' => $code,
  308. 'over_time' => date('Y-m-d H:i:s', time() + 5 * 60),
  309. ]);
  310. Until::output(['code' => $code]);
  311. }
  312. /**
  313. * @OA\Get(path="/api/Order/writeOffOrder",
  314. * tags={"订单管理"},
  315. * summary="核销订单",
  316. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  317. * @OA\RequestBody(
  318. * @OA\MediaType(
  319. * mediaType="multipart/form-data",
  320. * @OA\Schema(
  321. * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
  322. * @OA\Property(description="核销code", property="code", type="string", default="1"),
  323. * required={"orderId","code"})
  324. * )
  325. * ),
  326. * @OA\Response(response="200", description="请求成功")
  327. * )
  328. */
  329. public function writeOffOrder() {
  330. $input = Until::getInput();
  331. $rule = [
  332. 'orderId|订单id' => 'require',
  333. 'code|核销码' => 'require'
  334. ];
  335. Until::check($rule, $input);
  336. $rs = (new OrderModel())::where(['id' => $input['orderId']])->find();
  337. if ($rs['status'] != OrderModel::IS_PAY) {
  338. throw new ApiException('该订单未付款,不可核销');
  339. }
  340. $model = new WriteOffModel();
  341. $where = ['order_id' => $input['orderId'], 'write_off_code' => $input['code']];
  342. $writeOff = $model::where($where)->find();
  343. if ($writeOff === null) {
  344. throw new ApiException('无此核销单');
  345. }
  346. if($writeOff['write_off_status'] == 2){
  347. throw new ApiException('该订单已经核销了');
  348. }
  349. $model::where($where)
  350. ->update([
  351. 'write_off_status' => 2,
  352. 'write_off_time' => date('Y-m-d H:i:s'),
  353. 'admin_id' => $this->adminId
  354. ]);
  355. Until::output();
  356. }
  357. /**
  358. * @OA\Post(path="/api/Order/closeOrder",
  359. * tags={"订单管理"},
  360. * summary="订单关闭",
  361. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  362. * @OA\Parameter(name="orderId", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
  363. * @OA\RequestBody(
  364. * ),
  365. * @OA\Response(response="200", description="请求成功")
  366. * )
  367. */
  368. public function closeOrder() {
  369. $input = request()->get();
  370. $model = new OrderModel();
  371. $model::where([
  372. 'user_id' => $this->userId,
  373. 'id' => (int)$input['orderId']
  374. ])->update(['status' => OrderModel::IS_CLOSE]);
  375. Until::output([]);
  376. }
  377. /**
  378. * @OA\Post(path="/api/Order/deleteOrder",
  379. * tags={"订单管理"},
  380. * summary="订单删除",
  381. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  382. * @OA\Parameter(name="orderId", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
  383. * @OA\RequestBody(
  384. * ),
  385. * @OA\Response(response="200", description="请求成功")
  386. * )
  387. */
  388. public function deleteOrder() {
  389. $input = request()->get();
  390. $model = new OrderModel();
  391. $model::where([
  392. 'user_id' => $this->userId,
  393. 'id' => (int)$input['orderId']
  394. ])->update(['status' => OrderModel::IS_DELETE]);
  395. Until::output([]);
  396. }
  397. public function payOrderAgain() {
  398. $input = Until::getInput();
  399. $rule = [
  400. 'orderId|订单id' => 'require',
  401. ];
  402. Until::check($rule, $input);
  403. $data = (new OrderService())->payAgain((int)$input['orderId']);
  404. Until::output($data);
  405. }
  406. }