OrderService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace biz\order\services;
  3. use biz\base\services\BaseService;
  4. use biz\merchant\services\MerchantCapitalService;
  5. use biz\merchant\services\MerchantService;
  6. use biz\order\classes\OrderClass;
  7. use biz\order\models\Order;
  8. use biz\promote\services\CouponRightService;
  9. use biz\promote\services\CouponService;
  10. use biz\user\classes\UserAssetClass;
  11. use biz\user\services\UserService;
  12. use common\components\error;
  13. use common\components\stringUtil;
  14. use common\components\validate;
  15. use common\components\util;
  16. use Yii;
  17. use linslin\yii2\curl;
  18. class OrderService extends BaseService
  19. {
  20. public static $baseFile = '\biz\order\classes\OrderClass';
  21. //添加订单
  22. public static function addOrder($data)
  23. {
  24. return OrderClass::addOrder($data);
  25. }
  26. //获取订单信息 shish 2019.11.28
  27. public static function getOrderList($where)
  28. {
  29. $data = self::getList('*', $where, 'addTime DESC');
  30. if (isset($data['list']) == false || empty($data['list'])) {
  31. return $data;
  32. }
  33. $orderIds = array_column($data['list'], 'id');
  34. //获取订单的商品信息
  35. $goodsList = OrderGoodsService::getGoodsListByOrderIds($orderIds);
  36. //获取订花人的用户信息
  37. $orderUserIds = array_column($data['list'], 'userId');
  38. $orderUserIds = array_unique($orderUserIds);
  39. $userInfoList = UserService::getUserByIds($orderUserIds);
  40. $merchantIdList = array_column($data['list'], 'merchantId');
  41. $merchantIdList = array_filter(array_unique($merchantIdList));
  42. $merchantList = MerchantService::getByIds($merchantIdList, null, 'id');
  43. foreach ($data['list'] as $key => $val) {
  44. $id = $val['id'];
  45. $userId = $val['userId'];
  46. $data['list'][$key]['goodsInfoList'] = isset($goodsList[$id]) ? $goodsList[$id] : [];
  47. $data['list'][$key]['bookAvatar'] = isset($userInfoList[$userId]['smallAvatarUrl']) ? $userInfoList[$userId]['smallAvatarUrl'] : '';
  48. $merchantId = $val['merchantId'];
  49. $data['list'][$key]['merchantName'] = isset($merchantList[$merchantId]['merchantName']) ? $merchantList[$merchantId]['merchantName'] : '';
  50. $data['list'][$key]['reachTime'] = OrderClass::getReachTime($val);
  51. }
  52. return $data;
  53. }
  54. //取客户最近若干条订单 shish 2019.11.30
  55. public static function getUserOrder($userId, $limit = 10, $order = null, $with = null)
  56. {
  57. $where = ['userId' => $userId];
  58. return self::getLimitList('*', $where, $limit, $order, $with);
  59. }
  60. //支付前验证订单 shish 2019.12.6
  61. public static function checkBeforePay($order, $userId)
  62. {
  63. $now = time();
  64. OrderClass::setExpire($order);
  65. if (empty($order)) {
  66. util::fail('订单无效');
  67. }
  68. if (isset($order['userId']) && $order['userId'] != $userId) {
  69. util::fail('非法访问');
  70. }
  71. if ($order['status'] != 0) {
  72. util::fail('订单不是待付款状态');
  73. }
  74. if (!empty($order['deadline']) && $now > $order['deadline']) {
  75. util::fail('订单已过期');
  76. }
  77. if (ceil($order['actPrice']) <= 0) {
  78. util::fail('订单金额有问题');
  79. }
  80. if ($order['payStatus'] == 1) {
  81. util::fail('您已经付过了');
  82. }
  83. }
  84. //商家验证是否有效 shish 2019.12.15
  85. public static function valid($order, $merchantId)
  86. {
  87. OrderClass::setExpire($order);
  88. if (empty($order)) {
  89. util::fail('订单无效');
  90. }
  91. if ($order['merchantId'] != $merchantId) {
  92. util::fail('不是您的订单');
  93. }
  94. }
  95. //根据编号查询 shish 2019.12.14
  96. public static function getByOrderSn($orderSn)
  97. {
  98. return OrderClass::getByOrderSn($orderSn);
  99. }
  100. //点评 shish 2019.12.16
  101. public static function comment($data)
  102. {
  103. return OrderClass::comment($data);
  104. }
  105. //订单归类
  106. public static function classify($id, $categoryId, $usageId)
  107. {
  108. $connection = Yii::$app->db;//事务处理
  109. $transaction = $connection->beginTransaction();
  110. try {
  111. $order = OrderClass::getById($id, true);
  112. if ($order->payStatus != 1) {
  113. util::fail('订单还未付款');
  114. }
  115. if ($order->classify == 1) {
  116. util::fail('已经归类过了');
  117. }
  118. $order->categoryId = $categoryId;
  119. $order->usageId = $usageId;
  120. $order->classify = 1;
  121. $order->save();
  122. //设置流水的分类和用途并分配资金
  123. $setData = [
  124. 'merchantId' => $order->merchantId,
  125. 'capitalId' => $order->capitalId,
  126. 'time' => $order->payTime,
  127. 'usageId' => $order->usageId,
  128. 'categoryId' => $order->categoryId,
  129. 'amount' => $order->actPrice,
  130. 'payWay' => $order->payWay,
  131. ];
  132. MerchantCapitalService::orderClassify($setData);
  133. $transaction->commit();
  134. } catch (Exception $e) {
  135. $transaction->rollBack();
  136. util::fail('没有归类成功');
  137. }
  138. }
  139. //更新配送单 shish 2019.12.17
  140. public static function updateSheet($order, $data)
  141. {
  142. $id = $order['id'];
  143. if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) {
  144. util::fail('订单当前不是待配送状态');
  145. }
  146. $data['deliver'] = 1;
  147. OrderClass::updateById($id, $data);
  148. }
  149. //订单详情 shish 2020.1.2
  150. public static function getOrderById($id)
  151. {
  152. return OrderClass::getOrderById($id);
  153. }
  154. //查看订单详情 shish 2020.1.4
  155. public static function getOrderBySn($orderSn)
  156. {
  157. return OrderClass::getOrderBySn($orderSn);
  158. }
  159. //计算优惠 shish 2020.3.9
  160. public static function getDiscountPrice($params)
  161. {
  162. $couponId = isset($params['couponId']) ? $params['couponId'] : 0;
  163. $userId = isset($params['userId']) ? $params['userId'] : 0;
  164. $price = isset($params['price']) ? $params['price'] : 0;
  165. $sourceType = isset($params['sourceType']) ? $params['sourceType'] : 0;
  166. $discountAmount = 0;
  167. $discountType = 0;
  168. $discountPrice = $price;
  169. //使用优惠券
  170. if (!empty($couponId)) {
  171. $discountAmount = CouponService::checkBeforeUse($couponId, $price, $userId);
  172. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  173. $discountType = 1;
  174. } else {
  175. //使用会员
  176. $asset = UserAssetClass::getByUserId($userId);
  177. if (isset($asset['member']) && $asset['member'] > 0) {
  178. $currentDiscount = $asset['discount'];
  179. $currentPrice = $price * $currentDiscount;
  180. $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1);
  181. $discountType = 2;
  182. $discountAmount = stringUtil::calcSub($price, $newPrice);
  183. $discountPrice = $newPrice;
  184. }
  185. }
  186. //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 shish 2019.9.12
  187. if ($discountPrice == $price && $sourceType == 3) {
  188. $discountAmount = OrderClass::getRandDiscount($price);
  189. //开发和测试环境直接随机优惠
  190. $env = getenv('YII_ENV');
  191. if ($env == 'local' || $env == 'test') {
  192. $discountAmount = 0.01;
  193. }
  194. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  195. $discountType = 3;
  196. }
  197. $discountType = $discountAmount <= 0 ? 0 : $discountType;
  198. $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice;
  199. return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
  200. }
  201. }