123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- namespace app\api\model\qc;
- 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\qc\QcMjSendActivity as QcMjSendActivityModel;
- /**
- * 全场满件送赠品活动模型
- * @package app\store\model\coupon
- */
- class QcMjSendActivity extends QcMjSendActivityModel
- {
- /**
- * 获取列表
- * @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[] = ['ac.start_time','<',$now];
- $filter[] = ['ac.end_time','>',$now];
- $filter[] = ['ac.audit_status','=',1];
- $filter[] = ['ac.status','=',1];
- $filter[] = ['g.goods_id','=',$goodsId];
- // 执行查询
- $list = self::field('ac.*')
- ->with(['goodsExcept','giftGoods'])
- ->alias('ac')
- ->leftJoin('qc_mj_send_goods g','g.qc_mj_send_activity_id = ac.id')
- ->where($filter)
- ->find();
- if (!$list)return null;
- $list = $list->toArray();
- //判断商品还能不能使用活动
- foreach ($list['goodsExcept'] as $key=>$g){
- if ($g['goods_id']==$goodsId){
- if($g['residue_stock']<=0){
- return null;
- }else{
- $mj_props = array_values(array_sort($list['mj_props'], 'value'));
- if($mj_props[0]['value']>$g['residue_stock']){//剩余数量小于最小的赠送
- return null;
- }
- }
- }
- }
- $list['empty_gifts'] = false;//当所有的赠品库存都是0:true
- $giftsGoods = null;
- $count = count($list['giftGoods']);
- foreach ($list['giftGoods'] as $key=>$g){
- if ($g['goods_info']['stock_total'] < 1){
- $count--;
- }else{
- $giftsGoods[] = $g;//库存大于0的才展示这个赠品
- }
- }
- unset($list['giftGoods']);
- if ($count == 0){//
- $list['empty_gifts'] = true;
- }
- $list['gift_goods'] = $giftsGoods;
- $list['remain_secs'] = strtotime($list['end_time']) - time();
- 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_qc_send_send'] = 0; //是否使用了全场满件送活动
- $order['qc_send_activity_id'] = 0;//全场满件送活动ID
- // 检索查询条件
- $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_discount'],'2') !== false){
- return null;
- }
- //如果使用了优惠券又不叠加优惠券
- if($order['couponId']>0 && stripos($list['overlay_discount'],'1') !== false){
- return null;
- }
- //如果使用了推荐官专属折扣又不叠加专属折扣
- if($order['isUserDistributor']>0 && stripos($list['overlay_discount'],'3') !== false){
- return null;
- }
- $exceptGoodsIds = helper::getArrayColumn($list['goodsExcept'], 'goods_id');//获取全场满件送活动商品ID集合
- $goodsIds = [];
- // 整理当前订单所有商品ID集
- $orderGoodsIds = helper::getArrayColumn($orderGoodList, 'goods_id');
- $goodsIds = array_intersect($orderGoodsIds,$exceptGoodsIds);//获取当前订单参与全场满件送的商品名称
- $goodNum = 0; //参与全场满件送活动的商品总数
- $set_count = 0; //赠送当前几套赠品数量
- // 获取当前商品列表的订单金额
- foreach ($orderGoodList as $good){
- if(in_array($good['goods_id'],$goodsIds)){
- //如果没有使用N件X折活动,或者可以叠加全场满件送活动
- //如果库存没有
- $good_num = $good['total_num'];
- $good['qc_send_use_count'] = 0;
- foreach ($list['goodsExcept'] as $item){
- if($item['goods_id']==$good['goods_id']){
- if($item['residue_stock']<$good['total_num']){
- $good_num = $item['residue_stock'];
- }
- }
- }
- if($good['is_activity_discount']==0 || stripos($good['activity_discount_overlay'],'4') !== false){
- $good['qc_send_use_count'] = $good_num;
- $goodNum += $good_num;
- }
- // $goodFullPrice += $good['total_price'];
- }
- }
- $mj_props = array_values(array_sort($list['mj_props'], 'value', true));
- foreach ($mj_props as $arr){
- if($goodNum<$arr['value']){//商品数量小于当前活动满件送的最小数量
- continue;
- }else{
- $set_count = $arr['count'];
- break;//终止循环
- }
- }
- if($set_count<1){//赠送赠品数量为0
- foreach ($orderGoodList as $good){
- $good['qc_send_use_count'] = 0;
- }
- return null;
- }
- $qcMjSendGoodList = []; //全场满件送商品
- $giveGoodList = [];//满就送+全场满件送商品数量总和
- $order['is_qc_send_send'] = 1;
- $order['qc_send_activity_id'] = $list['id'];
- foreach ($orderGoodList as $good){
- $good['is_qc_send_send'] = 0;
- $good['qc_send_activity_id'] = 0;
- $good['qc_send_count'] = 0;
- if(in_array($good['goods_id'],$goodsIds)){
- $good['is_qc_send_send'] = 1;
- $good['qc_send_activity_id'] = $list['id'];
- $good['qc_send_count'] = $set_count;
- }
- if($good['goods_type']==GoodsType::GIFT){
- if(isset($giveGoodList[$good['goods_id']])){
- $giveGoodList[$good['goods_id']] += $good['total_num'];
- }else{
- $giveGoodList[$good['goods_id']] = $good['total_num'];
- }
- }
- }
- //获取赠送商品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'] = $set_count;
- $item['is_qc_send_send'] = 1;
- $item['qc_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(isset($giveGoodList[$item['goods_id']])){
- $giveGoodList[$item['goods_id']] += $set_count;
- }else{
- $giveGoodList[$item['goods_id']] = $set_count;
- }
- 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'] && $giveGoodList[$item['goods_id']]<=$ShopGoodsSku['stock_num']){//赠品有库存才出现在确认订单
- $orderGoodListNew[] = $item;
- }
- }else{
- if($item['skuInfo']['stock_num'] && $giveGoodList[$item['goods_id']]<=$item['skuInfo']['stock_num']){//赠品有库存才出现在确认订单
- $orderGoodListNew[] = $item;
- }
- }
- if(in_array($item['goods_id'],$orderGoodsIds)){
- $qcMjSendGoodList[$item['goods_id']] = $giveGoodList[$item['goods_id']];
- }
- }
- $orderGoodList = $orderGoodListNew;
- return $qcMjSendGoodList;
- }
- }
|