afa 5 сар өмнө
parent
commit
5a6635ea13

+ 1 - 1
app/admin/controller/shop/CustomerSpec.php

@@ -11,7 +11,7 @@ use app\common\model\Customer;
 use app\common\model\StockConfig;
 use think\facade\Db;
 use app\common\model\ProductConfig;
-use app\common\service\SpecService;
+use app\api\service\SpecService;
 use app\common\model\CustomerSpec as CustomerSpecModel;
 
 #[Group("shop/customer_spec")]

+ 11 - 30
app/api/controller/Inventory.php

@@ -3,9 +3,10 @@
 namespace app\api\controller;
 
 use app\common\model\StockConfig;
-use app\api\service\auth\ApiAuthService;
-use app\common\model\ProductConfig;
+use app\api\service\StockService;
+use app\common\model\StockLog;
 use think\exception\ValidateException;
+use app\api\validate\Stock as StockValidate;
 use think\facade\Db;
 
 class Inventory extends Base
@@ -18,27 +19,24 @@ class Inventory extends Base
         $type_id = $this->request->post('type_id', 'variety_name');
         if(!in_array($type_id, [$stockConfig::VarietyName, $stockConfig::PackingBox, $stockConfig::Material])) $this->error(__('参数错误'));
         $list = $stockConfig::where('type_id', $type_id)->field('id,title,field_name')->select();
-
         $this->success('ok', $list);
     }
 
 
     //添加出入库
-    public function stock(ApiAuthService $authService)
+    public function stock(StockLog $stockLog, StockService $stockService)
     {
         
         $data = $this->request->post();
         $result = false;
         Db::startTrans();
         try {
-            
-            $where['user_id'] = $this->userinfo['id'];
           
+            validate(StockValidate::class)->scene('add')->check($data);
+            
+            $resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
         
-            //join
-            $data['variety'] = json_encode($data['variety'], JSON_UNESCAPED_UNICODE);
-            $result = $userEnterLog::create($data);
-
+            $result = $stockLog->saveAll($resData);
             Db::commit();
         }catch (ValidateException $e) {
       
@@ -54,31 +52,14 @@ class Inventory extends Base
     }
 
     //出入库记录
-    public function stocklog(ApiAuthService $authService)
+    public function stocklog(StockLog $stockLog)
     {
         
         $data = $this->request->post();
-        $result = false;
-        Db::startTrans();
-        try {
-           
-            validate(ShopValidate::class)->scene('add')->check($data);
 
-            $data['user_id'] = $this->userinfo['id'];
-            $data['variety'] = json_encode($data['variety'], JSON_UNESCAPED_UNICODE);
-            $result = $userEnterLog::create($data);
+        //$result = $userEnterLog::create($data);
 
-            Db::commit();
-        }catch (ValidateException $e) {
-      
-            return $this->error($e->getError());
-        } catch (\Exception $e) {
-            Db::rollback();
-            $this->error($e->getMessage());
-        }
-        if ($result === false) {
-            $this->error(__('没有新增任何数据'));
-        }
+   
         $this->success();
     }
 

+ 1 - 2
app/api/controller/Shops.php

@@ -2,13 +2,12 @@
 
 namespace app\api\controller;
 
-
 use app\api\validate\Shop as ShopValidate;
 use app\common\model\ShopList;
 use app\common\model\ShopDelivery;
 use think\Exception;
 use think\facade\Db;
-use app\common\service\SpecService;
+use app\api\service\SpecService;
 use think\exception\ValidateException;
 
 class Shops extends Base

+ 3 - 7
app/common/service/SpecService.php → app/api/service/SpecService.php

@@ -1,21 +1,17 @@
 <?php
 declare(strict_types=1);
-namespace app\common\service;
+namespace app\api\service;
 
 use app\common\model\ShopList;
 use app\common\model\StockConfig;
 use app\common\model\ProductConfig;
 use app\common\model\StockDetail;
 /**
- * 消息服务
+ * 录入发货
  */
-class SpecService extends BaseService{
+class SpecService{
 
    
-      protected function init()
-      {
-  
-      }
 
       //判断价格为空
       public static function getIsZeroSpecsPrice(array $data): bool

+ 44 - 0
app/api/service/StockService.php

@@ -0,0 +1,44 @@
+<?php
+declare(strict_types=1);
+namespace app\api\service;
+
+use app\common\model\ShopList;
+use app\common\model\StockConfig;
+use app\common\model\ProductConfig;
+use app\common\model\StockDetail;
+/**
+ * 出入库
+ */
+class StockService {
+
+   
+       
+      
+      //出入库:  id 库存的id
+      public static function setGoOutStock(int $uid, array $data): array
+      {     
+            $result = [];
+            foreach ($data['variety'] as $item) 
+            {
+                  if(count($item) != 3 || empty(floatval($item['num'])))  throw new \Exception('参数有误!');
+                  $weight = ($data['type']== 2) ? abs($item['num']): $item['num'];
+                  $result[] = [
+                        'user_id'         => $uid, 
+                        'type_id'         => $data['type_id'],
+                        'type'            => $data['type'],
+                        'variety_id'      => $item['variety_id'],
+                        'change'          => $item['num']
+                  ];
+              
+                  //根据品种扣除库存
+                  StockDetail::setStockConfigNum((int)$item['variety_id'], (string)$weight, $data['type_id']);
+            }
+            return $result;
+      }
+
+
+   
+
+
+   
+}

+ 26 - 0
app/api/validate/Stock.php

@@ -0,0 +1,26 @@
+<?php
+namespace app\api\validate;
+
+use think\Validate;
+
+class Stock extends Validate
+{
+    protected $rule =   [
+        'type_id'  => 'require|string',
+        'type'     => 'require|number',
+        'variety'  => 'require',    
+    ];
+    
+    protected $message  =   [
+        'type_id'      => '参数有误',
+        'type'         => '参数有误',
+        'variety'      => '参数有误',    
+    ];
+
+
+ 
+    protected $scene = [
+        'add'  =>  ['type_id','type','variety'],
+      
+    ]; 
+}

+ 0 - 2
app/common/model/StockDetail.php

@@ -21,8 +21,6 @@ class StockDetail extends BaseModel
 
 
 
- 
-
 
       /**
        * 设置库存配置,根据操作类型对指定库存进行增加或减少操作

+ 30 - 0
app/common/model/StockLog.php

@@ -0,0 +1,30 @@
+<?php
+
+declare(strict_types=1);
+
+namespace app\common\model;
+
+
+use app\common\model\base\BaseModel;
+
+class StockLog extends BaseModel
+{
+
+
+    
+    // 自动写入时间戳字段
+    protected $autoWriteTimestamp = true;
+    protected $createTime = 'createtime';
+    protected $updateTime = 'updatetime';
+    protected $deleteTime = false;
+
+    protected $type = [
+        'createtime'     =>  'timestamp:Y-m-d H:i',
+        'updatetime'     =>  'timestamp:Y-m-d H:i',
+    
+    ];
+   
+  
+
+
+}