RandFeeService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace addons\CloudStock\common\service;
  3. use addons\CloudStock\common\models\CloudStockRandFeeOrder;
  4. use common\enums\AddonsEnum;
  5. use common\models\order\OrderDetail;
  6. use common\models\user\User;
  7. use Exception;
  8. use Yii;
  9. use yii\base\Model;
  10. use yii\helpers\Json;
  11. class RandFeeService extends Model
  12. {
  13. /**
  14. * 商户ID
  15. *
  16. * @var integer
  17. */
  18. public $mall_id;
  19. /**
  20. * 满减
  21. *
  22. * @param array $params
  23. * [
  24. * 'mall_id' => mall_id
  25. * 'orders' => [order, order]
  26. * ]
  27. * @return void
  28. */
  29. public static function minus($params)
  30. {
  31. if (empty($params['mall_id'])) {
  32. return 0;
  33. // throw new Exception('mall-id 未设置');
  34. }
  35. $mall_id = $params['mall_id'];
  36. if (empty($params['orders'])) {
  37. return 0;
  38. // throw new Exception('orders 不存在');
  39. }
  40. $orders = $params['orders'];
  41. if (empty($params['user_id'])) {
  42. return 0;
  43. // throw new Exception('orders 不存在');
  44. }
  45. $user_id = $params['user_id'];
  46. // 判断订单类型
  47. foreach ($orders as $k => $order) {
  48. // 未定义订单类型的不处理
  49. if (empty($order['order_type'])) {
  50. return 0;
  51. }
  52. //是否为云库存订单
  53. if ($orders[$k]['order_type'] != AddonsEnum::ADDONS_NAME_CLOUD_STOCK) {
  54. return 0;
  55. }
  56. //是否为补货订单
  57. if(substr($order['order_no'],0,3) != 'CSF'){
  58. return 0;
  59. }
  60. }
  61. // 获取设置
  62. $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
  63. if (empty($configs['is_enable_rand_fee'])) {
  64. return 0;
  65. }
  66. $condition = json_decode($configs['rand_condition'],true);
  67. if (empty($condition)) {
  68. return 0;
  69. }
  70. // 订单
  71. $total_price = 0;
  72. foreach ($orders as $order) {
  73. $total_price = bcadd($order['amount'], $total_price, 2);
  74. }
  75. // 获取随机减金额
  76. $min = 0;
  77. $max = 0;
  78. foreach ($condition as $val) {
  79. if ($total_price >= $val['total_price']) {
  80. $min = bcmul($val['rand_min'], 100, 2);
  81. $max = bcmul($val['rand_max'], 100, 2);
  82. }
  83. }
  84. if ($min == 0 && $max == 0) {
  85. return 0;
  86. }
  87. // 随机减金额
  88. $left = $rand_fee = bcdiv(mt_rand($min, $max), 100, 2);
  89. // 分配随机减金额
  90. $models = [];
  91. foreach ($orders as $order) {
  92. // 分配的金额
  93. $order_rand_fee = $rand_fee / ($total_price / $order['amount']);
  94. // 小于最小单位
  95. $order_rand_fee = $order_rand_fee < 0.01 ? 0.01 : $order_rand_fee;
  96. // 判断剩余为0时候 不使用最小单位
  97. $order_rand_fee = $left == 0 ? 0 : $order_rand_fee;
  98. $result = CloudStockRandFeeOrder::find()->where(['order_no'=>$order['order_no'],'user_id'=>$order['user_id'],'mall_id'=>$mall_id])->one();
  99. if(!empty($result)){
  100. $rand_fee = 0;
  101. $order_rand_fee = 0;
  102. continue;
  103. }
  104. // 创建订单
  105. ($model = new CloudStockRandFeeOrder())->load([
  106. 'mall_id' => $mall_id,
  107. 'order_id' => $order['order_id'] ?: 0,
  108. 'order_no' => $order['order_no'],
  109. 'user_id' => $user_id,
  110. 'rand_fee' => $order_rand_fee, // 订单随机减
  111. 'total_rand_fee' => $rand_fee, // 随机减
  112. 'order_type' => $order['order_type'],
  113. 'created_at' => time(),
  114. ], '');
  115. $res = $model->save();
  116. if ($res === false) throw new Exception('随机减订单生产失败');
  117. $models[] = $model;
  118. // 减少剩余的
  119. $left = bcsub($rand_fee, $order_rand_fee, 2);
  120. }
  121. return [$rand_fee, $models, [
  122. sprintf('%.2f', bcdiv($min, 100, 2)),
  123. sprintf('%.2f', bcdiv($max, 100, 2))
  124. ]];
  125. }
  126. }