| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace addons\CloudStock\common\service;
- use addons\CloudStock\common\models\CloudStockRandFeeOrder;
- use common\enums\AddonsEnum;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use Exception;
- use Yii;
- use yii\base\Model;
- use yii\helpers\Json;
- class RandFeeService extends Model
- {
- /**
- * 商户ID
- *
- * @var integer
- */
- public $mall_id;
- /**
- * 满减
- *
- * @param array $params
- * [
- * 'mall_id' => mall_id
- * 'orders' => [order, order]
- * ]
- * @return void
- */
- public static function minus($params)
- {
- if (empty($params['mall_id'])) {
- return 0;
- // throw new Exception('mall-id 未设置');
- }
- $mall_id = $params['mall_id'];
- if (empty($params['orders'])) {
- return 0;
- // throw new Exception('orders 不存在');
- }
- $orders = $params['orders'];
- if (empty($params['user_id'])) {
- return 0;
- // throw new Exception('orders 不存在');
- }
- $user_id = $params['user_id'];
- // 判断订单类型
- foreach ($orders as $k => $order) {
- // 未定义订单类型的不处理
- if (empty($order['order_type'])) {
- return 0;
- }
- //是否为云库存订单
- if ($orders[$k]['order_type'] != AddonsEnum::ADDONS_NAME_CLOUD_STOCK) {
- return 0;
- }
- //是否为补货订单
- if(substr($order['order_no'],0,3) != 'CSF'){
- return 0;
- }
- }
- // 获取设置
- $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
- if (empty($configs['is_enable_rand_fee'])) {
- return 0;
- }
- $condition = json_decode($configs['rand_condition'],true);
- if (empty($condition)) {
- return 0;
- }
- // 订单
- $total_price = 0;
- foreach ($orders as $order) {
- $total_price = bcadd($order['amount'], $total_price, 2);
- }
- // 获取随机减金额
- $min = 0;
- $max = 0;
- foreach ($condition as $val) {
- if ($total_price >= $val['total_price']) {
- $min = bcmul($val['rand_min'], 100, 2);
- $max = bcmul($val['rand_max'], 100, 2);
- }
- }
- if ($min == 0 && $max == 0) {
- return 0;
- }
- // 随机减金额
- $left = $rand_fee = bcdiv(mt_rand($min, $max), 100, 2);
- // 分配随机减金额
- $models = [];
- foreach ($orders as $order) {
- // 分配的金额
- $order_rand_fee = $rand_fee / ($total_price / $order['amount']);
- // 小于最小单位
- $order_rand_fee = $order_rand_fee < 0.01 ? 0.01 : $order_rand_fee;
- // 判断剩余为0时候 不使用最小单位
- $order_rand_fee = $left == 0 ? 0 : $order_rand_fee;
- $result = CloudStockRandFeeOrder::find()->where(['order_no'=>$order['order_no'],'user_id'=>$order['user_id'],'mall_id'=>$mall_id])->one();
- if(!empty($result)){
- $rand_fee = 0;
- $order_rand_fee = 0;
- continue;
- }
- // 创建订单
- ($model = new CloudStockRandFeeOrder())->load([
- 'mall_id' => $mall_id,
- 'order_id' => $order['order_id'] ?: 0,
- 'order_no' => $order['order_no'],
- 'user_id' => $user_id,
- 'rand_fee' => $order_rand_fee, // 订单随机减
- 'total_rand_fee' => $rand_fee, // 随机减
- 'order_type' => $order['order_type'],
- 'created_at' => time(),
- ], '');
- $res = $model->save();
- if ($res === false) throw new Exception('随机减订单生产失败');
- $models[] = $model;
- // 减少剩余的
- $left = bcsub($rand_fee, $order_rand_fee, 2);
- }
- return [$rand_fee, $models, [
- sprintf('%.2f', bcdiv($min, 100, 2)),
- sprintf('%.2f', bcdiv($max, 100, 2))
- ]];
- }
- }
|