123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <?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\GroupModel;
- use app\api\model\OrderModel;
- use app\api\model\ProductModel;
- use app\api\model\WriteOffModel;
- use app\common\until\Until;
- use think\Db;
- class Order extends BaseController {
- /**
- * @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 = [];
- Db::table('order')->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();
- 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="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);
- $input['productId'] = (int)$input['productId'];
- if ($input['num'] < 1) {
- throw new ApiException('数量必须大于1');
- }
- $orderSn = Until::createSn();
- $productInfo = (new ProductModel())::where(['id' => $input['productId']])->find();
- $model = new OrderModel();
- try {
- $userId = $this->userId;
- $orderType = 1;
- if ($this->isAdmin()) {
- $userId = 0;
- $orderType = 2;
- }
- $model->startTrans();
- $orderId = $model->insertGetId([
- 'order_sn' => $orderSn,
- 'order_money' => $productInfo['current_price'] * (int)$input['num'],
- 'product_id' => $input['productId'],
- 'store_id' => $input['storeId'],
- 'appointment_time' => $input['appointmentTime'],
- 'mobile' => $input['mobile'],
- 'status' => 1,
- 'order_type' => $orderType,
- 'user_id' => $userId,
- ]);
- (new WriteOffModel())->insertGetId([
- 'write_off_code' => '',
- 'order_id' => $orderId,
- 'write_off_status' => 1,
- ]);
- $model->commit();
- } catch (\Exception $e) {
- $model->rollback();
- throw new ApiException($e->getMessage());
- }
- Until::output(['orderSn' => $orderSn,'orderId' => $orderId]);
- }
- /**
- * @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() {
- }
- /**
- * @OA\Get(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('该订单已经核销了');
- }
- $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([]);
- }
- }
|