OrderService.php 17 KB

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