123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?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\HotelModel;
- use app\common\model\HotelOrderModel;
- use app\common\model\OfflineShopModel;
- use app\common\model\OfflineTypeModel;
- use app\utils\DataUtil;
- use think\Db;
- /**
- * 示例接口
- */
- class ShopHotel extends Api
- {
- protected $noNeedLogin = [''];
- protected $noNeedRight = ['*'];
- public function home()
- {
- $user_id = $this->auth->id;
- $model = new HotelModel();
- $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_order','status'=>2])->field('sum(total_amount - back_amount) as money')->find();
- // 已核销
- $used = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order','status'=>3])->field('sum(total_amount - back_amount) as money')->find();
- // 已退款
- $refund = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_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 bill()
- {
- $params = $this->request->param();
- $user_id = $this->auth->id;
- $model = new HotelModel();
- $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_order'],$params),
- with: [
- 'hotelorder' => function ($query) {
- $query->field('id,hotel_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 HotelModel();
- $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_order'],$params),
- with: [
- 'hotelorder' => function ($query) {
- $query->field('id,hotel_id,room_id,name,phone');
- }
- ]
- );
- return $this->success('success',$list);
- }
- // 酒店订单核销
- public function deal()
- {
- $params = $this->request->param();
- $user_id = $this->auth->id;
- $model = new HotelModel();
- $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_order'],$params),
- with: [
- 'hotelorder' => function ($query) {
- $query->field('id,hotel_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()]);
- HotelOrderModel::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_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 HotelModel();
- $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_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 HotelModel();
- $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_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
- ]);
- }
- }
|