MarketingService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace addons\TinyShop\services\marketing;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use common\helpers\BcHelper;
  6. use common\helpers\ArrayHelper;
  7. use addons\TinyShop\common\enums\MarketingEnum;
  8. use addons\TinyShop\common\enums\DecimalReservationEnum;
  9. use addons\TinyShop\common\enums\DiscountTypeEnum;
  10. /**
  11. * Class MarketingService
  12. * @package addons\TinyShop\services\marketing
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class MarketingService
  16. {
  17. /**
  18. * 重组营销显示列表
  19. *
  20. * @param $list
  21. * @param $isMarketing
  22. * @return array
  23. */
  24. public function regroupList($list, $isMarketing = true)
  25. {
  26. $data = [];
  27. foreach ($list as $item) {
  28. $product = $item['product'];
  29. $product['tags'] = Json::decode($product['tags']);
  30. $product['marketing_type'] = $item['marketing_type'];
  31. $product['marketing_id'] = $item['marketing_id'];
  32. $product['marketing_tags'] = $isMarketing == true ? '' : [];
  33. unset($item['product']);
  34. $product['marketing'] = ArrayHelper::merge($item, Json::decode($item['marketing_data']));;
  35. unset($product['marketing']['marketing_data']);
  36. // 排除打包一口价和第二件半价价格
  37. if (!in_array($product['marketing']['marketing_type'], [MarketingEnum::SECOND_HALF_DISCOUNT, MarketingEnum::BALE])) {
  38. $product['marketing']['marketing_price'] = Yii::$app->tinyShopService->marketing->getPrice(
  39. $product['marketing']['discount'],
  40. $product['marketing']['discount_type'],
  41. 1,
  42. $product['price'],
  43. $product['marketing']['decimal_reservation_number']
  44. );
  45. } else {
  46. $product['marketing']['marketing_price'] = $product['price'];
  47. }
  48. $product['price'] = floatval($product['price']);
  49. $product['market_price'] = floatval($product['market_price']);
  50. $product['marketing']['marketing_price'] = floatval($product['marketing']['marketing_price']);
  51. $data[] = $product;
  52. unset($product);
  53. }
  54. return $data;
  55. }
  56. /**
  57. * 获取金额
  58. *
  59. * @param float $discount 营销内容
  60. * @param int $discount_type 营销类型
  61. * @param int $num
  62. * @param float $total_price
  63. * @param int $decimal_reservation_number
  64. * @return int|string|null
  65. */
  66. public function getPrice(
  67. $discount,
  68. $discount_type,
  69. $num,
  70. $total_price,
  71. $decimal_reservation_number = DecimalReservationEnum::DEFAULT
  72. ) {
  73. $newPrice = 0;
  74. // 营销方式
  75. switch ($discount_type) {
  76. // 减钱 (营销类型 * 数量)
  77. case DiscountTypeEnum::MONEY :
  78. $discount_money = BcHelper::mul($discount, $num);
  79. $newPrice = BcHelper::sub($total_price, $discount_money);
  80. break;
  81. // 折扣
  82. case DiscountTypeEnum::DISCOUNT :
  83. $rate = BcHelper::div($discount, 10, 10);
  84. $newPrice = BcHelper::mul($total_price, $rate);
  85. break;
  86. // 促销价 (营销类型 * 数量)
  87. case DiscountTypeEnum::FIXATION :
  88. $newPrice = BcHelper::mul($discount, $num);
  89. break;
  90. }
  91. switch ($decimal_reservation_number) {
  92. // 抹去分
  93. case DecimalReservationEnum::CLEAR_DECIMAL_ONE :
  94. $newPrice = BcHelper::mul($newPrice, 1, 1);
  95. break;
  96. // 抹去角和分
  97. case DecimalReservationEnum::CLEAR_DECIMAL_TWO :
  98. $newPrice = BcHelper::mul($newPrice, 1, 0);
  99. break;
  100. }
  101. return $newPrice;
  102. }
  103. /**
  104. * 获取营销说明
  105. *
  106. * @param $marketing
  107. * @return array
  108. */
  109. public function getTagByCartItem($marketing)
  110. {
  111. $marketing['discount'] = floatval($marketing['discount']);
  112. switch ($marketing['marketing_type']) {
  113. case MarketingEnum::BALE :
  114. return [
  115. $marketing['discount'] . '元' . $marketing['number'] . '件',
  116. '仅需' . $marketing['discount'] . '元,可任选' . $marketing['number'] . '件'
  117. ];
  118. case MarketingEnum::SECOND_HALF_DISCOUNT :
  119. $tag = '第' . $marketing['number'] . '件' . $marketing['discount'] . '折';
  120. if ($marketing['discount'] == 5) {
  121. $tag = '第' . $marketing['number'] . '件' . '半价';
  122. }
  123. if ($marketing['number'] == 2 && $marketing['discount'] == 0) {
  124. $tag = '买一送一';
  125. }
  126. if ($marketing['number'] > 2 && $marketing['discount'] == 0) {
  127. $tag = '第' . $marketing['number'] . '件' . '0元';
  128. }
  129. return [$tag, '可叠加使用优惠'];
  130. case MarketingEnum::DISCOUNT :
  131. $explain = '限时抢低价优惠';
  132. if ($marketing['discount_type'] == DiscountTypeEnum::MONEY) {
  133. $explain = '限时立减' . $marketing['discount'] . '元/件';
  134. }
  135. if ($marketing['discount_type'] == DiscountTypeEnum::DISCOUNT) {
  136. $explain = '限时打' . $marketing['discount'] . '折';
  137. }
  138. return ['限时抢', $explain];
  139. case MarketingEnum::PRE_SELL :
  140. return ['预售', ''];
  141. case MarketingEnum::SEC_KILL :
  142. return ['秒杀', ''];
  143. case MarketingEnum::BARGAIN :
  144. return ['砍价', ''];
  145. case MarketingEnum::GROUP_BUY :
  146. return ['团购', '最少购买' . $marketing['min_buy'] . '件可享优惠'];
  147. case MarketingEnum::WHOLESALE :
  148. // TODO 拼团返利
  149. return ['拼团', ''];
  150. default :
  151. return ['', ''];
  152. }
  153. }
  154. /**
  155. * 营销标签
  156. *
  157. * @param $marketing
  158. * @param string $local list:列表显示;marketing_list:营销列表显示;view:详情显示
  159. * @return array
  160. */
  161. public function getTags($marketing, $local = 'list')
  162. {
  163. isset($marketing['discount']) && $marketing['discount'] = floatval($marketing['discount']);
  164. $tags = [];
  165. switch ($marketing['marketing_type']) {
  166. case MarketingEnum::BALE :
  167. $tags[] = $marketing['discount'] . '元' . $marketing['number'] . '件';
  168. break;
  169. case MarketingEnum::SECOND_HALF_DISCOUNT :
  170. $tag = '第' . $marketing['number'] . '件' . $marketing['discount'] . '折';
  171. if ($marketing['discount'] == 5) {
  172. $tag = '第' . $marketing['number'] . '件' . '半价';
  173. }
  174. if ($marketing['number'] == 2 && $marketing['discount'] == 0) {
  175. $tag = '买一送一';
  176. }
  177. if ($marketing['number'] > 2 && $marketing['discount'] == 0) {
  178. $tag = '第' . $marketing['number'] . '件' . '0元';
  179. }
  180. $tags[] = $tag;
  181. break;
  182. case MarketingEnum::DISCOUNT :
  183. in_array($local, ['list', 'view']) && $tags[] = '限时抢';
  184. break;
  185. case MarketingEnum::PRE_SELL :
  186. $local == 'list' && $tags[] = '预售';
  187. break;
  188. case MarketingEnum::SEC_KILL :
  189. $local == 'list' && $tags[] = '秒杀';
  190. break;
  191. case MarketingEnum::BARGAIN :
  192. $local == 'list' && $tags[] = '砍价';
  193. break;
  194. case MarketingEnum::GROUP_BUY :
  195. $local == 'list' && $tags[] = '团购';
  196. $local == 'list' && $tags[] = $marketing['min_buy'] . '件起';
  197. break;
  198. case MarketingEnum::WHOLESALE :
  199. in_array($local, ['list', 'view']) && $tags[] = $marketing['number'] . '人团';
  200. break;
  201. }
  202. return $tags;
  203. }
  204. /**
  205. * 合并相同的营销显示
  206. *
  207. * @param array $data
  208. * @return array
  209. */
  210. public function mergeIdenticalMarketing($data = [])
  211. {
  212. if (empty($data)) {
  213. return [];
  214. }
  215. $marketing = [];
  216. foreach ($data as $datum) {
  217. $marketing_type = $datum['marketing_type'];
  218. $datum['discount_money'] = floatval($datum['discount_money']);
  219. if (
  220. !isset($marketing[$marketing_type]) &&
  221. isset($datum['discount_money']) &&
  222. $datum['discount_money'] > 0
  223. ) {
  224. $marketing[$marketing_type] = [
  225. 'discount_money' => 0,
  226. 'marketing_name' => isset($datum['marketing_name']) ? $datum['marketing_name'] : MarketingEnum::getValue($datum['marketing_type']),
  227. 'marketing_type' => $datum['marketing_type'],
  228. ];
  229. }
  230. if (
  231. isset($marketing[$marketing_type]) &&
  232. isset($datum['discount_money']) &&
  233. $datum['discount_money'] > 0
  234. ) {
  235. $marketing[$marketing_type]['discount_money'] = BcHelper::add($marketing[$marketing_type]['discount_money'],
  236. $datum['discount_money']);
  237. }
  238. // 单独计算积分
  239. if (
  240. !isset($marketing[MarketingEnum::GIVE_POINT]) &&
  241. isset($datum['give_point']) &&
  242. $datum['give_point'] > 0
  243. ) {
  244. $marketing[MarketingEnum::GIVE_POINT] = [
  245. 'discount_money' => 0,
  246. 'marketing_name' => MarketingEnum::getValue(MarketingEnum::GIVE_POINT),
  247. 'marketing_type' => MarketingEnum::GIVE_POINT,
  248. ];
  249. }
  250. if (
  251. isset($marketing[MarketingEnum::GIVE_POINT]) &&
  252. isset($datum['give_point']) &&
  253. $datum['give_point'] > 0
  254. ) {
  255. $marketing[MarketingEnum::GIVE_POINT]['discount_money'] = BcHelper::add($marketing[MarketingEnum::GIVE_POINT]['discount_money'],
  256. $datum['give_point'], 0);
  257. }
  258. // 单独计算成长值
  259. if (
  260. !isset($marketing[MarketingEnum::GIVE_GROWTH]) &&
  261. isset($datum['give_growth']) &&
  262. $datum['give_growth'] > 0
  263. ) {
  264. $marketing[MarketingEnum::GIVE_GROWTH] = [
  265. 'discount_money' => 0,
  266. 'marketing_name' => MarketingEnum::getValue(MarketingEnum::GIVE_GROWTH),
  267. 'marketing_type' => MarketingEnum::GIVE_GROWTH,
  268. ];
  269. }
  270. if (
  271. isset($marketing[MarketingEnum::GIVE_GROWTH]) &&
  272. isset($datum['give_growth']) &&
  273. $datum['give_growth'] > 0
  274. ) {
  275. $marketing[MarketingEnum::GIVE_GROWTH]['discount_money'] = BcHelper::add($marketing[MarketingEnum::GIVE_GROWTH]['discount_money'],
  276. $datum['give_growth'], 0);
  277. }
  278. // 其他显示
  279. if (in_array($marketing_type, [MarketingEnum::FULL_MAIL])) {
  280. $marketing[$marketing_type] = [
  281. 'discount_money' => 0,
  282. 'marketing_name' => $datum['marketing_condition'] ?? '',
  283. 'marketing_type' => $datum['marketing_type'],
  284. ];
  285. }
  286. }
  287. $return = [];
  288. foreach ($marketing as $value) {
  289. $return[] = $value;
  290. }
  291. return $return;
  292. }
  293. }
粤ICP备19079148号