afa 7 달 전
부모
커밋
2043775a15

+ 0 - 1
application/api/controller/Order.php

@@ -7,7 +7,6 @@ use app\common\model\ProductTransfer;
 use app\common\model\ProductOrder;
 use app\common\model\ProductPopular;
 use app\common\model\ProductArea;
-use app\common\model\ProductLists;
 use app\common\model\LedgerWalletModel;
 use app\common\model\UserArea;
 use app\common\model\UserModel;

+ 37 - 5
application/api/controller/Synthesis.php

@@ -7,7 +7,7 @@ use app\common\controller\Api;
 use app\common\model\ProductLists;
 use app\common\model\ProductOrder;
 use app\common\model\Synthesis as SynthesisModel;
-use fast\Action;
+use Exception;
 use fast\Asset;
 use fast\Http;
 use think\Db;
@@ -45,11 +45,11 @@ class Synthesis extends Api
     {
         $id = $this->request->param('id', 0, 'intval');
         if(empty($id)) $this->error(__("参数有误,无可用产品"));
+        $lan = $this->request->getLan();
         //合成详情
-        $synthesis = SynthesisModel::get($id);
+        $synthesis = SynthesisModel::getBySynthesis($id, $lan);
         if (!empty($synthesis)) {
             //条件一
-            $lan = $this->request->getLan();
             $material_one = $productLists->getBySynthesisProduct($synthesis->material_one, $lan);
             $synthesis->material_one = $productOrder->getByOrderProductNum($material_one, $synthesis->material_one_num, $this->auth->id);
             //条件二
@@ -60,13 +60,45 @@ class Synthesis extends Api
             $synthesis->material_three = $productOrder->getByOrderProductNum($material_three, $synthesis->material_one_num, $this->auth->id);
         }
         $this->success('', $synthesis);
-     
-       
     }
 
  
 
+    /*
+     * 合成
+     */
+    public function create( ProductOrder $productOrder)
+    {
+        $id         = $this->request->post('id', 0, 'intval');
+        $num        = $this->request->post('num', 0, 'intval');
+        $productList= $this->request->param('product_list/a'); //选中产品
+        if(empty($id) || empty($num)) $this->error(__("参数有误,无可用产品"));
+        //合成详情
+        $synthesis = SynthesisModel::get($id);
+        if (empty($synthesis)) $this->error(__("合成活动不存在"));
    
+        $time = time();
+        if ($synthesis->start_time > $time || $synthesis->end_time < $time)$this->error(__("合成活动已结束"));
+        if ($synthesis->num + $num > $synthesis->stock)$this->error(__("库存不足"));
+
+        Db::startTrans();
+        try {
+            
+            // 合成订单
+            $productOrder->setSynthesisOrder($synthesis, $productList, $num, $this->auth->id);
+
+            // 添加新订单
+            dump( 11);die;
+          
+            // 提交事务
+            Db::commit();
+        } catch (Exception $e) {
+            // 回滚事务
+            Db::rollback();
+            $this->error($e->getMessage(), null, $e->getCode());
+        }
+        $this->success('ok');
+    }
    
 
     

+ 25 - 2
application/common/model/ProductOrder.php

@@ -3,7 +3,7 @@
 namespace app\common\model;
 
 use think\Model;
-
+use Exception;
 
 class ProductOrder extends Model
 {
@@ -84,6 +84,23 @@ class ProductOrder extends Model
         return $result;
     }
 
+    //合成订单
+    public static function setSynthesisOrder($synthesis, $productId, $num, $uid):bool
+    {
+        $result = true;
+        foreach ($productId as $key => $item) {
+
+            //获取需求数量
+            if(!isset($synthesis[$key.'_num'])) throw new Exception(__("参数有误,无可用产品"));;
+            //判断持有数量足够
+            $total_num = $synthesis[$key.'_num'] * $num;
+            if($total_num > self::getUserOrderNum($uid, $item)) throw new Exception(__("库存不足"));;
+            
+            //关闭持有订单
+            $result = self::where('user_id', $uid)->where('product_id', $item)->where('type_id', self::Popular)->where('status', self::Paid)->limit($total_num)->setField('status', self::Closure);
+        }
+        return $result;
+    }
 
     /**
      * @param int   $orderId 订单id
@@ -117,13 +134,19 @@ class ProductOrder extends Model
         if(empty($productId)) return 0;
         foreach ($productId as &$item) {
             $item['num'] = $num;
-            $count = self::where('user_id', $uid)->where('product_id', $item['id'])->where('type_id', self::Popular)->where('status', self::Paid)->sum('num');
+            $count = self::getUserOrderNum($uid, $item['id']);
             if(empty($count)) $count = 0;
             $item['hold_num'] = $count;
         }
         return $productId;
     }
        
+    //获取持有产品数量
+    private static function getUserOrderNum(int $uid, int $product_id): int
+    {
+        return  self::where('user_id', $uid)->where('product_id', $product_id)->where('type_id', self::Popular)->where('status', self::Paid)->sum('num');
+    }
+
     // 获取订单状态
     public static function getProductOrder($orderId, $status, string $field){
         return self::alias('a')->where('a.id', $orderId)

+ 8 - 1
application/common/model/Synthesis.php

@@ -32,7 +32,14 @@ class Synthesis extends Model
     const Hidden           = 0;
     const Normal           = 1;
 
-
+    //获取详情
+    public function getBySynthesis(int $id, string $lan)
+    {
+        return self::alias('a')->where('a.id', $id)
+                ->join("product_list b", "a.product_id = b.id", "left")
+                ->field('a.*,b.thum as img_url,b.'.$lan.'_name as name')
+                ->find();
+    }
 
 
     protected static function init()