Files
chazhi_admin_parten/app/api/logic/OrderLogic.php
2026-03-14 16:20:49 +08:00

979 lines
38 KiB
PHP

<?php
namespace app\api\logic;
use app\common\enum\RefundEnum;
use app\common\logic\BaseLogic;
use app\common\model\order\Order;
use app\common\model\order\OrderAll;
use app\common\model\order\OrderGroup;
use app\common\model\order\OrderStore;
use app\common\model\order\OrderStoreRenew;
use app\common\model\refund\RefundRecord;
use app\common\model\store\Store;
use app\common\model\teamaster\Tea;
use app\common\model\teamaster\Teamaster;
use app\common\model\teamaster\TeamasterLabel;
use app\common\model\teastore\TeaStore;
use app\common\model\teastore\TeaStoreGroup;
use app\common\model\teastore\TeaStoreRecharge;
use app\common\model\teastore\TeaStoreRoom;
use app\common\model\user\User;
use app\common\model\user\UserAddress;
use app\common\model\user\UserCoupon;
use app\common\model\user\UserCouponType;
use app\common\model\user\UserGroup;
use app\common\service\ConfigService;
use app\common\service\FileService;
use app\common\model\order\OrderStoreRecharge;
use DateTime;
use think\facade\{Db};
class OrderLogic extends BaseLogic
{
/**
* @notes 茶艺师订单提交
* @author 胥聪
* @date 2025/10/14 17:01
*/
public static function addOrder($post)
{
$post['room_price'] = isset($post['room_price']) ? $post['room_price'] : 0;
$post['address_id'] = isset($post['address_id']) ? $post['address_id'] : 0;
$post['store_id'] = isset($post['store_id']) ? $post['store_id'] : 0;
// 茶具价格
$post['cup_price'] = 0;
// $post['cup_price'] = ConfigService::get("gzh_setting","cup_price");
// 茶艺服务价格计算
$post['tea_price'] = Tea::where('id', 'in', [$post['tea_id']])->sum('tea_price');
// 茶艺师费用
$teamaster_msg = Teamaster::where("id", $post['teamaster_id'])->find();
$post['teamaster_price'] = $teamaster_msg['price'];
// 车马费
$distance = calculateDistanceKm($teamaster_msg['latitude'], $teamaster_msg['longitude'], $post['latitude'], $post['longitude']);
$distance = ceil($distance);
$fare_price = ConfigService::get("gzh_setting", "fare_price");
$post['fare_price'] = $distance * $fare_price;
// 优惠券计算
$coupon_msg = UserCoupon::where('id', 'in', [$post['coupon_id']])->select();
$post['coupon_price'] = 0;
foreach ($coupon_msg as $key => $value) {
$post['coupon_price'] += UserCouponType::where('id', $value['coupon_id'])->value("coupon_price");
}
$amount_price = $post['cup_price'] + $post['tea_price'] + $post['fare_price'] + $post['teamaster_price'] + $post['room_price'] - $post['coupon_price'];
$order_sn = createSn("order", "order_sn");
$order = Order::create([
'user_id' => $post['user_id'],
'order_sn' => $order_sn,
'service_type' => $post['service_type'],
'teamaster_id' => $post['teamaster_id'],
'address_id' => $post['address_id'],
'store_id' => $post['store_id'],
'start_time' => $post['start_time'],
'end_time' => $post['end_time'],
'nums' => $post['nums'],
'tea_id' => $post['tea_id'],
'tea_price' => $post['tea_price'],
'fare_price' => $post['fare_price'],
// 'cup_price'=>$post['cup_price'],
'teamaster_price' => $post['teamaster_price'],
'remark' => $post['remark'],
'coupon_id' => $post['coupon_id'],
'coupon_price' => $post['coupon_price'],
'order_amount' => $amount_price,
'longitude' => $post['longitude'],
'latitude' => $post['latitude'],
'hours' => $post['hours'],
// 'pay_type'=>$post['pay_type'],
'dtime' => date("Y-m-d H:i:s")
]);
return $order->id;
}
/**
* @notes 茶艺师列表
* @author 胥聪
* @date 2025/10/14 17:01
*/
public static function orderList($post, $userId)
{
$os = $os1 = "";
if (isset($post['order_status'])) {
if ($post['order_status'] != "" || $post['order_status'] != null) {
$os = "b.order_status = " . $post['order_status'] . "";
$os1 = "order_status = " . $post['order_status'] . "";
}
}
$s = $s1 = "";
if (isset($post['search'])) {
if ($post['search'] != "" || $post['search'] != null) {
$a = $post['search'];
$s = "a.name like '%" . $a . "%'";
$s1 = "name like '%" . $a . "%'";
}
}
$count = Order::where('user_id', $userId)
->where('del', 0)
->where($os1)
->where($s1)
->count();
$lists = Teamaster::alias("a")
->field('a.id as teamaster_id,a.level_id,a.label_id,a.sex,a.both,a.name,a.image,b.id,
b.service_type,b.start_time,b.end_time,b.order_status')
->join("order b", "b.teamaster_id = a.id", "left")
->where('b.user_id', $userId)
->where('b.del', 0)
->where($os)
->where($s)
->with(['teamasterLevel'])
->page($post['page'], $post['size'])
->order('b.id desc')
->select();
foreach ($lists as $key => $value) {
$a = array_filter(explode(",", $value['label_id']));
// 标签
$lists[$key]['teamasterlabel'] = TeamasterLabel::wherein("id", $a)->select()->toArray();
$lists[$key]['both'] = (new DateTime())->diff(new DateTime($value['both']))->y;
}
$data = [
'list' => $lists,
'page' => $post['page'],
'size' => $post['size'],
'count' => $count,
'more' => is_more($count, $post['page'], $post['size'])
];
return $data;
}
public static function orderDetails($data)
{
$details = Order::where("id", $data['id'])->find();
$teamaster = Teamaster::where('id', $details['teamaster_id'])->with(['teamasterLevel'])->find();
$teamaster['real'] = Teamaster::teamasterReal($teamaster['real_id']);
$teamaster['collect'] = 0;
$a = array_filter(explode(",", $teamaster['label_id']));
$teamaster['teamasterlabel'] = TeamasterLabel::wherein("id", $a)->select()->toArray();
$details['teamaster'] = $teamaster;
$b = array_filter(explode(",", $details['tea_id']));
$details['tea'] = Tea::wherein("id", $b)->select()->toArray();
// 订单过期时间
$f = strtotime($details['dtime']) + 30 * 60;
$current_timestamp = time();
$details['time1'] = $f - $current_timestamp;
// 按公里车马费
$details['fare_distance_price'] = ConfigService::get("gzh_setting", "fare_price");
// $details['distance'] = calculateDistanceKm($value['latitude'],$value['longitude'],$post['latitude'],$post['longitude']);
// 距离多少分钟路程
$details['customer_service_phone'] = ConfigService::get("gzh_setting", "customer_service_phone");
$details['reach_time'] = 30;
$details['store_address'] = Store::where("id", $details['store_id'])->find();
$details['address'] = UserAddress::where("id", $details['address_id'])->find();
$details['pay_way_title'] = "";
if ($details['pay_way'] == 1) {
$details['pay_way_title'] = "余额支付";
}
if ($details['pay_way'] == 2) {
$details['pay_way_title'] = "微信支付";
}
if ($details['pay_way'] == 3) {
$details['pay_way_title'] = "支付宝支付";
}
return $details;
}
public static function cancelOrder($data)
{
return Order::where("id", $data['id'])->update(["order_status" => 2]);
}
public static function delOrder($data)
{
return Order::where("id", $data['id'])->update(["del" => 1]);
}
public static function userConfirmOrder($data)
{
return Order::where("id", $data['id'])->update(["order_status" => 5]);
}
/**
* @notes 茶室订单提交
* @author 胥聪
* @date 2025/10/14 17:01
*/
public static function addStoreOrder($post, $userId)
{
Db::startTrans();
try {
$hours = $post['hours'];
$timeslot = $post['timeslot'];
$day_time = $post['day_time'];
$room_id = $post['room_id'];
$group_coupon_id = $post['group_coupon_id'];
$user_coupon_id = $post['user_coupon_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];
if ($room_msg['hours'] > $post['hours']) {
throw new \Exception('起订小时数不能小于包间规定小时数');
}
$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('所选时间段存在不可选项');
}
$room_all_price = round($room_msg['price'] * $post['hours'], 2);//价格
$user_msg = User::where("id", $userId)->find();
if ($user_msg == null) {
throw new \Exception('用户不存在');
}
$total_price = $hours * $room_msg->price;
$actual_group_price = 0;//门店需要结算的套餐价格
$discount_price = 0;//套餐实际价格
$group_hours = 0;
$coupon_price = 0;//优惠券价格
if ($group_coupon_id > 0) {
$groupData = self::getGroup($group_coupon_id, $userId, $store_id, $room_id);
$actual_group_price = $groupData['actual_group_price'];
$discount_price = $groupData['discount_price'];
$group_hours = $groupData['group_hours'];
$total_hours = $hours - $group_hours;
if ($total_hours <= 0) {
$total_price = 0;
} else {
$total_price = ($hours - $group_hours) * $room_msg->price;
}
}
if ($user_coupon_id > 0) {
$user_coupon = UserCoupon::where([
'id' => $user_coupon_id,
'status' => 0
])->find();
if (!$user_coupon) {
throw new \Exception('当前优惠券不可用');
}
$coupon = UserCouponType::where([
'id' => $user_coupon->coupon_id,
// 'status' => 1,
'type_id' => 2
])->find();
if (!$coupon) {
throw new \Exception('当前优惠券不可用');
}
$use_price = $coupon['use_price'];
$coupon_price = $coupon['coupon_price'];
if ($total_price > $use_price) {
$total_price = $total_price - $coupon_price;
}
}
$member_price = 0;
if ($user_msg->member == 1) {
$member_price = round($total_price - ($total_price * 0.9), 2);
$total_price = $total_price * 0.9;
}
$total_price = round($total_price, 2);
$store_income_price = 0;
if($total_price>0){
$store_income_price = round($total_price - $actual_group_price, 2);
}
$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' => $userId,
'day_title' => $post['day_title'],
'day_time' => $post['day_time'],
'start_time' => $start,
'end_time' => $end,
'timeslot' => $post['timeslot'],
'hours' => $post['hours'],
'group_coupon_id' => $post['group_coupon_id'],
'room_price' => $room_msg['price'],
'room_all_price' => $room_all_price,
'user_coupon_id' => $post['user_coupon_id'],
'coupon_price' => $coupon_price,
'group_price' => $discount_price,
'member_price' => $member_price,
// 'cup_price'=>$post['cup_price'],
'order_amount' => $total_price,
'store_income_price' => $store_income_price,
'dtime' => date("Y-m-d H:i:s")
]);
// type 0为茶艺师 1为茶室支付 2购买套餐 3购买会员 4充值
OrderStore::orderAll("order_store", 1, $order_sn, $store_id, $store_income_price, $userId);
Db::commit();
return $order->id;
} catch (\Exception $e) {
Db::rollback();
self::$error = $e->getMessage();
return false;
}
}
private static function getGroup($group_coupon_id, $userId, $store_id, $room_id)
{
$user_group = UserGroup::where([
'id' => $group_coupon_id,
'status' => 0,
'user_id' => $userId,
'store_id' => $store_id
])->find();
if (!$user_group) {
throw new \Exception('团购套餐券不可用');
}
$store_group = TeaStoreGroup::where('id', $user_group['group_id'])->find();
if (!$store_group) {
throw new \Exception('此团购券不可用,请联系门店内客服');
}
// 将 $roomArr 转换为整数数组
$roomArr = array_map('intval', explode(',', $store_group['room_id']));
$group_room = [0, (int)$room_id];
$hasIntersection = !empty(array_intersect($group_room, $roomArr));
if (!$hasIntersection) {
throw new \Exception('此团购券不适用此包间');
}
$actual_group_price = 0;
if ($user_group->type == 1) {
$actual_group_price = $store_group->discount_price;
}
$group_hours = $store_group->hour;
$discount_price = $store_group->discount_price;
return [
'actual_group_price' => $actual_group_price,
'group_hours' => $group_hours,
'discount_price' => $discount_price
];
}
public static function orderStoreList($post, $userId)
{
$os = "";
if (isset($post['order_status'])) {
if ($post['order_status'] != "" || $post['order_status'] != null) {
$os = "a.order_status = " . $post['order_status'] . "";
}
}
$s = "";
if (isset($post['search'])) {
if ($post['search'] != "" || $post['search'] != null) {
$a = $post['search'];
$s = "c.title like '%" . $a . "%'";
}
}
$count = OrderStore::alias("a")
->where("a.del", 0)
->where('a.user_id', $userId)
->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,b.image,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.user_id', $userId)
->where($os)
->where($s)
->page($post['page'], $post['size'])
->order('a.id desc')
->select();
foreach ($lists as $key => $value) {
$lists[$key]['image'] = FileService::getImgUrl($value['image']);
$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 orderStoreDetails($data)
{
$details = OrderStore::where("id", $data['id'])->find();
$room_msg = TeaStoreRoom::where("id", $details['room_id'])->find();
$room_img = explode(",",$room_msg['img']);
$room_msg['img'] = FileService::getImgUrl($room_img[0]);
$details['room_msg'] = $room_msg;
$store_msg = TeaStore::where("id", $details['store_id'])->find();
$store_msg['image'] = FileService::getImgUrl($store_msg['image']);
$details['store_msg'] = $store_msg;
$details['start_time'] = date("H:i", $details['start_time']);
$details['end_time'] = date("H:i", $details['end_time']);
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', $value);
$r_msg['end_time'] = date('H:i', $value);
$arr[$key] = $r_msg;
}
// $details['renew_dtime'] = $arr;
$details['renew_dtime'] = array_slice($arr, -1)[0];
}
// 订单过期时间
$f = strtotime($details['dtime']) + 30 * 60;
$current_timestamp = time();
$details['time1'] = $f - $current_timestamp;
$details['distance'] = calculateDistanceKm($store_msg['latitude'], $store_msg['longitude'], $data['latitude'], $data['longitude']);
if ($details['pay_way'] == 1) {
$details['pay_way_title'] = "余额支付";
}
if ($details['pay_way'] == 2) {
$details['pay_way_title'] = "微信支付";
}
if ($details['pay_way'] == 3) {
$details['pay_way_title'] = "门店余额支付";
}
return $details;
}
public static function getOrderAmount($data)
{
$details = OrderStore::where("id", $data['id'])->find();
$store_msg = TeaStore::where("id", $details['store_id'])->find();
$details['store_name'] = $store_msg['name'];
// 订单过期时间
$f = strtotime($details['dtime']) + 5 * 60;
$current_timestamp = time();
$details['time1'] = $f - $current_timestamp;
$details['order_amount'] = $details->order_amount;
$details['is_member_price'] = $details->is_member_price;
if($details['is_member_price'] == 0 &&$data['pay_way'] ==3){
$details['order_amount'] = $details->order_amount+$details->member_price;
$details['is_member_price'] = 1;
}elseif ($details['is_member_price'] == 1 &&($data['pay_way'] ==1||$data['pay_way'] ==2)){
$details['order_amount'] = $details->order_amount-$details->member_price;
$details['is_member_price'] = 0;
}
OrderStore::where('id',$details['id'])->update([
'order_amount'=>$details['order_amount'],
'is_member_price'=>$details['is_member_price'],
'store_income_price'=>$details['order_amount'],
'update_dtime'=>date('Y-m-d H:i:s')
]);
return $details;
}
public static function getOrderRenewAmount($data)
{
$details = OrderStoreRenew::where("id", $data['id'])->find();
$details['order_amount'] = $details->price;
$details['is_member_price'] = $details->is_member_price;
if($details['is_member_price'] == 0 &&$data['pay_way'] ==3){
$details['order_amount'] = $details->price+$details->member_price;
$details['is_member_price'] = 1;
}elseif ($details['is_member_price'] == 1 &&($data['pay_way'] ==1||$data['pay_way'] ==2)){
$details['order_amount'] = $details->price-$details->member_price;
$details['is_member_price'] = 0;
}
$details['price'] = $details['order_amount'];
OrderStoreRenew::where('id',$details['id'])->update([
'price'=>$details['order_amount'],
'is_member_price'=>$details['is_member_price'],
'uptime'=>time()
]);
return $details;
}
public static function cancelOrderStore($data)
{
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 userConfirmOrderStore($data)
{
return OrderStore::where("id", $data['id'])->update(["order_status" => 4]);
}
public static function renewDtime($data, $userId)
{
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");
$user =User::where('id',$userId)->find();
$price = round($data['renew_hour'] * $order['room_price'], 2);
$member_price = 0;
if($user->member ==1){
$member_price = round($price - ($price * 0.9), 2);
$price =$price*0.9;
}
$insert = [
'order_sn' => $order_sn,
'user_id' => $userId,
'source_id' => $source_id,
'member_price'=>$member_price,
'timeslot' => implode(',', $extra),
'dtime' => time(),
'hour' => $hour,
'pay_status' => 0,
'price' => $price,
'expire_time' => time() + 60 * 3
];
$id = OrderStoreRenew::insertGetId($insert);
Db::commit();
return [
'order_id' =>$id,
'price'=>$price
];
} catch (\Exception $e) {
Db::rollback();
self::$error = $e->getMessage();
return false;
}
}
public static function addGroupOrder($post)
{
$group = TeaStoreGroup::where("id", $post['group_id'])->find();
$order_sn = createSn("order_group", "order_sn");
$order_amount = $group['discount_price'];
$user = User::where('id',$post['user_id'])->find();
if($user->member == 1){
$order_amount = round($order_amount*0.9,2);
}
$order = OrderGroup::create([
'user_id' => $post['user_id'],
'order_sn' => $order_sn,
'group_id' => $post['group_id'],
'room_id' => $post['room_id'],
'store_id' => $group['store_id'],
'order_amount' => $order_amount,
// 'pay_type'=>$post['pay_type'],
'dtime' => date("Y-m-d H:i:s")
]);
// 0为茶艺师 1为茶室支付 2购买套餐 3购买会员 4充值 5团购退款
OrderStore::orderAll("order_group", 2, $order_sn, $group['store_id'], $group['discount_price'], $post['user_id']);
return [
'id'=>$order->id,
'order_amount'=>$order_amount
];
}
public static function orderGroupList($post, $userId)
{
$s = "";
if (isset($post['search'])) {
if ($post['search'] != "" || $post['search'] != null) {
$a = $post['search'];
$s = "b.title like '%" . $a . "%'";
}
}
$st = "";
if (isset($post['use_status'])) {
if ($post['use_status'] != "" || $post['use_status'] != null) {
$st = "a.status = " . $post['use_status'] . "";
}
}
$o = "";
if (isset($post['operation_type'])) {
if ($post['operation_type'] != "" || $post['operation_type'] != null) {
$o = "c.operation_type = " . $post['operation_type'] . "";
}
}
$t = "";
if (isset($post['type'])) {
if ($post['type'] != "" || $post['type'] != null) {
$t = "a.type = " . $post['type'] . "";
}
}
$count = UserGroup::alias("a")
->join("tea_store_group b", "a.group_id = b.id", "left")
->join("tea_store c", "b.store_id = c.id", "left")
->where('a.user_id', $userId)
->join("order_group d", "a.order_id = d.id", "left")
->where($t)
->where($s)
->where($st)
->where($o)
->count();
$lists = UserGroup::alias("a")
->field('a.*,b.title,b.img,c.name,b.hour,b.room_id,c.operation_type,d.id as order_id')
->join("tea_store_group b", "a.group_id = b.id", "left")
->join("tea_store c", "b.store_id = c.id", "left")
->join("order_group d", "a.order_id = d.id", "left")
->where('a.user_id', $userId)
->where($t)
->where($s)
->where($st)
->where($o)
->page($post['page'], $post['size'])
->order('a.id',"desc")
->select();
foreach ($lists as $key => $value) {
$i = explode(",", $value['img']);
$lists[$key]['image'] = FileService::getFileUrl($i[0]);
$lists[$key]['room_title'] = "";
if ($value['room_id'] == 0) {
$lists[$key]['room_title'] = "通用";
} else {
$room = TeaStoreRoom::where("id", "in", $value['room_id'])->select();
foreach ($room as $k => $v) {
if ($k == 0) {
$lists[$key]['room_title'] .= $v['title'];
} else {
$lists[$key]['room_title'] .= "," . $v['title'];
}
}
}
}
$data = [
'list' => $lists,
'page' => $post['page'],
'size' => $post['size'],
'count' => $count,
'more' => is_more($count, $post['page'], $post['size'])
];
return $data;
}
public static function orderGroupDetails($data)
{
$user_group = UserGroup::where("id", $data['id'])->find();
$details = OrderGroup::where("id", $user_group['order_id'])->find();
if($details){
if ($details['pay_way'] == 1) {
$details['pay_way_title'] = "余额支付";
}
if ($details['pay_way'] == 2) {
$details['pay_way_title'] = "微信支付";
}
if ($details['pay_way'] == 3) {
$details['pay_way_title'] = "支付宝支付";
}
}else{
$details['pay_way_title'] = "抖音核销";
}
$details['user_group'] = $user_group;
if ($details['user_group']['qr_url'] != null & $details['user_group']['qr_url'] != "") {
$details['user_group']['qr_url'] = "https://76458.com/".$details['user_group']['qr_url'];
}
$store_msg = TeaStore::where("id", $user_group['store_id'])->find();
$store_msg['image'] = FileService::getImgUrl($store_msg['image']);
$store_msg['distance'] = calculateDistanceKm($store_msg['latitude'], $store_msg['longitude'], $data['latitude'], $data['longitude']);
$details['store'] = $store_msg;
$group = TeaStoreGroup::where("id", $user_group['group_id'])->find();
$group_img_arr = explode(",", $group['img']);
$group['img'] = FileService::getImgUrl($group_img_arr[0]);
$details['group'] = $group;
$details['room_name'] = "";
if ($group['room_id'] == 0) {
$group['room_name'] = "通用";
} else {
$room = TeaStoreRoom::where("id", "in", $user_group['room_id'])->select();
foreach ($room as $key => $value) {
if ($key == 0) {
$group['room_name'] .= $value['title'];
} else {
$group['room_name'] .= "," . $value['title'];
}
}
}
return $details;
}
public static function teaStoreGroupUseLists($post, $userId)
{
$lists = UserGroup::alias("a")->field("a.id,a.status,b.title,b.type,b.store_id,b.sku_id,b.img,b.hour,a.group_id")
->join("tea_store_group b", "a.group_id=b.id", "left")
->where("a.store_id", $post['store_id'])
->where("a.status", 0)
->where("a.user_id", $userId)
->whereRaw("FIND_IN_SET(0, b.room_id) or FIND_IN_SET(" . $post['room_id'] . ", b.room_id)")
->select();
foreach ($lists as $key => $value) {
$img = explode(",", $value['img']);
$lists[$key]['img'] = FileService::getFileUrl($img[0]);
$lists[$key]['store_msg'] = TeaStore::where("id", $value['store_id'])
->where('status', 1)
->where("del", 0)
->find();
}
$data = [
'list' => $lists
];
return $data;
}
public static function countPrice($data, $userId)
{
$user_msg = User::where("id", $userId)->find();
$room_msg = TeaStoreRoom::where("id", $data['room_id'])->find();
$arr['discount_room_price'] = 0;
$arr['room_price'] = round($room_msg['price'] * $data['nums'], 2);
$arr['nums'] = $data['nums'];
// 优惠券
$coupon_user_msg = UserCoupon::where("id", $data['coupon_id'])->find();
$arr['coupon_price'] = 0;
$discount = ConfigService::get("member", "discount");
if ($coupon_user_msg != null) {
$coupon_msg = UserCouponType::where("id", $coupon_user_msg['coupon_id'])->find();
$arr['coupon_price'] = $coupon_msg['coupon_price'];
}
// 团购券价格
$arr['group_price'] = 0;
$user_group = UserGroup::where("id", $data['group_coupon_id'])->find();
if($user_group!=null){
$order_group_msg = TeaStoreGroup::where("id", $user_group['group_id'])->find();
if ($order_group_msg != null) {
$arr['group_price'] = round($order_group_msg['hour']*$room_msg['price'] , 2);
}
}
if ($user_msg['member'] == 1) {
$arr['discount_room_price'] = round(($arr['room_price']-$arr['group_price']- $arr['coupon_price']) * (1 - $discount), 2);
}
if ($user_msg['member'] == 1) {
$arr['order_amount'] = round(($arr['room_price'] - $arr['group_price'] - $arr['coupon_price']) * $discount, 2);
} else {
$arr['order_amount'] = round($arr['room_price'] - $arr['group_price'] - $arr['coupon_price'], 2);
}
if ($arr['order_amount'] < 0) {
$arr['order_amount'] = 0;
}
$arr['discount_all_price'] = round($arr['discount_room_price'] + $arr['coupon_price'] + $arr['group_price'], 2);
$data = [
'details' => $arr
];
return $data;
}
public static function transferOrder(array $post, int $userId)
{
Db::startTrans();
try {
// 1. 验证原订单
$sourceOrder = OrderStore::find($post['order_id']);
if (!$sourceOrder) {
throw new \Exception('原订单不存在');
}
// 2. 检查订单是否已转让
if ($sourceOrder->is_transfer == 1) {
// 如果已经是转让订单,找到原始订单
$originalOrderId = $sourceOrder->transfer_order_id;
// 检查用户是否已接收过此订单
$existsTransfer = OrderStore::where([
"transfer_order_id" => $originalOrderId,
'user_id' => $userId
])->find();
if ($existsTransfer) {
throw new \Exception('此账号已接收过此账单');
}
$targetOrderId = $originalOrderId;
} else {
// 原始订单,检查用户是否已接收过
$existsTransfer = OrderStore::where([
"transfer_order_id" => $sourceOrder->id,
'user_id' => $userId
])->find();
if ($existsTransfer) {
throw new \Exception('此账号已接收过此账单');
}
$targetOrderId = $sourceOrder->id;
}
$countOrder = OrderStore::where('transfer_order_id', $targetOrderId)->count();
if ($countOrder > 10) {
throw new \Exception('此订单接收人数已超上限');
}
// 3. 创建新订单记录(转让记录)
$newOrderSn = createSn("order_store", "order_sn");
$sourceOrder->user_id = $userId;
$sourceOrder->dtime = date('Y-m-d H:i:s'); // 使用完整时间格式
$sourceOrder->remark = '转让订单';
$sourceOrder->transfer_order_id = $targetOrderId;
$sourceOrder->is_transfer = 1;
$sourceOrder->order_sn = $newOrderSn;
$data = $sourceOrder->toArray();
unset($data['id']);
// 保存新订单
$newOrderId = OrderStore::insertGetId($data);
if (!$newOrderId) {
throw new \Exception('创建转让订单失败');
}
Db::commit();
return [
'order_id' => $newOrderId,
];
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function addRechargeOrder(array $post, int $userId)
{
Db::startTrans();
try {
$id = $post['id'];
$store_recharge = TeaStoreRecharge::where([
'id'=>$id,
'del'=>0
])->find();
if(!$store_recharge){
throw new \Exception('此套餐不存在');
}
$data = [
'recharge_id'=>$store_recharge->id,
'order_sn'=>createSn("order_store_recharge", "order_sn"),
'user_id'=>$userId,
'store_id'=>$store_recharge->store_id,
'recharge_price'=>$store_recharge->price,
'gift_price'=>$store_recharge->gift_price,
'order_amount'=>$store_recharge->price,
'create_time'=>time()
];
$recharge_order = OrderStoreRecharge::insertGetId($data);
Db::commit();
return [
'order_id' => $recharge_order,
'order_amount'=>$store_recharge->price
];
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function submitRefund($post,$user_id){
// 0为茶艺师 1为茶室支付 2购买套餐 3购买会员 4充值 5团购退款 6茶室退款
$id = 0;
if($post['order_type'] == 5){
$id = $post['id'];
$user_group = UserGroup::where("id",$post['id'])->find();
$post["order_id"] = $user_group["order_id"];
$order = OrderGroup::where("id",$user_group["order_id"])->find();
}
$recordSn = generate_sn(RefundRecord::class, 'order_sn');
$record = RefundRecord::create([
'order_sn' => $recordSn,
'user_id' => $user_id,
'store_id'=>$order["store_id"],
'order_id' => $post['order_id'],
'content_id'=>$id,//关联相关id
'source_sn' => $order['order_sn'],
'order_type' => $post['order_type'],
'order_amount' => $order['order_amount'],
'refund_amount' => $order['order_amount'],
'refund_type' => 1,
'refund_way' => 1
// 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
]);
OrderStore::orderAll("refund_record",5,$recordSn,$order["store_id"],$order['order_amount'],$user_id);
return $record->id;
}
}