Order.php 15 KB

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