OrderService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /**
  3. * 商城订单服务
  4. * 用途:mallApp 下单与订单列表组装;列表为拼团单批量附带 groupBuyStatus
  5. */
  6. namespace bizMall\order\services;
  7. use bizMall\base\services\BaseService;
  8. use bizMall\goods\classes\GoodsClass;
  9. use bizMall\hd\classes\HdClass;
  10. use bizMall\item\classes\ItemClass;
  11. use bizMall\merchant\services\MerchantCapitalService;
  12. use bizHd\groupBuy\classes\GroupBuyClass;
  13. use bizMall\order\classes\OrderClass;
  14. use bizMall\order\classes\OrderGoodsClass;
  15. use bizMall\order\classes\OrderItemClass;
  16. use bizMall\promote\services\CouponService;
  17. use bizMall\shop\classes\ShopClass;
  18. use bizMall\user\classes\UserAssetClass;
  19. use bizHd\hb\classes\HbClass;
  20. use common\components\dict;
  21. use common\components\delivery\platform\fengniao\OrderStatusUtil;
  22. use common\components\imgUtil;
  23. use common\components\orderSn;
  24. use common\components\stringUtil;
  25. use common\components\util;
  26. use Yii;
  27. class OrderService extends BaseService
  28. {
  29. public static $baseFile = '\bizMall\order\classes\OrderClass';
  30. public static function createOrder($data, $custom)
  31. {
  32. $customId = $data['customId'] ?? 0;
  33. $hdId = $data['hdId'] ?? 0;
  34. $orderSn = orderSn::getOrderSn();
  35. $sjId = $data['sjId'] ?? 0;
  36. $shopId = $data['shopId'] ?? 0;
  37. $mainId = $data['mainId'] ?? 0;
  38. $shop = ShopClass::getById($shopId, true);
  39. if (empty($shop)) {
  40. util::fail('没有找到门店86');
  41. }
  42. $product = $data['product'] ?? [];
  43. if (empty($product)) {
  44. util::fail('没有花材');
  45. }
  46. $gIds = [];
  47. $itIds = [];
  48. foreach ($product as $val) {
  49. $currentId = $val['productId'] ?? 0;
  50. $property = $val['property'];
  51. if ($property == dict::getDict('property', 'goods')) {
  52. $gIds[] = $currentId;
  53. }
  54. if ($property == dict::getDict('property', 'item')) {
  55. $itIds[] = $currentId;
  56. }
  57. }
  58. $goodsInfo = [];
  59. $gIds = array_unique(array_filter($gIds));
  60. if (!empty($gIds)) {
  61. $goodsInfo = GoodsClass::getByIds($gIds, null, 'id');
  62. }
  63. $itemInfo = [];
  64. $itIds = array_unique(array_filter($itIds));
  65. if (!empty($itIds)) {
  66. $itemInfo = ItemClass::getByIds($itIds, null, 'id');
  67. }
  68. $weight = 0;
  69. $goodsPrice = 0;
  70. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  71. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  72. foreach ($product as $key => $val) {
  73. if (!isset($val['property'])) {
  74. util::fail('没有找到产品属性');
  75. }
  76. $property = $val['property'];
  77. $unitType = $val['unitType'] ?? 0;
  78. $currentId = $val['productId'] ?? 0;
  79. $num = $val['num'] ?? 0;
  80. $unitPrice = 0;
  81. $currentWeight = 0;
  82. if ($property == dict::getDict('property', 'goods')) {
  83. $name = $goodsInfo[$currentId]['name'] ?? '';
  84. $cover = $goodsInfo[$currentId]['cover'] ?? '';
  85. $flower = $goodsInfo[$currentId]['flower'] ?? 0;
  86. $currentWeight = $goodsInfo[$currentId]['weight'] ?? 0;
  87. if (!empty($groupCover)) {
  88. $groupCover = $cover;
  89. }
  90. $currentPrice = $goodsInfo[$currentId]['price'] ?? 0;
  91. $currentMainId = $goodsInfo[$currentId]['mainId'] ?? 0;
  92. $unitPrice = $currentPrice;
  93. if ($currentMainId != $mainId) {
  94. util::fail('不是您的商品');
  95. }
  96. $currentGoodsData = [
  97. 'orderSn' => $orderSn,
  98. 'goodsId' => $currentId,
  99. 'flower' => $flower,
  100. 'customId' => $customId,
  101. 'hdId' => $hdId,
  102. 'sjId' => $sjId,
  103. 'shopId' => $shopId,
  104. 'name' => $name,
  105. 'cover' => $cover,
  106. 'unitPrice' => $unitPrice,
  107. 'num' => $num,
  108. 'price' => bcmul($unitPrice, $num, 2),
  109. 'mainId' => $mainId,
  110. ];
  111. //库存变化在这里
  112. OrderGoodsClass::addData($currentGoodsData);
  113. }
  114. if ($property == dict::getDict('property', 'item')) {
  115. $currentItemInfo = $itemInfo[$currentId] ?? [];
  116. $name = $itemInfo[$currentId]['name'] ?? '';
  117. $cover = $itemInfo[$currentId]['cover'] ?? '';
  118. if (!empty($groupCover)) {
  119. $groupCover = $cover;
  120. }
  121. $ratio = $itemInfo[$currentId]['ratio'] ?? 20;
  122. $currentMainId = $itemInfo[$currentId]['mainId'] ?? 0;
  123. $currentPrice = \bizGhs\product\classes\ProductClass::getFinalPrice($currentItemInfo, 0, $priceMap, $addPriceMap);
  124. $currentWeight = $itemInfo[$currentId]['weight'] ?? 0;
  125. if ($currentMainId != $mainId) {
  126. util::fail('不是您的花材' . $mainId . ' ' . $currentMainId);
  127. }
  128. if ($unitType == dict::getDict('unitType', 'big')) {
  129. $unitName = $itemInfo[$currentId]['bigUnit'] ?? '';
  130. $unitId = $itemInfo[$currentId]['bigUnitId'] ?? 0;
  131. $totalNum = $num;
  132. $unitPrice = $currentPrice;
  133. } else {
  134. $unitName = $itemInfo[$currentId]['smallUnit'] ?? '';
  135. $unitId = $itemInfo[$currentId]['smallUnitId'] ?? 0;
  136. $totalNum = bcdiv($num, $ratio, 2);
  137. $div = bcdiv($currentPrice, $ratio, 2);
  138. $smallRatio = $itemInfo[$currentId]['smallRatio'] ?? 0;
  139. $unitPrice = bcmul($div, $smallRatio, 2);
  140. }
  141. $ptItemId = $itemInfo[$currentId]['itemId'] ?? 0;
  142. $classId = $itemInfo[$currentId]['classId'] ?? 0;
  143. $belongCost = $itemInfo[$currentId]['belongCost'] ?? 0;
  144. $currentItemData = [
  145. 'name' => $name,
  146. 'cover' => $cover,
  147. 'ratio' => $ratio,
  148. 'unitType' => $unitType,
  149. 'unitName' => $unitName,
  150. 'unitId' => $unitId,
  151. 'unitPrice' => $unitPrice,
  152. 'num' => $num,
  153. 'orderSn' => $orderSn,
  154. 'itemId' => $currentId,
  155. 'ptItemId' => $ptItemId,
  156. 'totalNum' => $totalNum,
  157. 'totalPrice' => 0,
  158. 'customId' => $customId,
  159. 'hdId' => $hdId,
  160. 'sjId' => $sjId,
  161. 'shopId' => $shopId,
  162. 'mainId' => $mainId,
  163. 'price' => bcmul($unitPrice, $num, 2),
  164. 'classId' => $classId,
  165. 'belongCost' => $belongCost,
  166. ];
  167. //库存变化在这里
  168. OrderItemClass::addData($currentItemData, $custom);
  169. }
  170. $weight = bcadd($weight, $currentWeight, 2);
  171. $goodsPrice = bcadd($goodsPrice, bcmul($unitPrice, $num, 2), 2);
  172. }
  173. //计算运费
  174. $sendDistance = 0;
  175. $sendCost = 0;
  176. $sendType = $data['sendType'] ?? dict::getDict('sendType', 'thirdSend');
  177. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  178. $lat = $data['lat'] ?? '';
  179. $lng = $data['long'] ?? '';
  180. if (empty($lat) || empty($lng)) {
  181. //util::fail('请填写收花地址...');
  182. }
  183. $shopLat = $shop->lat ?? '';
  184. $shopLong = $shop->long ?? '';
  185. if (empty($shopLat) || empty($shopLong)) {
  186. //util::fail('请完成门店地址');
  187. }
  188. $disRespond = OrderClass::getDistanceFee($lat, $lng, $shopLat, $shopLong, $weight);
  189. $sendCost = $disRespond['fee'] ?? 0;
  190. $sendDistance = $disRespond['distance'] ?? 0;
  191. }
  192. $data['sendDistance'] = $sendDistance;
  193. $data['sendCost'] = $sendCost;
  194. $totalPrice = bcadd($goodsPrice, $sendCost, 2);
  195. $modifyPrice = $totalPrice;
  196. if ($modifyPrice > $totalPrice) {
  197. //人工资材包装费
  198. $data['labourCost'] = bcsub($modifyPrice, $totalPrice, 2);
  199. }
  200. if ($modifyPrice < $totalPrice) {
  201. //优惠
  202. $data['discountType'] = dict::getDict('discountType', 'discount');
  203. $discountAmount = bcsub($totalPrice, $modifyPrice, 2);
  204. $data['discountAmount'] = $discountAmount;
  205. }
  206. $data['goodsPrice'] = $goodsPrice;
  207. $data['prePrice'] = $totalPrice;
  208. $data['orderPrice'] = $modifyPrice;
  209. $data['actPrice'] = $modifyPrice;
  210. $data['realPrice'] = $modifyPrice;
  211. $data['mainPay'] = $modifyPrice;
  212. $data['cash'] = 0;
  213. $data['customId'] = $customId;
  214. $data['hdId'] = $hdId;
  215. $data['sjId'] = $sjId;
  216. $data['shopId'] = $shopId;
  217. $data['mainId'] = $mainId;
  218. $data['payWay'] = dict::getDict('payWay', 'unPay');
  219. $data['orderSn'] = $orderSn;
  220. $data['status'] = OrderClass::ORDER_STATUS_UN_PAY;
  221. return OrderClass::addOrder($data);
  222. }
  223. /**
  224. * 商城顾客订单列表
  225. * 拼团单批量附带 groupBuyStatus(1拼团中 2成功 3失败 4已取消),并按成团结果覆盖 statusDesc
  226. * @param array $where
  227. * @return array
  228. */
  229. public static function getOrderList($where)
  230. {
  231. // customId:订单列表「联系客服」进 chatPage 所需
  232. // payStatus + groupBuyId:拼团单不能只看 status,需结合支付与成团结果控制履约
  233. $fields = 'id, shopId, hdId, customId, orderSn, groupBuyId, orderType, actPrice,orderPrice, goodsNum, reachDate, reachPeriod, status, payStatus, addTime';
  234. $data = self::getList($fields, $where, 'addTime DESC');
  235. if (empty($data['list'])) {
  236. return $data;
  237. }
  238. $list = $data['list'];
  239. $hdIds = array_values(array_filter(array_unique(array_column($list, 'hdId'))));
  240. $hdMap = [];
  241. if (!empty($hdIds)) {
  242. $hdMap = HdClass::getByIds($hdIds, null, 'id');
  243. }
  244. $orderSns = array_values(array_filter(array_unique(array_column($list, 'orderSn'))));
  245. // 花束明细在 xhOrderGoods,花材明细在 xhOrderItem;列表按实际明细合并,避免 orderType=0/错标导致空列表
  246. $goodsListMap = OrderGoodsService::getGoodsListByOrderSns($orderSns);
  247. $itemListMap = OrderItemService::getItemListByOrderSns($orderSns);
  248. $groupBuyIds = [];
  249. foreach ($list as $val) {
  250. $gid = intval($val['groupBuyId'] ?? 0);
  251. if ($gid > 0) {
  252. $groupBuyIds[] = $gid;
  253. }
  254. }
  255. $groupBuyStatusMap = GroupBuyClass::getStatusMapByIds($groupBuyIds);
  256. $periodData = [0 => '上午', 1 => '下午', 2 => '晚上'];
  257. foreach ($data['list'] as $key => $val) {
  258. $orderSn = $val['orderSn'] ?? '';
  259. $hdId = intval($val['hdId'] ?? 0);
  260. $orderStatus = intval($val['status'] ?? 0);
  261. $data['list'][$key]['hdName'] = $hdMap[$hdId]['name'] ?? '';
  262. $data['list'][$key]['statusDesc'] = OrderStatusUtil::getSystemStatusDesc($orderStatus);
  263. $fromGoods = (!empty($orderSn) && isset($goodsListMap[$orderSn])) ? $goodsListMap[$orderSn] : [];
  264. $fromItems = (!empty($orderSn) && isset($itemListMap[$orderSn])) ? $itemListMap[$orderSn] : [];
  265. // 有啥展示啥:纯花束/纯花材/混合都能出图;orderType 仅作业务标记,不作为列表组装门槛
  266. $goodsInfoList = array_merge($fromGoods, $fromItems);
  267. if (isset($val['goodsNum']) && $val['goodsNum'] == 0) {
  268. $data['list'][$key]['goodsNum'] = count($goodsInfoList);
  269. }
  270. $data['list'][$key]['goodsInfoList'] = $goodsInfoList;
  271. $reachTime = OrderClass::getReachTime($val);
  272. $data['list'][$key]['reachTime'] = $reachTime;
  273. $requireTime = '';
  274. if ($val['reachDate'] != '0000-00-00') {
  275. $reachPeriodId = $val['reachPeriod'];
  276. $reachPeriod = isset($periodData[$reachPeriodId]) ? $periodData[$reachPeriodId] : '上午';
  277. $requireTime = date("m-d", strtotime($val['reachDate'])) . ' ' . $reachPeriod;
  278. }
  279. $data['list'][$key]['requireReachTime'] = $requireTime;
  280. $data['list'][$key]['smallHdAvatar'] = imgUtil::groupImg('') . "?x-oss-process=image/resize,m_fill,h_80,w_80";
  281. // 拼团单附带团状态:1拼团中 2成功 3失败 4已取消,供列表控制履约按钮与状态文案
  282. $groupBuyId = intval($val['groupBuyId'] ?? 0);
  283. $groupBuyStatus = $groupBuyId > 0 ? intval($groupBuyStatusMap[$groupBuyId] ?? 0) : 0;
  284. $data['list'][$key]['groupBuyId'] = $groupBuyId;
  285. $data['list'][$key]['groupBuyStatus'] = $groupBuyStatus;
  286. // 已付款但未成团时覆盖 statusDesc,避免把拼团中误显示成待配送
  287. if ($groupBuyId > 0) {
  288. if ($groupBuyStatus === GroupBuyClass::STATUS_ONGOING
  289. && $orderStatus !== OrderClass::ORDER_STATUS_UN_PAY
  290. && $orderStatus !== OrderClass::ORDER_STATUS_CANCEL) {
  291. $data['list'][$key]['statusDesc'] = '拼团中';
  292. } elseif ($groupBuyStatus === GroupBuyClass::STATUS_FAIL) {
  293. $data['list'][$key]['statusDesc'] = '拼团失败';
  294. } elseif ($groupBuyStatus === GroupBuyClass::STATUS_CANCEL) {
  295. $data['list'][$key]['statusDesc'] = '拼团已取消';
  296. }
  297. }
  298. }
  299. return $data;
  300. }
  301. //取客户最近若干条订单 ssh 2019.11.30
  302. public static function getUserOrder($userId, $limit = 10, $order = null, $with = null)
  303. {
  304. $where = ['userId' => $userId, 'payStatus' => 1];
  305. return self::getLimitList('*', $where, $limit, $order, $with);
  306. }
  307. //支付前验证订单 ssh 2019.12.6
  308. public static function checkBeforePay($order)
  309. {
  310. $now = time();
  311. if (empty($order)) {
  312. util::fail('订单无效');
  313. }
  314. if ($order['status'] != 1) {
  315. util::fail('订单不是待付款状态');
  316. }
  317. if ($order['deadline'] <= ($now - 60)) {
  318. util::fail('订单已过期');
  319. }
  320. if (ceil($order['actPrice']) <= 0) {
  321. $hbId = isset($order['hbId']) ? intval($order['hbId']) : 0;
  322. // 红包全额抵扣后实付可以为 0;无红包的 0 元或负数仍视为金额异常
  323. if ($hbId <= 0 || bccomp((string)$order['actPrice'], '0', 2) < 0) {
  324. util::fail('订单金额有问题');
  325. }
  326. }
  327. if ($order['payStatus'] == 1) {
  328. util::fail('您已经付过了');
  329. }
  330. }
  331. //根据编号查询 ssh 2019.12.14
  332. public static function getByOrderSn($orderSn)
  333. {
  334. return OrderClass::getByOrderSn($orderSn);
  335. }
  336. //点评 ssh 2019.12.16
  337. public static function comment($data)
  338. {
  339. return OrderClass::comment($data);
  340. }
  341. //订单归类
  342. public static function classify($id, $categoryId, $usageId)
  343. {
  344. $connection = Yii::$app->db;
  345. $transaction = $connection->beginTransaction();
  346. try {
  347. $order = OrderClass::getById($id, true);
  348. if ($order->payStatus != 1) {
  349. util::fail('订单还未付款');
  350. }
  351. if ($order->classify == 1) {
  352. util::fail('已经归类过了');
  353. }
  354. $order->categoryId = $categoryId;
  355. $order->usageId = $usageId;
  356. $order->classify = 1;
  357. $order->save();
  358. //设置流水的分类和用途并分配资金
  359. $setData = [
  360. 'sjId' => $order->sjId,
  361. 'capitalId' => $order->capitalId,
  362. 'time' => $order->payTime,
  363. 'usageId' => $order->usageId,
  364. 'categoryId' => $order->categoryId,
  365. 'amount' => $order->actPrice,
  366. 'payWay' => $order->payWay,
  367. ];
  368. MerchantCapitalService::orderClassify($setData);
  369. $transaction->commit();
  370. } catch (Exception $e) {
  371. $transaction->rollBack();
  372. util::fail('没有归类成功');
  373. }
  374. }
  375. //更新配送单 ssh 2019.12.17
  376. public static function updateSheet($order, $data)
  377. {
  378. $id = $order['id'];
  379. if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) {
  380. util::fail('订单当前不是待配送状态');
  381. }
  382. $data['deliver'] = 1;
  383. OrderClass::updateById($id, $data);
  384. }
  385. //订单详情 ssh 2020.1.2
  386. public static function getOrderById($id)
  387. {
  388. return OrderClass::getOrderById($id);
  389. }
  390. //查看订单详情 ssh 2020.1.4
  391. public static function getOrderBySn($orderSn)
  392. {
  393. return OrderClass::getOrderBySn($orderSn);
  394. }
  395. //计算优惠 ssh 2020.3.9
  396. public static function getDiscountPrice($params)
  397. {
  398. $couponId = isset($params['couponId']) ? $params['couponId'] : 0;
  399. $userId = isset($params['userId']) ? $params['userId'] : 0;
  400. $price = isset($params['price']) ? $params['price'] : 0;
  401. $sourceType = $params['sourceType'] ?? 0;
  402. $discountAmount = 0;
  403. $discountType = 0;
  404. $discountPrice = $price;
  405. //使用优惠券
  406. if (!empty($couponId)) {
  407. $discountAmount = CouponService::checkBeforeUse($couponId, $price, $userId);
  408. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  409. $discountType = 1;
  410. } else {
  411. //使用会员
  412. $asset = UserAssetClass::getByUserId($userId);
  413. if (isset($asset['member']) && $asset['member'] > 0) {
  414. $currentDiscount = $asset['discount'];
  415. $currentPrice = $price * $currentDiscount;
  416. $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1);
  417. $discountType = 2;
  418. $discountAmount = stringUtil::calcSub($price, $newPrice);
  419. $discountPrice = $newPrice;
  420. }
  421. }
  422. //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 ssh 2019.9.12
  423. if ($discountPrice == $price && $sourceType == 3 && 1 == 2) {
  424. $discountAmount = OrderClass::getRandDiscount($price);
  425. //开发和测试环境直接随机优惠
  426. $env = getenv('YII_ENV');
  427. if ($env == 'local' || $env == 'test') {
  428. $discountAmount = 0.01;
  429. }
  430. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  431. $discountType = 3;
  432. }
  433. $discountType = $discountAmount <= 0 ? 0 : $discountType;
  434. $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice;
  435. return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
  436. }
  437. }