Files
anmo/server/app/api/logic/ShopLogic.php
2025-08-19 14:16:51 +08:00

190 lines
7.8 KiB
PHP
Executable File

<?php
namespace app\api\logic;
use app\common\enum\GoodsEnum;
use app\common\enum\PayEnum;
use app\common\enum\shop\ShopEnum;
use app\common\model\coach\Coach;
use app\common\model\coach\Collect;
use app\common\model\goods\Goods;
use app\common\model\goods\GoodsCategory;
use app\common\model\goods\GoodsComment;
use app\common\model\IndexVisit;
use app\common\model\order\Order;
use app\common\model\shop\Shop;
use app\common\model\shop\ShopCoachApply;
use app\common\model\shop\ShopVisit;
use app\common\model\user\User;
use app\common\service\FileService;
class ShopLogic
{
/**
* @notes 商家详情
* @param $params
* @param $userInfo
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author cjhao
* @date 2024/11/19 12:14
*/
public function detail($params,$userInfo)
{
$field = 'id,name,good_comment,logo,work_status,province_id,region_id,city_id,shop_address_detail,longitude,latitude,monday,tuesday,wednesday,thursday,friday,saturday,sunday,business_start_time,business_end_time,synopsis,business_license,mobile';
if (!empty($params['longitude']) && !empty($params['latitude'])) {
$field .= ',round(st_distance_sphere(point('.$params['longitude'].','.$params['latitude'].'),
point(longitude, latitude))/1000,2) as distance';
}
$detail = Shop::where(['id'=>$params['id'],'audit_status'=>ShopEnum::AUDIT_STATUS_PASS,'server_status'=>ShopEnum::SERVERSTATUSOPEN])
->with(['shop_image'])
->field($field)
->append(['distance_desc','category_ids','work_status_desc','region_desc','business_time_desc'])
->findOrEmpty();
$ids = array_column($detail->category_ids->toArray(),'category_id');
$categoryLists = GoodsCategory::where(['is_show'=>1,'id'=>$ids])->column('name');
$detail['category_name'] = implode('|',$categoryLists);
if (!is_array($detail['category_name'])) {
$detail['category_name'] = [$detail['category_name']];
}
$week = strtolower(date('l'));
$shopWeek = $detail[$week] ?? 0;
if(!$shopWeek){
$detail['work_status'] = 0;
$detail['work_status_desc'] = ShopEnum::getWorkStatus($detail['work_status']);
}else{
$nowHi = date('H:i');
$detail['business_start_time'] = format_business_time($detail['business_start_time']);
$detail['business_end_time'] = format_business_time($detail['business_end_time']);
if($detail['business_start_time'] > $nowHi || $detail['business_end_time'] < $nowHi){
$detail['work_status'] = 0;
$detail['work_status_desc'] = ShopEnum::getWorkStatus($detail['work_status']);
}
}
$consumption = Order::where(['pay_status'=>PayEnum::ISPAID])
->where('shop_id','=',$params['id'])
->value('round(sum(order_amount)/count(id),2) as consumption');
$detail['consumption'] = $consumption ? : 0;
$map1 = [
['SGI.shop_id','=',$params['id']],
];
$detail['goods_lists'] = Goods::alias('G')
->leftjoin('shop_goods_index SGI','G.id = SGI.goods_id')
->whereRaw('(SGI.shop_id ='. $params['id'].') or (G.shop_id = '.$params['id'].' and G.audit_status = '.GoodsEnum::AUDIT_STATUS_PASS .')')
->field('G.id,name,image,price,scribing_price,duration,order_num')
->limit(8)
->select()
->toArray();
$coachIds = ShopCoachApply::where(['shop_id'=>$params['id'],'audit_status'=>ShopEnum::AUDIT_STATUS_PASS])->column('coach_id');
$commentLists = GoodsComment::where(['coach_id'=>$coachIds])
->field('id,user_id,service_comment,comment,reply')
->append(['goods_comment_image'])
->limit(5)
->select()
->toArray();
$userIds = array_column($commentLists,'user_id');
$userLists = User::where(['id'=>$userIds])->column('nickname,avatar','id');
foreach ($commentLists as $key => $comment_list){
$commentLists[$key]['nickname'] = $userLists[$comment_list['user_id']]['nickname'] ?? '';
$commentLists[$key]['avatar'] = FileService::getFileUrl($userLists[$comment_list['user_id']]['avatar'] ?? '');
}
$detail['comment_lists'] = $commentLists;
if(!empty($params['terminal'])){
$ip = request()->ip();
// 一个ip一个终端一天只生成一条记录
$record = ShopVisit::where([
'shop_id' => $params['id'],
'ip' => $ip,
'terminal' => $params['terminal']
])->whereDay('create_time')->findOrEmpty();
if (!$record->isEmpty()) {
// 增加访客在终端的浏览量
$record->visit += 1;
$record->save();
}
// 生成访客记录
ShopVisit::create([
'shop_id' => $params['id'],
'ip' => $ip,
'terminal' => $params['terminal'],
'visit' => 1
]);
}
$detail['is_collect'] = Collect::where(['relation_id'=>$params['id'],'user_id'=>$userInfo['user_id'] ?? 0,'type'=>3])->findOrEmpty()->toArray() ? 1 : 0;
return $detail->toArray();
}
public function commentCategory($shopId)
{
$coachIds = Coach::where(['shop_id'=>$shopId])->field('id')->select()->toArray();
$coachIds = array_column($coachIds,'id');
$orderGoodsIds = Order::alias('O')
->join('order_goods OG','O.id = OG.order_id')
->where(['coach_id'=>$coachIds,'is_comment'=>1])
->field('OG.id')
->select()
->toArray();
$orderGoodsIds = array_column($orderGoodsIds,'id');
$all_count = GoodsComment::where('order_goods_id','in', $orderGoodsIds)->count();
$image_count = GoodsComment::alias('gc')->where('order_goods_id','in',$orderGoodsIds)->join('goods_comment_image gci', 'gc.id = gci.comment_id')->group('gci.comment_id')->count();
$good_count = GoodsComment::where('order_goods_id', 'in',$orderGoodsIds)->where('service_comment','>',3)->count();
$medium_bad_count = GoodsComment::where('order_goods_id', 'in',$orderGoodsIds)->where('service_comment','<=',3)->count();
if($all_count == 0) {
$percentStr = '100%';
$star = 5;
}else {
$percent = round((($good_count / $all_count) * 100));
$percentStr = round((($good_count / $all_count) * 100)).'%';
if ($percent >= 100) {
$star = 5;
} else if ($percent >= 80) {
$star = 4;
} else if ($percent >= 60) {
$star = 3;
} else if ($percent >= 40) {
$star = 2;
} else if ($percent >= 20) {
$star = 1;
} else {
$star = 0;
}
}
return ['comment'=>
[
[
'id' => 0,
'name' => '全部',
'count' => $all_count
],
[
'id' => 1,
'name' => '有图',
'count' => $image_count
],
[
'id' => 2,
'name' => '好评',
'count' => $good_count
],
[
'id' => 3,
'name' => '中差评',
'count' => $medium_bad_count
]
] ,
'percent' => $percentStr,
'star' => $star,
];
}
}