| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- namespace biz\order\services;
- use biz\base\services\BaseService;
- use biz\merchant\services\MerchantCapitalService;
- 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;
- use linslin\yii2\curl;
- class OrderService extends BaseService
- {
-
- public static $baseFile = '\biz\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');
-
- 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);
- }
- return $data;
- }
-
- //取客户最近若干条订单 shish 2019.11.30
- public static function getUserOrder($userId, $limit = 10, $order = null, $with = null)
- {
- $where = ['userId' => $userId];
- 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,
- ];
- MerchantCapitalService::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];
- }
-
- }
|