|
|
@@ -6,11 +6,16 @@ use app\common\model\decorate\DecoratePage;
|
|
|
use app\common\model\decorate\DecorateTabbar;
|
|
|
use app\common\model\setting\PostageRegion;
|
|
|
use app\common\model\shops\ShopAddress;
|
|
|
+use app\common\model\shops\ShopCart;
|
|
|
+use app\common\model\shops\ShopGoods;
|
|
|
+use app\common\model\shops\ShopGoodSpecsInventory;
|
|
|
+use app\common\model\shops\ShopOrderGoods;
|
|
|
use app\common\model\shops\ShopOrders;
|
|
|
use app\common\model\user\UserAddress;
|
|
|
use app\common\service\ConfigService;
|
|
|
use app\common\service\FileService;
|
|
|
use think\db\Query;
|
|
|
+use think\Exception;
|
|
|
use think\facade\Db;
|
|
|
|
|
|
|
|
|
@@ -19,8 +24,92 @@ use think\facade\Db;
|
|
|
*/
|
|
|
class ShopOrderLogic extends BaseLogic
|
|
|
{
|
|
|
+ /**
|
|
|
+ * 提交订单
|
|
|
+ * @param array $params
|
|
|
+ * @return array|false
|
|
|
+ */
|
|
|
+ public static function submitOrder($params)
|
|
|
+ {
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ //获取购物车商品
|
|
|
+ $shop_carts = ShopCart::whereIn('id',$params['shop_cart_id'])->where('worker_id',$params['user_id'])->select()->toArray();
|
|
|
+ if(empty($shop_carts)){
|
|
|
+ throw new Exception('购物车商品不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ //生成订单编号
|
|
|
+ $sn = generate_sn(ShopOrders::class, 'sn');
|
|
|
+ $shop_order_goods = array();
|
|
|
+
|
|
|
+ $amount_total = 0;//总价
|
|
|
+ $amount = 0;//应支付价格
|
|
|
+ //验证商品库存是否存在
|
|
|
+ foreach ($shop_carts as $v){
|
|
|
+ $inventory = ShopGoodSpecsInventory::where('id',$v['goods_specs_inventory_id'])->findOrEmpty();
|
|
|
+ $shop_good = ShopGoods::where('id',$v['shop_goods_id'])->findOrEmpty()->toArray();
|
|
|
+ if($inventory->remaining_inventory<$v['number']){
|
|
|
+ throw new Exception($shop_good['goods_name'].'库存不足');
|
|
|
+ }
|
|
|
+ //新增订单商品镜像
|
|
|
+ $shop_order_goods[] = [
|
|
|
+ 'sn'=>$sn,
|
|
|
+ 'shop_goods_id'=>$shop_good['id'],
|
|
|
+ 'goods_category_ids' => $shop_good['goods_category_ids'],
|
|
|
+ 'goods_category_id' => $shop_good['goods_category_id'],
|
|
|
+ 'goods_name' => $shop_good['goods_name'],
|
|
|
+ 'goods_image' => $shop_good['goods_image'],
|
|
|
+ 'goods_banners' => $shop_good['goods_banners'],
|
|
|
+ 'description' => $shop_good['description'],
|
|
|
+ 'specs_type' => $shop_good['specs_type'],
|
|
|
+ 'custom_attribute_items'=>json_encode($shop_good['custom_attribute_items'],JSON_UNESCAPED_UNICODE),
|
|
|
+ 'goods_specs_inventory_id'=>$v['goods_specs_inventory_id'],
|
|
|
+ 'specs'=>json_encode($inventory->specs,JSON_UNESCAPED_UNICODE),
|
|
|
+ 'number'=>$v['number'],
|
|
|
+ 'service_total'=>$inventory->service_total,
|
|
|
+ 'service_fee'=>$inventory->service_fee
|
|
|
+ ];
|
|
|
+
|
|
|
+ //减少商品库存
|
|
|
+ $inventory->remaining_inventory = $inventory->remaining_inventory-1;
|
|
|
+ $inventory->save();
|
|
|
|
|
|
+ $amount_total += $inventory['service_total'];
|
|
|
+ $amount += $inventory['service_fee'];
|
|
|
+ }
|
|
|
+
|
|
|
+ //生成订单
|
|
|
+ $order = ShopOrders::create([
|
|
|
+ 'shop_order_type' => 1,
|
|
|
+ 'worker_id' => $params['user_id'],
|
|
|
+ 'sn' => $sn,
|
|
|
+ 'real_name' => $params['real_name'],
|
|
|
+ 'mobile' => $params['mobile'],
|
|
|
+ 'address' => $params['address'],
|
|
|
+ 'pay_status' => 0,
|
|
|
+ 'paw_way' => 2,
|
|
|
+ 'order_terminal' => $params['terminal'],
|
|
|
+ 'amount_total' => $amount_total,
|
|
|
+ 'amount' => $amount,
|
|
|
+ ]);
|
|
|
|
|
|
+ //生成订单商品镜像
|
|
|
+ $ShopOrderGoods = new ShopOrderGoods();
|
|
|
+ $ShopOrderGoods->saveAll($shop_order_goods);
|
|
|
+ //清除购物车商品
|
|
|
+ ShopCart::whereIn('id',$params['shop_cart_id'])->delete();
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ self::setError($e->getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'order_id' => (int)$order['id'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* @notes 获取详情
|