1216 lines
50 KiB
PHP
Executable File
1216 lines
50 KiB
PHP
Executable File
<?php
|
|
|
|
|
|
namespace app\api\logic;
|
|
|
|
use app\common\model\distribution\Distribution;
|
|
use app\common\model\distribution\DistributionLevel;
|
|
use app\common\model\goods\GoodsCategory;
|
|
use app\common\model\goods\GoodsSubsidy;
|
|
use app\common\model\order\Order;
|
|
use app\common\model\procurement\Mprocurement;
|
|
use app\common\model\procurement\Procurementset;
|
|
use app\common\model\shop\ShopFollow;
|
|
use app\common\model\user\User;
|
|
use app\common\basics\Logic;
|
|
use app\common\enum\FootprintEnum;
|
|
use app\common\model\distribution\DistributionGoods;
|
|
use app\common\model\goods\Goods;
|
|
use app\common\model\goods\GoodsCollect;
|
|
use app\common\model\goods\GoodsClick;
|
|
use app\common\model\goods\GoodsSpec;
|
|
use app\common\model\goods\GoodsComment;
|
|
use app\common\model\goods\GoodsCommentImage;
|
|
use app\common\model\SearchRecord;
|
|
use app\common\enum\GoodsEnum;
|
|
use app\common\model\seckill\SeckillGoods;
|
|
use app\common\model\shop\Shop;
|
|
use app\common\model\team\TeamActivity;
|
|
use app\common\model\team\TeamFound;
|
|
use app\common\model\team\TeamGoods;
|
|
use app\common\model\user\UserLevel;
|
|
use app\common\model\groupbuy\GroupbuyGoods;
|
|
use app\common\server\ConfigServer;
|
|
use app\common\server\UrlServer;
|
|
use think\facade\Db;
|
|
|
|
|
|
class GoodsLogic extends Logic
|
|
{
|
|
/**
|
|
* 商品详情
|
|
*/
|
|
public static function getGoodsDetail($goodsId, $userId,$p_id,$set_id)
|
|
{
|
|
//获取用户折扣
|
|
$discount = 10;
|
|
if($userId){
|
|
$user = User::where('id', $userId)->find();
|
|
if($user && isset($user['level'])){
|
|
$user_discount = UserLevel::where('id', $user['level'])->value('discount');
|
|
if($user_discount && $user_discount > 0 && $user_discount <= 10){
|
|
$discount = $user_discount;
|
|
}
|
|
}
|
|
}
|
|
|
|
$onSaleWhere = "";
|
|
$group_msg = Db::name("groupbuy_goods")->where("goods_id",$goodsId)->where("del",1)->where("status",1)->find();
|
|
if($group_msg==null){
|
|
// 销售中商品:未删除/审核通过/已上架
|
|
$onSaleWhere = [
|
|
'del' => GoodsEnum::DEL_NORMAL, // 未删除
|
|
'status' => GoodsEnum::STATUS_SHELVES, // 上架中
|
|
'audit_status' => GoodsEnum::AUDIT_STATUS_OK, // 审核通过
|
|
];
|
|
|
|
}
|
|
|
|
$goodsDetail = Goods::with(['goods_image', 'goods_item', 'shop'])
|
|
->field('id,type,name,image,video,remark,goods_remark,content,purchase_price,issuing_price,wholesale_price,market_price,min_price,max_price,is_show_stock,stock,sales_actual,sales_virtual,clicks,clicks_virtual,shop_id,poster,subsidy_id,presale,presale_mask')
|
|
->where($onSaleWhere)
|
|
->where('id', $goodsId)
|
|
->findOrEmpty();
|
|
// $goodsDetail['min_price'] = round(0.88*$goodsDetail['min_price'],2);
|
|
if ($goodsDetail->isEmpty()) {
|
|
self::$error = '商品已下架';
|
|
return false;
|
|
}
|
|
|
|
Db::startTrans();
|
|
try{
|
|
// 获取返补比例, subsidy_id为0的就传递一个ID为4的固定ID
|
|
$goodsDetail['subsidy'] = (new GoodsSubsidy())->getSubsidyById($goodsDetail['subsidy_id'] > 0 ? $goodsDetail['subsidy_id'] : 4);
|
|
$goodsDetail['subsidy_txt'] = '最高返' . $goodsDetail['subsidy']['subsidy'];
|
|
|
|
// 轮播图加域名
|
|
foreach($goodsDetail['goods_image'] as &$item) {
|
|
$item['uri'] = empty($item['uri']) ? '' : UrlServer::getFileUrl($item['uri']);
|
|
}
|
|
// 会员价
|
|
$goodsDetail['member_price'] = 0;
|
|
// 会员价数组
|
|
$member_price = [];
|
|
foreach ($goodsDetail['goods_item'] as &$goods_item) {
|
|
$is_member = Goods::where('id',$goods_item['goods_id'])->value('is_member');
|
|
$goods_item['is_member'] = $is_member;
|
|
if($is_member == 1 && $discount && $userId){
|
|
$goods_item['member_price'] = round($goods_item['price']* $discount/10,2);
|
|
$goodsDetail['member_price'] = round($goods_item['price']* $discount/10,2);
|
|
$member_price[] = $goodsDetail['member_price'];
|
|
}
|
|
// 规格图片处理
|
|
$goods_item['image'] = empty($goods_item['image']) ? $goodsDetail['image'] : $goods_item['image'];
|
|
}
|
|
|
|
!empty($member_price) && $goodsDetail['member_price'] = min($member_price);
|
|
|
|
// 增加点击量
|
|
$goodsDetail->clicks += 1;
|
|
$goodsDetail->save();
|
|
|
|
// 转数组
|
|
$goodsDetailArr = $goodsDetail->toArray();
|
|
$goodsDetailArr['poster'] = !empty($goodsDetailArr['poster']) ? UrlServer::getFileUrl($goodsDetailArr['poster']) : '';
|
|
foreach ($goodsDetailArr['goods_item'] as &$valItem){
|
|
$valItem['limited_num'] = 0;
|
|
}
|
|
// 新增点击记录
|
|
GoodsClick::create([
|
|
'shop_id' => $goodsDetailArr['shop_id'],
|
|
'user_id' => $userId,
|
|
'goods_id' => $goodsId,
|
|
'create_time' => time()
|
|
]);
|
|
//店铺信息
|
|
switch ($goodsDetailArr['shop']['type']){
|
|
case 1 :
|
|
$type_desc = '官方自营';
|
|
break;
|
|
case 2 :
|
|
$type_desc = '入驻商家';
|
|
break;
|
|
default :
|
|
$type_desc = '入驻商家';
|
|
break;
|
|
}
|
|
$follow = Db::name('shop_follow')->where(['shop_id' => $goodsDetailArr['shop_id'],'status' => 1])->count('id');
|
|
$goodsDetailArr['shop']['type_desc'] = $type_desc; //商家类型
|
|
$goodsDetailArr['shop']['follow_num'] = $follow; //收藏人数
|
|
|
|
//客服二维码
|
|
$customer_image = ConfigServer::get('shop_customer_service','image','',$goodsDetailArr['shop_id']);
|
|
if($customer_image){
|
|
$customer_image = UrlServer::getFileUrl($customer_image);
|
|
}
|
|
$goodsDetailArr['shop']['customer_image'] = $customer_image;
|
|
// 用户是否关注店铺
|
|
$goodsDetailArr['shop']['shop_follow_status'] = 0;
|
|
if($userId) { // 用户已登录
|
|
$shopFollow = ShopFollow::where(['user_id'=>$userId, 'shop_id'=>$goodsDetailArr['shop_id']])->findOrEmpty();
|
|
if(!$shopFollow->isEmpty()) {
|
|
$goodsDetailArr['shop']['shop_follow_status'] = $shopFollow['status'];
|
|
}
|
|
}
|
|
|
|
// 店铺在售商品数量
|
|
$goodsDetailArr['shop']['goods_on_sale'] = Goods::where($onSaleWhere)
|
|
->where('shop_id', $goodsDetailArr['shop_id'])
|
|
->count();
|
|
$set = null;
|
|
$w = [];
|
|
$settpye = 0;
|
|
if($p_id != 0&&$set_id!=0){
|
|
$set = Procurementset::where("set_id",$set_id)->where("set_state",1)->find();
|
|
if($set['set_pro']!=0){
|
|
$w[] = ['purchase_price', '>', 0];
|
|
}else if($set['set_issued']!=0){
|
|
$w[] = ['issuing_price', '>', 0];
|
|
}else if($set['set_wholesale']!=0){
|
|
$w[] = ['wholesale_price', '>', 0];
|
|
}
|
|
}
|
|
if($p_id != 0&&$set_id==0){
|
|
$settpye = 1;
|
|
$set = Procurementset::where("u_id",$p_id)->where("set_state",1)->order("set_update desc")->limit(1)->find();
|
|
$w[] = ['purchase_price|issuing_price|wholesale_price', '>', 0];
|
|
}
|
|
// 店铺推荐商品列表(9个)
|
|
$goodsDetailArr['shop']['goods_list'] = Goods::field('id,name,image,market_price,min_price,market_price,purchase_price,issuing_price,wholesale_price, subsidy_id')
|
|
->where($onSaleWhere)
|
|
->where($w)
|
|
->where([
|
|
'shop_id' => $goodsDetailArr['shop_id'],
|
|
'is_recommend' => 1, // 推荐
|
|
])
|
|
->order([
|
|
'sales_actual' => 'desc',
|
|
'id' => 'desc'
|
|
])
|
|
->limit(9)
|
|
->select()
|
|
->toArray();
|
|
|
|
foreach ($goodsDetailArr['shop']['goods_list'] as $key => $value) {
|
|
// 获取返补比例, subsidy_id为0的就传递一个ID为4的固定ID
|
|
$goodsDetailArr['shop']['goods_list'][$key]['subsidy'] = (new GoodsSubsidy())->getSubsidyById($value['subsidy_id'] > 0 ? $value['subsidy_id'] : 4);
|
|
$goodsDetailArr['shop']['goods_list'][$key]['subsidy_txt'] = '最高返' . $goodsDetailArr['shop']['goods_list'][$key]['subsidy']['subsidy'];
|
|
}
|
|
|
|
if ($set != null&&$settpye!=1) {
|
|
foreach ($goodsDetailArr['shop']['goods_list'] as $key => $value) {
|
|
if ($set['set_pro'] != 0) {
|
|
if ($set['set_pro_type'] == 1) {
|
|
$goodsDetailArr['shop']['goods_list'][$key]['purchase_price'] = round($value['purchase_price'] * (100 + $set['set_pro']) / 100, 2);
|
|
}
|
|
if ($set['set_pro_type'] == 3) {
|
|
$goodsDetailArr['shop']['goods_list'][$key]['purchase_price'] = round($value['purchase_price'] + $set['set_pro'], 2);
|
|
}
|
|
}
|
|
if ($set['set_issued'] != 0) {
|
|
if ($set['set_issued_type'] == 1) {
|
|
$goodsDetailArr['shop']['goods_list'][$key]['issuing_price'] = round($value['issuing_price'] * (100 + $set['set_issued']) / 100, 2);
|
|
}
|
|
if ($set['set_issued_type'] == 3) {
|
|
$goodsDetailArr['shop']['goods_list'][$key]['issuing_price'] = round($value['issuing_price'] + $set['set_issued'], 2);
|
|
}
|
|
}
|
|
if ($set['set_wholesale'] != 0) {
|
|
if ($set['set_wholesale_type'] == 1) {
|
|
$goodsDetailArr['shop']['goods_list'][$key]['wholesale_price'] = round($value['wholesale_price'] * (100 + $set['set_wholesale']) / 100, 2);
|
|
}
|
|
if ($set['set_wholesale_type'] == 3) {
|
|
$goodsDetailArr['shop']['goods_list'][$key]['wholesale_price'] = round($value['wholesale_price'] + $set['set_wholesale'], 2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// 总销量 = 实际销量 + 虚拟销量
|
|
$goodsDetailArr['sales_sum'] = $goodsDetailArr['sales_actual'] + $goodsDetailArr['sales_virtual'];
|
|
// 标识活动信息
|
|
$goodsDetailArr['activity'] = [
|
|
'type' => 0,
|
|
'type_desc' => '普通商品'
|
|
];
|
|
|
|
// 检查商品是否在参与活动,替换商品价格
|
|
$goodsDetailArr = self::checkActivity($goodsDetailArr);
|
|
// 检查商品是否参与抢购活动,替换商品价格、库存。
|
|
$goodsDetailArr = self::rushBuy($goodsDetailArr, $userId);
|
|
// 是否收藏
|
|
$goodsDetailArr['is_collect'] = 0;
|
|
if($userId) { // 非游客
|
|
$goodsCollect = GoodsCollect::where([
|
|
'user_id' => $userId,
|
|
'goods_id' => $goodsId
|
|
])->findOrEmpty();
|
|
if(!$goodsCollect->isEmpty()) {
|
|
$goodsDetailArr['is_collect'] = $goodsCollect->status ? 1 : 0;
|
|
}
|
|
}
|
|
// 规格项及规格值信息
|
|
$goodsDetailArr['goods_spec'] = GoodsSpec::with('spec_value')
|
|
->where('goods_id', $goodsId)->select();
|
|
$rst = Mprocurement::where("user_id",$userId)->where("u_state",1)->where("u_examine_state",1)->where("wholesale_state",2)->find();
|
|
|
|
|
|
// 商品评价
|
|
$commentCategory = GoodsCommentLogic::category(['goods_id'=>$goodsId]);
|
|
$goodsDetailArr['comment']['percent'] = $commentCategory['percent'];
|
|
|
|
$all_comment = Db::name('goods_comment')->where(['goods_id' => $goodsId])->sum('goods_comment');
|
|
$goods_comment_count = Db::name('goods_comment')->where(['goods_id' => $goodsId])->count('id');
|
|
if($goods_comment_count){
|
|
$goods_comment = round($all_comment / $goods_comment_count,2);
|
|
$goodsDetailArr['comment']['goods_comment'] = $goods_comment;
|
|
}else{
|
|
$goodsDetailArr['comment']['goods_comment'] = 0;
|
|
}
|
|
// 最新一条评论
|
|
$one = GoodsComment::alias('gc')
|
|
->field('gc.id,gc.goods_comment,gc.create_time,gc.comment,u.avatar,u.nickname,g.name as goods_name')
|
|
->leftJoin('user u', 'u.id=gc.user_id')
|
|
->leftJoin('goods g', 'g.id=gc.goods_id')
|
|
->where([
|
|
['gc.goods_id', '=', $goodsId],
|
|
['gc.del', '=', 0],
|
|
['gc.status', '=', 1],
|
|
])
|
|
->order('create_time', 'desc')
|
|
->findOrEmpty();
|
|
if($one->isEmpty()) {
|
|
$one = [];
|
|
}else {
|
|
$one = $one->toArray();
|
|
// 头像
|
|
$one['avatar'] = UrlServer::getFileUrl($one['avatar']);
|
|
// 图片评价
|
|
$one['image'] = GoodsCommentImage::where('goods_comment_id', $one['id'])->column('uri');
|
|
foreach($one['image'] as $subKey => $subItem) {
|
|
$one['image'][$subKey] = UrlServer::getFileUrl($subItem);
|
|
}
|
|
}
|
|
$goodsDetailArr['comment']['one'] = $one;
|
|
|
|
// 判断是否是拼团商品
|
|
$teamActivity = (new TeamActivity())
|
|
->field(['id,people_num,team_max_price,team_min_price,sales_volume,activity_end_time,share_title,share_intro'])
|
|
->where([
|
|
['goods_id', '=', $goodsId],
|
|
['audit', '=', 1],
|
|
['status', '=', 1],
|
|
['del', '=', 0],
|
|
['activity_start_time', '<=', time()],
|
|
['activity_end_time', '>=', time()]
|
|
])->findOrEmpty()->toArray();
|
|
|
|
if ($teamActivity) {
|
|
$teamFound = (new TeamFound())->alias('TF')
|
|
->field(['TF.*', 'U.nickname,U.avatar'])
|
|
->limit(8)
|
|
->order('id desc')
|
|
->where('TF.team_activity_id', '=', $teamActivity['id'])
|
|
->where('TF.people','exp',' > TF.join ')
|
|
->where([
|
|
['status', '=', 0],
|
|
['invalid_time', '>=', time()]
|
|
])->join('procurement U', 'U.id=TF.user_id')
|
|
->select()->toArray();
|
|
|
|
foreach ($teamFound as &$found) {
|
|
unset($found['shop_id']);
|
|
unset($found['team_sn']);
|
|
unset($found['goods_snap']);
|
|
unset($found['team_end_time']);
|
|
$found['avatar'] = UrlServer::getFileUrl($found['avatar']);
|
|
$found['surplus_time'] = intval($found['invalid_time'] - time());
|
|
}
|
|
|
|
$teamActivity['share_title'] = !empty($teamActivity['share_title']) ? $teamActivity['share_title'] : $goodsDetailArr['name'];
|
|
$teamActivity['share_intro'] = !empty($teamActivity['share_intro']) ? $teamActivity['share_intro'] : $goodsDetailArr['remark'];
|
|
|
|
$goodsDetailArr['activity'] = ['type'=>2, 'type_desc'=>'拼团商品', 'info'=>$teamActivity, 'found'=>$teamFound];
|
|
$teamGoods = (new TeamGoods())->where(['team_id'=>$teamActivity['id']])->select()->toArray();
|
|
foreach ($goodsDetailArr['goods_item'] as &$item) {
|
|
foreach ($teamGoods as $team) {
|
|
if ($item['id'] === $team['item_id']) {
|
|
$item['team_price'] = $team['team_price'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 预估佣金(计算出最高可得佣金)
|
|
$goodsDetailArr['distribution'] = self::getDistribution($goodsId, $userId);
|
|
|
|
// 虚拟浏览量
|
|
$goodsDetailArr['clicks'] += $goodsDetailArr['clicks_virtual'];
|
|
|
|
// 记录访问足迹
|
|
event('Footprint', [
|
|
'type' => FootprintEnum::BROWSE_GOODS,
|
|
'user_id' => $userId,
|
|
'foreign_id' => $goodsId
|
|
]);
|
|
|
|
Db::commit();
|
|
return $goodsDetailArr;
|
|
}catch(\Exception $e) {
|
|
Db::rollback();
|
|
self::$error = $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 热销榜单
|
|
*/
|
|
public static function getHotList($get)
|
|
{
|
|
// 销售中商品:未删除/审核通过/已上架
|
|
$where = [
|
|
['del', '=', GoodsEnum::DEL_NORMAL], // 未删除
|
|
['status', '=', GoodsEnum::STATUS_SHELVES], // 上架中
|
|
['audit_status', '=', GoodsEnum::AUDIT_STATUS_OK], // 审核通过
|
|
];
|
|
$order = [
|
|
'sales_total' => 'desc', // 实际销量+虚拟销量倒序
|
|
'sales_actual' => 'desc', // 实际销量倒序
|
|
'id' => 'desc'
|
|
];
|
|
|
|
return self::getGoodsListTemplate($where, $order, $get);
|
|
}
|
|
|
|
/**
|
|
* 商品列表
|
|
*/
|
|
public static function getGoodsList($get)
|
|
{
|
|
// 销售中商品:未删除/审核通过/已上架
|
|
$where = [
|
|
['del', '=', GoodsEnum::DEL_NORMAL], // 未删除
|
|
['status', '=', GoodsEnum::STATUS_SHELVES], // 上架中
|
|
['audit_status', '=', GoodsEnum::AUDIT_STATUS_OK], // 审核通过
|
|
];
|
|
$order = [
|
|
'sort_weight' => 'asc', // 商品权重,数字越小权重越大
|
|
'sort' => 'asc',
|
|
'id' => 'desc'
|
|
];
|
|
return self::getGoodsListTemplate($where, $order, $get);
|
|
}
|
|
|
|
/**
|
|
* 商品列表模板
|
|
* 作用:代码复用
|
|
*/
|
|
public static function getGoodsListTemplate($where, $order, $get)
|
|
{
|
|
$wh = "";
|
|
if (!empty(self::filterShopsIds())) {
|
|
// 过滤已删除、已冻结、已暂停营业、已到期的店铺
|
|
$where[] = ['shop_id', 'not in', self::filterShopsIds()];
|
|
}
|
|
// 平台分类
|
|
if(isset($get['platform_cate_id']) && !empty($get['platform_cate_id']) && filter_var($get['platform_cate_id'], FILTER_VALIDATE_INT)) {
|
|
$category_msg = GoodsCategory::where("id",$get['platform_cate_id'])->find();
|
|
if($category_msg['pid'] == 0){
|
|
$wh = "find_in_set(".$get['platform_cate_id'].", first_cate_id)";
|
|
}else{
|
|
$category_second_msg = GoodsCategory::where("pid",$category_msg['id'])->find();
|
|
if($category_second_msg==null){
|
|
$wh = "find_in_set(".$get['platform_cate_id'].", second_cate_id)";
|
|
}else{
|
|
$wh = "find_in_set(".$get['platform_cate_id'].", third_cate_id)";
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// 品牌
|
|
if(isset($get['brand_id']) && !empty($get['brand_id']) && filter_var($get['brand_id'], FILTER_VALIDATE_INT)) {
|
|
$where[] = ['brand_id', '=', $get['brand_id']];
|
|
}
|
|
|
|
// 关键词
|
|
if(isset($get['keyword']) && !empty($get['keyword'])) {
|
|
$where[] = ['name', 'like', '%'.trim($get['keyword']).'%'];
|
|
if($get['user_id']) { // 记录关键词
|
|
self::recordKeyword(trim($get['keyword']), $get['user_id']);
|
|
}
|
|
}
|
|
|
|
// 店铺id
|
|
if(isset($get['shop_id']) && !empty($get['shop_id']) && filter_var($get['shop_id'], FILTER_VALIDATE_INT)) {
|
|
$where[] = ['shop_id', '=', $get['shop_id']];
|
|
}
|
|
|
|
// 店铺分类
|
|
if(isset($get['shop_cate_id']) && !empty($get['shop_cate_id']) && filter_var($get['shop_cate_id'], FILTER_VALIDATE_INT)) {
|
|
$where[] = ['shop_cate_id', '=', $get['shop_cate_id']];
|
|
}
|
|
|
|
// 销量排序(实际销量 + 虚拟销量)
|
|
if(isset($get['sort_by_sales']) && !empty($get['sort_by_sales'])) {
|
|
$elt = ['sales_total'=> trim($get['sort_by_sales'])];
|
|
$order = array_merge($elt, $order);
|
|
}
|
|
|
|
// 价格排序
|
|
if(isset($get['sort_by_price']) && !empty($get['sort_by_price'])) {
|
|
$elt = ['min_price'=> trim($get['sort_by_price'])];
|
|
$order = array_merge($elt, $order);
|
|
}
|
|
|
|
// 新品排序
|
|
if(isset($get['sort_by_create']) && !empty($get['sort_by_create'])) {
|
|
$elt = ['create_time'=> trim($get['sort_by_create'])];
|
|
$order = array_merge($elt, $order);
|
|
}
|
|
if(isset($get['p_id'])){
|
|
$p_id= $get['p_id'];
|
|
}else{
|
|
$p_id= 0;
|
|
}
|
|
if(isset($get['set'])){
|
|
$set_id = $get['set'];
|
|
}else{
|
|
$set_id= 0;
|
|
}
|
|
$set = null;
|
|
$settpye = 0;
|
|
if($p_id != 0&&$set_id!=0){
|
|
$set = Procurementset::where("set_id",$set_id)->where("set_state",1)->find();
|
|
// $where[] = ['second_cate_id', 'in', $set['goods_twotype']];
|
|
$set_array = json_decode($set['goods_onetype'], true);
|
|
$w = ['second_cate_id', 'in', $set_array[0]['child_id']];
|
|
if(isset($get['platform_cate_id'])){
|
|
foreach($set_array as $key=>$value){
|
|
if($value['id'] == $get['platform_cate_id']){
|
|
$w = ['second_cate_id', 'in', $value['child_id']];
|
|
}
|
|
}
|
|
}
|
|
|
|
$where[] = $w;
|
|
if($set != null){
|
|
if($set['set_pro']!=0){
|
|
$where[] = ['purchase_price', '>', 0];
|
|
}else if($set['set_issued']!=0){
|
|
$where[] = ['issuing_price', '>', 0];
|
|
}else if($set['set_wholesale']!=0){
|
|
$where[] = ['wholesale_price', '>', 0];
|
|
}
|
|
}
|
|
}
|
|
if($p_id != 0&&$set_id==0){
|
|
$settpye = 1;
|
|
$set = Procurementset::where("u_id",$p_id)->where("set_state",1)->order("set_update desc")->limit(1)->find();
|
|
if($set != null){
|
|
$where[] = ['purchase_price|issuing_price|wholesale_price', '>', 0];
|
|
}
|
|
}
|
|
$field = 'id,image,name,wholesale_price,min_price,market_price,sales_actual,first_cate_id,
|
|
second_cate_id,third_cate_id,sort_weight,brand_id,shop_id,sales_virtual,
|
|
(sales_actual + sales_virtual) as sales_total,purchase_price,issuing_price,subsidy_id,presale';
|
|
|
|
// dump($wh);
|
|
// dump($where);
|
|
// dump($order);die;
|
|
$list = Goods::with(['shop'])
|
|
->field($field)
|
|
->where($wh)
|
|
->where($where)
|
|
->order($order)
|
|
->page($get['page_no'], $get['page_size'])
|
|
->select();
|
|
// foreach ($list as &$v) {
|
|
// $v['market_price'] = round($v['min_price'],2);
|
|
// $v['min_price'] = round(0.88*$v['min_price'],2);
|
|
// }
|
|
$set_price_type = 0;
|
|
foreach ($list as $k => $item) {
|
|
// 获取返补比例, subsidy_id为0的就传递一个ID为4的固定ID
|
|
$list[$k]['subsidy'] = (new GoodsSubsidy())->getSubsidyById($item['subsidy_id'] > 0 ? $item['subsidy_id'] : 4);
|
|
$list[$k]['subsidy_txt'] = '最高返' . $list[$k]['subsidy']['subsidy'];
|
|
|
|
$item['shop_type'] = $item['shop']['type'];
|
|
unset($item['shop']);
|
|
if($set != null&&$settpye!=1){
|
|
if($set['price_type']==1){
|
|
$set_price_type = 1;
|
|
}
|
|
if($set['price_type']==2){
|
|
$set_price_type = 2;
|
|
}
|
|
if($set['price_type']==3){
|
|
$set_price_type = 3;
|
|
}
|
|
if($set['set_pro']!=0){
|
|
// $set_price_type = 1;
|
|
//min_price
|
|
if($set['set_pro_type'] == 1){
|
|
$item['purchase_price'] = round($item['purchase_price']*(100+$set['set_pro'])/100,2);
|
|
}
|
|
if($set['set_pro_type'] == 3){
|
|
if($item['purchase_price'] >0){
|
|
$item['purchase_price'] = round($item['purchase_price']+$set['set_pro'],2);
|
|
}else{
|
|
$item['purchase_price'] = "0.00";
|
|
}
|
|
}
|
|
}
|
|
if($set['set_issued']!=0){
|
|
// $set_price_type = 2;
|
|
//market_price
|
|
if($set['set_issued_type'] == 1){
|
|
$item['issuing_price'] = round($item['issuing_price']*(100+$set['set_issued'])/100,2);
|
|
}
|
|
if($set['set_issued_type'] == 3) {
|
|
if ($item['issuing_price'] > 0) {
|
|
$item['issuing_price'] = round($item['issuing_price'] + $set['set_issued'], 2);
|
|
}else{
|
|
$item['issuing_price'] = "0.00";
|
|
}
|
|
}
|
|
}
|
|
if($set['set_wholesale']!=0){
|
|
// $set_price_type = 3;
|
|
//market_price
|
|
if($set['set_wholesale_type'] == 1){
|
|
$item['wholesale_price'] = round($item['wholesale_price']*(100+$set['set_wholesale'])/100,2);
|
|
}
|
|
if($set['set_wholesale_type'] == 3) {
|
|
if ($item['wholesale_price'] > 0) {
|
|
$item['wholesale_price'] = round($item['wholesale_price'] + $set['set_wholesale'], 2);
|
|
}else{
|
|
$item['wholesale_price'] = "0.00";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$count = Goods::where($where)->where($wh)->count();
|
|
|
|
$list = $list ? $list->toArray() : [];
|
|
|
|
$more = is_more($count, $get['page_no'], $get['page_size']);
|
|
|
|
$data = [
|
|
'lists' => $list,
|
|
'page_no' => $get['page_no'],
|
|
'page_size' => $get['page_size'],
|
|
'count' => $count,
|
|
'more' => $more,
|
|
'set_price_type'=> $set_price_type
|
|
];
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 根据商品栏目获取商品列表
|
|
*/
|
|
public static function getGoodsListByColumnId($columnId, $page_no, $page_size,$p_id,$set_id)
|
|
{
|
|
|
|
// 销售中商品:未删除/审核通过/已上架
|
|
$onSaleWhere = [
|
|
['del', '=', GoodsEnum::DEL_NORMAL],
|
|
['status', '=', GoodsEnum::STATUS_SHELVES],
|
|
['audit_status', '=', GoodsEnum::AUDIT_STATUS_OK],
|
|
];
|
|
|
|
if (!empty(self::filterShopsIds())) {
|
|
// 过滤已删除、已冻结、已暂停营业、已到期的店铺
|
|
$onSaleWhere[] = ['shop_id', 'not in', self::filterShopsIds()];
|
|
}
|
|
|
|
$order = [
|
|
'sort_weight' => 'asc', // 数字越小,权重越大
|
|
'sales_actual' => 'desc',
|
|
'id' => 'desc'
|
|
];
|
|
$set = null;
|
|
$where = [];
|
|
$settpye = 0;
|
|
if($p_id != 0&&$set_id!=0){
|
|
$set = Procurementset::where("set_id",$set_id)->where("set_state",1)->find();
|
|
if($set != null){
|
|
if($set['set_pro']!=0){
|
|
$where[] = ['purchase_price', '>', 0];
|
|
}else if($set['set_issued']!=0){
|
|
$where[] = ['issuing_price', '>', 0];
|
|
}else if($set['set_wholesale']!=0){
|
|
$where[] = ['wholesale_price', '>', 0];
|
|
}
|
|
}
|
|
}
|
|
if($p_id != 0&&$set_id==0){
|
|
$settpye = 1;
|
|
if($set != null) {
|
|
$set = Procurementset::where("u_id", $p_id)->where("set_state", 1)->order("set_update desc")->limit(1)->find();
|
|
$where[] = ['purchase_price|issuing_price|wholesale_price', '>', 0];
|
|
}
|
|
}
|
|
$list = Goods::field('id,name,image,market_price,min_price,wholesale_price,purchase_price,issuing_price,sales_actual,column_ids,sort_weight,sales_virtual,(sales_actual + sales_virtual) as sales_total, subsidy_id, presale')
|
|
->where($onSaleWhere)
|
|
->where($where)
|
|
->whereFindInSet('column_ids', $columnId)
|
|
->order($order)
|
|
->page($page_no, $page_size)
|
|
->select();
|
|
// foreach ($list as &$v) {
|
|
// $v['market_price'] = round($v['min_price'],2);
|
|
// $v['min_price'] = round(0.88*$v['min_price'],2);
|
|
// }
|
|
$set_price_type = 0;
|
|
if ($set != null&&$settpye!=1) {
|
|
foreach ($list as $key => $value) {
|
|
if($set['price_type']==1){
|
|
$set_price_type = 1;
|
|
}
|
|
if($set['price_type']==2){
|
|
$set_price_type = 2;
|
|
}
|
|
if($set['price_type']==3){
|
|
$set_price_type = 3;
|
|
}
|
|
if ($set['set_pro'] != 0) {
|
|
// $set_price_type = 1;
|
|
if ($set['set_pro_type'] == 1) {
|
|
$list[$key]['purchase_price'] = round($value['purchase_price'] * (100 + $set['set_pro']) / 100, 2);
|
|
}
|
|
if ($set['set_pro_type'] == 3) {
|
|
$list[$key]['purchase_price'] = round($value['purchase_price'] + $set['set_pro'], 2);
|
|
}
|
|
}
|
|
if ($set['set_issued'] != 0) {
|
|
// $set_price_type = 2;
|
|
if ($set['set_issued_type'] == 1) {
|
|
$list[$key]['issuing_price'] = round($value['issuing_price'] * (100 + $set['set_issued']) / 100, 2);
|
|
}
|
|
if ($set['set_issued_type'] == 3) {
|
|
$list[$key]['issuing_price'] = round($value['issuing_price'] + $set['set_issued'], 2);
|
|
}
|
|
}
|
|
if ($set['set_wholesale'] != 0) {
|
|
// $set_price_type = 3;
|
|
if ($set['set_wholesale_type'] == 1) {
|
|
$list[$key]['wholesale_price'] = round($value['wholesale_price'] * (100 + $set['set_wholesale']) / 100, 2);
|
|
}
|
|
if ($set['set_wholesale_type'] == 3) {
|
|
$list[$key]['wholesale_price'] = round($value['wholesale_price'] + $set['set_wholesale'], 2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ($list as $k => $v) {
|
|
// 获取返补比例, subsidy_id为0的就传递一个ID为4的固定ID
|
|
$list[$k]['subsidy'] = (new GoodsSubsidy())->getSubsidyById($v['subsidy_id'] > 0 ? $v['subsidy_id'] : 4);
|
|
$list[$k]['subsidy_txt'] = '最高返' . $list[$k]['subsidy']['subsidy'];
|
|
}
|
|
|
|
$count = Goods::where($onSaleWhere)
|
|
->where($where)
|
|
->whereFindInSet('column_ids', $columnId)
|
|
->count();
|
|
|
|
$list = $list ? $list->toArray() : [];
|
|
|
|
$more = is_more($count, $page_no, $page_size);
|
|
|
|
$data = [
|
|
'lists' => $list,
|
|
'page_no' => $page_no,
|
|
'page_size' => $page_size,
|
|
'count' => $count,
|
|
'more' => $more,
|
|
'set_price_type' => $set_price_type
|
|
];
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @notes 获取已删除、已冻结、已暂停营业、已到期店铺的id
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @author Tab
|
|
* @date 2021/7/20 14:29
|
|
*/
|
|
public static function filterShopsIds()
|
|
{
|
|
// 已删除、已冻结、已暂停营业的店铺
|
|
$invalidShops = Shop::field('id,name')->whereOr([
|
|
['del', '=', 1], // 已删除
|
|
['is_freeze', '=', 1], // 已冻结
|
|
['is_run', '=', 0] // 暂停营业
|
|
])->select()->toArray();
|
|
|
|
// 已过期的店铺
|
|
$expiredShops = Shop::field('id,name')->where([
|
|
['expire_time', '<>', 0],
|
|
['expire_time', '<=', time()],
|
|
])->select()->toArray();
|
|
|
|
$filterShops = array_merge($invalidShops, $expiredShops);
|
|
$filterShopsIds = array_column($filterShops, 'id');
|
|
return $filterShopsIds;
|
|
}
|
|
|
|
/**
|
|
* 记录关键词
|
|
*/
|
|
public static function recordKeyword($keyword, $user_id)
|
|
{
|
|
$record = SearchRecord::where(['user_id'=>$user_id,'keyword'=>$keyword,'del'=>0])->find();
|
|
if($record){
|
|
// 有该关键词记录, 更新
|
|
return SearchRecord::where(['id'=>$record['id']])->update(['count'=>Db::raw('count+1'),'update_time'=>time()]);
|
|
}
|
|
// 无该关键词记录 > 新增
|
|
return SearchRecord::create([
|
|
'user_id'=>$user_id,
|
|
'keyword'=>$keyword,
|
|
'count' => 1,
|
|
'update_time' => time(),
|
|
'del' => 0
|
|
]);
|
|
}
|
|
|
|
//检查商品是否正在参加活动
|
|
public static function checkActivity($goods){
|
|
// 获取正在秒杀的时段
|
|
$seckill_time = SeckillGoodsLogic::getSeckillTimeIng();
|
|
if($seckill_time === false) {
|
|
// 不在秒杀时段,直接返回
|
|
return $goods;
|
|
}
|
|
// 判断是否是秒杀中的商品
|
|
$seckill_goods = SeckillGoods::where([
|
|
['del', '=', 0],
|
|
['seckill_id', '=', $seckill_time['id']],
|
|
['goods_id', '=', $goods['id']],
|
|
['review_status', '=', 1],
|
|
])->select()->toArray();
|
|
if(!$seckill_goods) {
|
|
// 不是秒杀商品
|
|
return $goods;
|
|
}
|
|
// 判断参与日期是否包含今天
|
|
$flag = false;
|
|
$now = time();
|
|
foreach($seckill_goods as $item) {
|
|
$start_date_time = strtotime($item['start_date'].' 00:00:00');
|
|
$end_date_time = strtotime($item['end_date'].' 00:00:00');
|
|
if($start_date_time < $now && $end_date_time > $now) {
|
|
$flag = true;
|
|
// 获取该商品的秒杀信息
|
|
$seckill_goods_info = SeckillGoods::where([
|
|
'goods_id' => $goods['id'],
|
|
'seckill_id' => $seckill_time['id'],
|
|
'start_date' => $item['start_date'],
|
|
'end_date' => $item['end_date'],
|
|
])->column('goods_id,item_id,price', 'item_id');
|
|
break;
|
|
}
|
|
}
|
|
if($flag === false) {
|
|
// 参与日期不在今天
|
|
return $goods;
|
|
}
|
|
// 确定是秒杀中的商品
|
|
// 先将商品市场价换成原SKU最小价
|
|
$goods['market_price'] = $goods['min_price'];
|
|
// 替换活动价
|
|
foreach($goods['goods_item'] as &$item) {
|
|
// 商品价格替换为最小的秒杀价
|
|
if($goods['min_price'] > $seckill_goods_info[$item['id']]['price']) {
|
|
$goods['min_price'] = $seckill_goods_info[$item['id']]['price'];
|
|
}
|
|
// 原市场价替换为原SKU售价
|
|
$item['market_price'] = $item['price'];
|
|
// SKU替换秒杀价
|
|
$item['price'] = $seckill_goods_info[$item['id']]['price'];
|
|
}
|
|
$today_date = date('Y-m-d');
|
|
$goods['activity'] = [
|
|
'type' => 1,
|
|
'type_desc' => '秒杀商品',
|
|
'end_time' => strtotime($today_date.' '.$seckill_time['end_time'])
|
|
];
|
|
return $goods;
|
|
|
|
}
|
|
|
|
/**
|
|
* @notes 获取商品分销信息
|
|
* @param $goodsId
|
|
* @param $userId
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @author Tab
|
|
* @date 2021/9/6 18:48
|
|
*/
|
|
public static function getDistribution($goodsId, $userId)
|
|
{
|
|
$earnings = 0;
|
|
$goods = Goods::findOrEmpty($goodsId)->toArray();
|
|
$distributionGoods = DistributionGoods::where('goods_id', $goodsId)->select()->toArray();
|
|
if(!empty($distributionGoods) && $distributionGoods[0]['is_distribution'] && $distributionGoods[0]['rule'] == 2) {
|
|
foreach($distributionGoods as $item) {
|
|
$earnings = max($earnings, round($goods['max_price'] * $item['first_ratio'] / 100, 2));
|
|
$earnings = max($earnings, round($goods['max_price'] * $item['second_ratio'] / 100, 2));
|
|
}
|
|
}
|
|
if(!empty($distributionGoods) && $distributionGoods[0]['is_distribution'] && $distributionGoods[0]['rule'] == 1) {
|
|
$levels = DistributionLevel::select()->toArray();
|
|
foreach($levels as $item) {
|
|
$earnings = max($earnings, round($goods['max_price'] * $item['first_ratio'] / 100, 2));
|
|
$earnings = max($earnings, round($goods['max_price'] * $item['second_ratio'] / 100, 2));
|
|
}
|
|
}
|
|
|
|
// 详情页是否显示佣金
|
|
$isShow = ConfigServer::get('distribution', 'is_show_earnings', 0);
|
|
// 系统总分销开关
|
|
$distributionOpen = ConfigServer::get('distribution', 'is_open', 0);
|
|
// 商家信息-获取商家是否被禁用分销功能(is_distribution)
|
|
$shop = Shop::findOrEmpty($goods['shop_id'])->toArray();
|
|
|
|
if ($distributionOpen && $shop['is_distribution'] && $isShow) {
|
|
//详情页佣金可见用户 0-全部用户 1-分销商
|
|
$scope = ConfigServer::get('distribution', 'show_earnings_scope', 0);
|
|
$user = Distribution::where(['user_id' => $userId])->findOrEmpty()->toArray();
|
|
if ($scope && empty($user['is_distribution'])) {
|
|
$isShow = 0;
|
|
}
|
|
} else {
|
|
$isShow = 0;
|
|
}
|
|
|
|
return [
|
|
'is_show' => $isShow,
|
|
'earnings' => $earnings
|
|
];
|
|
}
|
|
|
|
public static function searchByName($shop_name, $user_id)
|
|
{
|
|
if (!$shop_name) {
|
|
return [];
|
|
}
|
|
|
|
$onSaleWhere = [
|
|
'del' => GoodsEnum::DEL_NORMAL, // 未删除
|
|
'status' => GoodsEnum::STATUS_SHELVES, // 上架中
|
|
'audit_status' => GoodsEnum::AUDIT_STATUS_OK, // 审核通过
|
|
];
|
|
|
|
$where = [];
|
|
if (!empty(self::filterShopsIds())) {
|
|
// 过滤已删除、已冻结、已暂停营业、已到期的店铺
|
|
$where[] = ['shop_id', 'not in', self::filterShopsIds()];
|
|
}
|
|
|
|
$where[] = ['name', 'like', '%' . trim($shop_name) . '%'];
|
|
|
|
SearchRecord::create([
|
|
'user_id'=>$user_id,
|
|
'keyword'=>$shop_name,
|
|
'count' => 1,
|
|
'update_time' => time(),
|
|
'del' => 0
|
|
]);
|
|
|
|
$category = GoodsCategory::where(['is_show' => 1, 'del' => 0])
|
|
->where([['name', 'like', '%' . trim($shop_name) . '%']])
|
|
->field('id, level')
|
|
->select()
|
|
->toArray();
|
|
|
|
if (!empty($category)) {
|
|
$con = array(
|
|
1 => 'first_cate_id',
|
|
2 => 'second_cate_id',
|
|
3 => 'second_cate_id',
|
|
);
|
|
foreach ($category as &$v) {
|
|
$goods[] = Goods::where($onSaleWhere)
|
|
->where([$con[$v['level']] => $v['id']])
|
|
->order('sort_weight asc, sort asc, id desc')
|
|
->field('id, name')
|
|
->select()
|
|
->toArray();
|
|
}
|
|
$goods = call_user_func_array('array_merge', $goods);
|
|
} else {
|
|
$goods = Goods::field('id, name')
|
|
->where($onSaleWhere)
|
|
->where($where)
|
|
->order('sort_weight asc, sort asc, id desc')
|
|
->select()->toArray();
|
|
}
|
|
|
|
if ($goods) {
|
|
return $goods;
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
public static function hotList($page, $size)
|
|
{
|
|
$hot_list = [];
|
|
if ($page == 1) {
|
|
$hot_list = Db::name('hot_list')
|
|
->alias('hl')
|
|
->join('goods g', 'hl.goods_id = g.id')
|
|
->where('delete_time', 0)
|
|
->field('hl.id as hl_id, g.id as id, g.name, g.image, g.issuing_price, g.market_price, g.min_price, g.purchase_price, g.wholesale_price, g.subsidy_id')
|
|
->select()
|
|
->toArray();
|
|
foreach ($hot_list as $k => &$v) {
|
|
if (isset($v['image'])) {
|
|
$v['image'] = UrlServer::getFileUrl($v['image']);
|
|
}
|
|
|
|
// 获取返补比例, subsidy_id为0的就传递一个ID为4的固定ID
|
|
$hot_list[$k]['subsidy'] = (new GoodsSubsidy())->getSubsidyById($v['subsidy_id'] > 0 ? $v['subsidy_id'] : 4);
|
|
$hot_list[$k]['subsidy_txt'] = '最高返' . $hot_list[$k]['subsidy']['subsidy'];
|
|
}
|
|
}
|
|
|
|
$shopWhere = [];
|
|
if (!empty(self::filterShopsIds())) {
|
|
// 过滤已删除、已冻结、已暂停营业、已到期的店铺
|
|
$shopWhere[] = ['og.shop_id', 'not in', self::filterShopsIds()];
|
|
}
|
|
|
|
$onSaleWhere = [
|
|
'del' => GoodsEnum::DEL_NORMAL, // 未删除
|
|
'status' => GoodsEnum::STATUS_SHELVES, // 上架中
|
|
'audit_status' => GoodsEnum::AUDIT_STATUS_OK, // 审核通过
|
|
];
|
|
|
|
$order_goods = Db::name('order_goods')
|
|
->alias('og')
|
|
->join('goods g', 'og.goods_id = g.id')
|
|
->where($onSaleWhere)
|
|
->where($shopWhere)
|
|
->field('og.goods_id, og.create_time, count(*) as count, g.id as gid, g.name, g.image, g.issuing_price, g.market_price, g.min_price, g.purchase_price, g.wholesale_price, g.subsidy_id')
|
|
->group('og.goods_id')
|
|
->order('og.create_time desc, count desc')
|
|
->page($page, $size)
|
|
->select()
|
|
->toArray();
|
|
|
|
$count = Db::name('order_goods')
|
|
->alias('og')
|
|
->join('goods g', 'og.goods_id = g.id')
|
|
->where($onSaleWhere)
|
|
->where($shopWhere)
|
|
->group('og.goods_id')
|
|
->count();
|
|
|
|
foreach ($order_goods as $k => &$v) {
|
|
if (isset($v['image'])) {
|
|
$v['image'] = UrlServer::getFileUrl($v['image']);
|
|
}
|
|
|
|
// 获取返补比例, subsidy_id为0的就传递一个ID为4的固定ID
|
|
$order_goods[$k]['subsidy'] = (new GoodsSubsidy())->getSubsidyById($v['subsidy_id'] > 0 ? $v['subsidy_id'] : 4);
|
|
$order_goods[$k]['subsidy_txt'] = '最高返' . $order_goods[$k]['subsidy']['subsidy'];
|
|
}
|
|
|
|
$more = is_more($count, $page, $size); //是否有下一页
|
|
|
|
$list = $order_goods;
|
|
if ($page == 1) {
|
|
$list = array_merge($hot_list, $order_goods);
|
|
}
|
|
|
|
$data = [
|
|
'lists' => $list,
|
|
'hot_list' => $hot_list,
|
|
'page_no' => $page,
|
|
'page_size' => $size,
|
|
'count' => $count,
|
|
'more' => $more
|
|
];
|
|
|
|
return $data;
|
|
}
|
|
|
|
public static function goodsList($type, $page, $size) {
|
|
$shopWhere = [];
|
|
if (!empty(self::filterShopsIds())) {
|
|
// 过滤已删除、已冻结、已暂停营业、已到期的店铺
|
|
$shopWhere[] = ['shop_id', 'not in', self::filterShopsIds()];
|
|
}
|
|
|
|
$onSaleWhere = [
|
|
'del' => GoodsEnum::DEL_NORMAL, // 未删除
|
|
'status' => GoodsEnum::STATUS_SHELVES, // 上架中
|
|
'audit_status' => GoodsEnum::AUDIT_STATUS_OK, // 审核通过
|
|
];
|
|
|
|
$list = $goods = [];
|
|
|
|
if ($type == 'recommend') {
|
|
$list = Db::name('new_goods')
|
|
->alias('ng')
|
|
->join('goods g', 'ng.goods_id = g.id')
|
|
->where($onSaleWhere)
|
|
->where($shopWhere)
|
|
->where('delete_time', 0)
|
|
->field('ng.id as ng_id, g.id as id, g.name, g.image, g.issuing_price, g.market_price, g.min_price, g.purchase_price, g.wholesale_price, g.subsidy_id')
|
|
->limit(4)
|
|
->select()
|
|
->toArray();
|
|
$count = count($list);
|
|
|
|
if ($count < 4) {
|
|
$goods = Db::name('goods')
|
|
->where($onSaleWhere)
|
|
->where($shopWhere)
|
|
->limit(4 - $count)
|
|
->order('create_time desc')
|
|
->select()
|
|
->toArray();
|
|
}
|
|
|
|
$result = array_merge($list, $goods);
|
|
foreach ($result as $k => &$v) {
|
|
// $v['market_price'] = round($v['min_price'],2);
|
|
// $v['min_price'] = round(0.88*$v['min_price'],2);
|
|
if (isset($v['image'])) {
|
|
$v['image'] = UrlServer::getFileUrl($v['image']);
|
|
}
|
|
|
|
// 获取返补比例, subsidy_id为0的就传递一个ID为4的固定ID
|
|
$result[$k]['subsidy'] = (new GoodsSubsidy())->getSubsidyById($v['subsidy_id'] > 0 ? $v['subsidy_id'] : 4);
|
|
$result[$k]['subsidy_txt'] = '最高返' . $result[$k]['subsidy']['subsidy'];
|
|
}
|
|
return $result;
|
|
} else if ($type == 'all') {
|
|
$list = Db::name('new_goods')
|
|
->alias('ng')
|
|
->join('goods g', 'ng.goods_id = g.id')
|
|
->where($shopWhere)
|
|
->where($onSaleWhere)
|
|
->where('delete_time', 0)
|
|
->field('ng.id as ng_id, g.id as id, g.name, g.image, g.issuing_price, g.market_price, g.min_price, g.purchase_price, g.wholesale_price, g.subsidy_id')
|
|
->select()
|
|
->toArray();
|
|
$goods = Db::name('goods')
|
|
->where($onSaleWhere)
|
|
->where($shopWhere)
|
|
->order('create_time desc')
|
|
->page($page, $size)
|
|
->select()
|
|
->toArray();
|
|
|
|
$count = Db::name('goods')
|
|
->where($onSaleWhere)
|
|
->count();
|
|
|
|
$result = array_merge($list, $goods);
|
|
|
|
foreach ($result as $k => &$v) {
|
|
// $result[$k]['market_price'] = round($v['min_price'],2);
|
|
// $result[$k]['min_price'] = round(0.88*$v['min_price'],2);
|
|
if (isset($v['image'])) {
|
|
$v['image'] = UrlServer::getFileUrl($v['image']);
|
|
}
|
|
|
|
// 获取返补比例, subsidy_id为0的就传递一个ID为4的固定ID
|
|
$result[$k]['subsidy'] = (new GoodsSubsidy())->getSubsidyById($v['subsidy_id'] > 0 ? $v['subsidy_id'] : 4);
|
|
$result[$k]['subsidy_txt'] = '最高返' . $result[$k]['subsidy']['subsidy'];
|
|
}
|
|
|
|
$more = is_more($count, $page, $size); //是否有下一页
|
|
|
|
return [
|
|
'lists' => $result,
|
|
'page_no' => $page,
|
|
'page_size' => $size,
|
|
'count' => $count,
|
|
'more' => $more
|
|
];
|
|
}
|
|
}
|
|
|
|
public static function rushBuy($goods, $userId){
|
|
|
|
// 判断是否是抢购中的商品
|
|
$GroupbuyGoods = GroupbuyGoods::where([
|
|
['del', '=', 1],
|
|
['goods_id', '=', $goods['id']],
|
|
['status', '=', 1],
|
|
])->select()->toArray();
|
|
|
|
// 判断此商品是否为抢购商品
|
|
if(!$GroupbuyGoods){
|
|
return $goods;
|
|
}
|
|
|
|
foreach($GroupbuyGoods as $item) {
|
|
// 获取该商品的秒杀信息
|
|
$GroupbuyGoods_info = GroupbuyGoods::where([
|
|
'goods_id' => $goods['id'],
|
|
])->column('goods_id,item_id,activity_price,stock,limited_num', 'item_id');
|
|
}
|
|
|
|
// 确定是抢购商品 先将商品市场价换成原SKU最小价
|
|
$goods['market_price'] = $goods['min_price'];
|
|
// 替换活动价
|
|
foreach($goods['goods_item'] as &$item) {
|
|
|
|
// 商品价格替换为最小的秒杀价
|
|
if($goods['min_price'] > $GroupbuyGoods_info[$item['id']]['activity_price']) {
|
|
$goods['min_price'] = $GroupbuyGoods_info[$item['id']]['activity_price'];
|
|
}
|
|
// 原市场价替换为原SKU售价
|
|
$item['market_price'] = $item['price'];
|
|
// SKU替换抢购价
|
|
$item['price'] = $GroupbuyGoods_info[$item['id']]['activity_price'];
|
|
// SKU库存替换抢购库存
|
|
$item['stock'] = $GroupbuyGoods_info[$item['id']]['stock'];
|
|
$item['limited_num'] =$GroupbuyGoods_info[$item['id']]['limited_num'];
|
|
}
|
|
|
|
// 如果当前用户参与了抢购的活动(则不允许再次抢购)
|
|
// $in_activity = Order::where(['user_id' => $userId, 'order_type' => 4])->where('order_status', '<', 4)->find();
|
|
//
|
|
// $goods['activity'] = [
|
|
// 'type' => 4,
|
|
// 'type_desc' => '抢购商品',
|
|
// 'in_groupbuy_activity' => (bool)$in_activity,
|
|
// 'msg' => '您已参与过本次活动,无法再次参与!'
|
|
// ];
|
|
|
|
// 2024-11-29 更改规则:活动中的每件商品用户都能购买,但是只能买一件
|
|
$in_activity = Order::alias('a')
|
|
->join('order_goods og', 'a.id = og.order_id')
|
|
->where(['a.user_id' => $userId, 'a.order_type' => 4, 'og.goods_id' => $goods['id']])
|
|
->where('a.order_status', '<', 4)
|
|
->find();
|
|
|
|
$goods['activity'] = [
|
|
'type' => 4,
|
|
'type_desc' => '抢购商品',
|
|
'in_groupbuy_activity' => !empty($in_activity),
|
|
'msg' => '您已购买过此商品!'
|
|
];
|
|
|
|
return $goods;
|
|
}
|
|
}
|