提交其他文件

This commit is contained in:
2026-03-14 16:20:49 +08:00
parent a227deaecd
commit 0a19b334f8
1385 changed files with 73568 additions and 0 deletions

View File

@ -0,0 +1,500 @@
<?php
namespace app\storeapi\logic;
use app\api\controller\AccountLogController;
use app\common\logic\BaseLogic;
use app\common\model\teastore\TeaStore;
use app\common\model\teastore\TeaStoreGroup;
use app\common\model\teastore\TeaStoreRoom;
use app\common\model\teastore\TeaStoreRoom as roomModel;
use app\common\model\user\UserGroup;
use app\common\model\store\StoreUserAccountLog;
use app\common\model\order\OrderGroup;
use app\common\service\FileService;
use think\facade\Db;
class GroupLogic extends BaseLogic
{
public static function addGroup($params){
Db::startTrans();
try {
$store = TeaStore::where([
'id' => $params['store_id'],
'del' => 0
])->find();
if(!$store){
throw new \Exception('门店不存在');
}
if (!in_array($params['type'], [1, 2])) {
throw new \Exception('套餐类型错误');
}
if($params['price']<$params['discount_price']){
throw new \Exception('门市价不能小于团购价');
}
$discount = $params['discount_price']/$params['price'];
$data = [
'store_id'=>$params['store_id'],
'type'=>$params['type'],
'sku_id'=>$params['sku_id'],
'title'=>$params['title'],
'img'=>$params['img'],
'hour'=>$params['hour'],
'description'=>$params['description'],
'introduce'=>$params['introduce'],
'storepl_number'=>$params['pl_number'],
'rests_introduce'=>$params['rests_introduce'],
'price'=>$params['price'],
'sold'=>$params['sold'],
'pl_number'=>$params['pl_number'],
'discount_price'=>$params['discount_price'],
'returd_details'=>$params['returd_details'],
'discount'=>$discount,
'room_id'=>$params['room_id'],
'status'=>$params['status'],
'dtime'=>time()
];
TeaStoreGroup::create($data);
Db::commit();
return [];
}catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function editGroup($params){
Db::startTrans();
try {
// 1. 查找记录
$group = TeaStoreGroup::find($params['group_id']);
if (!$group) {
throw new \Exception('团购套餐不存在');
}
// 2. 基础验证
if (isset($params['type']) && !in_array($params['type'], [1, 2])) {
throw new \Exception('套餐类型错误');
}
// 3. 验证门店(如果修改门店)
if (isset($params['store_id'])) {
$storeExists = TeaStore::where([
'id' => $params['store_id'],
'del' => 0
])->find();
if (!$storeExists) {
throw new \Exception('门店不存在');
}
}
// 4. 价格验证
$price = $params['price'] ?? $group->price;
$discountPrice = $params['discount_price'] ?? $group->discount_price;
if ($price < $discountPrice) {
throw new \Exception('门市价不能小于团购价');
}
// 5. 准备更新数据
$updateData = [];
$fields = ['store_id', 'type', 'sku_id', 'title', 'img', 'hour',
'description', 'introduce', 'rests_introduce', 'price','pl_number','discount_price',
'returd_details', 'room_id', 'status','sold'];
foreach ($fields as $field) {
if (isset($params[$field])) {
$updateData[$field] = $params[$field];
}
}
// 计算折扣
if (isset($params['price']) || isset($params['discount_price'])) {
if ($params['price'] != 0 && $params['price'] > 0) {
$updateData['discount'] = round(($discountPrice / $params['price']) * 10, 1);
} else {
$updateData['discount'] = 0;
}
}
$updateData['update_dtime'] =time();
TeaStoreGroup::where("id",$params['group_id'])->update($updateData);
// 6. 更新记录
// $group->save($updateData);
Db::commit();
return [];
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function groupLists($params){
// 验证状态参数
$status = $params['status'];
if (!in_array($params['status'], [0, 1, 2])) {
throw new \Exception('状态参数错误');
}
// 构建查询条件
$query = TeaStoreGroup::where('del', 0)
->where([
'status'=> $status,
'store_id'=>$params['store_id']
]);
// 添加标题模糊查询如果传入了search参数
if (!empty($params['search'])) {
$query->where('title', 'like', '%' . $params['search'] . '%');
}
// 查询并分页
$list = $query->order('dtime', 'desc')
->paginate([
'page' => $params['page'],
'list_rows' => $params['size']
]);
$statusCount = self::getGroupStatusCount($params['store_id']);
// 处理列表数据中的图片
$items = $list->items();
foreach ($items as &$item) {
// 处理img字段分割为数组并拼接URL
$item['img_list'] = explode(",",$item['img']);
$img = [];
foreach($item['img_list'] as $k=>$v){
$img[] = FileService::getImgUrl($v);
}
$item['img_list'] = $img;
// 添加主图(第一张)
$item['main_image'] = !empty($item['img_list']) ? $item['img_list'][0] : '';
// 图片数量
$item['img_count'] = count($item['img_list']);
$item['code'] = $item['id'];
$item['discount'] = round($item['discount'],2);
// 保留原始img字段可选
// $item['img'] 保持原样
}
// 返回分页数据
return [
'offline'=>$statusCount['offline'],
'online'=>$statusCount['online'],
'draft'=>$statusCount['draft'],
'list' => $list->items(),
'total' => $list->total(),
'per_page' => $list->listRows(),
'current_page' => $list->currentPage(),
'last_page' => $list->lastPage(),
'more' => is_more($list->total(), $params['page'], $params['size'])
];
}
/**
* 获取套餐统计(三个状态的计数)
* @return array 统计结果
*/
public static function getGroupStatusCount($store_id)
{
$counts = TeaStoreGroup::where('del', 0)
->where('store_id',$store_id)
->field('status, COUNT(*) as count')
->group('status')
->select()
->toArray();
// 格式化结果
$result = [
'offline' => 0, // 已下架
'online' => 0, // 已上架
'draft' => 0, // 草稿箱
'total' => 0 // 总数
];
foreach ($counts as $count) {
switch ($count['status']) {
case 0:
$result['offline'] = $count['count'];
break;
case 1:
$result['online'] = $count['count'];
break;
case 2:
$result['draft'] = $count['count'];
break;
}
$result['total'] += $count['count'];
}
return $result;
}
/**
* 格式化图片列表
* @param string $imgString 图片字符串,逗号分隔
* @return array 拼接完整URL的图片数组
*/
private static function formatImageList($imgString)
{
if (empty($imgString)) {
return [];
}
// 用逗号分割字符串
$images = explode(',', $imgString);
// 过滤空值
$images = array_filter($images);
if (empty($images)) {
return [];
}
// 去除空白字符
$images = array_map('trim', $images);
// 获取当前域名
$domain = request()->domain();
$result = [];
foreach ($images as $image) {
// 如果已经是完整URL直接返回
if (strpos($image, 'http://') === 0 || strpos($image, 'https://') === 0) {
$result[] = $image;
continue;
}
// 移除开头的斜杠
$image = ltrim($image, '/');
// 拼接完整URL假设图片放在uploads目录
$result[] = $domain . '/' . $image;
}
return $result;
}
public static function delGroup($params){
Db::startTrans();
try {
// 1. 验证记录存在性且未删除
$group = TeaStoreGroup::where([
'id' => $params['group_id'],
'del' => 0 // 只查找未删除的记录
])->find();
if (!$group) {
throw new \Exception('团购套餐不存在或已被删除');
}
// 3. 执行逻辑删除
$group->del = 1;
$group->update_dtime = time();
$result = $group->save();
if ($result === false) {
throw new \Exception('删除失败');
}
Db::commit();
return [];
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function operateGroup($params){
Db::startTrans();
try {
// 1. 验证记录存在性且未删除
$group = TeaStoreGroup::where([
'id' => $params['group_id'],
'del' => 0 // 只查找未删除的记录
])->find();
if (!$group) {
throw new \Exception('团购套餐不存在或已被删除');
}
// 3. 执行逻辑删除
$group->status = $params['status'];
$group->update_dtime = time();
$result = $group->save();
if ($result === false) {
throw new \Exception('更新失败');
}
Db::commit();
return [];
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function groupDetails($params){
try{
$group = TeaStoreGroup::where('id', $params['group_id'])
->where('del', 0)
->find();
if (!$group) {
throw new \Exception('套餐不存在或已被删除');
}
$data = $group->toArray();
// $domain = request()->domain();
// 处理多图
$data['img_list'] = self::getImageList($data['img']);
foreach ($data['img_list'] as &$items){
// $fullImages[] = $domain . '/' . $items;
$fullImages[] = FileService::getImgUrl($items);
}
$data['img_list']= $fullImages;
// 添加状态文本
$data['status_text'] = self::getStatusText($data['status']);
$room_id = $data['room_id'];
$data['room_title'] = '';
if($room_id == 0){
$data['room_title'] = '通用';
}else{
$roomData = explode(',',$room_id);
$room = TeaStoreRoom::whereIn('id', $roomData)->field('title')
->select()
->toArray();
$data['room_title'] = implode(',', array_column($room, 'title'));
}
return $data;
}catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
// 查询套餐
}
public static function cancelCode($params){
$qr_sn =$params['qr_sn'];
$store_id =$params['store_id'];
Db::startTrans();
try {
$user_group = UserGroup::where('qr_sn',$qr_sn)->find();
if(!$user_group){
throw new \Exception('券码错误,无效券码');
}
if($user_group['status'] !=0){
throw new \Exception('已被核销或已过期');
}
$group_order = OrderGroup::where('id',$user_group['order_id'])->find();
if(!$group_order){
throw new \Exception('核销错误请联系客服');
}
if($group_order['store_id'] !=$store_id){
throw new \Exception('此券码不属于当前门店');
}
$order_sn = $group_order['order_sn'];
$store = TeaStore::where([
'id'=>$store_id,
'del'=>0
])->find();
if(!$store){
throw new \Exception('门店不存在');
}
$balance = $store['balance'];
$after_amount = $balance+$group_order['order_amount'];
// 方法1使用随机数组合
$part1 = mt_rand(100000, 999999); // 6位
$part2 = mt_rand(100000, 999999); // 6位
$part3 = mt_rand(100000, 999999); // 6位
$sn = $part1 . $part2 . $part3;
// 组合成18位
$number = $part1 . $part2 . $part3;
$accountData = [
'sn'=>$sn,
'user_id'=>$params['user_id'],
'change_object'=>4,
'change_type'=>'4',
'action'=>'1',
'amount'=>$group_order['order_amount'],
'before_amount'=>$after_amount,
'source_sn'=>$order_sn,
'store_id'=>$store_id,
'room_id'=>'0',
'remark'=>'门店核销',
'create_time'=>time(),
'status'=>'1'
];
StoreUserAccountLog::create($accountData);
$store->balance = $after_amount;
$store->save();
$user_group->status = 1;
$user_group->update_dtime = date('Y-m-d H:i');
$user_group->save();
Db::commit();
return [];
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
private static function getImageList($imgString)
{
if (empty($imgString)) {
return [];
}
// 用逗号分隔字符串
$images = explode(',', $imgString);
// 去除空值和空白字符
$images = array_filter(array_map('trim', $images));
return array_values($images); // 重新索引数组
}
private static function getStatusText($status)
{
$statusMap = [
0 => '已下架',
1 => '已上架',
2 => '草稿箱'
];
return $statusMap[$status] ?? '未知状态';
}
}