|
@@ -0,0 +1,308 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\api\controller\shop;
|
|
|
+
|
|
|
+use app\common\business\ShopWalletBusiness;
|
|
|
+use app\common\controller\Api;
|
|
|
+use app\common\model\BillModel;
|
|
|
+use app\common\model\HotelCanteenModel;
|
|
|
+use app\common\model\HotelCanteenOrderModel;
|
|
|
+use think\Db;
|
|
|
+/**
|
|
|
+ * 示例接口
|
|
|
+ */
|
|
|
+class ShopHotelCanteen extends Api
|
|
|
+{
|
|
|
+ protected $noNeedLogin = [''];
|
|
|
+ protected $noNeedRight = ['*'];
|
|
|
+
|
|
|
+ public function home()
|
|
|
+ {
|
|
|
+ $user_id = $this->auth->id;
|
|
|
+
|
|
|
+ $model = new HotelCanteenModel();
|
|
|
+ $info = $model->getDetail(
|
|
|
+ params: ['user_id' => $user_id],
|
|
|
+ select: ['id','invite_id','user_id','name','image','images','back_rate','address']
|
|
|
+ );
|
|
|
+ if (!$info){
|
|
|
+ return $this->error('未开通门店');
|
|
|
+ }
|
|
|
+ // 待核销金额
|
|
|
+ $wait_use = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order','status'=>2])->field('sum(total_amount - back_amount) as money')->find();
|
|
|
+
|
|
|
+ // 已核销
|
|
|
+ $used = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order','status'=>3])->field('sum(total_amount - back_amount) as money')->find();
|
|
|
+
|
|
|
+ // 已退款
|
|
|
+ $refund = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order','status'=>4])->field('sum(total_amount - back_amount) as money')->find();
|
|
|
+
|
|
|
+ $statistics = [
|
|
|
+ 'wait_use' => $wait_use['money'] ?? 0,
|
|
|
+ 'used' => $used['money'] ?? 0,
|
|
|
+ 'refund' => $refund['money'] ?? 0,
|
|
|
+ ];
|
|
|
+
|
|
|
+ return $this->success('success',[
|
|
|
+ 'info' => $info,
|
|
|
+ 'statistics' => $statistics
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置让利比例
|
|
|
+ public function set_back_rate()
|
|
|
+ {
|
|
|
+ $params = $this->request->param();
|
|
|
+ $user_id = $this->auth->id;
|
|
|
+ $model = new HotelCanteenModel();
|
|
|
+ $info = $model->getDetail(
|
|
|
+ params: ['user_id' => $user_id],
|
|
|
+ select: ['id','invite_id','user_id','name','image','images','back_rate','address']
|
|
|
+ );
|
|
|
+ if (!$info){
|
|
|
+ return $this->error('未开通门店');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!HotelCanteenModel::where('id',$info['id'])->update(['back_rate'=>$params['rate']??20])){
|
|
|
+ return $this->error('操作失败');
|
|
|
+ }
|
|
|
+ return $this->success('设置成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 订单列表
|
|
|
+ public function bill()
|
|
|
+ {
|
|
|
+ $params = $this->request->param();
|
|
|
+ $user_id = $this->auth->id;
|
|
|
+
|
|
|
+ $model = new HotelCanteenModel();
|
|
|
+ $info = $model->getDetail(
|
|
|
+ params: ['user_id' => $user_id],
|
|
|
+ select: ['id','invite_id','user_id','name','image','images','back_rate','address']
|
|
|
+ );
|
|
|
+ if (!$info){
|
|
|
+ return $this->error('未开通门店');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 全部
|
|
|
+ if (empty($params['status'])){
|
|
|
+ $params['status_in'] = [1,2,3];
|
|
|
+ unset($params['status']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $model = new BillModel();
|
|
|
+ $list = $model->getList(
|
|
|
+ params: array_merge(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order'],$params),
|
|
|
+ with: [
|
|
|
+ 'hotelcanteenorder' => function ($query) {
|
|
|
+ $query->field('id,hotel_canteen_id,room_id,name,phone');
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ return $this->success('success',$list);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 订单详情
|
|
|
+ public function bill_detail()
|
|
|
+ {
|
|
|
+ $params = $this->request->param();
|
|
|
+ $user_id = $this->auth->id;
|
|
|
+
|
|
|
+ $model = new HotelCanteenModel();
|
|
|
+ $info = $model->getDetail(
|
|
|
+ params: ['user_id' => $user_id],
|
|
|
+ select: ['id','invite_id','user_id','name','image','images','back_rate','address']
|
|
|
+ );
|
|
|
+ if (!$info){
|
|
|
+ return $this->error('未开通门店');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 全部
|
|
|
+ if (empty($params['status'])){
|
|
|
+ $params['status_in'] = [1,2,3];
|
|
|
+ unset($params['status']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $model = new BillModel();
|
|
|
+ $list = $model->getDetail(
|
|
|
+ params: array_merge(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order'],$params),
|
|
|
+ with: [
|
|
|
+ 'hotelcanteenorder' => function ($query) {
|
|
|
+ $query->field('id,hotel_canteen_id,room_id,name,phone');
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ return $this->success('success',$list);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 酒店订单核销
|
|
|
+ public function deal()
|
|
|
+ {
|
|
|
+ $params = $this->request->param();
|
|
|
+ $user_id = $this->auth->id;
|
|
|
+
|
|
|
+ $model = new HotelCanteenModel();
|
|
|
+ $info = $model->getDetail(
|
|
|
+ params: ['user_id' => $user_id],
|
|
|
+ select: ['id','invite_id','user_id','name','image','images','back_rate','address']
|
|
|
+ );
|
|
|
+ if (!$info){
|
|
|
+ return $this->error('未开通门店');
|
|
|
+ }
|
|
|
+
|
|
|
+ $params['status_in'] = [1,2,3];
|
|
|
+
|
|
|
+ $model = new BillModel();
|
|
|
+ $order = $model->getDetail(
|
|
|
+ params: array_merge(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order'],$params),
|
|
|
+ with: [
|
|
|
+ 'hotelcanteenorder' => function ($query) {
|
|
|
+ $query->field('id,hotel_canteen_id,room_id,name,phone');
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ );
|
|
|
+ if (empty($order)){
|
|
|
+ return $this->error('未找到该订单');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($params['status'] == 3){
|
|
|
+ return $this->error('订单已核销');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($params['status'] != 2){
|
|
|
+ return $this->error('订单状态错误');
|
|
|
+ }
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+ // 更改订单状态
|
|
|
+ BillModel::where('id',$order['id'])->update(['status'=>3,'back_status' => 1,'back_time' => time()]);
|
|
|
+ HotelCanteenOrderModel::where('id',$order['table_id'])->update(['is_use'=>1,'update_time' => time()]);
|
|
|
+ // 发放商家收益
|
|
|
+ $money = bcsub($order['total_amount'],$order['back_amount'],2);// 除去让利后的金额
|
|
|
+ $wallet = new ShopWalletBusiness('hotel_canteen_order');
|
|
|
+ if (!$wallet->change($info['id'],$money,'money',ShopWalletBusiness::log_type[40],'订单核销','bill',$order['id'])){
|
|
|
+ Db::rollback();
|
|
|
+ return $this->error($wallet->getMessage(),$wallet->getData());
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+
|
|
|
+ return $this->success('操作成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 交易统计
|
|
|
+ public function statistics()
|
|
|
+ {
|
|
|
+ $params = $this->request->param();
|
|
|
+ $user_id = $this->auth->id;
|
|
|
+
|
|
|
+ if (empty($params['type'])){
|
|
|
+ $params['type'] = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $model = new HotelCanteenModel();
|
|
|
+ $info = $model->getDetail(
|
|
|
+ params: ['user_id' => $user_id],
|
|
|
+ select: ['id','invite_id','user_id','name','image','images','back_rate','address']
|
|
|
+ );
|
|
|
+ if (!$info){
|
|
|
+ return $this->error('未开通门店');
|
|
|
+ }
|
|
|
+ switch ($params['type']){
|
|
|
+ case 1:
|
|
|
+ // 今天
|
|
|
+ $createtimeBetween = [
|
|
|
+ strtotime(date('Y-m-d 00:00:00')),
|
|
|
+ strtotime(date('Y-m-d 23:59:59')),
|
|
|
+ ];
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ // 昨天
|
|
|
+ $createtimeBetween = [
|
|
|
+ strtotime(date('Y-m-d 00:00:00', strtotime('-1 day'))),
|
|
|
+ strtotime(date('Y-m-d 23:59:59', strtotime('-1 day'))),
|
|
|
+ ];
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ // 本月
|
|
|
+ $createtimeBetween = [
|
|
|
+ strtotime(date('Y-m-01 00:00:00')),
|
|
|
+ strtotime(date('Y-m-d 23:59:59')),
|
|
|
+ ];
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ $createtimeBetween = [
|
|
|
+ strtotime(date('Y-m-d 00:00:00'),strtotime($params['date'])),
|
|
|
+ strtotime(date('Y-m-d 23:59:59'),strtotime($params['date'])),
|
|
|
+ ];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ $wait_use = Db::name('bill')
|
|
|
+ ->where(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order'])
|
|
|
+ ->whereIn('status',[1,2,3])
|
|
|
+ ->whereBetween('createtime',$createtimeBetween)
|
|
|
+ ->field('sum(total_amount - back_amount) as money,count(id) as num')
|
|
|
+ ->find();
|
|
|
+
|
|
|
+ return $this->success('success',[
|
|
|
+ 'money' => $wait_use['money'] ?? 0,
|
|
|
+ 'num' => $used['num'] ?? 0,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 趋势图
|
|
|
+ public function trend()
|
|
|
+ {
|
|
|
+ $user_id = $this->auth->id;
|
|
|
+
|
|
|
+ $model = new HotelCanteenModel();
|
|
|
+ $info = $model->getDetail(
|
|
|
+ params: ['user_id' => $user_id],
|
|
|
+ select: ['id','invite_id','user_id','name','image','images','back_rate','address']
|
|
|
+ );
|
|
|
+ if (!$info){
|
|
|
+ return $this->error('未开通门店');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 七日数据走势统计
|
|
|
+ $createtimeBetween = [
|
|
|
+ strtotime(date('Y-m-d 00:00:00', strtotime('-6 day'))),
|
|
|
+ strtotime(date('Y-m-d 23:59:59')),
|
|
|
+ ];
|
|
|
+ $dates = [];
|
|
|
+ for ($i = 6; $i >= 0; $i--){
|
|
|
+ $dates[] = date('Y-m-d', strtotime('-'.$i.' day'));
|
|
|
+ }
|
|
|
+
|
|
|
+ $wait_use = Db::name('bill')
|
|
|
+ ->field('DATE(FROM_UNIXTIME(createtime)) as order_date,sum(total_amount - back_amount) as money,count(id) as num')
|
|
|
+ ->where(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order'])
|
|
|
+ ->whereIn('status',[1,2,3])
|
|
|
+ ->whereBetween('createtime',$createtimeBetween)
|
|
|
+ ->group('order_date')
|
|
|
+ ->order('order_date','asc')
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ $num = [];
|
|
|
+ $money = [];
|
|
|
+ foreach ($dates as $key=>$date){
|
|
|
+ $num[$key] = 0;
|
|
|
+ $money[$key] = '0';
|
|
|
+ foreach ($wait_use as $k=>$v){
|
|
|
+ if ($date == $v['order_date']){
|
|
|
+ $num[$key] = $v['num'];
|
|
|
+ $money[$key] = $v['money'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $dates[$key] = date('m/d',strtotime($date));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('success',[
|
|
|
+ 'money' => $money,
|
|
|
+ 'num' => $num,
|
|
|
+ 'dates' => $dates
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+}
|