Order.php 15 KB

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