QcMjSendActivity.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace app\api\model\qc;
  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\qc\QcMjSendActivity as QcMjSendActivityModel;
  10. /**
  11. * 全场满件送赠品活动模型
  12. * @package app\store\model\coupon
  13. */
  14. class QcMjSendActivity extends QcMjSendActivityModel
  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[] = ['ac.start_time','<',$now];
  29. $filter[] = ['ac.end_time','>',$now];
  30. $filter[] = ['ac.audit_status','=',1];
  31. $filter[] = ['ac.status','=',1];
  32. $filter[] = ['g.goods_id','=',$goodsId];
  33. // 执行查询
  34. $list = self::field('ac.*')
  35. ->with(['goodsExcept','giftGoods'])
  36. ->alias('ac')
  37. ->leftJoin('qc_mj_send_goods g','g.qc_mj_send_activity_id = ac.id')
  38. ->where($filter)
  39. ->find();
  40. if (!$list)return null;
  41. $list = $list->toArray();
  42. //判断商品还能不能使用活动
  43. foreach ($list['goodsExcept'] as $key=>$g){
  44. if ($g['goods_id']==$goodsId){
  45. if($g['residue_stock']<=0){
  46. return null;
  47. }else{
  48. $mj_props = array_values(array_sort($list['mj_props'], 'value'));
  49. if($mj_props[0]['value']>$g['residue_stock']){//剩余数量小于最小的赠送
  50. return null;
  51. }
  52. }
  53. }
  54. }
  55. $list['empty_gifts'] = false;//当所有的赠品库存都是0:true
  56. $giftsGoods = null;
  57. $count = count($list['giftGoods']);
  58. foreach ($list['giftGoods'] as $key=>$g){
  59. if ($g['goods_info']['stock_total'] < 1){
  60. $count--;
  61. }else{
  62. $giftsGoods[] = $g;//库存大于0的才展示这个赠品
  63. }
  64. }
  65. unset($list['giftGoods']);
  66. if ($count == 0){//
  67. $list['empty_gifts'] = true;
  68. }
  69. $list['gift_goods'] = $giftsGoods;
  70. $list['remain_secs'] = strtotime($list['end_time']) - time();
  71. return $list;
  72. }
  73. /**
  74. * 获取确认订单赠送商品列表
  75. * @param $orderGoodList
  76. * @return mixed
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public static function validActivityList(&$orderGoodList,&$order)
  82. {
  83. $order['is_qc_send_send'] = 0; //是否使用了全场满件送活动
  84. $order['qc_send_activity_id'] = 0;//全场满件送活动ID
  85. // 检索查询条件
  86. $now = date('Y-m-d H:i:s');
  87. $filter[] = ['start_time','<',$now];
  88. $filter[] = ['end_time','>',$now];
  89. $filter[] = ['status','=',1];
  90. $filter[] = ['audit_status','=',1];
  91. // 执行查询当前进行中的活动
  92. $list = self::with(['goodsExcept','giftGoods'])->where($filter)->find();
  93. if (!$list) return null;
  94. $list = $list->toArray();
  95. //如果使用了会员折扣又不叠加会员折扣
  96. if($order['isUserMember']>0 && stripos($list['overlay_discount'],'2') !== false){
  97. return null;
  98. }
  99. //如果使用了优惠券又不叠加优惠券
  100. if($order['couponId']>0 && stripos($list['overlay_discount'],'1') !== false){
  101. return null;
  102. }
  103. //如果使用了推荐官专属折扣又不叠加专属折扣
  104. if($order['isUserDistributor']>0 && stripos($list['overlay_discount'],'3') !== false){
  105. return null;
  106. }
  107. $exceptGoodsIds = helper::getArrayColumn($list['goodsExcept'], 'goods_id');//获取全场满件送活动商品ID集合
  108. $goodsIds = [];
  109. // 整理当前订单所有商品ID集
  110. $orderGoodsIds = helper::getArrayColumn($orderGoodList, 'goods_id');
  111. $goodsIds = array_intersect($orderGoodsIds,$exceptGoodsIds);//获取当前订单参与全场满件送的商品名称
  112. $goodNum = 0; //参与全场满件送活动的商品总数
  113. $set_count = 0; //赠送当前几套赠品数量
  114. // 获取当前商品列表的订单金额
  115. foreach ($orderGoodList as $good){
  116. if(in_array($good['goods_id'],$goodsIds)){
  117. //如果没有使用N件X折活动,或者可以叠加全场满件送活动
  118. //如果库存没有
  119. $good_num = $good['total_num'];
  120. $good['qc_send_use_count'] = 0;
  121. foreach ($list['goodsExcept'] as $item){
  122. if($item['goods_id']==$good['goods_id']){
  123. if($item['residue_stock']<$good['total_num']){
  124. $good_num = $item['residue_stock'];
  125. }
  126. }
  127. }
  128. if($good['is_activity_discount']==0 || stripos($good['activity_discount_overlay'],'4') !== false){
  129. $good['qc_send_use_count'] = $good_num;
  130. $goodNum += $good_num;
  131. }
  132. // $goodFullPrice += $good['total_price'];
  133. }
  134. }
  135. $mj_props = array_values(array_sort($list['mj_props'], 'value', true));
  136. foreach ($mj_props as $arr){
  137. if($goodNum<$arr['value']){//商品数量小于当前活动满件送的最小数量
  138. continue;
  139. }else{
  140. $set_count = $arr['count'];
  141. break;//终止循环
  142. }
  143. }
  144. if($set_count<1){//赠送赠品数量为0
  145. foreach ($orderGoodList as $good){
  146. $good['qc_send_use_count'] = 0;
  147. }
  148. return null;
  149. }
  150. $qcMjSendGoodList = []; //全场满件送商品
  151. $giveGoodList = [];//满就送+全场满件送商品数量总和
  152. $order['is_qc_send_send'] = 1;
  153. $order['qc_send_activity_id'] = $list['id'];
  154. foreach ($orderGoodList as $good){
  155. $good['is_qc_send_send'] = 0;
  156. $good['qc_send_activity_id'] = 0;
  157. $good['qc_send_count'] = 0;
  158. if(in_array($good['goods_id'],$goodsIds)){
  159. $good['is_qc_send_send'] = 1;
  160. $good['qc_send_activity_id'] = $list['id'];
  161. $good['qc_send_count'] = $set_count;
  162. }
  163. if($good['goods_type']==GoodsType::GIFT){
  164. if(isset($giveGoodList[$good['goods_id']])){
  165. $giveGoodList[$good['goods_id']] += $good['total_num'];
  166. }else{
  167. $giveGoodList[$good['goods_id']] = $good['total_num'];
  168. }
  169. }
  170. }
  171. //获取赠送商品ID集
  172. $giftGoodsIds = helper::getArrayColumn($list['giftGoods'], 'goods_id');
  173. $model = new GoodsModel;
  174. $goodsList = $model->getListByIdsFromApi($giftGoodsIds);
  175. // 隐藏冗余的属性
  176. $goodsList->hidden(array_merge($model->hidden, ['content', 'goods_images', 'images']));
  177. // 返回商品处理
  178. $orderGoodListNew = $orderGoodList;
  179. foreach ($goodsList as &$item) {
  180. // 商品sku信息
  181. $item['skuInfo'] = GoodsSkuModel::detail($item['goods_id'], 0);//赠品都是单规格,sku=0
  182. // 商品单价
  183. $item['skuInfo']['goods_price'] = 0.00;
  184. $item['goods_price'] = $item['skuInfo']['goods_price'];
  185. // 商品购买数量
  186. $item['total_num'] = $set_count;
  187. $item['is_qc_send_send'] = 1;
  188. $item['qc_send_activity_id'] = $list['id'];
  189. // 商品SKU索引
  190. $item['goods_sku_id'] = $item['skuInfo']['goods_sku_id'];
  191. // 商品购买总金额
  192. $item['total_price'] = helper::bcmul($item['goods_price'], 1);
  193. $item['staffUserId'] = 0;
  194. $item['shopId'] = 0;
  195. $item['goods_type'] = GoodsType::GIFT;
  196. $item['distributorRadio'] = 0;
  197. if(isset($giveGoodList[$item['goods_id']])){
  198. $giveGoodList[$item['goods_id']] += $set_count;
  199. }else{
  200. $giveGoodList[$item['goods_id']] = $set_count;
  201. }
  202. if($order['delivery'] == DeliveryTypeEnum::SHOPS_DELIVERY){
  203. $ShopGoodsSku = ShopGoodsSku::get(['goods_id'=>$item['goods_id'],'goods_sku_id'=>0,'shop_id'=>$order['shopInfo']['shop_id']]);
  204. if(!empty($ShopGoodsSku) && $ShopGoodsSku['stock_num'] && $giveGoodList[$item['goods_id']]<=$ShopGoodsSku['stock_num']){//赠品有库存才出现在确认订单
  205. $orderGoodListNew[] = $item;
  206. }
  207. }else{
  208. if($item['skuInfo']['stock_num'] && $giveGoodList[$item['goods_id']]<=$item['skuInfo']['stock_num']){//赠品有库存才出现在确认订单
  209. $orderGoodListNew[] = $item;
  210. }
  211. }
  212. if(in_array($item['goods_id'],$orderGoodsIds)){
  213. $qcMjSendGoodList[$item['goods_id']] = $giveGoodList[$item['goods_id']];
  214. }
  215. }
  216. $orderGoodList = $orderGoodListNew;
  217. return $qcMjSendGoodList;
  218. }
  219. }