123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- <?php
- /**
- * Author: luzheng.liu
- * Time: 2020/12/16 23:06
- */
- namespace app\api\controller;
- use app\api\BaseController;
- use app\api\exception\ApiException;
- use app\api\model\BrandModel;
- use app\api\model\CartProductModel;
- use app\api\model\CartModel;
- use app\api\model\GroupModel;
- use app\api\model\OrderModel;
- use app\api\model\ProductModel;
- use app\api\model\UserModel;
- use app\api\model\WriteOffModel;
- use app\common\service\OrderService;
- use app\common\until\Until;
- use think\App;
- use think\Db;
- class Order extends BaseController {
- public function __construct(App $app) {
- parent::__construct($app);
- if ($this->userId == '123456789') {
- throw new ApiException(
- '网络繁忙'
- );
- }
- }
- /**
- * @OA\Get(path="/api/Order/index",
- * tags={"订单管理"},
- * summary="订单列表",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="ineger",default="1")),
- * @OA\Parameter(name="pageSize", in="query", description="页尺寸", @OA\Schema(type="integer",default="10")),
- * @OA\Parameter(name="orderStatus", in="query", description="订单状态 1未支付 2已支付 ", @OA\Schema(type="integer")),
- * @OA\Parameter(name="writeOffStatus", in="query", description="核销状态 1未核销 2已核销 ", @OA\Schema(type="integer")),
- * @OA\Parameter(name="discussStatus", in="query", description="评价状态 1未评价 2已评价 ", @OA\Schema(type="integer")),
- * @OA\Parameter(name="orderSn", in="query", description="订单号", @OA\Schema(type="string")),
- * @OA\Parameter(name="mobile", in="query", description="手机号", @OA\Schema(type="string")),
- * @OA\Parameter(name="storeId", in="query", description="门店id", @OA\Schema(type="integer")),
- * @OA\Parameter(name="appointmentTime", in="query", description="预约时间", @OA\Schema(type="2020-01-02,2021-12-30")),
- * @OA\Parameter(name="createTime", in="query", description="订单时间", @OA\Schema(type="2020-01-02,2021-12-30")),
- * @OA\Parameter(name="orderType", in="query", description="订单类型 1小程序下单 2后台增加", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function index() {
- $input = request()->get();
- $model = new OrderModel();
- $model->setPage($input['page'] ?? 1);
- $model->setPageSize($input['pageSize'] ?? 10);
- $where = [];
- $model::where([['status', '=', 1], ['create_time', '<',
- date('Y-m-d H:i:s', strtotime('-15minutes'))]])
- ->update(['status' => OrderModel::IS_CLOSE]);
- if (!empty($input['orderSn'])) {
- $where[] = ['o.order_sn', 'like', "%{$input['orderSn']}%"];
- }
- if (!empty($input['mobile'])) {
- $where[] = ['o.mobile', 'like', "%{$input['mobile']}%"];
- }
- if (!empty($input['writeOffStatus'])) {
- $where[] = ['wo.write_off_status', '=', (int)$input['writeOffStatus']];
- }
- if (!empty($input['orderStatus'])) {
- $where[] = ['o.status', '=', (int)$input['orderStatus']];
- }
- if (!empty($input['discussStatus'])) {
- $where[] = ['discussOrder.id', '=', null];
- }
- if (!empty($input['storeId'])) {
- $where[] = ['store.id', '=', $input['storeId']];
- }
- if (!empty($input['appointmentTime'])) {
- $data = explode(',', $input['appointmentTime']);
- $where[] = ['o.appointment_time', 'between', [$data[0], $data[1] . ' 23:59:59']];
- }
- if (!empty($input['createTime'])) {
- $data = explode(',', $input['createTime']);
- $where[] = ['o.create_time', 'between', [$data[0], $data[1] . ' 23:59:59']];
- }
- if (!empty($input['orderType'])) {
- $where[] = ['store.order_type', '=', $input['orderType']];
- }
- if (!$this->isAdmin()) {
- $where[] = ['o.user_id', '=', $this->userId];
- if (empty($input['orderStatus'])) {
- $where[] = ['o.status', '<>', OrderModel::IS_DELETE];
- }
- }
- $model->setWhere($where);
- $data = $model->getOrderList();
- $statusFilter = [1 => '未支付', 2 => '已支付', 3 => '已关闭'];
- foreach ($data['list'] as &$one) {
- $one['statusText'] = $statusFilter[$one['status']];
- if ($one['status'] === OrderModel::IS_PAY) {
- if ($one['write_off_status'] == 1) {
- $one['statusText'] = '待消费';
- } else if ($one['write_off_status'] == 2) {
- if (empty($one['discuss_id']) && !empty($input['discussStatus'])) {
- $one['statusText'] = '未评价';
- }
- $one['statusText'] = '已消费';
- }
- }
- }
- Until::output($data);
- }
- public function save() {
- $input = Until::getInput();
- $rule = [
- 'name|品牌名称' => 'require',
- 'groupId|集团id' => 'require',
- ];
- Until::check($rule, $input);
- $model = new BrandModel();
- if (!empty($input['id'])) {
- $id = (int)$input['id'];
- $model::where(['id' => $id])->update([
- 'brand_name' => $input['name'],
- 'group_id' => $input['groupId']
- ]);
- } else {
- $id = $model->insertGetId([
- 'brand_name' => $input['name'],
- 'group_id' => $input['groupId']
- ]);
- }
- $info = $model::get($id);
- Until::output(['info' => Until::modelToArray($info)]);
- }
- /**
- * @OA\GET(path="/api/Order/read",
- * tags={"订单管理"},
- * summary="查看订单信息",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\Parameter(name="id", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
- * @OA\RequestBody(
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function read($id) {
- $model = new OrderModel();
- $where[] = ['o.id', '=', (int)$id];
- $model->setWhere($where);
- $info = $model->getOrderInfo();
- Until::output(['info' => $info]);
- }
- /**
- * @OA\GET(path="/api/Order/delete",
- * tags={"订单管理"},
- * summary="删除品牌信息",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\Parameter(name="id", in="query", description="品牌id", @OA\Schema(type="ineger",default="1")),
- * @OA\Parameter(name="status", in="query", description="1正常 2删除", @OA\Schema(type="ineger",default="1")),
- * @OA\RequestBody(
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function delete($id, $status) {
- $model = new BrandModel();
- $where[] = ['id', '=', (int)$id];
- $data = ['status' => (int)$status];
- $isSuccess = $model::where($where)->update($data);
- Until::output(['isSuccess' => $isSuccess]);
- }
- /**
- * @OA\Post(path="/api/Order/createOrder",
- * tags={"订单管理"},
- * summary="创建订单信息",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * @OA\MediaType(
- * mediaType="multipart/form-data",
- * @OA\Schema(
- * @OA\Property(description="商品id", property="productId", type="integer", default="1"),
- * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
- * @OA\Property(description="预约时间", property="appointmentTime", type="string", default="2020-12-12 16:30"),
- * @OA\Property(description="预约结束时间", property="endTime", type="string", default="2020-12-12 16:30"),
- * @OA\Property(description="商品数量", property="num", type="integer", default="1"),
- * @OA\Property(description="手机号", property="mobile", type="string", default="15623655623"),
- * required={"productId","storeId","appointmentTime","num","mobile"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function createOrder() {
- $input = Until::getInput();
- $rule = [
- 'productId|商品id' => 'require',
- 'storeId|门店id' => 'require',
- 'appointmentTime|预约时间' => 'require',
- 'num|数量' => 'require',
- 'mobile|手机号' => 'require'
- ];
- Until::check($rule, $input);
- if (empty($input['endTime'])) {
- $input['endTime'] = date('Y-m-d H:i', strtotime($input['appointmentTime']) + 1800);
- }
- if (strtotime($input['appointmentTime']) >= strtotime($input['endTime'])) {
- throw new ApiException('结束时间必须大于开始时间');
- }
- $input['productId'] = (int)$input['productId'];
- if ($input['num'] < 1) {
- throw new ApiException('数量必须大于1');
- }
- $data = (new OrderService())->payOrder($input);
- Until::output($data);
- }
- /**
- * @OA\Post(path="/api/Order/createFoodOrder",
- * tags={"订单管理"},
- * summary="创建订单信息",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * @OA\MediaType(
- * mediaType="multipart/form-data",
- * @OA\Schema(
- * @OA\Property(description="商品id和数量['productId' => 1,'num' => 1]", property="productIdAndNum", type="integer", default="1"),
- * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
- * @OA\Property(description="手机号", property="mobile", type="string", default="15623655623"),
- * required={"productIdAndNum","storeId","mobile"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function createFoodOrder() {
- $input = Until::getInput();
- $rule = [
- 'productIdAndNum|商品id和数量' => 'require',
- 'storeId|门店id' => 'require',
- 'mobile|手机号' => 'require'
- ];
- Until::check($rule, $input);
- $data = (new OrderService())->payOrder($input);
- Until::output($data);
- }
- /**
- * @OA\Post(path="/api/Order/assignStaff",
- * tags={"订单管理"},
- * summary="分配职员",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * @OA\MediaType(
- * mediaType="multipart/form-data",
- * @OA\Schema(
- * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
- * @OA\Property(description="职员id", property="staffId", type="integer", default="1"),
- * required={"orderId","staffId"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function assignStaff() {
- $input = Until::getInput();
- $rule = [
- 'orderId|订单id' => 'require',
- 'staffId|职员id' => 'require',
- ];
- Until::check($rule, $input);
- $model = new OrderModel();
- $model::where(['id' => (int)$input['orderId']])->update(['staff_id' => (int)$input['staffId']]);
- Until::output([]);
- }
- /**
- * @OA\Post(path="/api/Order/payOrder",
- * tags={"订单管理"},
- * summary="手动支付",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * @OA\MediaType(
- * mediaType="multipart/form-data",
- * @OA\Schema(
- * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
- * required={"orderId"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function payOrder() {
- $input = Until::getInput();
- $rule = [
- 'orderId|订单id' => 'require',
- ];
- Until::check($rule, $input);
- $model = new OrderModel();
- $orderInfo = $model::where(['id' => (int)$input['orderId']])->find();
- if ($orderInfo === null) {
- throw new ApiException('无此订单');
- }
- if ($orderInfo['status'] === OrderModel::IS_PAY) {
- throw new ApiException('该订单已支付');
- }
- $model::where(['id' => (int)$input['orderId']])->update([
- 'status' => OrderModel::IS_PAY,
- 'pay_time' => date('Y-m-d H:i:s')
- ]);
- $code = random_int(10000, 99999);
- $wModel = new WriteOffModel();
- $wModel::where(['order_id' => $input['orderId']])->update([
- 'write_off_code' => $code,
- ]);
- Until::output([]);
- }
- public function notifyOrder() {
- $input = Until::getInput();
- $rule = [
- 'OrderNumber|订单号' => 'require',
- ];
- Until::check($rule, $input);
- if (md5('ef17f532-4661-b07c-5346-65dfa304c0d8' . $input['OrderNumber']) === $this->request->header('serchkey')) {
- (new OrderService())->notify($input['OrderNumber']);
- Until::output([]);
- }
- // (new OrderService())->notify($input['OrderNumber']);
- Until::output(['decode' => md5('ef17f532-4661-b07c-5346-65dfa304c0d8' . $input['OrderNumber']),
- 'encode' => $this->request->header('serchkey')]);
- }
- public function changeCode() {
- $input = Until::getInput();
- $rule = [
- 'orderId|订单号' => 'require',
- ];
- Until::check($rule, $input);
- $ser = new OrderService();
- $code = $ser->changeCode();
- $wModel = new WriteOffModel();
- $wModel::where(['order_id' => $input['orderId'], 'write_off_status' => 1])->update([
- 'write_off_code' => $code,
- 'over_time' => date('Y-m-d H:i:s', time() + 2 * 60),
- ]);
- Until::output(['code' => $code]);
- }
- /**
- * @OA\Post(path="/api/Order/writeOffOrder",
- * tags={"订单管理"},
- * summary="核销订单",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * @OA\MediaType(
- * mediaType="multipart/form-data",
- * @OA\Schema(
- * @OA\Property(description="订单id", property="orderId", type="integer", default="1"),
- * @OA\Property(description="核销code", property="code", type="string", default="1"),
- * required={"orderId","code"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function writeOffOrder() {
- $input = Until::getInput();
- $rule = [
- 'orderId|订单id' => 'require',
- 'code|核销码' => 'require'
- ];
- Until::check($rule, $input);
- $rs = (new OrderModel())::where(['id' => $input['orderId']])->find();
- if ($rs['status'] != OrderModel::IS_PAY) {
- throw new ApiException('该订单未付款,不可核销');
- }
- $model = new WriteOffModel();
- $where = ['order_id' => $input['orderId'], 'write_off_code' => $input['code']];
- $writeOff = $model::where($where)->find();
- if ($writeOff === null) {
- throw new ApiException('核销码错误');
- }
- if ($writeOff['write_off_status'] == 2) {
- throw new ApiException('该订单已经核销了');
- }
- $userModel = new UserModel();
- $userModel::where([['id', '=', $rs['user_id']], ['first_store_id', '=', 0]])->update(['first_store_id' => $rs['store_id']]);
- $model::where($where)
- ->update([
- 'write_off_status' => 2,
- 'write_off_time' => date('Y-m-d H:i:s'),
- 'admin_id' => $this->adminId
- ]);
- Until::output();
- }
- /**
- * @OA\Post(path="/api/Order/closeOrder",
- * tags={"订单管理"},
- * summary="订单关闭",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\Parameter(name="orderId", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
- * @OA\RequestBody(
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function closeOrder() {
- $input = request()->get();
- $model = new OrderModel();
- $model::where([
- 'user_id' => $this->userId,
- 'id' => (int)$input['orderId']
- ])->update(['status' => OrderModel::IS_CLOSE]);
- Until::output([]);
- }
- /**
- * @OA\Post(path="/api/Order/deleteOrder",
- * tags={"订单管理"},
- * summary="订单删除",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\Parameter(name="orderId", in="query", description="订单id", @OA\Schema(type="ineger",default="1")),
- * @OA\RequestBody(
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function deleteOrder() {
- $input = request()->get();
- $model = new OrderModel();
- $model::where([
- 'user_id' => $this->userId,
- 'id' => (int)$input['orderId']
- ])->update(['status' => OrderModel::IS_DELETE]);
- Until::output([]);
- }
- public function payOrderAgain() {
- $input = Until::getInput();
- $rule = [
- 'orderId|订单id' => 'require',
- ];
- Until::check($rule, $input);
- $data = (new OrderService())->payAgain((int)$input['orderId']);
- Until::output($data);
- }
- /**
- * @OA\Post(path="/api/Order/updateCart",
- * tags={"订单管理"},
- * summary="加入购物车",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * @OA\MediaType(
- * mediaType="multipart/form-data",
- * @OA\Schema(
- * @OA\Property(description="商品id", property="productId", type="integer", default="1"),
- * @OA\Property(description="商品数量", property="num", type="integer", default="1"),
- * @OA\Property(description="购物车id", property="cartId", type="integer", default="1"),
- * @OA\Property(description="类型1增加 2减少", property="type", type="integer", default="1"),
- * required={"productId","num","cartId","type"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function updateCart() {
- $input = Until::getInput();
- $rule = [
- 'productId|订单id' => 'require',
- 'num|数量' => 'require',
- 'cartId|购物车id' => 'require',
- ];
- Until::check($rule, $input);
- $cartModel = new CartModel();
- $cartInfo = $cartModel::where([
- 'id' => $input['cartId']
- ])->find();
- if (empty($cartInfo)) {
- throw new ApiException('不存在的购物车id');
- }
- $cartProductModel = new CartProductModel();
- $rs = $cartProductModel::where(['product_id' => $input['productId'], 'cart_id' => $input['cartId']])->find();
- if (empty($rs)) {
- $cartProductModel->insertGetId([
- 'product_id' => $input['productId'],
- 'cart_id' => $input['cartId'],
- 'num' => $input['num'] ?? 1
- ]);
- }else {
- if ($input['type'] == 1) {
- $cartProductModel::where(['id' => $rs['id']])->inc('num', 1)->update();
- }
- if ($input['type'] == 2) {
- $cartProductModel::where(['id' => $rs['id']])->dec('num', 1)->update();
- }
- }
- $cartInfo = Until::modelToArray($cartInfo);
- $rs = $cartModel->getCartList();
- $cartInfo['list'] = $rs;
- Until::output($cartInfo);
- Until::output();
- }
- /**
- * @OA\Post(path="/api/Order/getCart",
- * tags={"订单管理"},
- * summary="获取购物车",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * @OA\MediaType(
- * mediaType="multipart/form-data",
- * @OA\Schema(
- * @OA\Property(description="门店id", property="storeId", type="integer", default="1"),
- * @OA\Property(description="桌台id", property="tableId", type="integer", default="1"),
- * required={"storeId","tableId"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function getCart() {
- $input = Until::getInput();
- $rule = [
- 'storeId|门店id' => 'require',
- 'tableId|桌台id' => 'require'
- ];
- Until::check($rule, $input);
- $model = new CartModel();
- $cartInfo = $model::where([
- 'table_id' => $input['tableId'],
- 'store_id' => (int)$input['storeId'],
- 'user_id' => $this->userId,
- 'status' => CartModel::NORMAL
- ])->find();
- if (empty($cartInfo)) {
- $model->insertGetId([
- 'table_id' => $input['tableId'],
- 'store_id' => (int)$input['storeId'],
- 'user_id' => $this->userId,
- 'status' => CartModel::NORMAL
- ]);
- }
- $model->setWhere([
- 'c.status' => CartModel::NORMAL,
- 'c.user_id' => $this->userId,
- 'c.table_id' => $input['tableId'],
- 'c.store_id' => $input['storeId']
- ]);
- $cartInfo = Until::modelToArray($cartInfo);
- $rs = $model->getCartList();
- $cartInfo['list'] = $rs;
- Until::output($cartInfo);
- }
- }
|