DiscountLogic.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\logic;
  20. use app\common\enum\DiscountEnum;
  21. use app\common\model\DiscountGoods;
  22. use app\common\model\DiscountGoodsItem;
  23. use app\common\model\Goods;
  24. use app\common\model\User;
  25. use app\common\model\UserLevel;
  26. /**
  27. * 会员折扣逻辑层
  28. * Class DiscountLogic
  29. * @package app\common\logic
  30. */
  31. class DiscountLogic
  32. {
  33. /**
  34. * @notes 生成会员等级和商品规格的数据
  35. * @param $goods
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. * @author cjhao
  41. * @date 2022/5/6 18:51
  42. */
  43. public static function leveGoodsItem($goods)
  44. {
  45. $userLevel = UserLevel::field('id,name,discount')->order('rank asc')->select();
  46. $discountGoods = DiscountGoodsItem::where(['goods_id' => $goods->id])->column('discount_price', 'CONCAT_WS(\'-\',level,item_id)');
  47. $specValueList = $goods->spec_value_list;
  48. foreach ($userLevel as $key => $level) {
  49. $goodsItem = [];
  50. foreach ($specValueList as $specValue) {
  51. $searchKey = $level->id . '-' . $specValue->id;
  52. //折扣价
  53. $discountPrice = $discountGoods[$searchKey] ?? $specValue->sell_price;
  54. $goodsItem[] = [
  55. 'item_id' => $specValue['id'],
  56. 'spec_value_str' => $specValue->spec_value_str,
  57. 'sell_price' => $specValue->sell_price,
  58. 'discount_price' => $discountPrice,
  59. ];
  60. }
  61. $userLevel[$key]['goods_item'] = $goodsItem;
  62. }
  63. return $userLevel->toArray();
  64. }
  65. /**
  66. * @notes 根据用户当前等级获取商品折扣价 tips:如果商品没折扣价则不返回
  67. * @param $userId
  68. * @param $goodsIds
  69. * @return array
  70. * @author cjhao
  71. * @date 2022/5/7 15:09
  72. * tips: 返回数据格式 =>[
  73. * 'goods_id' => [
  74. * '规格id' => []
  75. * ]
  76. * ]
  77. */
  78. public static function getGoodsDiscount(int $userId, array $goodsIds)
  79. {
  80. $userLevel = User::where(['id' => $userId])
  81. ->field('id,level')
  82. ->with('user_level')
  83. ->find();
  84. $levelDiscount = $userLevel->discount;
  85. $discountGoods = DiscountGoods::where(['goods_id' => $goodsIds])->column('*', 'goods_id');
  86. $discountGoodsItem = DiscountGoodsItem::where(['goods_id' => $goodsIds,'level'=>$userLevel->level])->column('discount_price', 'item_id');
  87. $goodsLists = Goods::where(['id' => $goodsIds])->with('spec_value_list')->field('id')->select()->toArray();
  88. $discountData = [];
  89. /**
  90. * 1.判断是否参与折扣,否-直接跳过
  91. * 2.判断当前会员等级是否有折扣权益,否-看该商品是否自定义折扣
  92. */
  93. foreach ($goodsLists as $goods) {
  94. $discount = $discountGoods[$goods['id']] ?? [];
  95. if (empty($discount)) {
  96. continue;
  97. }
  98. //没参与折扣
  99. if (DiscountEnum::DISCOUNT_NOT_JOIN == $discount['is_discount']) {
  100. continue;
  101. }
  102. //参与折扣,当前等级没有折扣权益
  103. if (DiscountEnum::DISCOUNT_RULE_LEVEL == $discount['discount_rule'] && $levelDiscount <= 0) {
  104. continue;
  105. }
  106. foreach ($goods['spec_value_list'] as $goodsItem) {
  107. if (DiscountEnum::DISCOUNT_RULE_LEVEL == $discount['discount_rule']) {
  108. //参与折扣,按等级折扣计算
  109. $discountPrice = round($goodsItem['sell_price'] * ($levelDiscount / 10), 2);
  110. } else {
  111. //自定义会员价
  112. $discountPrice = $discountGoodsItem[$goodsItem['id']] ?? '';
  113. }
  114. //只返回折扣价大于等于零的
  115. if($discountPrice >= 0) {
  116. $discountData[$goodsItem['goods_id']][$goodsItem['id']] = [
  117. 'sell_price' => $goodsItem['sell_price'], //售价
  118. 'discount_price' => $discountPrice, //折扣价
  119. ];
  120. }
  121. }
  122. }
  123. return $discountData;
  124. }
  125. }