561 lines
22 KiB
PHP
561 lines
22 KiB
PHP
<?php
|
|
|
|
namespace app\storeapi\logic;
|
|
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\order\OrderGroup;
|
|
use app\common\model\order\OrderStore;
|
|
use app\common\model\order\OrderStoreHistory;
|
|
use app\common\model\store\StoreUser;
|
|
use app\common\model\teastore\TeaStore;
|
|
use app\common\model\teastore\TeaStoreGroup;
|
|
use app\common\model\teastore\TeaStoreRoom;
|
|
use app\common\model\user\User;
|
|
use app\common\model\user\UserCoupon;
|
|
use app\common\model\user\UserCouponType;
|
|
use app\common\service\FileService;
|
|
use app\common\service\iot\IotService;
|
|
use app\common\model\user\UserStoreMoney;
|
|
use app\common\model\user\UserAccountLog;
|
|
use app\common\model\store\StoreUserAccountLog;
|
|
use think\facade\{Db};
|
|
use app\common\service\pay\WeChatPayService;
|
|
use app\common\model\user\UserGroup;
|
|
class OrderLogic extends BaseLogic
|
|
{
|
|
|
|
public static function orderStoreList($post,$userId){
|
|
$os = "";
|
|
$od = "a.start_time desc";
|
|
if(isset($post['order_status'])){
|
|
if($post['order_status']!= "" || $post['order_status']!= null){
|
|
$os = "a.order_status = ".$post['order_status']."";
|
|
$od = "a.start_time asc";
|
|
if($post['order_status'] == 3){
|
|
$od = "a.start_time desc";
|
|
}
|
|
}
|
|
}
|
|
$s = "";
|
|
if(isset($post['search'])){
|
|
if($post['search']!= "" || $post['search']!= null){
|
|
$a = $post['search'];
|
|
$s = "c.title like '%".$a."%'";
|
|
$hostory = OrderStoreHistory::where("content",$a)
|
|
->where("user_id",$userId)
|
|
->where("del",0)
|
|
->where("status",1)
|
|
->find();
|
|
if($hostory == null){
|
|
OrderStoreHistory::create([
|
|
"user_id"=>$userId,
|
|
"content"=>$a,
|
|
"dtime"=>time()
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
$count = OrderStore::alias("a")
|
|
->where("a.del",0)
|
|
->where("a.is_transfer",0)
|
|
->where('a.store_id',$post['store_id'])
|
|
->join("tea_store b","b.id = a.store_id","left")
|
|
->join("tea_store_room c","c.id = a.room_id","left")
|
|
->where($os)
|
|
->where($s)
|
|
->count();
|
|
$lists = OrderStore::alias("a")
|
|
->field('a.day_title,a.group_coupon_id,b.operation_type,a.id,a.order_sn,a.store_id,a.room_id,a.day_time,a.start_time,a.end_time,a.hours,
|
|
a.order_status,b.name as store_name,c.title as room_name,c.img,a.user_id,a.renew_hour,order_amount,renew_price')
|
|
->join("tea_store b","b.id = a.store_id","left")
|
|
->join("tea_store_room c","c.id = a.room_id","left")
|
|
->where("a.del",0)
|
|
->where("a.is_transfer",0)
|
|
->where('a.store_id',$post['store_id'])
|
|
->where($os)
|
|
->where($s)
|
|
->page($post['page'], $post['size'])
|
|
->order($od)
|
|
->select();
|
|
foreach($lists as $key=>$value){
|
|
$lists[$key]['user_mobile'] = "";
|
|
$user = User::where("id",$value['user_id'])->find();
|
|
if($user!=null){
|
|
$lists[$key]['user_mobile'] = $user['mobile'];
|
|
}
|
|
$lists[$key]['img'] = FileService::getImgUrl($value['img']);
|
|
$lists[$key]['start_time'] = date("H:i",$value['start_time']);
|
|
$lists[$key]['end_time'] = date("H:i",$value['end_time']);
|
|
$lists[$key]['hours'] = $value['renew_hour']+$value['hours'];
|
|
$lists[$key]['price'] = $value['order_amount']+$value['renew_price'];
|
|
}
|
|
$data = [
|
|
'list' => $lists,
|
|
'page' => $post['page'],
|
|
'size' => $post['size'],
|
|
'count' => $count,
|
|
'more' => is_more($count, $post['page'], $post['size'])
|
|
];
|
|
return $data;
|
|
}
|
|
public static function orderRenewList($post){
|
|
$count = OrderStore::alias("a")
|
|
->where("a.del",0)
|
|
->where("a.is_transfer",0)
|
|
->where('a.store_id',$post['store_id'])
|
|
->join("tea_store b","b.id = a.store_id","left")
|
|
->join("tea_store_room c","c.id = a.room_id","left")
|
|
->where("a.order_status","in","1,2")
|
|
->count();
|
|
$lists = OrderStore::alias("a")
|
|
->field('a.day_title,a.group_coupon_id,b.operation_type,a.id,a.order_sn,a.store_id,a.room_id,a.day_time,a.start_time,a.end_time,a.hours,
|
|
a.order_status,b.name as store_name,c.title as room_name,c.img,a.user_id,a.renew_hour')
|
|
->join("tea_store b","b.id = a.store_id","left")
|
|
->join("tea_store_room c","c.id = a.room_id","left")
|
|
->where("a.del",0)
|
|
->where("a.is_transfer",0)
|
|
->where('a.store_id',$post['store_id'])
|
|
->where("a.order_status","in","1,2")
|
|
->page($post['page'], $post['size'])
|
|
->order('a.id asc')
|
|
->select();
|
|
foreach($lists as $key=>$value){
|
|
$lists[$key]['user_mobile'] = "";
|
|
$user = User::where("id",$value['user_id'])->find();
|
|
if($user!=null){
|
|
$lists[$key]['user_mobile'] = $user['mobile'];
|
|
}
|
|
$lists[$key]['img'] = FileService::getImgUrl($value['img']);
|
|
$lists[$key]['start_time'] = date("H:i",$value['start_time']);
|
|
$lists[$key]['end_time'] = date("H:i",$value['end_time']);
|
|
$lists[$key]['hours'] = $value['renew_hour']+$value['hours'];
|
|
}
|
|
$data = [
|
|
'list' => $lists,
|
|
'page' => $post['page'],
|
|
'size' => $post['size'],
|
|
'count' => $count,
|
|
'more' => is_more($count, $post['page'], $post['size'])
|
|
];
|
|
return $data;
|
|
}
|
|
public static function getOrderStoreSearchHistory($userId){
|
|
$lists = OrderStoreHistory::where("user_id",$userId)
|
|
->where("status",1)
|
|
->where("del",0)
|
|
->order("id","desc")
|
|
->limit(15)
|
|
->select();
|
|
$data = [
|
|
'list' => $lists
|
|
];
|
|
return $data;
|
|
}
|
|
public static function delOrderStoreSearchHistory($userId){
|
|
return OrderStoreHistory::where("user_id",$userId)
|
|
->where("status",1)
|
|
->where("del",0)
|
|
->update(['del'=>1]);
|
|
}
|
|
public static function orderStoreDetails($data){
|
|
$details = OrderStore::where("id",$data['id'])->find();
|
|
$room_msg = TeaStoreRoom::where("id",$details['room_id'])->find();
|
|
$room_msg['img'] = FileService::getImgUrl($room_msg['img'] );
|
|
$details['room_msg'] = $room_msg;
|
|
$store_msg = TeaStore::where("id",$details['store_id'])->find();
|
|
$details['store_msg'] = $store_msg;
|
|
$details['mobile'] = "";
|
|
$user = User::where('id',$details['user_id'])->find();
|
|
if($user != null){
|
|
$details['mobile'] = $user['mobile'];
|
|
}
|
|
// if($details['renew_dtime']!=null&&$details['renew_dtime']!=""){
|
|
// $renew_msg = explode("-",$details['renew_dtime']);
|
|
// foreach($renew_msg as $key=>$value){
|
|
// $r_msg = json_decode($value,true);
|
|
// $r_msg['start_time'] = date('H:i', $r_msg['start_time']);
|
|
// $r_msg['end_time'] = date('H:i', $r_msg['end_time']);
|
|
// $arr[$key] =$r_msg;
|
|
// }
|
|
//// $details['renew_dtime'] = $arr;
|
|
// $details['renew_dtime'] = array_slice($arr, -1)[0];
|
|
// }
|
|
$group_type = '';
|
|
$details['pay_way_title'] = "";
|
|
if($details['group_coupon_id']!==0){
|
|
$user_group = UserGroup::where('id',$details['group_coupon_id'])->find();
|
|
if($user_group['type'] ==1){
|
|
$group_type = '本地套餐';
|
|
}elseif($user_group['type'] ==2){
|
|
$group_type = '抖音套餐';
|
|
}
|
|
}
|
|
if($details->order_amount>0){
|
|
if( $details['pay_way'] == 1){
|
|
$details['pay_way_title'] = "平台余额支付".'+'.$group_type;
|
|
}
|
|
if( $details['pay_way'] == 2){
|
|
$details['pay_way_title'] = "微信支付".'+'.$group_type;
|
|
}
|
|
if( $details['pay_way'] == 3){
|
|
$details['pay_way_title'] = "门店余额支付".'+'.$group_type;
|
|
}
|
|
|
|
}else{
|
|
$details['pay_way_title'] = $group_type;
|
|
if( $details['pay_way'] == 4){
|
|
$details['pay_way_title'] = "管理员添加".'+'.$group_type;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
return $details;
|
|
}
|
|
public static function submitRefund($data){
|
|
$order_store = OrderStore::where([
|
|
'id'=>$data['order_id'],
|
|
'is_transfer'=>0,
|
|
])->whereIn('pay_status',[1,2])->find();
|
|
|
|
}
|
|
public static function cancelOrderStore($data){
|
|
|
|
$order = OrderStore::where("id",$data['id'])->find();
|
|
$tes_store = TeaStore::where('id', $order['store_id'])->find();
|
|
//查询是否使用优惠券
|
|
if($order['user_coupon_id'] != 0){
|
|
UserCoupon::where("id",$order['user_coupon_id'])->update(['status'=>0]);
|
|
}
|
|
//查询是否使用团购券
|
|
if($order['group_coupon_id'] != 0){
|
|
UserGroup::where("id",$order['group_coupon_id'])->update(['status'=>0]);
|
|
$group = UserGroup::where("id",$order['group_coupon_id'])->find();
|
|
$store_group = TeaStoreGroup::where("id",$group['group_id'])->find();
|
|
$change_object = 4;
|
|
if($group['type'] == 2){
|
|
$store_group['discount_price'] = 0;
|
|
$change_object = 5;
|
|
}
|
|
StoreUserAccountLog::create([
|
|
'sn'=>createSn("store_user_account_log","sn"),
|
|
'change_object'=>$change_object,
|
|
'change_type'=>5,
|
|
'user_id'=>$order->user_id,
|
|
'action'=>2,
|
|
'amount' => $store_group['discount_price'],
|
|
'before_amount'=>$tes_store['balance'],
|
|
'after_amount' => round($tes_store['balance']-$store_group['discount_price'],2),
|
|
'source_sn' => $order['order_sn'],
|
|
'store_id'=>$order->store_id,
|
|
'room_id'=>$order->room_id,
|
|
'remark'=>"团购套餐退款",
|
|
'create_time' => time()
|
|
]);
|
|
TeaStore::where('id', $order['store_id'])->dec('total_amount', $store_group['discount_price'])->update();
|
|
TeaStore::where('id', $order['store_id'])->dec('balance', $store_group['discount_price'])->update();
|
|
|
|
}
|
|
$tes_store = TeaStore::where('id', $order['store_id'])->find();
|
|
if($order['pay_way'] == 3){
|
|
$user = UserStoreMoney::where("user_id",$order['user_id'])
|
|
->where("store_id",$order['store_id'])
|
|
->find();
|
|
$money = round($user['money']+$order['order_amount'],2);
|
|
UserStoreMoney::where("user_id",$order['user_id'])
|
|
->where("store_id",$order['store_id'])
|
|
->update(['money'=>$money]);
|
|
UserAccountLog::create([
|
|
"sn"=>createSn("user_account_log","sn"),
|
|
"user_id"=>$order['user_id'],
|
|
"change_object"=>3,
|
|
"change_type"=>8,
|
|
"action"=>1,
|
|
"amount"=>$order['order_amount'],
|
|
"before_amount"=>$user['money'],
|
|
"after_amount"=>$money,
|
|
"source_sn"=>$order['order_sn'],
|
|
"store_id"=>$order['id'],
|
|
"remark"=>'取消订单退款',
|
|
"create_time"=>time()
|
|
]);
|
|
StoreUserAccountLog::create([
|
|
'sn'=>createSn("store_user_account_log","sn"),
|
|
'change_object'=>3,
|
|
'change_type'=>5,
|
|
'action'=>2,
|
|
'amount' => 0,
|
|
'before_amount'=>$tes_store['balance'],
|
|
'after_amount' => $tes_store['balance'],
|
|
'user_id' => $order['user_id'],
|
|
'source_sn' => $order['order_sn'],
|
|
'store_id'=>$order['store_id'],
|
|
'room_id'=>$order['room_id'],
|
|
'remark'=>"包间预定退款",
|
|
'create_time' => time()
|
|
]);
|
|
}elseif($order['pay_way'] == 1 || $order['pay_way'] == 2){
|
|
$user = User::where("id",$order['user_id'])->find();
|
|
if($order['pay_way'] == 2&&$order['order_amount']>0){
|
|
$refundData = [
|
|
'transaction_id'=>$order['transaction_id'],
|
|
'refund_sn'=>$order['order_sn'],
|
|
'total_amount'=>$order['order_amount'],
|
|
'notify_url'=>FileService::getFileUrl('api/pay/notifyRefundMnp')
|
|
// FileService::getFileUrl('api/pay/notifyMnp')
|
|
];
|
|
|
|
$payService = (new WeChatPayService(1, $user_id ?? null));
|
|
$result = $payService->refund($refundData);
|
|
if($result['status'] !=='PROCESSING'){
|
|
throw new \Exception('押金退款失败请联系客服');
|
|
}
|
|
$change_object = 2;
|
|
$change_type = 10;
|
|
$money = $user['user_money'];
|
|
}else{
|
|
$change_object = 1;
|
|
$change_type = 9;
|
|
$money = round($user['user_money']+$order['order_amount'],2);
|
|
User::where("id",$order['user_id'])->update(['user_money'=>$money]);
|
|
}
|
|
UserAccountLog::create([
|
|
"sn"=>createSn("user_account_log","sn"),
|
|
"user_id"=>$order['user_id'],
|
|
"change_object"=>$change_object,
|
|
"change_type"=>$change_type,
|
|
"action"=>1,
|
|
"amount"=>$order['order_amount'],
|
|
"before_amount"=>$user['user_money'],
|
|
"after_amount"=>$money,
|
|
"source_sn"=>$order['order_sn'],
|
|
"store_id"=>$order['id'],
|
|
"remark"=>'取消订单退款',
|
|
"create_time"=>time()
|
|
]);
|
|
// 扣除管理端门店余额
|
|
|
|
$order_amount = $order['order_amount'];
|
|
$total_amount = round($tes_store['total_amount'] - $order_amount,2);
|
|
$balance = round($tes_store['balance'] - $order_amount,2);
|
|
TeaStore::where('id', $order['store_id'])->update(['total_amount'=>$total_amount,'balance'=>$balance]);
|
|
$change_object = 1;
|
|
if($order['pay_way'] == 2){
|
|
$change_object = 2;
|
|
}
|
|
StoreUserAccountLog::create([
|
|
'sn'=>createSn("store_user_account_log","sn"),
|
|
'change_object'=>$change_object,
|
|
'change_type'=>5,
|
|
'action'=>2,
|
|
'amount' => $order_amount,
|
|
'before_amount'=>$tes_store['balance'],
|
|
'after_amount' => $balance,
|
|
'user_id' => $order['user_id'],
|
|
'source_sn' => $order['order_sn'],
|
|
'store_id'=>$order['store_id'],
|
|
'room_id'=>$order['room_id'],
|
|
'remark'=>"包间预定退款",
|
|
'create_time' => time()
|
|
]);
|
|
}
|
|
$ids = explode(',',$order->id);
|
|
$transfer = OrderStore::where('transfer_order_id',$order->id)->select();
|
|
if($transfer->count() > 0){
|
|
$orderIds = $transfer->column('id');
|
|
$ids = array_merge($ids,$orderIds);
|
|
}
|
|
OrderStore::whereIn("id",$ids)->update(["order_status"=>3]);
|
|
return OrderStore::where("id",$data['id'])->update(["order_status"=>4]);
|
|
}
|
|
public static function delOrderStore($data){
|
|
return OrderStore::where("id",$data['id'])->update(["del"=>1]);
|
|
}
|
|
public static function releaseOrderStore($data){
|
|
$result = OrderStore::where("id",$data['id'])->find();
|
|
if(!$result){
|
|
throw new \Exception('订单不存在');
|
|
}
|
|
$room = TeaStoreRoom::where("id",$result['room_id'])->find();
|
|
$transfer = OrderStore::where('transfer_order_id',$result->id)->select();
|
|
$ids = explode(',',$result->id);
|
|
|
|
if($transfer->count() > 0){
|
|
$orderIds = $transfer->column('id');
|
|
$ids = array_merge($ids,$orderIds);
|
|
}
|
|
$room->status = 1;
|
|
$iot = new IotService();
|
|
$iot->controlDevice($room['device_id'],0);
|
|
TeaStoreRoom::where('id',$room['id'])->update([
|
|
'status'=>$room->status,
|
|
'is_open'=>0
|
|
]);
|
|
OrderStore::whereIn("id",$ids)->update(["order_status"=>3,"is_release"=>1]);
|
|
return [];
|
|
}
|
|
|
|
public static function addOrder($post,$user_id){
|
|
Db::startTrans();
|
|
try {
|
|
$hours = $post['hours'];
|
|
$timeslot = $post['timeslot'];
|
|
$day_time = $post['day_time'];
|
|
$room_id = $post['room_id'];
|
|
|
|
$room_msg = TeaStoreRoom::where("id", $post['room_id'])->find();
|
|
if (!$room_msg) {
|
|
throw new \Exception('茶室不存在');
|
|
}
|
|
|
|
$store_id = $room_msg->store_id;
|
|
$timeslot = explode(',', $timeslot);
|
|
|
|
if ($hours != (count($timeslot) - 1) * 0.5) {
|
|
throw new \Exception('参数错误,请重新下单');
|
|
}
|
|
|
|
if (empty($timeslot)) {
|
|
throw new \Exception('时间段不能为空');
|
|
}
|
|
$start = $timeslot[0];
|
|
$end = $timeslot[count($timeslot) - 1];
|
|
|
|
$timeList = OrderStore::where([
|
|
'room_id' => $room_id,
|
|
'day_time' => $day_time,
|
|
])->whereIn('order_status', [0, 1, 2])->column('timeslot');
|
|
$times = [];
|
|
|
|
foreach ($timeList as $v) {
|
|
$times = array_merge($times, explode(',', $v));
|
|
}
|
|
|
|
$repeat = array_intersect($timeslot, $times);
|
|
if ($repeat) {
|
|
throw new \Exception('所选时间段存在不可选项');
|
|
}
|
|
|
|
$store_user = StoreUser::where('id',$user_id)->find();
|
|
$user_msg = User::where("id", $store_user->bind_user_id)->find();
|
|
if ($user_msg == null) {
|
|
throw new \Exception('用户不存在');
|
|
}
|
|
$order_sn = createSn("order_store", "order_sn");
|
|
|
|
$order = OrderStore::create([
|
|
'order_sn' => $order_sn,
|
|
'store_id' => $store_id,
|
|
'room_id' => $post['room_id'],
|
|
'user_id' => $user_msg['id'],
|
|
'day_title' => $post['day_time'],
|
|
'day_time' => $post['day_time'],
|
|
'start_time' => $start,
|
|
'end_time' => $end,
|
|
'timeslot' => $post['timeslot'],
|
|
'hours' => $post['hours'],
|
|
'group_coupon_id' => 0,
|
|
'room_price' => $room_msg['price'],
|
|
'room_all_price' => 0,
|
|
'user_coupon_id' => 0,
|
|
'coupon_price' => 0,
|
|
'group_price' => 0,
|
|
'member_price' => 0,
|
|
'store_user_order'=>1,
|
|
'pay_way'=>4,
|
|
'order_amount' => 0,
|
|
'store_income_price' => 0,
|
|
'dtime' => date("Y-m-d H:i:s"),
|
|
'order_status'=>1
|
|
]);
|
|
Db::commit();
|
|
return [];
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
public static function renewOrder($data,$user_id){
|
|
Db::startTrans();
|
|
try {
|
|
$order = OrderStore::where("id", $data['id'])->find();
|
|
if (!$order) {
|
|
throw new \Exception('订单不存在,无法续订');
|
|
}
|
|
if ($order['order_status'] >= 3) {
|
|
throw new \Exception('订单已结束,无法续订');
|
|
}
|
|
$source_id = $order->id;
|
|
if ($order->is_transfer == 1) {
|
|
$source_id = $order->transfer_order_id;
|
|
}
|
|
$hour = $data['renew_hour'];
|
|
$timeList = explode(',', $order['timeslot']);
|
|
$max = $hour * 2;
|
|
$extra = [];
|
|
for ($i = 0; $i < $max; $i++) {
|
|
// 获取最近的一个时间
|
|
$timeEnd = end($timeList);
|
|
$end = $timeEnd + 1800;
|
|
$extra[] = $end;
|
|
array_push($timeList, (string)$end);
|
|
}
|
|
|
|
foreach ($extra as &$v) {
|
|
$info = OrderStore::where('room_id', $order['room_id'])->whereIn('order_status',[0,1,2])->where("find_in_set($v,timeslot)")->find();
|
|
if ($info) {
|
|
throw new \Exception('当前时间已被预定');
|
|
}
|
|
}
|
|
|
|
$order_sn = createSn("order_store_renew", "order_sn");
|
|
$insert = [
|
|
'order_sn' => $order_sn,
|
|
'user_id' => 0,
|
|
'source_id' => $source_id,
|
|
'timeslot' => implode(',', $extra),
|
|
'dtime' => time(),
|
|
'hour' => $hour,
|
|
'pay_status' => 0,
|
|
'price' => 0,
|
|
'pay_way'=>4,
|
|
'expire_time' => time() + 60 * 3
|
|
];
|
|
$renew_timeslot = $extra;
|
|
$store_timeslot = explode(',', $order->timeslot);
|
|
$timeList = array_merge($store_timeslot, $renew_timeslot);
|
|
$end = end($timeList);
|
|
|
|
$transfer = OrderStore::where('transfer_order_id',$order->id)->select();
|
|
$ids = explode(',',$order->id);
|
|
|
|
if($transfer->count() > 0){
|
|
$orderIds = $transfer->column('id');
|
|
$ids = array_merge($ids,$orderIds);
|
|
}
|
|
|
|
$uporder = OrderStore::whereIn('id',$ids)->update([
|
|
'end_time'=>$end,
|
|
'timeslot'=> implode(',', $timeList),
|
|
'renew_dtime'=>time(),
|
|
'is_renewal'=>1,
|
|
'renew_price'=>0,
|
|
'renew_hour'=>$hour+$order->renew_hour
|
|
]);
|
|
|
|
Db::commit();
|
|
return [
|
|
];
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|
|
} |