123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace app\api\model\fullsend;
- use app\api\model\Goods as GoodsModel;
- use app\api\model\GoodsSku as GoodsSkuModel;
- use app\api\model\ShopGoodsSku;
- use app\common\enum\goods\GoodsType;
- use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
- use app\common\library\helper;
- use app\common\model\fullsend\FullSendActivity as FullSendActivityModel;
- /**
- * 满就送活动模型
- * @package app\store\model\coupon
- */
- class FullSendActivity extends FullSendActivityModel
- {
- /**
- * 获取列表
- * @param $goodsId
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function validActivity($goodsId)
- {
- // 检索查询条件
- $now = date('Y-m-d H:i:s');
- $filter[] = ['start_time','<',$now];
- $filter[] = ['end_time','>',$now];
- $filter[] = ['audit_status','=',1];
- $filter[] = ['status','=',1];
- // 执行查询
- $list = self::where($filter)->find();
- if (!$list)return null;
- $list = $list->toArray();
- //判断商品是否在此有效活动中
- $flag = FullSendGoods::justiceGoods($list['id'],$list['is_except'],$goodsId);
- $giftsGoods = null;
- $list['empty_gifts'] = false;//当所有的赠品库存都是0:true
- if ($flag){
- if ($flag['goods_id']==$goodsId && $flag['residue_stock']<=0){//满就送没有阶梯数据
- return null;
- }
- //寻找活动赠品
- $gifts = FullSendGiftGoods::gifts($list['id']);
- $count = count($gifts);
- if ($count){
- foreach ($gifts as $key=>$g){
- if ($g['goods']['stock_total'] == 0){
- $count--;
- }else{
- $giftsGoods[] = $g;//库存大于0的才展示这个赠品
- }
- }
- if ($count == 0){//
- $list['empty_gifts'] = true;
- }
- }
- }else{
- return null;
- }
- $list['remain_secs'] = strtotime($list['end_time']) - time();
- $list['gift_goods'] = $giftsGoods;
- return $list;
- }
- /**
- * 获取确认订单赠送商品列表
- * @param $orderGoodList
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function validActivityList(&$orderGoodList,&$order)
- {
- $order['is_full_send'] = 0;
- $order['full_send_activity_id'] = 0;
- // 检索查询条件
- $now = date('Y-m-d H:i:s');
- $filter[] = ['start_time','<',$now];
- $filter[] = ['end_time','>',$now];
- $filter[] = ['status','=',1];
- $filter[] = ['audit_status','=',1];
- // 执行查询
- $list = self::with(['goodsExcept','giftGoods'])->where($filter)->find();
- if (!$list) return null;
- $list = $list->toArray();
- //如果使用了会员折扣又不叠加会员折扣
- if($order['isUserMember']>0 && stripos($list['overlay_type'],'2') !== false){
- return null;
- }
- //如果使用了优惠券又不叠加优惠券
- if($order['couponId']>0 && stripos($list['overlay_type'],'1') !== false){
- return null;
- }
- //如果使用了推荐官专属折扣又不叠加专属折扣
- if($order['isUserDistributor']>0 && stripos($list['overlay_type'],'3') !== false){
- return null;
- }
- $exceptGoodsIds = helper::getArrayColumn($list['goodsExcept'], 'goods_id');
- $goodsIds = [];
- // 整理当前订单所有商品ID集
- $orderGoodsIds = helper::getArrayColumn($orderGoodList, 'goods_id');
- if($list['is_except']){//除外
- $goodsIds = array_diff($orderGoodsIds,$exceptGoodsIds);
- }else{
- $goodsIds = array_intersect($orderGoodsIds,$exceptGoodsIds);
- }
- $goodFullPrice = 0;
- // 获取当前商品列表的订单金额
- foreach ($orderGoodList as $good){
- $good['full_send_count'] = 0;
- if(in_array($good['goods_id'],$goodsIds)){
- //如果库存没有
- $good_num = $good['total_num'];
- foreach ($list['goodsExcept'] as $item){
- if($item['goods_id']==$good['goods_id'] && $list['is_except']==0){
- if($item['residue_stock']<$good['total_num']){
- $good_num = $item['residue_stock'];
- }
- }
- }
- $good['full_send_count'] = $good_num;
- if($good['is_activity_discount']==0 || stripos($good['activity_discount_overlay'],'2') !== false){
- $goodFullPrice += helper::bcmul($good['goods_price'], $good_num);
- }
- // $goodFullPrice += $good['total_price'];
- }
- }
- if($list['full_money']>$goodFullPrice){//如果商品金额不满足订单金额
- foreach ($orderGoodList as $good){
- $good['full_send_count'] = 0;
- }
- return null;
- }
- $order['is_full_send'] = 1;
- $order['full_send_activity_id'] = $list['id'];
- foreach ($orderGoodList as $good){
- $good['is_full_send'] = 0;
- $good['full_send_activity_id'] = 0;
- if(in_array($good['goods_id'],$goodsIds)){
- $good['is_full_send'] = 1;
- $good['full_send_activity_id'] = $list['id'];
- }
- }
- //获取赠送商品ID集
- $giftGoodsIds = helper::getArrayColumn($list['giftGoods'], 'goods_id');
- $model = new GoodsModel;
- $goodsList = $model->getListByIdsFromApi($giftGoodsIds);
- // 隐藏冗余的属性
- $goodsList->hidden(array_merge($model->hidden, ['content', 'goods_images', 'images']));
- // 返回商品处理
- $orderGoodListNew = $orderGoodList;
- foreach ($goodsList as &$item) {
- // 商品sku信息
- $item['skuInfo'] = GoodsSkuModel::detail($item['goods_id'], 0);//赠品都是单规格,sku=0
- // 商品单价
- $item['skuInfo']['goods_price'] = 0.00;
- $item['goods_price'] = $item['skuInfo']['goods_price'];
- // 商品购买数量
- $item['total_num'] = 1;
- $item['is_full_send'] = 1;
- $item['full_send_activity_id'] = $list['id'];
- // 商品SKU索引
- $item['goods_sku_id'] = $item['skuInfo']['goods_sku_id'];
- // 商品购买总金额
- $item['total_price'] = helper::bcmul($item['goods_price'], 1);
- $item['staffUserId'] = 0;
- $item['shopId'] = 0;
- $item['goods_type'] = GoodsType::GIFT;
- $item['distributorRadio'] = 0;
- if($order['delivery'] == DeliveryTypeEnum::SHOPS_DELIVERY){
- $ShopGoodsSku = ShopGoodsSku::get(['goods_id'=>$item['goods_id'],'goods_sku_id'=>0,'shop_id'=>$order['shopInfo']['shop_id']]);
- if(!empty($ShopGoodsSku) && $ShopGoodsSku['stock_num']){//赠品有库存才出现在确认订单
- $orderGoodListNew[] = $item;
- }
- }else{
- if($item['skuInfo']['stock_num']){//赠品有库存才出现在确认订单
- $orderGoodListNew[] = $item;
- }
- }
- }
- $orderGoodList = $orderGoodListNew;
- return true;
- }
- }
|