FullSendActivity.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\api\model\fullsend;
  3. use app\api\model\Goods as GoodsModel;
  4. use app\api\model\GoodsSku as GoodsSkuModel;
  5. use app\api\model\ShopGoodsSku;
  6. use app\common\enum\goods\GoodsType;
  7. use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
  8. use app\common\library\helper;
  9. use app\common\model\fullsend\FullSendActivity as FullSendActivityModel;
  10. /**
  11. * 满就送活动模型
  12. * @package app\store\model\coupon
  13. */
  14. class FullSendActivity extends FullSendActivityModel
  15. {
  16. /**
  17. * 获取列表
  18. * @param $goodsId
  19. * @return mixed
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\DbException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. */
  24. public static function validActivity($goodsId)
  25. {
  26. // 检索查询条件
  27. $now = date('Y-m-d H:i:s');
  28. $filter[] = ['start_time','<',$now];
  29. $filter[] = ['end_time','>',$now];
  30. $filter[] = ['audit_status','=',1];
  31. $filter[] = ['status','=',1];
  32. // 执行查询
  33. $list = self::where($filter)->find();
  34. if (!$list)return null;
  35. $list = $list->toArray();
  36. //判断商品是否在此有效活动中
  37. $flag = FullSendGoods::justiceGoods($list['id'],$list['is_except'],$goodsId);
  38. $giftsGoods = null;
  39. $list['empty_gifts'] = false;//当所有的赠品库存都是0:true
  40. if ($flag){
  41. if ($flag['goods_id']==$goodsId && $flag['residue_stock']<=0){//满就送没有阶梯数据
  42. return null;
  43. }
  44. //寻找活动赠品
  45. $gifts = FullSendGiftGoods::gifts($list['id']);
  46. $count = count($gifts);
  47. if ($count){
  48. foreach ($gifts as $key=>$g){
  49. if ($g['goods']['stock_total'] == 0){
  50. $count--;
  51. }else{
  52. $giftsGoods[] = $g;//库存大于0的才展示这个赠品
  53. }
  54. }
  55. if ($count == 0){//
  56. $list['empty_gifts'] = true;
  57. }
  58. }
  59. }else{
  60. return null;
  61. }
  62. $list['remain_secs'] = strtotime($list['end_time']) - time();
  63. $list['gift_goods'] = $giftsGoods;
  64. return $list;
  65. }
  66. /**
  67. * 获取确认订单赠送商品列表
  68. * @param $orderGoodList
  69. * @return mixed
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public static function validActivityList(&$orderGoodList,&$order)
  75. {
  76. $order['is_full_send'] = 0;
  77. $order['full_send_activity_id'] = 0;
  78. // 检索查询条件
  79. $now = date('Y-m-d H:i:s');
  80. $filter[] = ['start_time','<',$now];
  81. $filter[] = ['end_time','>',$now];
  82. $filter[] = ['status','=',1];
  83. $filter[] = ['audit_status','=',1];
  84. // 执行查询
  85. $list = self::with(['goodsExcept','giftGoods'])->where($filter)->find();
  86. if (!$list) return null;
  87. $list = $list->toArray();
  88. //如果使用了会员折扣又不叠加会员折扣
  89. if($order['isUserMember']>0 && stripos($list['overlay_type'],'2') !== false){
  90. return null;
  91. }
  92. //如果使用了优惠券又不叠加优惠券
  93. if($order['couponId']>0 && stripos($list['overlay_type'],'1') !== false){
  94. return null;
  95. }
  96. //如果使用了推荐官专属折扣又不叠加专属折扣
  97. if($order['isUserDistributor']>0 && stripos($list['overlay_type'],'3') !== false){
  98. return null;
  99. }
  100. $exceptGoodsIds = helper::getArrayColumn($list['goodsExcept'], 'goods_id');
  101. $goodsIds = [];
  102. // 整理当前订单所有商品ID集
  103. $orderGoodsIds = helper::getArrayColumn($orderGoodList, 'goods_id');
  104. if($list['is_except']){//除外
  105. $goodsIds = array_diff($orderGoodsIds,$exceptGoodsIds);
  106. }else{
  107. $goodsIds = array_intersect($orderGoodsIds,$exceptGoodsIds);
  108. }
  109. $goodFullPrice = 0;
  110. // 获取当前商品列表的订单金额
  111. foreach ($orderGoodList as $good){
  112. $good['full_send_count'] = 0;
  113. if(in_array($good['goods_id'],$goodsIds)){
  114. //如果库存没有
  115. $good_num = $good['total_num'];
  116. foreach ($list['goodsExcept'] as $item){
  117. if($item['goods_id']==$good['goods_id'] && $list['is_except']==0){
  118. if($item['residue_stock']<$good['total_num']){
  119. $good_num = $item['residue_stock'];
  120. }
  121. }
  122. }
  123. $good['full_send_count'] = $good_num;
  124. if($good['is_activity_discount']==0 || stripos($good['activity_discount_overlay'],'2') !== false){
  125. $goodFullPrice += helper::bcmul($good['goods_price'], $good_num);
  126. }
  127. // $goodFullPrice += $good['total_price'];
  128. }
  129. }
  130. if($list['full_money']>$goodFullPrice){//如果商品金额不满足订单金额
  131. foreach ($orderGoodList as $good){
  132. $good['full_send_count'] = 0;
  133. }
  134. return null;
  135. }
  136. $order['is_full_send'] = 1;
  137. $order['full_send_activity_id'] = $list['id'];
  138. foreach ($orderGoodList as $good){
  139. $good['is_full_send'] = 0;
  140. $good['full_send_activity_id'] = 0;
  141. if(in_array($good['goods_id'],$goodsIds)){
  142. $good['is_full_send'] = 1;
  143. $good['full_send_activity_id'] = $list['id'];
  144. }
  145. }
  146. //获取赠送商品ID集
  147. $giftGoodsIds = helper::getArrayColumn($list['giftGoods'], 'goods_id');
  148. $model = new GoodsModel;
  149. $goodsList = $model->getListByIdsFromApi($giftGoodsIds);
  150. // 隐藏冗余的属性
  151. $goodsList->hidden(array_merge($model->hidden, ['content', 'goods_images', 'images']));
  152. // 返回商品处理
  153. $orderGoodListNew = $orderGoodList;
  154. foreach ($goodsList as &$item) {
  155. // 商品sku信息
  156. $item['skuInfo'] = GoodsSkuModel::detail($item['goods_id'], 0);//赠品都是单规格,sku=0
  157. // 商品单价
  158. $item['skuInfo']['goods_price'] = 0.00;
  159. $item['goods_price'] = $item['skuInfo']['goods_price'];
  160. // 商品购买数量
  161. $item['total_num'] = 1;
  162. $item['is_full_send'] = 1;
  163. $item['full_send_activity_id'] = $list['id'];
  164. // 商品SKU索引
  165. $item['goods_sku_id'] = $item['skuInfo']['goods_sku_id'];
  166. // 商品购买总金额
  167. $item['total_price'] = helper::bcmul($item['goods_price'], 1);
  168. $item['staffUserId'] = 0;
  169. $item['shopId'] = 0;
  170. $item['goods_type'] = GoodsType::GIFT;
  171. $item['distributorRadio'] = 0;
  172. if($order['delivery'] == DeliveryTypeEnum::SHOPS_DELIVERY){
  173. $ShopGoodsSku = ShopGoodsSku::get(['goods_id'=>$item['goods_id'],'goods_sku_id'=>0,'shop_id'=>$order['shopInfo']['shop_id']]);
  174. if(!empty($ShopGoodsSku) && $ShopGoodsSku['stock_num']){//赠品有库存才出现在确认订单
  175. $orderGoodListNew[] = $item;
  176. }
  177. }else{
  178. if($item['skuInfo']['stock_num']){//赠品有库存才出现在确认订单
  179. $orderGoodListNew[] = $item;
  180. }
  181. }
  182. }
  183. $orderGoodList = $orderGoodListNew;
  184. return true;
  185. }
  186. }