|
|
@@ -5,6 +5,7 @@ namespace bizHd\order\services;
|
|
|
use bizHd\base\services\BaseService;
|
|
|
use biz\sj\services\SjCapitalService;
|
|
|
use biz\sj\services\MerchantService;
|
|
|
+use bizHd\custom\classes\CustomClass;
|
|
|
use bizHd\order\classes\OrderClass;
|
|
|
use bizHd\order\models\Order;
|
|
|
use bizHd\promote\services\CouponRightService;
|
|
|
@@ -20,214 +21,214 @@ use linslin\yii2\curl;
|
|
|
|
|
|
class OrderService extends BaseService
|
|
|
{
|
|
|
-
|
|
|
- public static $baseFile = '\bizHd\order\classes\OrderClass';
|
|
|
-
|
|
|
- //添加订单
|
|
|
- public static function addOrder($data)
|
|
|
- {
|
|
|
- return OrderClass::addOrder($data);
|
|
|
- }
|
|
|
-
|
|
|
- //获取订单信息 shish 2019.11.28
|
|
|
- public static function getOrderList($where)
|
|
|
- {
|
|
|
- $data = self::getList('*', $where, 'addTime DESC');
|
|
|
- if (isset($data['list']) == false || empty($data['list'])) {
|
|
|
- return $data;
|
|
|
- }
|
|
|
-
|
|
|
- $orderIds = array_column($data['list'], 'id');
|
|
|
-
|
|
|
- //获取订单的商品信息
|
|
|
- $goodsList = OrderGoodsService::getGoodsListByOrderIds($orderIds);
|
|
|
-
|
|
|
- //获取订花人的用户信息
|
|
|
- $orderUserIds = array_column($data['list'], 'userId');
|
|
|
- $orderUserIds = array_unique($orderUserIds);
|
|
|
- $userInfoList = UserService::getUserByIds($orderUserIds);
|
|
|
-
|
|
|
- $merchantIdList = array_column($data['list'], 'merchantId');
|
|
|
- $merchantIdList = array_filter(array_unique($merchantIdList));
|
|
|
- $merchantList = MerchantService::getByIds($merchantIdList, null, 'id');
|
|
|
-
|
|
|
- $periodData = [0 => '上午', 1 => '下午', 2 => '晚上'];
|
|
|
- foreach ($data['list'] as $key => $val) {
|
|
|
- $id = $val['id'];
|
|
|
- $userId = $val['userId'];
|
|
|
- $data['list'][$key]['goodsInfoList'] = isset($goodsList[$id]) ? $goodsList[$id] : [];
|
|
|
- $data['list'][$key]['bookAvatar'] = isset($userInfoList[$userId]['smallAvatarUrl']) ? $userInfoList[$userId]['smallAvatarUrl'] : '';
|
|
|
- $merchantId = $val['merchantId'];
|
|
|
- $data['list'][$key]['merchantName'] = isset($merchantList[$merchantId]['merchantName']) ? $merchantList[$merchantId]['merchantName'] : '';
|
|
|
- $data['list'][$key]['reachTime'] = OrderClass::getReachTime($val);
|
|
|
- $requireReachTime = '';
|
|
|
- if ($val['reachDate'] != '0000-00-00') {
|
|
|
- $reachPeriodId = $val['reachPeriod'];
|
|
|
- $reachPeriod = isset($periodData[$reachPeriodId]) ? $periodData[$reachPeriodId] : '上午';
|
|
|
- $requireReachTime = date("m-d", strtotime($val['reachDate'])) . ' ' . $reachPeriod;
|
|
|
- }
|
|
|
- $data['list'][$key]['requireReachTime'] = $requireReachTime;
|
|
|
- }
|
|
|
- return $data;
|
|
|
- }
|
|
|
-
|
|
|
- //取客户最近若干条订单 shish 2019.11.30
|
|
|
- public static function getUserOrder($userId, $limit = 10, $order = null, $with = null)
|
|
|
- {
|
|
|
- $where = ['adminId' => $userId, 'payStatus' => 1];
|
|
|
- return self::getLimitList('*', $where, $limit, $order, $with);
|
|
|
- }
|
|
|
-
|
|
|
- //支付前验证订单 shish 2019.12.6
|
|
|
- public static function checkBeforePay($order, $userId)
|
|
|
- {
|
|
|
- $now = time();
|
|
|
- OrderClass::setExpire($order);
|
|
|
- if (empty($order)) {
|
|
|
- util::fail('订单无效');
|
|
|
- }
|
|
|
- if (isset($order['userId']) && $order['userId'] != $userId) {
|
|
|
- util::fail('非法访问');
|
|
|
- }
|
|
|
- if ($order['status'] != 0) {
|
|
|
- util::fail('订单不是待付款状态');
|
|
|
- }
|
|
|
- if (!empty($order['deadline']) && $now > $order['deadline']) {
|
|
|
- util::fail('订单已过期');
|
|
|
- }
|
|
|
- if (ceil($order['actPrice']) <= 0) {
|
|
|
- util::fail('订单金额有问题');
|
|
|
- }
|
|
|
- if ($order['payStatus'] == 1) {
|
|
|
- util::fail('您已经付过了');
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //商家验证是否有效 shish 2019.12.15
|
|
|
- public static function valid($order, $merchantId)
|
|
|
- {
|
|
|
- OrderClass::setExpire($order);
|
|
|
- if (empty($order)) {
|
|
|
- util::fail('订单无效');
|
|
|
- }
|
|
|
- if ($order['merchantId'] != $merchantId) {
|
|
|
- util::fail('不是您的订单');
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //根据编号查询 shish 2019.12.14
|
|
|
- public static function getByOrderSn($orderSn)
|
|
|
- {
|
|
|
- return OrderClass::getByOrderSn($orderSn);
|
|
|
- }
|
|
|
-
|
|
|
- //点评 shish 2019.12.16
|
|
|
- public static function comment($data)
|
|
|
- {
|
|
|
- return OrderClass::comment($data);
|
|
|
- }
|
|
|
-
|
|
|
- //订单归类
|
|
|
- public static function classify($id, $categoryId, $usageId)
|
|
|
- {
|
|
|
- $connection = Yii::$app->db;//事务处理
|
|
|
- $transaction = $connection->beginTransaction();
|
|
|
- try {
|
|
|
- $order = OrderClass::getById($id, true);
|
|
|
- if ($order->payStatus != 1) {
|
|
|
- util::fail('订单还未付款');
|
|
|
- }
|
|
|
- if ($order->classify == 1) {
|
|
|
- util::fail('已经归类过了');
|
|
|
- }
|
|
|
- $order->categoryId = $categoryId;
|
|
|
- $order->usageId = $usageId;
|
|
|
- $order->classify = 1;
|
|
|
- $order->save();
|
|
|
-
|
|
|
- //设置流水的分类和用途并分配资金
|
|
|
- $setData = [
|
|
|
- 'merchantId' => $order->merchantId,
|
|
|
- 'capitalId' => $order->capitalId,
|
|
|
- 'time' => $order->payTime,
|
|
|
- 'usageId' => $order->usageId,
|
|
|
- 'categoryId' => $order->categoryId,
|
|
|
- 'amount' => $order->actPrice,
|
|
|
- 'payWay' => $order->payWay,
|
|
|
- ];
|
|
|
- SjCapitalService::orderClassify($setData);
|
|
|
-
|
|
|
- $transaction->commit();
|
|
|
- } catch (Exception $e) {
|
|
|
- $transaction->rollBack();
|
|
|
- util::fail('没有归类成功');
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //更新配送单 shish 2019.12.17
|
|
|
- public static function updateSheet($order, $data)
|
|
|
- {
|
|
|
- $id = $order['id'];
|
|
|
- if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) {
|
|
|
- util::fail('订单当前不是待配送状态');
|
|
|
- }
|
|
|
- $data['deliver'] = 1;
|
|
|
- OrderClass::updateById($id, $data);
|
|
|
- }
|
|
|
-
|
|
|
- //订单详情 shish 2020.1.2
|
|
|
- public static function getOrderById($id)
|
|
|
- {
|
|
|
- return OrderClass::getOrderById($id);
|
|
|
- }
|
|
|
-
|
|
|
- //查看订单详情 shish 2020.1.4
|
|
|
- public static function getOrderBySn($orderSn)
|
|
|
- {
|
|
|
- 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;
|
|
|
- $discountAmount = 0;
|
|
|
- $discountType = 0;
|
|
|
- $discountPrice = $price;
|
|
|
- //使用优惠券
|
|
|
- if (!empty($couponId)) {
|
|
|
- $discountAmount = CouponService::checkBeforeUse($couponId, $price, $userId);
|
|
|
- $discountPrice = stringUtil::calcSub($price, $discountAmount);
|
|
|
- $discountType = 1;
|
|
|
- } else {
|
|
|
- //使用会员
|
|
|
- $asset = UserAssetClass::getByUserId($userId);
|
|
|
- if (isset($asset['member']) && $asset['member'] > 0) {
|
|
|
- $currentDiscount = $asset['discount'];
|
|
|
- $currentPrice = $price * $currentDiscount;
|
|
|
- $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1);
|
|
|
- $discountType = 2;
|
|
|
- $discountAmount = stringUtil::calcSub($price, $newPrice);
|
|
|
- $discountPrice = $newPrice;
|
|
|
- }
|
|
|
- }
|
|
|
- //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 shish 2019.9.12
|
|
|
- if ($discountPrice == $price && $sourceType == 3) {
|
|
|
- $discountAmount = OrderClass::getRandDiscount($price);
|
|
|
- //开发和测试环境直接随机优惠
|
|
|
- $env = getenv('YII_ENV');
|
|
|
- if ($env == 'local' || $env == 'test') {
|
|
|
- $discountAmount = 0.01;
|
|
|
- }
|
|
|
- $discountPrice = stringUtil::calcSub($price, $discountAmount);
|
|
|
- $discountType = 3;
|
|
|
- }
|
|
|
- $discountType = $discountAmount <= 0 ? 0 : $discountType;
|
|
|
- $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice;
|
|
|
- return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+ public static $baseFile = '\bizHd\order\classes\OrderClass';
|
|
|
+
|
|
|
+ //添加订单
|
|
|
+ public static function addOrder($data)
|
|
|
+ {
|
|
|
+ return OrderClass::addOrder($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取订单信息 shish 2019.11.28
|
|
|
+ public static function getOrderList($where)
|
|
|
+ {
|
|
|
+ $data = self::getList('*', $where, 'addTime DESC');
|
|
|
+ if (isset($data['list']) == false || empty($data['list'])) {
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ $orderIds = array_column($data['list'], 'id');
|
|
|
+
|
|
|
+ //获取订单的商品信息
|
|
|
+ $goodsList = OrderGoodsService::getGoodsListByOrderIds($orderIds);
|
|
|
+
|
|
|
+ //获取订花人的用户信息
|
|
|
+ $orderUserIds = array_column($data['list'], 'customId');
|
|
|
+ $orderUserIds = array_unique(array_filter($orderUserIds));
|
|
|
+ $customList = CustomClass::getCustomByIds($orderUserIds);
|
|
|
+
|
|
|
+ $merchantIdList = array_column($data['list'], 'merchantId');
|
|
|
+ $merchantIdList = array_filter(array_unique($merchantIdList));
|
|
|
+ $merchantList = MerchantService::getByIds($merchantIdList, null, 'id');
|
|
|
+
|
|
|
+ $periodData = [0 => '上午', 1 => '下午', 2 => '晚上'];
|
|
|
+ foreach ($data['list'] as $key => $val) {
|
|
|
+ $id = $val['id'];
|
|
|
+ $customId = $val['customId'];
|
|
|
+ $data['list'][$key]['goodsInfoList'] = isset($goodsList[$id]) ? $goodsList[$id] : [];
|
|
|
+ $data['list'][$key]['bookAvatar'] = isset($customList[$customId]['smallAvatarUrl']) ? $customList[$customId]['smallAvatarUrl'] : '';
|
|
|
+ $merchantId = $val['merchantId'];
|
|
|
+ $data['list'][$key]['merchantName'] = isset($merchantList[$merchantId]['merchantName']) ? $merchantList[$merchantId]['merchantName'] : '';
|
|
|
+ $data['list'][$key]['reachTime'] = OrderClass::getReachTime($val);
|
|
|
+ $requireReachTime = '';
|
|
|
+ if ($val['reachDate'] != '0000-00-00') {
|
|
|
+ $reachPeriodId = $val['reachPeriod'];
|
|
|
+ $reachPeriod = isset($periodData[$reachPeriodId]) ? $periodData[$reachPeriodId] : '上午';
|
|
|
+ $requireReachTime = date("m-d", strtotime($val['reachDate'])) . ' ' . $reachPeriod;
|
|
|
+ }
|
|
|
+ $data['list'][$key]['requireReachTime'] = $requireReachTime;
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ //取客户最近若干条订单 shish 2019.11.30
|
|
|
+ public static function getUserOrder($customId, $limit = 10, $order = null, $with = null)
|
|
|
+ {
|
|
|
+ $where = ['adminId' => $customId, 'payStatus' => 1];
|
|
|
+ return self::getLimitList('*', $where, $limit, $order, $with);
|
|
|
+ }
|
|
|
+
|
|
|
+ //支付前验证订单 shish 2019.12.6
|
|
|
+ public static function checkBeforePay($order, $customId)
|
|
|
+ {
|
|
|
+ $now = time();
|
|
|
+ OrderClass::setExpire($order);
|
|
|
+ if (empty($order)) {
|
|
|
+ util::fail('订单无效');
|
|
|
+ }
|
|
|
+ if (isset($order['userId']) && $order['userId'] != $customId) {
|
|
|
+ util::fail('非法访问');
|
|
|
+ }
|
|
|
+ if ($order['status'] != 0) {
|
|
|
+ util::fail('订单不是待付款状态');
|
|
|
+ }
|
|
|
+ if (!empty($order['deadline']) && $now > $order['deadline']) {
|
|
|
+ util::fail('订单已过期');
|
|
|
+ }
|
|
|
+ if (ceil($order['actPrice']) <= 0) {
|
|
|
+ util::fail('订单金额有问题');
|
|
|
+ }
|
|
|
+ if ($order['payStatus'] == 1) {
|
|
|
+ util::fail('您已经付过了');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //商家验证是否有效 shish 2019.12.15
|
|
|
+ public static function valid($order, $merchantId)
|
|
|
+ {
|
|
|
+ OrderClass::setExpire($order);
|
|
|
+ if (empty($order)) {
|
|
|
+ util::fail('订单无效');
|
|
|
+ }
|
|
|
+ if ($order['merchantId'] != $merchantId) {
|
|
|
+ util::fail('不是您的订单');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据编号查询 shish 2019.12.14
|
|
|
+ public static function getByOrderSn($orderSn)
|
|
|
+ {
|
|
|
+ return OrderClass::getByOrderSn($orderSn);
|
|
|
+ }
|
|
|
+
|
|
|
+ //点评 shish 2019.12.16
|
|
|
+ public static function comment($data)
|
|
|
+ {
|
|
|
+ return OrderClass::comment($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ //订单归类
|
|
|
+ public static function classify($id, $categoryId, $usageId)
|
|
|
+ {
|
|
|
+ $connection = Yii::$app->db;//事务处理
|
|
|
+ $transaction = $connection->beginTransaction();
|
|
|
+ try {
|
|
|
+ $order = OrderClass::getById($id, true);
|
|
|
+ if ($order->payStatus != 1) {
|
|
|
+ util::fail('订单还未付款');
|
|
|
+ }
|
|
|
+ if ($order->classify == 1) {
|
|
|
+ util::fail('已经归类过了');
|
|
|
+ }
|
|
|
+ $order->categoryId = $categoryId;
|
|
|
+ $order->usageId = $usageId;
|
|
|
+ $order->classify = 1;
|
|
|
+ $order->save();
|
|
|
+
|
|
|
+ //设置流水的分类和用途并分配资金
|
|
|
+ $setData = [
|
|
|
+ 'merchantId' => $order->merchantId,
|
|
|
+ 'capitalId' => $order->capitalId,
|
|
|
+ 'time' => $order->payTime,
|
|
|
+ 'usageId' => $order->usageId,
|
|
|
+ 'categoryId' => $order->categoryId,
|
|
|
+ 'amount' => $order->actPrice,
|
|
|
+ 'payWay' => $order->payWay,
|
|
|
+ ];
|
|
|
+ SjCapitalService::orderClassify($setData);
|
|
|
+
|
|
|
+ $transaction->commit();
|
|
|
+ } catch (Exception $e) {
|
|
|
+ $transaction->rollBack();
|
|
|
+ util::fail('没有归类成功');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新配送单 shish 2019.12.17
|
|
|
+ public static function updateSheet($order, $data)
|
|
|
+ {
|
|
|
+ $id = $order['id'];
|
|
|
+ if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) {
|
|
|
+ util::fail('订单当前不是待配送状态');
|
|
|
+ }
|
|
|
+ $data['deliver'] = 1;
|
|
|
+ OrderClass::updateById($id, $data);
|
|
|
+ }
|
|
|
+
|
|
|
+ //订单详情 shish 2020.1.2
|
|
|
+ public static function getOrderById($id)
|
|
|
+ {
|
|
|
+ return OrderClass::getOrderById($id);
|
|
|
+ }
|
|
|
+
|
|
|
+ //查看订单详情 shish 2020.1.4
|
|
|
+ public static function getOrderBySn($orderSn)
|
|
|
+ {
|
|
|
+ return OrderClass::getOrderBySn($orderSn);
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算优惠 shish 2020.3.9
|
|
|
+ public static function getDiscountPrice($params)
|
|
|
+ {
|
|
|
+ $couponId = isset($params['couponId']) ? $params['couponId'] : 0;
|
|
|
+ $customId = isset($params['userId']) ? $params['userId'] : 0;
|
|
|
+ $price = isset($params['price']) ? $params['price'] : 0;
|
|
|
+ $sourceType = isset($params['sourceType']) ? $params['sourceType'] : 0;
|
|
|
+ $discountAmount = 0;
|
|
|
+ $discountType = 0;
|
|
|
+ $discountPrice = $price;
|
|
|
+ //使用优惠券
|
|
|
+ if (!empty($couponId)) {
|
|
|
+ $discountAmount = CouponService::checkBeforeUse($couponId, $price, $customId);
|
|
|
+ $discountPrice = stringUtil::calcSub($price, $discountAmount);
|
|
|
+ $discountType = 1;
|
|
|
+ } else {
|
|
|
+ //使用会员
|
|
|
+ $asset = UserAssetClass::getByUserId($customId);
|
|
|
+ if (isset($asset['member']) && $asset['member'] > 0) {
|
|
|
+ $currentDiscount = $asset['discount'];
|
|
|
+ $currentPrice = $price * $currentDiscount;
|
|
|
+ $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1);
|
|
|
+ $discountType = 2;
|
|
|
+ $discountAmount = stringUtil::calcSub($price, $newPrice);
|
|
|
+ $discountPrice = $newPrice;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 shish 2019.9.12
|
|
|
+ if ($discountPrice == $price && $sourceType == 3) {
|
|
|
+ $discountAmount = OrderClass::getRandDiscount($price);
|
|
|
+ //开发和测试环境直接随机优惠
|
|
|
+ $env = getenv('YII_ENV');
|
|
|
+ if ($env == 'local' || $env == 'test') {
|
|
|
+ $discountAmount = 0.01;
|
|
|
+ }
|
|
|
+ $discountPrice = stringUtil::calcSub($price, $discountAmount);
|
|
|
+ $discountType = 3;
|
|
|
+ }
|
|
|
+ $discountType = $discountAmount <= 0 ? 0 : $discountType;
|
|
|
+ $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice;
|
|
|
+ return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
|
|
|
+ }
|
|
|
+
|
|
|
}
|