OrderClass.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace bizMall\order\classes;
  3. use biz\shop\models\Shop;
  4. use biz\stat\classes\StatOrderCountClass;
  5. use bizMall\merchant\classes\MerchantAssetClass;
  6. use bizMall\shop\classes\MainClass;
  7. use bizMall\shop\classes\ShopClass;
  8. use bizMall\stat\classes\StatOrderClass;
  9. use bizMall\user\classes\UserClass;
  10. use common\components\imgUtil;
  11. use common\components\mapUtil;
  12. use common\components\miniUtil;
  13. use common\components\noticeUtil;
  14. use common\components\orderSn;
  15. use bizMall\base\classes\BaseClass;
  16. use common\components\util;
  17. class OrderClass extends BaseClass
  18. {
  19. public static $baseFile = '\bizMall\order\models\Order';
  20. //完成
  21. const ORDER_STATUS_COMPLETE = 4;
  22. //已取消
  23. const ORDER_STATUS_CANCEL = 5;
  24. //配送中
  25. const ORDER_STATUS_SENDING = 3;
  26. //待配送
  27. const ORDER_STATUS_UN_SEND = 2;
  28. //待付款
  29. const ORDER_STATUS_UN_PAY = 1;
  30. //订单总流程数
  31. const ORDER_TOTAL_FLOW = 4;
  32. //未确认配送方式
  33. const SEND_TYPE_UNKNOWN = 0;
  34. //自取
  35. const SEND_TYPE_NO = 1;
  36. //自己送
  37. const SEND_TYPE_MYSELF = 2;
  38. //代送
  39. const SEND_TYPE_OTHER = 3;
  40. //送达时间段
  41. public static $reachPeriod = [0 => '上午', 1 => '下午', 2 => '晚上'];
  42. //优惠金额
  43. public static $randDiscount = [
  44. 50 => [1, 3],
  45. 100 => [5, 9],
  46. 300 => [10, 15],
  47. 500 => [16, 18],
  48. 1000 => [18, 30],
  49. 20000 => [90, 100],
  50. ];
  51. //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
  52. public static function getDistanceFee($userLat, $userLong, $shopLat, $shopLong, $weight)
  53. {
  54. return ['distance' => 0, 'showDistance' => 0, 'fee' => 0];
  55. //基础配送费8元,0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
  56. //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
  57. //00:00 - 06:00 +6元,22:00-24:00 +4元
  58. //恶劣天气,下单高峰期会临时调整价格
  59. //起步价
  60. $baseFee = 10;
  61. $distance = mapUtil::calculateDistance($userLong, $userLat, $shopLong, $shopLat);
  62. //0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
  63. $formatDistance = ceil($distance / 1000);
  64. $startKiloFee = 6;
  65. if ($formatDistance < 5) {
  66. $addDistanceFee = bcmul($formatDistance, 2);
  67. } elseif ($formatDistance == 5) {
  68. $addDistanceFee = $startKiloFee;
  69. } else {
  70. $overKilo = bcsub($formatDistance, 5);
  71. $overFee = bcmul($overKilo, 3);
  72. $addDistanceFee = bcadd($startKiloFee, $overFee);
  73. }
  74. //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
  75. $formatWeight = ceil($weight);
  76. if ($formatWeight > 15) {
  77. $addWeightFee = 20;
  78. } elseif ($formatWeight >= 10 && $formatWeight <= 15) {
  79. $addWeightFee = 12;
  80. } elseif ($formatWeight >= 5 && $formatWeight < 10) {
  81. $overWeight = bcsub($formatWeight, 4);
  82. $addWeightFee = bcmul($overWeight, 2);
  83. } else {
  84. $addWeightFee = 0;
  85. }
  86. $addFee = bcadd($addDistanceFee, $addWeightFee, 2);
  87. $totalFee = bcadd($baseFee, $addFee, 2);
  88. //特殊时段费
  89. $timeFee = 0;
  90. $hour = date("G");
  91. //22:00 - 24:00 +4元
  92. if (in_array($hour, [22, 23])) {
  93. $timeFee = 4;
  94. }
  95. //00:00 - 06:00 +6元
  96. if (in_array($hour, [0, 1, 2, 3, 4, 5])) {
  97. $timeFee = 6;
  98. }
  99. $totalFee = bcadd($totalFee, $timeFee, 2);
  100. //恶劣天气和下单高峰费用
  101. $weatherFee = 0;
  102. $totalFee = bcadd($totalFee, $weatherFee, 2);
  103. $showDistance = sprintf("%.1f", ($distance / 1000));
  104. //noticeUtil::push("零售商城 距离:{$distance} {$showDistance} 重量:{$weight} 费用:{$totalFee}", '15280215347');
  105. return ['distance' => $distance, 'showDistance' => $showDistance, 'fee' => $totalFee];
  106. }
  107. //添加订单 ssh 2019.12.5
  108. public static function addOrder($data)
  109. {
  110. $data['orderSn'] = isset($data['orderSn']) ? $data['orderSn'] : orderSn::getOrderSn();
  111. $data['payStatus'] = 0;
  112. $shopId = $data['shopId'] ?? 0;
  113. $shop = ShopClass::getLockById($shopId);
  114. if (empty($shop)) {
  115. util::fail('没有找到门店85');
  116. }
  117. $mainId = $shop->mainId ?? 0;
  118. $main = MainClass::getLockById($mainId);
  119. if (empty($main)) {
  120. util::fail('没有main信息33');
  121. }
  122. $riseNum = StatOrderCountClass::addOrder($shop, $main);
  123. $address = $data['address'] ?? '';
  124. $floor = $data['floor'] ?? '';
  125. $fullAddress = $address . $floor;
  126. $data['fullAddress'] = $fullAddress;
  127. //将花店每月的总订单数作为编号
  128. $data['sendNum'] = date('j') . $riseNum;
  129. $return = self::add($data, true);
  130. $main->unPayOrder += 1;
  131. $main->totalOrder += 1;
  132. $main->save();
  133. return $return;
  134. }
  135. public static function getByOrderSn($orderSn)
  136. {
  137. return self::getByCondition(['orderSn' => $orderSn]);
  138. }
  139. //评价 ssh 2019.12.16
  140. public static function comment($data)
  141. {
  142. $id = $data['id'];
  143. unset($data['id']);
  144. return self::updateById($id, $data);
  145. }
  146. //订单
  147. public static function getOrderById($id)
  148. {
  149. $order = self::getById($id);
  150. if (empty($order)) {
  151. return [];
  152. }
  153. $orderSn = $order['orderSn'] ?? '';
  154. if (empty($orderSn)) {
  155. return [];
  156. }
  157. $goodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  158. if (!empty($goodsList)) {
  159. foreach ($goodsList as $key => $val) {
  160. $shortCover = $val['cover'] ?? '';
  161. $cover = imgUtil::groupImg($shortCover);
  162. $goodsList[$key]['cover'] = $cover;
  163. }
  164. }
  165. $order['goodsInfoList'] = $goodsList;
  166. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  167. if (!empty($itemList)) {
  168. foreach ($itemList as $key => $item) {
  169. $shortCover = $item['cover'] ?? '';
  170. $cover = imgUtil::groupImg($shortCover);
  171. $itemList[$key]['cover'] = $cover;
  172. }
  173. }
  174. $order['itemList'] = $itemList;
  175. return $order;
  176. }
  177. //根据订单查询 ssh 2020.1.4
  178. public static function getOrderBySn($orderSn)
  179. {
  180. $order = self::getByCondition(['orderSn' => $orderSn]);
  181. if (empty($order)) {
  182. return [];
  183. }
  184. $goodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  185. if (!empty($goodsList)) {
  186. foreach ($goodsList as $key => $val) {
  187. $shortCover = $val['cover'] ?? '';
  188. $cover = imgUtil::groupImg($shortCover);
  189. $goodsList[$key]['cover'] = $cover;
  190. }
  191. }
  192. $order['goodsInfoList'] = $goodsList;
  193. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  194. if (!empty($itemList)) {
  195. foreach ($itemList as $key => $item) {
  196. $shortCover = $item['cover'] ?? '';
  197. $cover = imgUtil::groupImg($shortCover);
  198. $itemList[$key]['cover'] = $cover;
  199. }
  200. }
  201. $order['itemList'] = $itemList;
  202. return $order;
  203. }
  204. //返回随机优惠金额 ssh 2019.9.12
  205. public static function getRandDiscount($amount)
  206. {
  207. if ($amount <= 0) {
  208. return 0;
  209. }
  210. $randDiscount = self::$randDiscount;
  211. krsort($randDiscount);
  212. $rand = 0;
  213. foreach ($randDiscount as $currentAmount => $val) {
  214. if ($amount >= $currentAmount) {
  215. $rand = rand($val[0], $val[1]);
  216. break;
  217. }
  218. }
  219. return $rand / 10;
  220. }
  221. //订单完成 ssh 2020.3.12
  222. public static function complete($order, $currentFlow)
  223. {
  224. $id = $order['id'];
  225. self::updateById($id, ['status' => OrderClass::ORDER_STATUS_COMPLETE, 'currentFlow' => $currentFlow]);
  226. }
  227. //转化出送到时间 ssh 2020.3.15
  228. public static function getReachTime($order)
  229. {
  230. $reachTime = '';
  231. if (isset($order['reachDate']) && !empty($order['reachDate'])) {
  232. $prev = date("n-j", strtotime($order['reachDate']));
  233. $periodData = self::$reachPeriod;
  234. $periodKey = $order['reachPeriod'];
  235. $period = isset($periodData[$periodKey]) ? $periodData[$periodKey] : '上午';
  236. $reachTime = $prev . ' ' . $period;
  237. }
  238. return $reachTime;
  239. }
  240. //支付成功获取unionId ssh 2020.5.12
  241. public static function payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user)
  242. {
  243. $mcId = $merchantExtend['wxPayMerchantId'];
  244. $miniOpenId = isset($user['miniOpenId']) ? $user['miniOpenId'] : '';
  245. $unionId = isset($user['unionId']) ? $user['unionId'] : '';
  246. $userId = $user['id'];
  247. if (empty($unionId)) {
  248. $unionId = miniUtil::getPaidUnionId($merchant, $miniOpenId, $mcId, (string)$orderSn, 0);
  249. if (!empty($unionId)) {
  250. $findUser = UserClass::getByUnionId($unionId);
  251. if (empty($findUser)) {
  252. UserClass::updateByCondition(['id' => $userId], ['unionId' => $unionId]);
  253. } else {
  254. UserClass::mergeUser($findUser, $user);
  255. }
  256. }
  257. }
  258. }
  259. //订单到期未付款的订单置成取消状态 ssh 2020.5.25
  260. public static function setExpire($order)
  261. {
  262. $now = time();
  263. if (!empty($order['deadline']) && $now > $order['deadline']) {
  264. if ($order['status'] == 0) {
  265. $id = $order['id'];
  266. OrderClass::updateById($id, ['status' => 4]);
  267. }
  268. }
  269. }
  270. //支付成功需要处理的流程 ssh 2020.5.25
  271. public static function payAfter($order, $updateData)
  272. {
  273. $orderId = $order['id'];
  274. $orderSn = $order['orderSn'];
  275. $now = time();
  276. self::updateById($orderId, $updateData);
  277. //发货流程的完成下单
  278. OrderSendClass::placeOrder(['orderId' => $orderId, 'payTime' => $now, 'orderSn' => $orderSn]);
  279. $sjId = $order['sjId'];
  280. $asset = MerchantAssetClass::getBySjId($sjId);
  281. //增加订单数
  282. StatOrderClass::addOrder($sjId, $asset);
  283. //如果门店和朋友圈订单并且没有填写收花人的直接将订单置为自取并且是完成状态
  284. if (isset($order['fromType']) && in_array($order['fromType'], [0, 2])) {
  285. if (isset($order['receiveMobile']) && isset($order['address'])) {
  286. if (empty($order['receiveMobile']) && empty($order['address'])) {
  287. OrderSendClass::withoutSend($order);
  288. }
  289. }
  290. }
  291. }
  292. }