OrderService.php 17 KB

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