| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <?php
- namespace bizMall\order\classes;
- use biz\shop\models\Shop;
- use biz\stat\classes\StatOrderCountClass;
- use bizMall\merchant\classes\MerchantAssetClass;
- use bizMall\shop\classes\MainClass;
- use bizMall\shop\classes\ShopClass;
- use bizMall\stat\classes\StatOrderClass;
- use bizMall\user\classes\UserClass;
- use common\components\imgUtil;
- use common\components\mapUtil;
- use common\components\miniUtil;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use bizMall\base\classes\BaseClass;
- use common\components\util;
- class OrderClass extends BaseClass
- {
- public static $baseFile = '\bizMall\order\models\Order';
- //完成
- const ORDER_STATUS_COMPLETE = 4;
- //已取消
- const ORDER_STATUS_CANCEL = 5;
- //配送中
- const ORDER_STATUS_SENDING = 3;
- //待配送
- const ORDER_STATUS_UN_SEND = 2;
- //待付款
- const ORDER_STATUS_UN_PAY = 1;
- //订单总流程数
- const ORDER_TOTAL_FLOW = 4;
- //未确认配送方式
- const SEND_TYPE_UNKNOWN = 0;
- //自取
- const SEND_TYPE_NO = 1;
- //自己送
- const SEND_TYPE_MYSELF = 2;
- //代送
- const SEND_TYPE_OTHER = 3;
- //送达时间段
- public static $reachPeriod = [0 => '上午', 1 => '下午', 2 => '晚上'];
- //优惠金额
- public static $randDiscount = [
- 50 => [1, 3],
- 100 => [5, 9],
- 300 => [10, 15],
- 500 => [16, 18],
- 1000 => [18, 30],
- 20000 => [90, 100],
- ];
- //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
- public static function getDistanceFee($userLat, $userLong, $shopLat, $shopLong, $weight)
- {
- return ['distance' => 0, 'showDistance' => 0, 'fee' => 0];
- //基础配送费8元,0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
- //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
- //00:00 - 06:00 +6元,22:00-24:00 +4元
- //恶劣天气,下单高峰期会临时调整价格
- //起步价
- $baseFee = 10;
- $distance = mapUtil::calculateDistance($userLong, $userLat, $shopLong, $shopLat);
- //0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
- $formatDistance = ceil($distance / 1000);
- $startKiloFee = 6;
- if ($formatDistance < 5) {
- $addDistanceFee = bcmul($formatDistance, 2);
- } elseif ($formatDistance == 5) {
- $addDistanceFee = $startKiloFee;
- } else {
- $overKilo = bcsub($formatDistance, 5);
- $overFee = bcmul($overKilo, 3);
- $addDistanceFee = bcadd($startKiloFee, $overFee);
- }
- //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
- $formatWeight = ceil($weight);
- if ($formatWeight > 15) {
- $addWeightFee = 20;
- } elseif ($formatWeight >= 10 && $formatWeight <= 15) {
- $addWeightFee = 12;
- } elseif ($formatWeight >= 5 && $formatWeight < 10) {
- $overWeight = bcsub($formatWeight, 4);
- $addWeightFee = bcmul($overWeight, 2);
- } else {
- $addWeightFee = 0;
- }
- $addFee = bcadd($addDistanceFee, $addWeightFee, 2);
- $totalFee = bcadd($baseFee, $addFee, 2);
- //特殊时段费
- $timeFee = 0;
- $hour = date("G");
- //22:00 - 24:00 +4元
- if (in_array($hour, [22, 23])) {
- $timeFee = 4;
- }
- //00:00 - 06:00 +6元
- if (in_array($hour, [0, 1, 2, 3, 4, 5])) {
- $timeFee = 6;
- }
- $totalFee = bcadd($totalFee, $timeFee, 2);
- //恶劣天气和下单高峰费用
- $weatherFee = 0;
- $totalFee = bcadd($totalFee, $weatherFee, 2);
- $showDistance = sprintf("%.1f", ($distance / 1000));
- //noticeUtil::push("零售商城 距离:{$distance} {$showDistance} 重量:{$weight} 费用:{$totalFee}", '15280215347');
- return ['distance' => $distance, 'showDistance' => $showDistance, 'fee' => $totalFee];
- }
- //添加订单 ssh 2019.12.5
- public static function addOrder($data)
- {
- $data['orderSn'] = isset($data['orderSn']) ? $data['orderSn'] : orderSn::getOrderSn();
- $data['payStatus'] = 0;
- $shopId = $data['shopId'] ?? 0;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店85');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息33');
- }
- $riseNum = StatOrderCountClass::addOrder($shop, $main);
- $address = $data['address'] ?? '';
- $floor = $data['floor'] ?? '';
- $fullAddress = $address . $floor;
- $data['fullAddress'] = $fullAddress;
- //将花店每月的总订单数作为编号
- $data['sendNum'] = date('j') . $riseNum;
- $return = self::add($data, true);
- $main->unPayOrder += 1;
- $main->totalOrder += 1;
- $main->save();
- return $return;
- }
- public static function getByOrderSn($orderSn)
- {
- return self::getByCondition(['orderSn' => $orderSn]);
- }
- //评价 ssh 2019.12.16
- public static function comment($data)
- {
- $id = $data['id'];
- unset($data['id']);
- return self::updateById($id, $data);
- }
- //订单
- public static function getOrderById($id)
- {
- $order = self::getById($id);
- if (empty($order)) {
- return [];
- }
- $orderSn = $order['orderSn'] ?? '';
- if (empty($orderSn)) {
- return [];
- }
- $goodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- if (!empty($goodsList)) {
- foreach ($goodsList as $key => $val) {
- $shortCover = $val['cover'] ?? '';
- $cover = imgUtil::groupImg($shortCover);
- $goodsList[$key]['cover'] = $cover;
- }
- }
- $order['goodsInfoList'] = $goodsList;
- $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- if (!empty($itemList)) {
- foreach ($itemList as $key => $item) {
- $shortCover = $item['cover'] ?? '';
- $cover = imgUtil::groupImg($shortCover);
- $itemList[$key]['cover'] = $cover;
- }
- }
- $order['itemList'] = $itemList;
- return $order;
- }
- //根据订单查询 ssh 2020.1.4
- public static function getOrderBySn($orderSn)
- {
- $order = self::getByCondition(['orderSn' => $orderSn]);
- if (empty($order)) {
- return [];
- }
- $goodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- if (!empty($goodsList)) {
- foreach ($goodsList as $key => $val) {
- $shortCover = $val['cover'] ?? '';
- $cover = imgUtil::groupImg($shortCover);
- $goodsList[$key]['cover'] = $cover;
- }
- }
- $order['goodsInfoList'] = $goodsList;
- $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- if (!empty($itemList)) {
- foreach ($itemList as $key => $item) {
- $shortCover = $item['cover'] ?? '';
- $cover = imgUtil::groupImg($shortCover);
- $itemList[$key]['cover'] = $cover;
- }
- }
- $order['itemList'] = $itemList;
- return $order;
- }
- //返回随机优惠金额 ssh 2019.9.12
- public static function getRandDiscount($amount)
- {
- if ($amount <= 0) {
- return 0;
- }
- $randDiscount = self::$randDiscount;
- krsort($randDiscount);
- $rand = 0;
- foreach ($randDiscount as $currentAmount => $val) {
- if ($amount >= $currentAmount) {
- $rand = rand($val[0], $val[1]);
- break;
- }
- }
- return $rand / 10;
- }
- //订单完成 ssh 2020.3.12
- public static function complete($order, $currentFlow)
- {
- $id = $order['id'];
- self::updateById($id, ['status' => OrderClass::ORDER_STATUS_COMPLETE, 'currentFlow' => $currentFlow]);
- }
- //转化出送到时间 ssh 2020.3.15
- public static function getReachTime($order)
- {
- $reachTime = '';
- if (isset($order['reachDate']) && !empty($order['reachDate'])) {
- $prev = date("n-j", strtotime($order['reachDate']));
- $periodData = self::$reachPeriod;
- $periodKey = $order['reachPeriod'];
- $period = isset($periodData[$periodKey]) ? $periodData[$periodKey] : '上午';
- $reachTime = $prev . ' ' . $period;
- }
- return $reachTime;
- }
- //支付成功获取unionId ssh 2020.5.12
- public static function payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user)
- {
- $mcId = $merchantExtend['wxPayMerchantId'];
- $miniOpenId = isset($user['miniOpenId']) ? $user['miniOpenId'] : '';
- $unionId = isset($user['unionId']) ? $user['unionId'] : '';
- $userId = $user['id'];
- if (empty($unionId)) {
- $unionId = miniUtil::getPaidUnionId($merchant, $miniOpenId, $mcId, (string)$orderSn, 0);
- if (!empty($unionId)) {
- $findUser = UserClass::getByUnionId($unionId);
- if (empty($findUser)) {
- UserClass::updateByCondition(['id' => $userId], ['unionId' => $unionId]);
- } else {
- UserClass::mergeUser($findUser, $user);
- }
- }
- }
- }
- //订单到期未付款的订单置成取消状态 ssh 2020.5.25
- public static function setExpire($order)
- {
- $now = time();
- if (!empty($order['deadline']) && $now > $order['deadline']) {
- if ($order['status'] == 0) {
- $id = $order['id'];
- OrderClass::updateById($id, ['status' => 4]);
- }
- }
- }
- //支付成功需要处理的流程 ssh 2020.5.25
- public static function payAfter($order, $updateData)
- {
- $orderId = $order['id'];
- $orderSn = $order['orderSn'];
- $now = time();
- self::updateById($orderId, $updateData);
- //发货流程的完成下单
- OrderSendClass::placeOrder(['orderId' => $orderId, 'payTime' => $now, 'orderSn' => $orderSn]);
- $sjId = $order['sjId'];
- $asset = MerchantAssetClass::getBySjId($sjId);
- //增加订单数
- StatOrderClass::addOrder($sjId, $asset);
- //如果门店和朋友圈订单并且没有填写收花人的直接将订单置为自取并且是完成状态
- if (isset($order['fromType']) && in_array($order['fromType'], [0, 2])) {
- if (isset($order['receiveMobile']) && isset($order['address'])) {
- if (empty($order['receiveMobile']) && empty($order['address'])) {
- OrderSendClass::withoutSend($order);
- }
- }
- }
- }
- }
|