瀏覽代碼

计算优惠方法

shish 6 年之前
父節點
當前提交
6c53fcbb15

+ 1 - 1
app/client/controllers/BaseController.php

@@ -22,7 +22,7 @@ class BaseController extends PublicController
 	
 	public $validate = true;
 	public $withoutLogin = [];//不需要登陆可以访问的方法
-	public $userId, $user, $shopId;
+	public $userId = 0, $user, $shopId;
 	public $merchantId, $merchant, $merchantExtend;
 	public $isWx;
 	

+ 2 - 3
app/client/controllers/OrderController.php

@@ -246,7 +246,8 @@ class OrderController extends BaseController
 		$sourceType = 1;
 		$post['userId'] = $this->userId;
 		$prePrice = round($post['prePrice'], 2);
-		$actPrice = $prePrice;
+		$couponId = isset($post['couponId']) ? $post['couponId'] : 0;
+		$actPrice = OrderService::getDiscountPrice(['price' => $prePrice, 'couponId' => $couponId, 'userId' => $this->userId]);
 		$post['actPrice'] = $actPrice;
 		$post['payStyle'] = 0;
 		$post['merchantId'] = $this->merchantId;
@@ -264,8 +265,6 @@ class OrderController extends BaseController
 		$orderId = $order['id'];
 		$orderSn = $order['orderSn'];
 		$merchantId = $this->merchantId;
-		$couponId = isset($post['couponId']) ? $post['couponId'] : 0;
-		
 		if ($payWay == 0) {
 			$orderId = $order['id'];
 			$name = '购买商品';

+ 26 - 0
biz/order/classes/OrderClass.php

@@ -72,4 +72,30 @@ class OrderClass extends BaseClass
 		return $order;
 	}
 
+	//返回随机优惠金额 shish 2019.9.12
+	public static function getRandDiscount($amount)
+	{
+		if ($amount <= 0) {
+			return 0;
+		}
+		if ($amount >= 20000) {
+			$rand = rand(90, 100);
+		} elseif ($amount > 1000) {
+			$rand = rand(10, 30);
+		} elseif ($amount > 500) {
+			$rand = rand(10, 20);
+		} elseif ($amount > 260) {
+			$rand = rand(10, 15);
+		} elseif ($amount > 100) {
+			$rand = rand(5, 9);
+		} elseif ($amount > 50) {
+			$rand = rand(1, 3);
+		} elseif ($amount > 10) {
+			$rand = 1;
+		} else {
+			$rand = 0;
+		}
+		return $rand / 10;
+	}
+
 }

+ 39 - 0
biz/order/services/OrderService.php

@@ -8,8 +8,11 @@ use biz\merchant\services\MerchantService;
 use biz\order\classes\OrderClass;
 use biz\order\models\Order;
 use biz\promote\services\CouponRightService;
+use biz\promote\services\CouponService;
+use biz\user\classes\UserAssetClass;
 use biz\user\services\UserService;
 use common\components\error;
+use common\components\stringUtil;
 use common\components\validate;
 use common\components\util;
 use Yii;
@@ -170,4 +173,40 @@ class OrderService extends BaseService
 		return OrderClass::getOrderBySn($orderSn);
 	}
 	
+	//计算优惠 shish 2020.3.9
+	public static function getDiscountPrice($params)
+	{
+		$couponId = isset($params['couponId']) ? $params['couponId'] : 0;
+		$userId = isset($params['userId']) ? $params['userId'] : 0;
+		$price = isset($params['price']) ? $params['price'] : 0;
+		$sourceType = isset($params['sourceType']) ? $params['sourceType'] : 0;
+		//使用优惠券
+		if (!empty($couponId)) {
+			$amount = CouponService::valid($couponId, $price, $userId);
+			return stringUtil::calcSub($price, $amount);
+		}
+		//使用会员
+		$asset = UserAssetClass::getByUserId($userId);
+		if (isset($asset['member']) && $asset['member'] > 0) {
+			$currentDiscount = $asset['discount'];
+			$currentPrice = $price * $currentDiscount;
+			$actPrice = substr(sprintf("%.3f", $currentPrice), 0, -2);
+			return $actPrice;
+		}
+		
+		//使用随机优惠 shish 2019.9.12
+		if ($sourceType == 3) {
+			$discountAmount = OrderClass::getRandDiscount($price);
+			//测试环境直接随机优惠0.01
+			if (isset(Yii::$app->params['merchant'])) {
+				$merchant = Yii::$app->params['merchant'];
+				if ($merchant['merchantName'] == '花美灵') {
+					$discountAmount = 0.01;
+				}
+			}
+			return stringUtil::calcSub($price, $discountAmount);
+		}
+		return $price;
+	}
+
 }

+ 2 - 2
biz/promote/services/CouponService.php

@@ -48,11 +48,11 @@ class CouponService extends BaseService
 			util::fail('优惠券不可用');
 		}
 		if ($coupon['miniCost'] > $price) {
-			util::fail('券要求最低消费' . $coupon['miniCost'] . '元');
+			util::fail('优惠券要求最低消费' . $coupon['miniCost'] . '元');
 		}
 		return $coupon['amount'];
 	}
-	
+
 	//销卷并分成 shish 2019.8.25
 	public static function destroyToGetReward($data, $merchantId)
 	{

+ 16 - 46
common/components/stringUtil.php

@@ -8,29 +8,20 @@ class stringUtil
 {
 	
 	const KEY = 'Aos4eLEmqpo8dmJleG610tR1KxUVQ5OzgkzpYM23PLqwSaU3Io';
-	
-	/**
-	 * 随机编号,英文开头,用于鲜花商品编号
-	 * @return string
-	 */
+
+	//随机编号,英文开头,用于鲜花商品编号
 	public static function buildRandNo($id)
 	{
 		return 'F' . $id . time();// F == flower
 	}
 	
-	/**
-	 * 创建微信Token使用
-	 * @return string
-	 */
+	//创建微信Token使用
 	public static function buildWxToken($id)
 	{
 		return 'wt' . $id . time();
 	}
 	
-	/**
-	 * 订单号,用于购买商品生成的订单等,
-	 * @return string
-	 */
+	//订单号,用于购买商品生成的订单等
 	public static function buildOrderNo($id = 0)
 	{
 		mt_srand((double)microtime() * 1000000);
@@ -39,11 +30,7 @@ class stringUtil
 		return $no;
 	}
 	
-	/**
-	 * 判断是否手机号
-	 * @param unknown $no
-	 * @return boolean
-	 */
+	//判断是否手机号
 	public static function isMobile($no)
 	{
 		return preg_match("/^1[0-9]\d{9}$/", $no) == true ? true : false;
@@ -57,15 +44,7 @@ class stringUtil
 		}
 		return false;
 	}
-	
-	
-	/**
-	 *
-	 * @param string $string
-	 * @param int $length 汉字字数
-	 * @param tring $ext
-	 * @return string|string
-	 */
+
 	static function subStringUtf8($string, $length, $ext = '')
 	{
 		$num = self::getWordNum($string);
@@ -102,12 +81,7 @@ class stringUtil
 		return substr($string, 0, $slen) . $ext;
 	}
 	
-	/**
-	 * 计算文字字数,二个字母当一个汉字
-	 * @param unknown_type $string
-	 * @param unknown_type $code
-	 * @return number
-	 */
+	//计算文字字数,二个字母当一个汉字
 	static function getWordNum($string, $code = 'utf-8')
 	{
 		$i = 0; //开始字节数
@@ -321,8 +295,7 @@ class stringUtil
 	}
 	
 	/**
-	 * 返回8位随机字符,重复概率较低。
-	 * @return string
+	 * 返回8位随机字符,重复概率较低
 	 */
 	public static function getRandStr($id = null)
 	{
@@ -346,11 +319,7 @@ class stringUtil
 		return $ip1id . "." . $ip2id . "." . $ip3id . "." . $ip4id;
 	}
 	
-	/**
-	 * 写日志
-	 * @param unknown_type $content
-	 * @param unknown_type $file
-	 */
+	//写日志
 	public static function log($content, $file)
 	{
 		$content .= "\r\n";
@@ -359,22 +328,23 @@ class stringUtil
 		fclose($fh);
 	}
 	
-	/**
-	 * 减法
-	 */
+	//减法
 	public static function calcSub($a, $b)
 	{
 		return ($a * 1000 - $b * 1000) / 1000;
 	}
 	
-	/**
-	 * 加法
-	 */
+	//加法
 	public static function calcAdd($a, $b)
 	{
 		return ($a * 1000 + $b * 1000) / 1000;
 	}
 	
+	public static function ride($a,$b)
+	{
+
+	}
+	
 	/**
 	 * 生成商家提现需要使用的订单号
 	 */