123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810 |
- <?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\AllocateModel;
- 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\OrderProductModel;
- 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;
- use think\Model;
- 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="createTcreateTimeime", in="query", description="订单时间", @OA\Schema(type="2020-01-02,2021-12-30")),
- * @OA\Parameter(name="orderType", in="query", description="订单类型 1小程序下单 2后台增加 3点餐", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function index() {
- $input = Until::getInput();
- $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'])) {
- if (empty($input['foodOrder'])) {
- $where[] = ['wo.write_off_status', '=', (int)$input['writeOffStatus']];
- } else if ($input['foodOrder'] == 2) {
- $model->setOp('write_off_status', 'left');
- }
- }
- 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'])) {
- $type = explode(',', $input['orderType']);
- $where[] = ['o.order_type', 'in', $type];
- }
- 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();
- $orderIds = array_column($data['list'], 'id');
- $productList = (new OrderProductModel())::where([['order_id', 'in', $orderIds]])->select();
- $productList = Until::modelToArray($productList);
- $product = [];
- foreach ($productList as &$v) {
- unset($v['product_snap']);
- $product[$v['order_id']][] = $v;
- }
- $statusFilter = [1 => '未支付', 2 => '已支付', 3 => '已关闭', 4 => '已删除'];
- foreach ($data['list'] as &$one) {
- if ($one['allocateStatus'] == 1) {
- $one['allocateOrderStatus'] = 2; //分配职员完成 可开始服务
- } elseif ($one['allocateStatus'] == 2) {
- $one['allocateOrderStatus'] = 3; //服务中 可结束服务
- } elseif ($one['allocateStatus'] == 3) {
- $one['allocateOrderStatus'] = 3; //服务结束
- } else {
- $one['allocateOrderStatus'] = 1; //还没分配职员 可分配职员 不可开始服务
- }
- $one['productList'] = $product[$one['id']] ?? [];
- $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();
- $productList = (new OrderProductModel())::where([['order_id', '=', $info['id']]])->select();
- $productList = Until::modelToArray($productList);
- foreach ($productList as &$v) {
- unset($v['product_snap']);
- }
- $info['productList'] = $productList;
- 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="职员id", property="staffId", 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"),
- * @OA\Property(description="姓名", property="name", type="string", default="夏铭"),
- * @OA\Property(description="支付方式", property="payType", type="string", default="1微信 2支付宝 3银行卡 4现金 "),
- * 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',
- 'staffId|技师' => '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');
- }
- if ($this->isAdmin() && !empty($input['name'])) {
- $model = new UserModel();
- $rs = $model::where(['mobile' => $input['mobile']])->find();
- if (empty($rs)) {
- $id = $model->insertGetId([
- 'name' => '',
- 'real_name' => $input['name'],
- 'avatar' => '',
- 'mobile' => $input['mobile'],
- 'join_type' => 2,
- ]);
- $input['userId'] = $id;
- } else {
- $model::where(['id' => $rs['id']])->update(['real_name' => $input['name']]);
- $input['userId'] = $rs['id'];
- }
- }
- $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", property="cartId", type="integer", default="1"),
- * @OA\Property(description="台桌id", property="tableId", type="integer", default="1"),
- * required={"cartId","tableId"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function createFoodOrder() {
- $input = Until::getInput();
- $rule = [
- 'cartId|商品id和数量' => 'require',
- 'tableId|桌台id' => 'require',
- ];
- Until::check($rule, $input);
- $data = (new OrderService())->payFoodOrder($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();
- $allocateModel = new AllocateModel();
- $rs = $allocateModel::where(['order_id' => (int)$input['orderId'], 'staff_id' => $input['staffId']])->find();
- if (empty($rs)) {
- $allocateModel->insertGetId([
- 'order_id' => (int)$input['orderId'],
- 'staff_id' => $input['staffId'],
- 'admin_id' => $this->adminId
- ]);
- } else {
- if ($rs['status'] == 2) {
- throw new ApiException('服务已开始,无法分配职员');
- }elseif ($rs['status'] == 3) {
- throw new ApiException('服务已结束,无法分配职员');
- }
- $allocateModel::where(['id' => $rs['id']])->update(['staff_id' => $input['staffId']]);
- }
- $model::where(['id' => (int)$input['orderId']])->update(['staff_id' => (int)$input['staffId']]);
- Until::output();
- }
- /**
- * @OA\Post(path="/api/Order/serverStart",
- * 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 serverStart() {
- $input = Until::getInput();
- $rule = [
- 'orderId|订单id' => 'require',
- ];
- Until::check($rule, $input);
- $allocateModel = new AllocateModel();
- $model = new OrderModel();
- $order = $model::where(['id' => $input['orderId']])->find();
- if (empty($order)) {
- throw new ApiException('无此订单');
- }
- if ($order['status'] != 2) {
- throw new ApiException('订单必须是支付状态');
- }
- $rs = $allocateModel::where(['order_id' => (int)$input['orderId']])->find();
- if (empty($rs)) {
- throw new ApiException('该服务还没分配职员');
- } else {
- if ($rs['status'] == 3) {
- throw new ApiException('该服务已完成');
- }
- if ($rs['status'] == 2) {
- throw new ApiException('该服务正在进行');
- }
- }
- $allocateModel::where(['id' => $rs['id']])->update(['status' => 2]);
- Until::output();
- }
- /**
- * @OA\Post(path="/api/Order/serverComplete",
- * 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 serverComplete() {
- $input = Until::getInput();
- $rule = [
- 'orderId|订单id' => 'require',
- ];
- Until::check($rule, $input);
- $model = new OrderModel();
- $allocateModel = new AllocateModel();
- $rs = $allocateModel::where(['order_id' => (int)$input['orderId']])->find();
- if (empty($rs)) {
- throw new ApiException('该服务还没分配职员');
- } else {
- if ($rs['status'] == 3) {
- throw new ApiException('该服务已完成');
- }
- }
- $allocateModel::where(['id' => $rs['id']])->update(['status' => 3]);
- $model::where(['id' => $input['orderId']])->update(['status' => 5]);
- 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"),
- * @OA\Property(description="支付方式", property="payType", type="string", default="1微信 2支付宝 3银行卡 4现金 "),
- * @OA\Property(description="备注", property="remark", type="integer", default="1"),
- * required={"orderId","payType"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function payOrder() {
- $input = Until::getInput();
- $rule = [
- 'orderId|订单id' => 'require',
- 'payType|支付方式' => '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('该订单已支付');
- }
- if ($orderInfo['order_type'] !== 2) {
- throw new ApiException('该订单已不是pad端下单');
- }
- $model::where(['id' => (int)$input['orderId']])->update([
- 'status' => OrderModel::IS_PAY,
- 'pay_type' => $input['payType'],
- 'remark' => $input['remark'],
- 'pay_time' => date('Y-m-d H:i:s')
- ]);
- Db::table('pay_record')->insertGetId(['order_id' => $input['orderId'], 'admin_id' => $this->adminId]);
- $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 = Until::getInput();
- $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 = Until::getInput();
- $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) {
- if ($rs['num'] == 1) {
- $cartProductModel::where(['id' => $rs['id']])->delete();
- }
- $cartProductModel::where(['id' => $rs['id']])->dec('num', 1)->update();
- }
- }
- $cartInfo = Until::modelToArray($cartInfo);
- $cartModel->setFields('c.*,cp.product_id,cp.num,p.product_name,p.current_price,p.product_img');
- $rs = $cartModel->getCartList();
- $cartInfo['totalNum'] = 0;
- $cartInfo['totalPrice'] = 0;
- foreach ($rs as $v) {
- $cartInfo['totalNum'] += $v['num'];
- $cartInfo['totalPrice'] += $v['num'] * $v['current_price'];
- }
- $cartInfo['id'] = $v['id'];
- $cartInfo['table_id'] = $v['table_id'];
- $cartInfo['store_id'] = $v['store_id'];
- $cartInfo['user_id'] = $v['user_id'];
- $cartInfo['create_time'] = $v['create_time'];
- $cartInfo['list'] = $cartInfo['totalNum'] == 0 ? [] : $rs;
- Until::output($cartInfo);
- }
- /**
- * @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();
- try {
- $model->startTrans();
- $whereData = [
- 'table_id' => (int)$input['tableId'],
- 'store_id' => (int)$input['storeId'],
- 'user_id' => $this->userId,
- 'status' => CartModel::NORMAL
- ];
- $cartInfo = $model::where($whereData)->find();
- $cartInfo = Until::modelToArray($cartInfo);
- if (empty($cartInfo)) {
- $id = $model->insertGetId($whereData);
- }
- $model->setWhere([
- 'c.status' => CartModel::NORMAL,
- 'c.user_id' => $this->userId,
- 'c.table_id' => (int)$input['tableId'],
- 'c.store_id' => (int)$input['storeId']
- ]);
- $model->setFields('c.*,cp.product_id,cp.num,p.product_name,p.current_price,p.product_img');
- $rs = $model->getCartList();
- $model->commit();
- } catch (\Exception $e) {
- $model->rollback();
- throw new ApiException($e->getMessage());
- }
- $cartInfo['totalPrice'] = 0;
- $cartInfo['totalNum'] = 0;
- foreach ($rs as $v) {
- $cartInfo['totalNum'] += $v['num'];
- $cartInfo['totalPrice'] += $v['num'] * $v['current_price'];
- }
- $cartInfo['id'] = $v['id'];
- $cartInfo['table_id'] = $v['table_id'];
- $cartInfo['store_id'] = $v['store_id'];
- $cartInfo['user_id'] = $v['user_id'];
- $cartInfo['create_time'] = $v['create_time'];
- $cartInfo['totalPrice'] = number_format($cartInfo['totalPrice'], 2);
- $cartInfo['list'] = $cartInfo['totalNum'] == 0 ? [] : $rs;
- Until::output($cartInfo);
- }
- /**
- * @OA\Post(path="/api/Order/clearCart",
- * 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="cartId", type="integer", default="1"),
- * required={"cartId"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function clearCart() {
- $input = Until::getInput();
- $rule = [
- 'cartId|购物车id' => 'require',
- ];
- Until::check($rule, $input);
- $model = new CartModel();
- $model::where(['id' => $input['cartId']])->update(['status' => 0]);
- Until::output();
- }
- }
|