CheckoutMiaosha.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service\order;
  13. use app\api\model\card\UserRiceCard as UserRiceCardModel;
  14. use app\api\model\card\UserRiceCardConsume as UserRiceCardConsumeModel;
  15. use app\api\model\fullsend\FullSendActivity;
  16. use app\api\model\GoodsCategoryRel as GoodsCategoryRelModel;
  17. use app\api\model\GoodsPackage as GoodsPackageModel;
  18. use app\api\model\kj\KjActivityJoin;
  19. use app\api\model\member\DeductionLimit as DeductionLimitModel;
  20. use app\api\model\member\GoldRice as GoldRiceModel;
  21. use app\api\model\OrderGoodsTaocan as OrderGoodsTaocanModel;
  22. use app\api\model\Order as OrderModel;
  23. use app\api\model\PromotionMonitor;
  24. use app\api\model\User as UserModel;
  25. use app\api\model\Goods as GoodsModel;
  26. use app\api\model\Setting as SettingModel;
  27. use app\api\model\shop\Shops;
  28. use app\api\model\UserAddress;
  29. use app\api\model\UserCoupon as UserCouponModel;
  30. use app\api\service\User as UserService;
  31. use app\api\service\Payment as PaymentService;
  32. use app\api\service\coupon\GoodsDeduct as GoodsDeductService;
  33. use app\api\service\ricecard\GoodsDeduct as RiceDeductService;
  34. use app\api\service\points\GoodsDeduct as PointsDeductService;
  35. use app\api\service\order\source\checkout\Factory as CheckoutFactory;
  36. use app\common\enum\Setting as SettingEnum;
  37. use app\common\enum\order\PayType as OrderPayTypeEnum;
  38. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  39. use app\common\enum\order\OrderSource as OrderSourceEnum;
  40. use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
  41. use app\common\service\BaseService;
  42. use app\common\service\card\UserRiceCard as UserRiceCardService;
  43. use app\common\service\commission\RecordWaitCommission;
  44. use app\common\service\delivery\Express as ExpressService;
  45. use app\common\service\goods\source\Factory as StockFactory;
  46. use app\common\library\helper;
  47. use app\common\exception\BaseException;
  48. use app\common\enum\order\PayStatus as PayStatusEnum;
  49. use app\common\model\ShopGoodsSku;
  50. use app\common\model\User;
  51. use http\Header\Parser;
  52. /**
  53. * 订单结算台服务类
  54. * Class Checkout
  55. * @package app\api\service\order
  56. */
  57. class CheckoutMiaosha extends BaseService
  58. {
  59. /* $model OrderModel 订单模型 */
  60. public $model;
  61. /* @var UserModel $user 当前用户信息 */
  62. private $user;
  63. // 订单结算商品列表
  64. private $goodsList = [];
  65. // 错误信息
  66. protected $error;
  67. // 错误信息状态
  68. protected $errorStatus;
  69. /**
  70. * 订单结算api参数
  71. * @var array
  72. */
  73. private $param = [
  74. 'delivery' => null, // 配送方式 默认:快递配送
  75. 'couponId' => 0, // 优惠券id
  76. 'riceCardId' => 0, // 现金米卡id
  77. 'isUsePoints' => 0, // 是否使用积分抵扣
  78. 'isUseGoldRice' => 1, // 默认使用金米粒
  79. 'remark' => '', // 买家留言
  80. 'payType' => OrderPayTypeEnum::WECHAT, // 支付方式
  81. 'addressId' => 0, //地址ID
  82. 'staffUserId' => 0, //分享链接用户ID
  83. ];
  84. /**
  85. * 订单结算的规则
  86. * @var array
  87. */
  88. private $checkoutRule = [
  89. 'isUserGrade' => false, // 会员等级折扣
  90. 'isCoupon' => true, // 优惠券抵扣
  91. 'isRiceCard' => true, // 现金米卡抵扣
  92. 'isUsePoints' => false, // 是否使用积分抵扣
  93. ];
  94. /**
  95. * 订单来源
  96. * @var array
  97. */
  98. private $orderSource = [
  99. 'source' => OrderSourceEnum::MIAOSHA,
  100. 'source_id' => 0,
  101. ];
  102. /**
  103. * 订单结算数据
  104. * @var array
  105. */
  106. private $orderData = [];
  107. /**
  108. * 构造函数
  109. * Checkout constructor.
  110. * @throws BaseException
  111. */
  112. public function __construct()
  113. {
  114. parent::__construct();
  115. $this->user = UserService::getCurrentLoginUser(true);
  116. $this->model = new OrderModel;
  117. $this->storeId = $this->getStoreId();
  118. }
  119. /**
  120. * 设置结算台请求的参数
  121. * @param $param
  122. * @return array
  123. */
  124. public function setParam($param)
  125. {
  126. $this->param = array_merge($this->param, $param);
  127. return $this->getParam();
  128. }
  129. /**
  130. * 获取结算台请求的参数
  131. * @return array
  132. */
  133. public function getParam()
  134. {
  135. return $this->param;
  136. }
  137. /**
  138. * 订单结算的规则
  139. * @param $data
  140. * @return $this
  141. */
  142. public function setCheckoutRule($data)
  143. {
  144. $this->checkoutRule = array_merge($this->checkoutRule, $data);
  145. return $this;
  146. }
  147. /**
  148. * 设置订单来源(普通订单)
  149. * @param $data
  150. * @return $this
  151. */
  152. public function setOrderSource($data)
  153. {
  154. $this->orderSource = array_merge($this->orderSource, $data);
  155. return $this;
  156. }
  157. /**
  158. * 订单确认-结算台
  159. * @param $goodsList
  160. * @return array
  161. * @throws BaseException
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. */
  166. public function onCheckout($goodsList)
  167. {
  168. // 订单确认-立即购买
  169. $this->goodsList = $goodsList;
  170. return $this->checkout();
  171. }
  172. /**
  173. * 订单结算台
  174. * @return array
  175. * @throws BaseException
  176. * @throws \think\db\exception\DataNotFoundException
  177. * @throws \think\db\exception\DbException
  178. * @throws \think\db\exception\ModelNotFoundException
  179. */
  180. private function checkout()
  181. {
  182. // 整理订单数据
  183. $this->orderData = $this->getOrderData();
  184. // 门店自提处理
  185. $this->setShopPickupData();
  186. // 验证商品状态, 是否允许购买
  187. $this->validateGoodsList();
  188. // 设置订单商品总金额()
  189. $this->setOrderTotalPrice();
  190. //订单总运费金额
  191. $this->setOrderExpress();
  192. //金米粒抵扣-V1.4.0版本添加2022年6月6日
  193. $this->setOrderGolRiceAmount();
  194. //获取当前可用的现金卡列表
  195. $userRiceCardList = $this->getUserRiceCardList();
  196. //计算使用米卡现金券抵扣后的金额
  197. $this->setOrderRiceCardMoney($userRiceCardList,(int)$this->param['riceCardId']);
  198. // 计算订单商品的实际付款金额
  199. $this->setOrderGoodsPayPrice();
  200. // 计算订单最终金额+运费
  201. $this->setOrderPayPrice();
  202. //订单商品的所有店铺集合2021年9月28日 10:56:45
  203. $this->setOrderProviderIds();
  204. // 订单商品总数量
  205. $orderTotalNum = helper::getArrayColumnSum($this->goodsList, 'total_num');
  206. // 返回订单数据
  207. return array_merge([
  208. 'goodsList' => $this->goodsList, // 商品信息
  209. 'orderTotalNum' => $orderTotalNum, // 商品总数量
  210. // 'couponList' => array_values($couponList), // 优惠券列表
  211. 'hasError' => $this->hasError(),
  212. 'errorMsg' => $this->getError(),
  213. 'errorStatus' => $this->getErrorStatus(),
  214. ], $this->orderData);
  215. }
  216. /**
  217. * 门店自提处理
  218. */
  219. public function setShopPickupData() {
  220. $deliveryType = $this->param['delivery'];
  221. // 返回商品处理
  222. $normalGoodsList = []; // 正常状态商品
  223. $invalidGoodsList = []; // 无货状态商品
  224. // 门店信息
  225. $userStaff = User::detail($this->param['staffUserId']);
  226. $shopInfo = [];
  227. $isPickup = false;
  228. if (!empty($userStaff) && $userStaff['role'] == User::SHOP_SELLER) {
  229. $shopInfo = Shops::find($userStaff['shop_id']);
  230. if ($shopInfo['is_pickup']) {
  231. $isPickup = true;
  232. }
  233. }
  234. if ($deliveryType == DeliveryTypeEnum::SHOPS_DELIVERY) {
  235. if (empty($shopInfo)) {
  236. // 门店自提方式 且门店不存在
  237. throwError("不支持门店自提方式");
  238. }
  239. if (!$shopInfo['is_pickup']) {
  240. // 门店自提开关
  241. throwError("不支持门店自提方式");
  242. }
  243. }
  244. // 门店库存总和
  245. $shopStockTotal = 0;
  246. foreach($this->goodsList as &$item) {
  247. // 获取商品门店库存
  248. if ($shopInfo) {
  249. $shopGoodsSku = ShopGoodsSku::with('shopGoods')->where(['shop_id' => $shopInfo['shop_id'], 'goods_id' => $item['goods_id'], 'goods_sku_id' => $item['goods_sku_id']])->find();
  250. if (!empty($shopGoodsSku) && $shopGoodsSku['stock_num'] > 0) {
  251. $shopStockTotal += $shopGoodsSku['stock_num'];
  252. }
  253. }
  254. // 判断配送方式
  255. if ($deliveryType == DeliveryTypeEnum::EXPRESS) { // 快递配送
  256. $normalGoodsList[] = $item;
  257. // 判断库存 todo 购买数量大于实际库存 取实际库存
  258. // 有库存
  259. if ($item['skuInfo']['stock_num'] >= $item['total_num']) {
  260. $item['stock_enough'] = true;
  261. } else {
  262. $item['stock_enough'] = false;
  263. $item['total_num'] = $item['skuInfo']['stock_num']; // 库存不够时,取最大库存
  264. $item['total_price'] = helper::bcmul($item['goods_price'], $item['total_num']);
  265. }
  266. $item['is_pickup_invalid'] = false;
  267. } elseif ($deliveryType == DeliveryTypeEnum::SHOPS_DELIVERY) { // 门店自提
  268. // 判断有无库存
  269. if (!empty($shopGoodsSku)) {
  270. $item['stock_total'] = $shopGoodsSku['shopGoods']['stock_total'];
  271. $item['alarm_stock_total'] = $shopGoodsSku['shopGoods']['alarm_stock_total'];
  272. $item['skuInfo']['stock_num'] = $shopGoodsSku['stock_num'];
  273. $item['skuInfo']['alarm_stock_num'] = $shopGoodsSku['alarm_stock_num'];
  274. if ($shopGoodsSku['stock_num'] > 0) {
  275. // 有库存
  276. if ($shopGoodsSku['stock_num'] >= $item['total_num']) {
  277. $item['stock_enough'] = true;
  278. } else {
  279. $item['stock_enough'] = false;
  280. $item['total_num'] = $shopGoodsSku['stock_num']; // 库存不够时,取最大库存
  281. $item['total_price'] = helper::bcmul($item['goods_price'], $item['total_num']);
  282. }
  283. $normalGoodsList[] = $item;
  284. } else {
  285. // 没有库存
  286. $item['stock_enough'] = false;
  287. $item['is_pickup_invalid'] = true;
  288. $invalidGoodsList[] = $item;
  289. }
  290. } else {
  291. // 无库存
  292. // $item['stock_total'] = 0;
  293. // $item['alarm_stock_total'] = 0;
  294. // $item['skuInfo']['stock_num'] = 0;
  295. // $item['skuInfo']['alarm_stock_num'] = 0;
  296. $item['stock_enough'] = false;
  297. // $item['total_num'] = 0; // 无门店库存
  298. // $item['total_price'] = 0;
  299. $item['is_pickup_invalid'] = true;
  300. $invalidGoodsList[] = $item;
  301. }
  302. }
  303. }
  304. if ($shopStockTotal <= 0) { // 门店总库存不大于0
  305. $isPickup = false;
  306. }
  307. $this->shopInfo = $shopInfo; // 门店信息
  308. $this->isPickup = $isPickup; // 是否支持门店自提
  309. $this->orderData['shopInfo'] = $shopInfo;
  310. // $this->goodsList = $normalGoodsList;
  311. // $this->invalidGoodsList = $invalidGoodsList;
  312. }
  313. /**
  314. * 设置订单使用金米粒抵扣信息
  315. * @return bool
  316. * @throws BaseException
  317. */
  318. private function setOrderGolRiceAmount()
  319. {
  320. // 设置默认数据:订单信息
  321. helper::setDataAttribute($this->orderData, [
  322. 'goldRiceAmount' => 0, // 金米粒抵扣总数量 1金米粒=0.01元
  323. 'goldRiceMoney' => 0.00, // 金米粒抵扣总金额
  324. ], false);
  325. // 设置默认数据:订单商品列表
  326. helper::setDataAttribute($this->goodsList, [
  327. 'gold_rice_amount' => 0, // 金米粒抵扣数量
  328. 'gold_rice_money' => 0, // 金米粒抵扣金额
  329. ], true);
  330. $this->orderData['isUseGoldRice'] = 0;
  331. if($this->orderData['isUseGoldRice']<1){//没有使用金米粒
  332. return false;
  333. }
  334. $orderTotalPrice = $this->orderData['orderTotalMiaoshaPrice']; //实际支付金额
  335. // 如果没有可用的金米粒,直接返回
  336. $DeductionLimit = DeductionLimitModel::where('id','>',0)->find();
  337. $score = floor($DeductionLimit['discount']*$orderTotalPrice);
  338. //不够起用数量
  339. if($score<$DeductionLimit['score']){
  340. $this->orderData['isUseGoldRice'] = -1;
  341. return false;
  342. }
  343. //用户可用金米粒数量不够起用数量
  344. if($this->user['gold_rice']<$DeductionLimit['score']){
  345. $this->orderData['isUseGoldRice'] = -1;
  346. return false;
  347. }
  348. if($score>$this->user['gold_rice']){
  349. $score = $this->user['gold_rice'];
  350. }
  351. // 获取优惠券信息
  352. $goodsListTemp = [];
  353. foreach ($this->goodsList as $key=> $good){
  354. $goodsListTemp[$key]['total_price'] = helper::bcsub($good['ms_price'],0);
  355. }
  356. $RiceMoney = new RiceDeductService;
  357. $gold_rice_money = helper::bcdiv($score,100);
  358. $completed = $RiceMoney->setGoodsCouponMoney($goodsListTemp, $gold_rice_money);
  359. // 分配订单商品抵扣的金米粒数量
  360. foreach ($this->goodsList as $key => &$goods) {
  361. $goods['gold_rice_amount'] = isset($completed[$key]['rice_card_money']) ? $completed[$key]['rice_card_money'] : 0;
  362. $goods['gold_rice_money'] = helper::bcdiv($goods['gold_rice_amount'],100);
  363. }
  364. // 记录订单抵扣的金米粒数量
  365. $this->orderData['goldRiceAmount'] = $score;
  366. $this->orderData['goldRiceMoney'] = $gold_rice_money;
  367. return true;
  368. }
  369. //我的可用现金卡
  370. public function getUserRiceCardList(){
  371. $user = UserService::getCurrentLoginUser(true);
  372. $list = UserRiceCardModel::getUserRiceCardList($user['user_id'])->toArray();
  373. $data = [];
  374. foreach ($list as $key=>&$item){
  375. $i = 0;
  376. $exceptGoodsId = helper::getArrayColumn($item['riceCardGoodsExcept'],'goods_id');
  377. foreach ($this->goodsList as $good) {
  378. if(!empty($item['dk_cat_ids']) && strpos((string)$item['dk_cat_ids'],(string)$good['category']['category_id']) === false){
  379. unset($list[$key]);
  380. $i = 1;
  381. }
  382. if(!empty($exceptGoodsId) && in_array($good['goods_id'],$exceptGoodsId)){
  383. unset($list[$key]);
  384. $i = 1;
  385. }
  386. }
  387. if(!$i){
  388. unset($item['riceCardGoodsExcept']);
  389. $data[$item['id']] = $item;
  390. }
  391. }
  392. if($this->param['riceCardId']==0){ //-1表示不使用米卡
  393. $this->param['riceCardId'] = key($data)??0;//若果没有选择米卡,默认使用第一张
  394. }
  395. return $data;
  396. }
  397. /**
  398. * 整理订单数据(结算台初始化)
  399. * @return array
  400. * @throws \think\db\exception\DataNotFoundException
  401. * @throws \think\db\exception\DbException
  402. * @throws \think\db\exception\ModelNotFoundException
  403. */
  404. private function getOrderData()
  405. {
  406. // 系统支持的配送方式 (后台设置)
  407. $deliveryType = SettingModel::getItem(SettingEnum::DELIVERY)['delivery_type'];
  408. return [
  409. 'order_source_id'=>$this->param['msId'],
  410. // 当前配送类型
  411. 'delivery' => $this->param['delivery'] > 0 ? $this->param['delivery'] : $deliveryType[0],
  412. // 默认地址
  413. 'address' => '',
  414. // 是否存在收货地址
  415. 'existAddress' => $this->user['address_id'] > 0,
  416. // 配送费用
  417. 'expressPrice' => 0.00,
  418. //是否使用了金米粒
  419. 'isUseGoldRice' => $this->param['isUseGoldRice'],
  420. //金米粒数量
  421. 'userGoldRiceNum' => $this->user['gold_rice']??0,
  422. // 当前用户收货城市是否存在配送规则中
  423. 'isIntraRegion' => true,
  424. // 是否允许使用积分抵扣
  425. 'isAllowPoints' => false,
  426. // 是否使用积分抵扣
  427. 'isUsePoints' => $this->param['isUsePoints'],
  428. // 积分抵扣金额
  429. 'pointsMoney' => 0.00,
  430. // 赠送的积分数量
  431. 'pointsBonus' => 0,
  432. // 支付方式
  433. 'payType' => $this->param['payType'],
  434. // 系统设置
  435. 'setting' => $this->getSetting(),
  436. // 推广监控ID
  437. "promotionMonitorId" => $this->param['promotionMonitorId'] ?? 0
  438. ];
  439. }
  440. /**
  441. * 获取订单页面中使用到的系统设置
  442. * @return array
  443. * @throws \think\db\exception\DataNotFoundException
  444. * @throws \think\db\exception\DbException
  445. * @throws \think\db\exception\ModelNotFoundException
  446. */
  447. private function getSetting()
  448. {
  449. // 系统支持的配送方式 (后台设置)
  450. $deliveryType = SettingModel::getItem(SettingEnum::DELIVERY)['delivery_type'];
  451. // 积分设置
  452. $pointsSetting = SettingModel::getItem(SettingEnum::POINTS);
  453. return [
  454. 'deliveryType' => $deliveryType, // 支持的配送方式
  455. 'points_name' => $pointsSetting['points_name'], // 积分名称
  456. 'points_describe' => $pointsSetting['describe'], // 积分说明
  457. ];
  458. }
  459. /**
  460. * 验证订单商品的状态
  461. * @return bool
  462. */
  463. private function validateGoodsList()
  464. {
  465. $Checkout = CheckoutFactory::getFactory(
  466. $this->user,
  467. $this->goodsList,
  468. $this->orderData['delivery'],
  469. $this->shopInfo['shop_id'] ?? 0,
  470. $this->orderSource['source']
  471. );
  472. $status = $Checkout->validateGoodsList();
  473. if(!$status){
  474. $this->setError($Checkout->getError());
  475. $this->setErrorStatus(422);
  476. }
  477. return $status;
  478. }
  479. /**
  480. * 设置订单的商品总金额(不含优惠折扣)
  481. */
  482. private function setOrderTotalPrice()
  483. {
  484. // 订单商品的总金额
  485. $this->orderData['orderTotalPrice'] = helper::number2(helper::getArrayColumnSum($this->goodsList, 'total_price'));
  486. //秒杀价总金额
  487. $this->orderData['orderTotalMiaoshaPrice'] = helper::number2(helper::getArrayColumnSum($this->goodsList, 'ms_price')*helper::getArrayColumnSum($this->goodsList, 'total_num'));
  488. }
  489. /**
  490. * 订单商品的所有店铺集合
  491. */
  492. private function setOrderProviderIds()
  493. {
  494. // 订单商品的所有店铺集合
  495. $this->orderData['orderProviderIds'] = implode(',',array_values(array_unique(helper::getArrayColumn($this->goodsList, 'provider_id'))));
  496. }
  497. /**
  498. * 设置订单的实际支付金额(含配送费)
  499. */
  500. private function setOrderPayPrice()
  501. {
  502. // 订单金额(含优惠折扣)
  503. //(商品总价 - N件X折 - 优惠券 - 分销员)+ 运费 = 微信支付+米卡抵扣+米卡抵扣运费
  504. //(total_price-coupon_money-distrbutor_total_money)+express_price = pay_price+rice_card_money+rice_card_express_money
  505. $this->orderData['orderPrice'] = helper::number2(helper::getArrayColumnSum($this->goodsList, 'total_pay_price'));
  506. // 订单实付款金额(订单金额 + 运费 - 金米粒抵扣金额 - 现金卡运费抵扣金额)
  507. $this->orderData['orderPayPrice'] = helper::number2(helper::bcadd($this->orderData['orderPrice'], $this->orderData['expressPrice']-$this->orderData['riceCardExpressMoney']));
  508. }
  509. /**
  510. * 计算订单商品的实际付款金额
  511. * @return bool
  512. */
  513. private function setOrderGoodsPayPrice()
  514. {
  515. // 商品总价 - 优惠抵扣
  516. foreach ($this->goodsList as &$goods) {
  517. //减去金米粒抵扣金额和现金米卡抵扣的金额
  518. $value = helper::bcsub($goods['ms_price']*$goods['total_num'], $goods['rice_card_money']+$goods['gold_rice_money']);
  519. $goods['total_pay_price'] = helper::number2($value);
  520. }
  521. return true;
  522. }
  523. /**
  524. * 设置订单现金米卡抵扣信息
  525. * @param int $riceCardId 可使用米卡列表
  526. * @param int $riceCardId 当前选择的现金米卡id
  527. * @return bool
  528. * @throws BaseException
  529. */
  530. private function setOrderRiceCardMoney($userRiceCardList,int $riceCardId)
  531. {
  532. // 设置默认数据:订单信息
  533. helper::setDataAttribute($this->orderData, [
  534. 'riceCardId' => $riceCardId, // 用户现金米卡id
  535. 'riceCardMoney' => 0, // 现金米卡抵扣总金额
  536. 'riceCardExpressMoney' => 0, // 现金米卡抵扣运费
  537. 'userRiceCardList' => [], // 米卡列表
  538. 'userRiceCard' => [], // 已选择的米卡
  539. ], false);
  540. // 设置默认数据:订单商品列表
  541. helper::setDataAttribute($this->goodsList, [
  542. 'rice_card_id' => 0, // 用户现金米卡id
  543. 'rice_card_money' => 0, // 金米卡抵扣金额
  544. ], true);
  545. if($riceCardId<0){//如果米卡ID小于0表示不使用,但是也要返回可使用列表
  546. $this->orderData['userRiceCardList'] = array_values($userRiceCardList);
  547. return true;
  548. }
  549. // 验证选择的优惠券ID是否合法
  550. if (!$this->verifyUserRiceCard($riceCardId, $userRiceCardList)) {
  551. return false;
  552. }
  553. // 获取优惠券信息
  554. $riceCard = $this->getRiceCardInfo($riceCardId, $userRiceCardList);
  555. $goodsListTemp = [];
  556. foreach ($this->goodsList as $key=> $good){
  557. $goodsListTemp[$key]['total_price'] = helper::bcsub($good['ms_price']*$good['total_num'],$good['gold_rice_money']);
  558. }
  559. $RiceMoney = new RiceDeductService;
  560. $completed = $RiceMoney->setGoodsCouponMoney($goodsListTemp, $riceCard['balance']);
  561. // 分配订单商品优惠券抵扣金额
  562. $rice_card_money = 0;
  563. foreach ($this->goodsList as $key => &$goods) {
  564. $goods['rice_card_money'] = isset($completed[$key]['rice_card_money']) ? $completed[$key]['rice_card_money'] / 100 : 0;
  565. $goods['rice_card_id'] = $riceCardId;
  566. $rice_card_money += $goods['rice_card_money'];
  567. }
  568. $orderTotalMiaoshaPrice = $this->orderData['orderTotalMiaoshaPrice']-$this->orderData['goldRiceMoney']; //实际支付金额
  569. //有运费,并且现金米卡可用的金额大于商品总价
  570. $rice_card_express_money = 0;
  571. if($riceCard['balance']>$orderTotalMiaoshaPrice && $this->orderData['expressPrice']>0){
  572. $residueMoney = $riceCard['balance']-$orderTotalMiaoshaPrice;
  573. if($residueMoney-$this->orderData['expressPrice']>0){
  574. $rice_card_express_money += $this->orderData['expressPrice'];
  575. }else{
  576. $rice_card_express_money += $residueMoney;
  577. }
  578. }
  579. // 记录订单现金米卡信息
  580. $this->orderData['userRiceCard'] = $userRiceCardList[$riceCardId];
  581. $this->orderData['riceCardId'] = $riceCardId;
  582. $this->orderData['riceCardMoney'] = helper::bcmul($rice_card_money+$rice_card_express_money,1);
  583. $this->orderData['riceCardExpressMoney'] = helper::bcmul($rice_card_express_money,1);
  584. $this->orderData['userRiceCardList'] = array_values($userRiceCardList);
  585. return true;
  586. }
  587. /**
  588. * 验证用户选择的现金米卡
  589. * @param $riceCard
  590. * @return bool
  591. * @throws BaseException
  592. */
  593. private function verifyUserRiceCard($riceCardId,$userRiceCardList)
  594. {
  595. // 是否开启现金米卡抵扣
  596. if (!$this->checkoutRule['isRiceCard']) {
  597. return false;
  598. }
  599. // 如果没有可用的现金米卡,直接返回
  600. if ($riceCardId <= 0) {
  601. return false;
  602. }
  603. // 判断优惠券是否存在
  604. $riceCard = $this->getRiceCardInfo($riceCardId, $userRiceCardList);
  605. // 如果没有现金米卡,直接返回
  606. if (empty($riceCard)) {
  607. throwError('未找到米卡信息');
  608. }
  609. return true;
  610. }
  611. /**
  612. * 查找指定的优惠券信息
  613. * @param int $couponId 优惠券ID
  614. * @param array $couponList 优惠券列表
  615. * @return false|mixed
  616. */
  617. private function getRiceCardInfo(int $riceCardId, array $userRiceCardList)
  618. {
  619. return helper::getArrayItemByColumn($userRiceCardList, 'id', $riceCardId);
  620. }
  621. /**
  622. * 订单配送-快递配送
  623. * @return bool
  624. * @throws \think\db\exception\DataNotFoundException
  625. * @throws \think\db\exception\DbException
  626. * @throws \think\db\exception\ModelNotFoundException
  627. */
  628. private function setOrderExpress()
  629. {
  630. // 设置默认数据:配送费用
  631. helper::setDataAttribute($this->goodsList, [
  632. 'expressPrice' => 0,
  633. ], true);
  634. // 当前用户收货城市id
  635. if($this->param['addressId']){ //有传参
  636. $addredd = UserAddress::get(['user_id' => $this->user['user_id'], 'address_id' => $this->param['addressId']]);
  637. $cityId = $addredd ? (int)$addredd['city_id'] : 0;
  638. $this->orderData['address'] = $addredd;
  639. }else{ //没有传参读默认地址
  640. $cityId = $this->user['address_default'] ? (int)$this->user['address_default']['city_id'] : 0;
  641. $this->orderData['address'] = $this->user['address_default'];
  642. }
  643. if ($this->param['delivery'] == DeliveryTypeEnum::EXPRESS) { // 普通快递
  644. // 初始化配送服务类
  645. $ExpressService = new ExpressService($cityId, $this->goodsList);
  646. // 验证商品是否在限购配送范围
  647. $isDeliveryLimit = $ExpressService->isDeliveryLimit();
  648. if($cityId > 0){
  649. if ($isDeliveryLimit == false) {
  650. $this->setError("很抱歉,您的收货地址在限购地区的配送范围内");
  651. $this->setErrorStatus(423);
  652. $this->orderData['isIntraRegion'] = $isDeliveryLimit;
  653. }else{
  654. // 验证商品是否在配送范围
  655. $isIntraRegion = $ExpressService->isIntraRegion();
  656. if ($isIntraRegion == false) {
  657. $notInRuleGoodsName = $ExpressService->getNotInRuleGoodsName();
  658. $this->setError("很抱歉,您的收货地址不在商品 [".limit_str((string)$notInRuleGoodsName)."] 的配送范围内");
  659. $this->setErrorStatus(423);
  660. }
  661. $this->orderData['isIntraRegion'] = $isIntraRegion;
  662. }
  663. }
  664. // 订单总运费金额
  665. $this->orderData['expressPrice'] = $ExpressService->getDeliveryFee();
  666. } else { // 门店自提
  667. $this->orderData['expressPrice'] = "0.00";
  668. }
  669. return true;
  670. }
  671. /**
  672. * 创建新订单
  673. * @param array $order 订单信息
  674. * @return bool
  675. */
  676. public function createOrder(array $order)
  677. {
  678. // 表单验证
  679. if (!$this->validateOrderForm($order)) {
  680. return false;
  681. }
  682. // 创建新的订单
  683. $status = $this->model->transaction(function () use ($order) {
  684. // dd($order);
  685. // 创建订单事件
  686. return $this->createOrderEvent($order);
  687. });
  688. // 余额支付标记订单已支付
  689. if ($status && $order['payType'] == OrderPayTypeEnum::BALANCE) {
  690. return $this->model->onPaymentByBalance($this->model['order_no']);
  691. }
  692. return $status;
  693. }
  694. /**
  695. * 创建订单事件
  696. * @param $order
  697. * @return bool
  698. * @throws BaseException
  699. * @throws \Exception
  700. */
  701. private function createOrderEvent($order)
  702. {
  703. // 门店自提
  704. if ($order['delivery'] == DeliveryTypeEnum::SHOPS_DELIVERY) {
  705. $order['hx_code'] = $this->model->hxCode();
  706. $order['pickup_deadline'] = time() + OrderModel::getPickupDeadline() * 60; // 提货截止时间:提交订单2天内
  707. $order['shop_id'] = $order['shopInfo']['shop_id']; // 订单自提门店id
  708. }
  709. // 新增订单记录
  710. $status = $this->add($order, $this->param['remark']);
  711. if ($order['delivery'] == DeliveryTypeEnum::EXPRESS) {
  712. // 记录收货地址
  713. $this->saveOrderAddress($order['address']);
  714. }
  715. // 保存订单商品信息
  716. $this->saveOrderGoods($order);
  717. // 保存订单套餐商品信息
  718. // $this->saveOrderGoodsTaocan($order);
  719. // 更新商品库存 (针对下单减库存的商品)
  720. $this->updateGoodsStockNum($order);
  721. // //更新砍价信息
  722. // $this->saveKjActivityJoin($order);
  723. //金米粒处理逻辑
  724. if($order['orderPayPrice']>0){
  725. //冻结金米粒数量
  726. if($order['goldRiceAmount']>0) UserModel::setIncDecByField($this->user['user_id'],['gold_rice_frozen'=>(float)$order['goldRiceAmount']],['gold_rice'=>(float)$order['goldRiceAmount']]);
  727. }else{
  728. if($order['goldRiceAmount']>0){
  729. //写金米粒出账记录
  730. GoldRiceModel::add($this->user['user_id'],$this->model['order_no'],'下单抵扣',$order['goldRiceAmount'],0);
  731. //用户直接扣除金米粒
  732. UserModel::setDecByField($this->user['user_id'],'gold_rice',(float)$order['goldRiceAmount']);
  733. }
  734. }
  735. if($order['riceCardId']>0 && $order['riceCardMoney']>0){
  736. //根据支付的金额来判断逻辑
  737. if($order['orderPayPrice']>0){
  738. //冻结现金卡金额
  739. UserRiceCardModel::setIncDecByField($order['riceCardId'],['frozen_amount'=>(float)$order['riceCardMoney']],['balance'=>(float)$order['riceCardMoney']]);
  740. }else{//不需要结算金额
  741. //写现金卡出账记录
  742. UserRiceCardConsumeModel::add($order['userRiceCard'],$this->model['order_no'],$order['orderPayPrice'],$order['riceCardMoney']);
  743. //如果可用余额等于要抵扣的金额
  744. if($order['riceCardMoney']==$order['userRiceCard']['balance']){
  745. //现金卡直接减可用余额,并且状态改为已失效
  746. UserRiceCardModel::setIncDecByField($order['riceCardId'],[],['balance'=>(float)$order['riceCardMoney']],['effect_state'=>2]);
  747. }else{
  748. //现金卡直接减可用余额
  749. UserRiceCardModel::setDecByField($order['riceCardId'],'balance',(float)$order['riceCardMoney']);
  750. }
  751. // 更新商品库存、销量(付款后减库存、加销量)
  752. $order['goods'] = $order['goodsList'];
  753. $order['delivery_type'] = $order['delivery'];
  754. //主商品销量
  755. // StockFactory::getFactory(OrderSourceEnum::MASTER)->updateStockSales($order);
  756. // //活动销量
  757. StockFactory::getFactory($this->orderSource['source'])->updateStockSales($order);
  758. }
  759. }
  760. //待结算佣金计算
  761. $RecordWaitCommission = new RecordWaitCommission;
  762. //分销记录放入队列
  763. $RecordWaitCommission->recordWaitCommission($this->model['order_id']);
  764. return $status;
  765. }
  766. /**
  767. * 构建支付请求的参数
  768. * @return array
  769. * @throws BaseException
  770. * @throws \think\db\exception\DataNotFoundException
  771. * @throws \think\db\exception\DbException
  772. * @throws \think\db\exception\ModelNotFoundException
  773. */
  774. public function onOrderPayment()
  775. {
  776. return PaymentService::orderPayment($this->model, $this->param['payType']);
  777. }
  778. /**
  779. * 表单验证 (订单提交)
  780. * @param array $order 订单信息
  781. * @return bool
  782. */
  783. private function validateOrderForm(array $order)
  784. {
  785. if ($order['delivery'] == DeliveryTypeEnum::EXPRESS) {
  786. if (empty($order['address'])) {
  787. $this->error = '您还没有选择配送地址';
  788. return false;
  789. }
  790. }
  791. // 余额支付时判断用户余额是否足够
  792. if ($order['payType'] == OrderPayTypeEnum::BALANCE) {
  793. if ($this->user['balance'] < $order['orderPayPrice']) {
  794. $this->error = '您的余额不足,无法使用余额支付';
  795. return false;
  796. }
  797. }
  798. return true;
  799. }
  800. /**
  801. * 当前订单是否存在和使用积分抵扣
  802. * @param $order
  803. * @return bool
  804. */
  805. private function isExistPointsDeduction($order)
  806. {
  807. return $order['isAllowPoints'] && $order['isUsePoints'];
  808. }
  809. /**
  810. * 新增订单记录
  811. * @param $order
  812. * @param string $remark
  813. * @return false|int
  814. */
  815. private function add($order, $remark = '')
  816. {
  817. // 根据推广监控ID判断活动效果归属
  818. $promotionMonitorId = PromotionMonitor::checkActEffectOwner(9, $order['promotionMonitorId'] ?? 0, $order['goodsList'][0]['ms_id'] ?? 0);
  819. // 订单数据
  820. $data = [
  821. 'user_id' => $this->user['user_id'],
  822. 'order_no' => $this->model->orderNo(),
  823. 'total_price' => $order['orderTotalPrice'],
  824. 'order_price' => $order['orderPrice'],
  825. 'pay_price' => $order['orderPayPrice'],
  826. 'delivery_type' => $order['delivery'],
  827. 'pay_type' => $order['orderPayPrice'] > 0 ? $order['payType'] : OrderPayTypeEnum::MICAH_EXCHANGE,
  828. 'buyer_remark' => trim($remark),
  829. 'order_source' => $this->orderSource['source'],
  830. 'order_source_id' => $order['goodsList'][0]['ms_id'] ?? 0,
  831. 'miaosha_money' => helper::bcsub($order['goodsList'][0]['total_price'],$order['goodsList'][0]['ms_price']*$order['goodsList'][0]['total_num']),
  832. 'order_status' => OrderStatusEnum::NORMAL,
  833. 'store_id' => $this->storeId,
  834. 'provider_ids' => $order['orderProviderIds']??0,
  835. 'gold_rice_amount' => $order['goldRiceAmount']??0,
  836. 'gold_rice_money' => $order['goldRiceMoney']??0,
  837. 'distributor_total_money' => $order['distributorTotalMoney']??0,
  838. 'rice_card_id' => $order['riceCardId']>0?$order['riceCardId']:0,
  839. 'rice_card_money' => $order['riceCardMoney']??0,
  840. 'rice_card_express_money' => $order['riceCardExpressMoney']??0,
  841. 'promotion_monitor_id' => $promotionMonitorId,
  842. 'staff_user_id' => $order['goodsList'][0]['staffUserId'] ?? 0,
  843. 'staff_shop_id' => $order['goodsList'][0]['shopId'] ?? 0,
  844. ];
  845. //米卡兑换直接成功
  846. if($order['orderPayPrice']<=0 || $order['payType']==OrderPayTypeEnum::MICAH_EXCHANGE){
  847. $data['pay_status'] = PayStatusEnum::SUCCESS;
  848. $data['pay_time'] = time();
  849. $data['invoice_deadline'] = time() + OrderModel::getInvoiceDeadline() * 60; // 申请开票截止时间:订单付款后的40天内可以申请开票
  850. }
  851. if ($order['delivery'] == DeliveryTypeEnum::EXPRESS) {
  852. $data['express_price'] = $order['expressPrice'];
  853. }
  854. // 保存订单记录
  855. return $this->model->save($data);
  856. }
  857. /**
  858. * 保存订单商品信息
  859. * @param $order
  860. * @return int
  861. */
  862. private function saveOrderGoods($order)
  863. {
  864. // 当前订单是否存在和使用积分抵扣
  865. $isExistPointsDeduction = $this->isExistPointsDeduction($order);
  866. // 订单商品列表
  867. $goodsList = [];
  868. foreach ($order['goodsList'] as $goods) {
  869. $item = [
  870. 'user_id' => $this->user['user_id'],
  871. 'store_id' => $this->storeId,
  872. 'goods_id' => $goods['goods_id'],
  873. 'goods_name' => $goods['goods_name'],
  874. 'goods_no' => $goods['goods_no'] ?: '',
  875. 'image_id' => (int)current($goods['goods_images'])['file_id'],
  876. 'deduct_stock_type' => $goods['deduct_stock_type'],
  877. 'spec_type' => $goods['spec_type'],
  878. 'goods_sku_id' => $goods['skuInfo']['goods_sku_id'],
  879. 'goods_props' => $goods['skuInfo']['goods_props'] ?: '',
  880. 'content' => $goods['content'] ?? '',
  881. 'goods_sku_no' => $goods['skuInfo']['goods_sku_no'] ?: '',
  882. 'goods_price' => $goods['skuInfo']['goods_price'],
  883. 'specs' => $goods['skuInfo']['specs'],
  884. 'supplier' => $goods['supplier'],
  885. 'line_price' => $goods['skuInfo']['line_price'],
  886. 'goods_weight' => $goods['skuInfo']['goods_weight'],
  887. 'is_user_grade' => (int)$goods['is_user_grade'],
  888. 'grade_ratio' => $goods['grade_ratio']??0,
  889. 'grade_goods_price' => $goods['grade_goods_price']??0,
  890. 'grade_total_money' => $goods['grade_total_money']??0,
  891. 'total_num' => $goods['total_num'],
  892. 'total_price' => $goods['total_price'],
  893. 'total_pay_price' => $goods['total_pay_price']??0,
  894. 'provider_id' => $goods['provider_id'],
  895. 'brand' => $goods['brand'],
  896. 'province_id' => $goods['province_id'],
  897. 'city_id' => $goods['city_id'],
  898. 'storage' => $goods['storage'],
  899. 'unit' => $goods['unit'],
  900. 'clearing_price' => $goods['skuInfo']['clearing_price']??'0.00',
  901. 'platform_rate' => $goods['skuInfo']['platform_rate']??'0.00',
  902. 'rice_card_id' => $goods['rice_card_id']??0,
  903. 'rice_card_money' => $goods['rice_card_money']??0,
  904. ];
  905. // 记录订单商品来源id
  906. $item['goods_source_id'] = isset($goods['goods_source_id']) ? $goods['goods_source_id'] : 0;
  907. $goodsList[] = $item;
  908. }
  909. return $this->model->goods()->saveAll($goodsList) !== false;
  910. }
  911. /**
  912. * 保存订单套餐商品信息
  913. * @param $order
  914. * @return int
  915. */
  916. private function saveOrderGoodsTaocan($order)
  917. {
  918. // 当前订单是否存在和使用积分抵扣
  919. $isExistPointsDeduction = $this->isExistPointsDeduction($order);
  920. // 订单商品列表
  921. $goodsList = [];
  922. foreach ($order['goodsList'] as $key=> $goods) {
  923. if($goods['goods_type']!=30){//是套餐商品
  924. continue;
  925. }
  926. $relGoodList = GoodsPackageModel::with(['relGood'=>['images','skuList']])->where('goods_id',$goods['goods_id'])->select();
  927. foreach ($relGoodList as $relGood){
  928. $relGood['skuList'] = [];
  929. foreach ($relGood['relGood']['skuList'] as $sku){
  930. if($sku['goods_sku_id']==$relGood['rel_goods_sku_id']){
  931. $relGood['skuList'] = $sku;
  932. }
  933. }
  934. $item = [
  935. 'user_id' => $this->user['user_id'],
  936. 'order_id' => $this->model['order_id'],
  937. 'order_goods_id' => $this->model->goods[$key]['order_goods_id'],
  938. 'store_id' => $this->storeId,
  939. 'goods_id' => $relGood['relGood']['goods_id'],
  940. 'goods_name' => $relGood['relGood']['goods_name'],
  941. 'goods_no' => $relGood['relGood']['goods_no'] ?: '',
  942. 'image_id' => (int)$relGood['relGood']['images'][0]['image_id']??0,
  943. 'deduct_stock_type' => $relGood['relGood']['deduct_stock_type'],
  944. 'spec_type' => $relGood['relGood']['spec_type'],
  945. 'goods_sku_id' => $relGood['skuList']['goods_sku_id']??0,
  946. 'goods_props' => $relGood['skuList']['goods_props'] ?? '',
  947. 'content' => $relGood['relGood']['content'] ?? '',
  948. 'goods_sku_no' => $relGood['skuList']['goods_sku_no'] ?? '',
  949. 'goods_price' => $relGood['skuList']['goods_price']??'0.00',
  950. 'line_price' => $relGood['skuList']['line_price']??'0.00',
  951. 'goods_weight' => $relGood['skuList']['goods_weight']??'0.00',
  952. 'is_user_grade' => (int)$relGood['relGood']['is_user_grade'],
  953. 'grade_ratio' => $relGood['relGood']['grade_ratio']??0,
  954. 'grade_goods_price' => $relGood['relGood']['grade_goods_price']??0,
  955. 'grade_total_money' => $relGood['relGood']['grade_total_money']??0,
  956. 'coupon_id' => $relGood['relGood']['coupon_id']??0,
  957. 'coupon_money' => $relGood['relGood']['coupon_money']??0,
  958. 'points_money' => $isExistPointsDeduction ? $relGood['relGood']['points_money'] : 0.00,
  959. 'points_num' => $isExistPointsDeduction ? $relGood['relGood']['points_num'] : 0,
  960. 'points_bonus' => $relGood['relGood']['points_bonus']??0,
  961. 'total_num' => 1,
  962. 'total_price' => $relGood['relGood']['total_price']??0,
  963. 'total_pay_price' => $relGood['relGood']['total_pay_price']??0,
  964. 'provider_id' => $relGood['relGood']['provider_id'],
  965. 'brand' => $relGood['relGood']['brand'],
  966. 'province_id' => $goods['province_id'],
  967. 'city_id' => $relGood['relGood']['city_id'],
  968. 'storage' => $relGood['relGood']['storage'],
  969. 'unit' => $relGood['relGood']['unit'],
  970. 'clearing_price' => $relGood['skuList']['clearing_price']??'0.00',
  971. 'platform_rate' => $relGood['skuList']['platform_rate']??'0.00',
  972. 'goods_type' => 30,
  973. ];
  974. // 记录订单商品来源id
  975. $item['goods_source_id'] = isset($relGood['goods_source_id']) ? $relGood['goods_source_id'] : 0;
  976. $goodsList[] = $item;
  977. }
  978. }
  979. return (new OrderGoodsTaocanModel())->saveAll($goodsList) !== false;
  980. }
  981. /**
  982. * 更新商品库存 (针对下单减库存的商品)
  983. * 普通快递、门店自提 分开扣库存
  984. * @param $order
  985. * @return mixed
  986. */
  987. private function updateGoodsStockNum($order)
  988. {
  989. //主商品库存
  990. // StockFactory::getFactory(OrderSourceEnum::MASTER)->updateGoodsStock($order);
  991. //活动商品库存
  992. StockFactory::getFactory($this->model['order_source'])->updateGoodsStock($order);
  993. }
  994. // /**
  995. // * 保存砍价信息
  996. // * @param $order
  997. // * @return int
  998. // */
  999. // private function saveKjActivityJoin($order)
  1000. // {
  1001. // // 订单商品列表
  1002. // foreach ($order['goodsList'] as $key=> $goods) {
  1003. // $data[] = [
  1004. // 'where' => ['id' => $goods['bargain_id']],
  1005. // 'data' => [
  1006. // 'order_id' => $this->model['order_id'],//订单ID
  1007. // 'order_price' => helper::bcadd($goods['bargain_price'],$this->orderData['expressPrice']),//订单金额+运费
  1008. // 'is_submit_order'=>1 //是否提交订单 1是
  1009. // ]
  1010. // ];
  1011. // }
  1012. // return (new KjActivityJoin())->updateAll($data) !== false;
  1013. // }
  1014. /**
  1015. * 记录收货地址
  1016. * @param $address
  1017. * @return false|\think\Model
  1018. */
  1019. private function saveOrderAddress($address)
  1020. {
  1021. return $this->model->address()->save([
  1022. 'user_id' => $this->user['user_id'],
  1023. 'store_id' => $this->storeId,
  1024. 'name' => $address['name'],
  1025. 'phone' => $address['phone'],
  1026. 'province_id' => $address['province_id'],
  1027. 'city_id' => $address['city_id'],
  1028. 'region_id' => $address['region_id'],
  1029. 'detail' => $address['detail'],
  1030. ]);
  1031. }
  1032. /**
  1033. * 设置错误信息
  1034. * @param $error
  1035. */
  1036. protected function setError($error)
  1037. {
  1038. empty($this->error) && $this->error = $error;
  1039. }
  1040. /**
  1041. * 获取错误信息
  1042. * @return mixed
  1043. */
  1044. public function getError()
  1045. {
  1046. return $this->error ?: '';
  1047. }
  1048. /**
  1049. * 设置错误状态
  1050. * @param $errorStatus
  1051. */
  1052. protected function setErrorStatus($errorStatus)
  1053. {
  1054. empty($this->errorStatus) && $this->errorStatus = $errorStatus;
  1055. }
  1056. /**
  1057. * 获取错误信息
  1058. * @return mixed
  1059. */
  1060. public function getErrorStatus()
  1061. {
  1062. return $this->errorStatus ?: 0;
  1063. }
  1064. /**
  1065. * 是否存在错误
  1066. * @return bool
  1067. */
  1068. public function hasError()
  1069. {
  1070. return !empty($this->error);
  1071. }
  1072. }