Browse Source

兑换商品库存

wufeng 2 năm trước cách đây
mục cha
commit
bede38568c

+ 10 - 35
app/admin/controller/InventoryExchange.php

@@ -4,7 +4,6 @@ namespace app\admin\controller;
 
 use app\admin\logic\InventoryExchangeLogic;
 use app\BaseController;
-use app\model\CommonModel;
 use think\exception\ValidateException;
 use think\facade\Config;
 use think\facade\Validate;
@@ -15,7 +14,7 @@ class InventoryExchange extends BaseController
     //获取兑换商品库存列表
     public function list()
     {
-        $param = $this->request->only(['page' => 1, 'size' => 10, 'status' => '', 'company_id' => '', 'card_id' => ''], 'post');
+        $param = $this->request->only(['page' => 1, 'size' => 10, 'status' => '', 'inventory_start' => '', 'inventory_end' => '', 'username' => '', 'name' => '', 'mobile' => ''], 'post');
 
         return InventoryExchangeLogic::list($param);
     }
@@ -42,47 +41,23 @@ class InventoryExchange extends BaseController
     //编辑兑换商品库存
     public function edit()
     {
-        $param = $this->request->only(['id', 'company_id', 'card_id', 'video_list', 'remark' => ''], 'post');
+        $param = $this->request->only(['id', 'inventory'], 'post');
 
-        $val = Validate::rule(array_merge(Config::get('validate_rules.VideoGroupAdd'), ['id|兑换商品库存id' => 'require|number|gt:0']));
+        $val = Validate::rule([
+            'id|兑换商品库存id' => 'require|number|gt:0',
+            'inventory|库存数' => 'require|number|gt:0',
+        ]);
 
         if (!$val->check($param)) throw new ValidateException($val->getError());
 
         return InventoryExchangeLogic::edit($param);
     }
 
-    //兑换商品库存启禁用
-    public function status()
-    {
-        $param = $this->request->only(['id','status'],'post');
-
-        $val = Validate::rule(Config::get('validate_rules.status'));
-
-        if (!$val->check($param)) throw new ValidateException($val->getError());
-
-        return InventoryExchangeLogic::status($param);
-    }
-
-    //删除兑换商品库存
-    public function delete()
+    //兑换商品库存变动记录
+    public function log()
     {
-        $id = $this->request->post('id/d', 0);
-        return InventoryExchangeLogic::delete($id);
-    }
-
-    //兑换商品库存置顶
-    public function top()
-    {
-        $param = $this->request->only(['id','is_top'],'post');
-
-        $val = Validate::rule([
-            'id' => 'require|number|gt:0',
-            'is_top|是否置顶' => 'require|number|in:' . CommonModel::$top_no . ',' . CommonModel::$top_yes,
-        ]);
-
-        if (!$val->check($param)) throw new ValidateException($val->getError());
-
-        return InventoryExchangeLogic::top($param);
+        $param = $this->request->only(['page' => 1, 'size' => 10, 'id' => 0], 'post');
+        return InventoryExchangeLogic::log($param);
     }
 
 }

+ 75 - 128
app/admin/logic/InventoryExchangeLogic.php

@@ -4,6 +4,7 @@ namespace app\admin\logic;
 
 use app\model\AccountModel;
 use app\model\CommonModel;
+use app\model\GoodModel;
 use app\model\InventoryExchangeModel;
 use think\Exception;
 use think\exception\ValidateException;
@@ -17,19 +18,19 @@ class InventoryExchangeLogic extends BaseLogic
     public static function list(array $data = []): Json
     {
         $db = InventoryExchangeModel::alias('a')
-            ->leftJoin('company b', 'b.id=a.company_id')
-            ->leftJoin('card c', 'c.id=a.card_id')
-            ->where('a.is_del', CommonModel::$del_normal);
+            ->leftJoin('account b', 'b.id=a.account_id AND b.is_del=' . CommonModel::$del_normal);
 
-        if ($data['status'] != '') $db->where('a.status', $data['status']);
-        if ($data['company_id'] != '') $db->where('a.company_id', $data['company_id']);
-        if ($data['card_id'] != '') $db->where('a.card_id', $data['card_id']);
+        if ($data['status'] != '') $db->where('b.status', $data['status']);
+        if (($data['inventory_start'] != '') && ($data['inventory_end'] != '')) $db->whereBetween('a.inventory', [$data['inventory_start'], $data['inventory_end']]);
+        if ($data['username'] != '') $db->whereLike('a.account_username', '%' . $data['username'] . '%');
+        if ($data['name'] != '') $db->whereLike('a.account_name', '%' . $data['name'] . '%');
+        if ($data['mobile'] != '') $db->whereLike('b.mobile', '%' . $data['mobile'] . '%');
 
         $count = $db->count('a.id');
 
-        $list = $db->field('a.id,b.title company_title,c.title card_title,a.num,a.status,a.addtime,a.is_top')
+        $list = $db->field('a.id,a.account_username,b.status,a.account_name,b.mobile,a.inventory,a.updatetime')
             ->page($data['page'], $data['size'])
-            ->order(['a.is_top' => 'desc', 'a.id' => 'desc'])
+            ->order(['a.id' => 'desc'])
             ->select()
             ->toArray();
 
@@ -51,41 +52,46 @@ class InventoryExchangeLogic extends BaseLogic
                 ->isEmpty();
             if (!$rs) throw new Exception('该账号下的兑换商品库存已存在,不能重复添加');
 
-            $account = AccountModel::field('id,name,nickname')
-                ->where(['id'=>$data['account_id']])
+            $account = AccountModel::field('id,username,name')
+                ->where(['id' => $data['account_id'], 'is_del' => CommonModel::$del_normal])
                 ->findOrEmpty();
-            if($account->isEmpty()) throw new Exception('该账号不存在');
+            if ($account->isEmpty()) throw new Exception('该账号不存在');
+
+            $good = GoodModel::field('id,good_code,good_name')
+                ->where(['id' => $data['good_id'], 'is_del' => CommonModel::$del_normal])
+                ->findOrEmpty();
+            if ($good->isEmpty()) throw new Exception('该商品不存在');
 
             $date = date('Y-m-d H:i:s');
 
-            $video_group_id = Db::name('video_group')
-                ->strict(false)
-                ->insertGetId(array_merge($data, [
-                    'num' => count($data['video_list']),
-                    'is_top' => CommonModel::$top_no,
-                    'status' => CommonModel::$status_normal,
-                    'is_del' => CommonModel::$del_normal,
-                    'createrid' => self::$uid,
-                    'creater' => self::$uname,
-                    'addtime' => $date,
-                    'updaterid' => self::$uid,
-                    'updater' => self::$uname,
-                    'updatetime' => $date,
-                ]));
-
-            $da = [];
-            foreach ($data['video_list'] as $item) {
-                $da[] = [
-                    'video_group_id' => $video_group_id,
-                    'video_id' => $item['video_id'],
-                    'is_top' => $item['is_top'],
-                    'is_del' => CommonModel::$del_normal,
-                    'addtime' => $date,
-                    'updatetime' => $date,
-                ];
-            }
+            $inventory_exchange_id = Db::name('inventory_exchange')->insertGetId([
+                'account_id' => $data['account_id'],
+                'account_username' => $account->username,
+                'account_name' => $account->name,
+                'good_id' => $data['good_id'],
+                'good_code' => $good->good_code,
+                'good_name' => $good->good_name,
+                'inventory' => $data['inventory'],
+                'createrid' => self::$uid,
+                'creater' => self::$uname,
+                'addtime' => $date,
+                'updaterid' => self::$uid,
+                'updater' => self::$uname,
+                'updatetime' => $date,
+            ]);
 
-            Db::name('video_group_item')->insertAll($da);
+            Db::name('inventory_exchange_log')->insert([
+                'inventory_exchange_id' => $inventory_exchange_id,
+                'before_inventory' => 0,
+                'after_inventory' => $data['inventory'],
+                'good_id' => $data['good_id'],
+                'good_code' => $good->good_code,
+                'good_name' => $good->good_name,
+                'source' => CommonModel::$source_admin,
+                'createrid' => self::$uid,
+                'creater' => self::$uname,
+                'addtime' => $date,
+            ]);
 
             Db::commit();
 
@@ -102,21 +108,12 @@ class InventoryExchangeLogic extends BaseLogic
     public static function read(int $id = 0): Json
     {
         $res = InventoryExchangeModel::field(true)
-            ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
+            ->where(['id' => $id])
             ->findOrEmpty()
             ->toArray();
 
         if (empty($res)) throw new ValidateException('该兑换商品库存为空');
 
-        $res['child'] = Db::name('video_group_item')
-            ->alias('a')
-            ->field('a.id,a.video_id,a.is_top,a.is_del,b.video_name,b.remark')
-            ->leftJoin('video b', 'b.id=a.video_id AND b.is_del=' . CommonModel::$del_normal)
-            ->where(['a.is_del' => CommonModel::$del_normal, 'a.video_group_id' => $id])
-            ->order(['a.is_top' => 'desc', 'a.id' => 'desc'])
-            ->select()
-            ->toArray();
-
         return json_show(CommonModel::$success, '获取兑换商品库存详情成功', $res);
     }
 
@@ -126,59 +123,31 @@ class InventoryExchangeLogic extends BaseLogic
         Db::startTrans();
         try {
 
-            $rs = InventoryExchangeModel::field('id,company_id,card_id')
-                ->where([
-                    'is_del' => CommonModel::$del_normal,
-                    'id' => $data['id']
-                ])
+            $rs = InventoryExchangeModel::field('id,inventory,good_id,good_code,good_name')
+                ->lock(true)
+                ->where('id', $data['id'])
                 ->findOrEmpty();
             if ($rs->isEmpty()) throw new Exception('该兑换商品库存不存在');
 
-            if (($rs->company_id != $data['company_id']) || ($rs->card_id != $data['card_id'])) {
-
-                $temp = InventoryExchangeModel::field('id')
-                    ->where([
-                        'is_del' => CommonModel::$del_normal,
-                        'company_id' => $data['company_id'],
-                        'card_id' => $data['card_id']
-                    ])
-                    ->findOrEmpty()
-                    ->isEmpty();
-                if (!$temp) throw new Exception('该分组已存在');
-
-            }
-
             $date = date('Y-m-d H:i:s');
 
-            InventoryExchangeModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
-                ->strict(false)
-                ->save(array_merge($data, ['updatetime' => $date, 'updaterid' => self::$uid, 'updater' => self::$uid]));
-
-            $del = $ins = [];
-
-            foreach ($data['video_list'] as $item) {
-                if (isset($item['id']) && $item['id'] != 0) {
-                    if ($item['is_del'] == CommonModel::$del_deleted) $del[] = $item['id'];
-                    else Db::name('video_group_item')->where('is_del', CommonModel::$del_normal)->where('id', $item['id'])->save(array_merge($item, ['updatetime' => $date]));
-                } else $ins[] = [
-                    'video_group_id' => $data['id'],
-                    'video_id' => $item['video_id'],
-                    'is_top' => $item['is_top'],
-                    'is_del' => CommonModel::$del_normal,
+            if ($rs->inventory != $data['inventory']) {
+                Db::name('inventory_exchange_log')->insert([
+                    'inventory_exchange_id' => $data['id'],
+                    'before_inventory' => $rs->inventory,
+                    'after_inventory' => $data['inventory'],
+                    'good_id' => $rs->good_id,
+                    'good_code' => $rs->good_code,
+                    'good_name' => $rs->good_name,
+                    'source' => CommonModel::$source_admin,
+                    'createrid' => self::$uid,
+                    'creater' => self::$uname,
                     'addtime' => $date,
-                    'updatetime' => $date,
-                ];
-            }
-
-            if ($del) Db::name('video_group_item')
-                ->whereIn('id', $del)
-                ->where('is_del', CommonModel::$del_normal)
-                ->update([
-                    'is_del' => CommonModel::$del_deleted,
-                    'updatetime' => $date,
                 ]);
+            }
 
-            if ($ins) Db::name('video_group_item')->insertAll($ins);
+            InventoryExchangeModel::where('id', $data['id'])
+                ->save(array_merge($data, ['updatetime' => $date, 'updaterid' => self::$uid, 'updater' => self::$uid]));
 
             Db::commit();
 
@@ -191,46 +160,24 @@ class InventoryExchangeLogic extends BaseLogic
 
     }
 
-    //兑换商品库存启禁用
-    public static function status(array $data = []): Json
+    //兑换商品库存变动记录
+    public static function log(array $data = []): Json
     {
-        $res = InventoryExchangeModel::where('id', $data['id'])
-            ->where('status', '<>', $data['status'])
-            ->save([
-                'status' => $data['status'],
-                'updatetime' => date('Y-m-d H:i:s'),
-            ]);
-
-        return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败');
-
-    }
+        $count = Db::name('inventory_exchange_log')
+            ->where('inventory_exchange_id', $data['id'])
+            ->count('id');
 
-    //删除兑换商品库存
-    public static function delete(int $id = 0): Json
-    {
-        $res = InventoryExchangeModel::where('id', $id)
-            ->where('is_del', CommonModel::$del_normal)
-            ->save([
-                'is_del' => CommonModel::$del_deleted,
-                'updatetime' => date('Y-m-d H:i:s'),
-            ]);
+        $res = Db::name('inventory_exchange_log')
+            ->field('id,before_inventory,after_inventory,good_id,good_code,good_name,source,creater,addtime')
+            ->where('inventory_exchange_id', $data['id'])
+            ->page($data['page'], $data['size'])
+            ->order('id', 'desc')
+            ->select()
+            ->toArray();
 
-        return $res ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败');
+        return $res ? json_show(CommonModel::$success, '获取兑换商品库存变动记录成功', ['count' => $count, 'list' => $res]) : json_show(CommonModel::$success, '获取兑换商品库存变动记录失败');
 
     }
 
-    //兑换商品库存置顶
-    public static function top(array $data = []): Json
-    {
-        $res = InventoryExchangeModel::where('id', $data['id'])
-            ->where('is_top', '<>', $data['is_top'])
-            ->save([
-                'is_top' => $data['is_top'],
-                'updatetime' => date('Y-m-d H:i:s'),
-            ]);
-
-        return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败');
-
-    }
 
 }

+ 24 - 18
app/admin/route/app.php

@@ -90,28 +90,34 @@ Route::rule('goodChange', 'admin/Good/status');//商品启禁用
 Route::rule('goodDelete', 'admin/Good/delete');//删除商品
 
 //【商品分组】
-Route::rule('goodGroupList', 'admin/goodGroup/list');//获取商品分组列表
-Route::rule('goodGroupAdd', 'admin/goodGroup/add');//添加商品分组
-Route::rule('goodGroupRead', 'admin/goodGroup/read');//获取商品分组详情
-Route::rule('goodGroupEdit', 'admin/goodGroup/edit');//编辑商品分组
-Route::rule('goodGroupChange', 'admin/goodGroup/status');//商品分组启禁用
-Route::rule('goodGroupDelete', 'admin/goodGroup/delete');//删除商品分组
-
+Route::rule('goodGroupList', 'admin/GoodGroup/list');//获取商品分组列表
+Route::rule('goodGroupAdd', 'admin/GoodGroup/add');//添加商品分组
+Route::rule('goodGroupRead', 'admin/GoodGroup/read');//获取商品分组详情
+Route::rule('goodGroupEdit', 'admin/GoodGroup/edit');//编辑商品分组
+Route::rule('goodGroupChange', 'admin/GoodGroup/status');//商品分组启禁用
+Route::rule('goodGroupDelete', 'admin/GoodGroup/delete');//删除商品分组
+
+//【兑换商品库存】
+Route::rule('inventoryExchangeList', 'admin/InventoryExchange/list');//获取兑换商品列表
+Route::rule('inventoryExchangeAdd', 'admin/InventoryExchange/add');//添加兑换商品库存
+Route::rule('inventoryExchangeRead', 'admin/InventoryExchange/read');//获取兑换商品库存详情
+Route::rule('inventoryExchangeEdit', 'admin/InventoryExchange/edit');//编辑兑换商品库存
+Route::rule('inventoryExchangeLog', 'admin/InventoryExchange/log');//兑换商品库存变动记录
 //【商城商品库存】
-Route::rule('InventoryShoppingList', 'admin/InventoryShopping/list');//获取商城商品库存列表
-Route::rule('InventoryShoppingAdd', 'admin/InventoryShopping/add');//添加商城商品库存
-Route::rule('InventoryShoppingRead', 'admin/InventoryShopping/read');//获取商城商品库存详情
-Route::rule('InventoryShoppingEdit', 'admin/InventoryShopping/edit');//编辑商城商品库存
-Route::rule('InventoryShoppingLog', 'admin/InventoryShopping/log');//商城商品库存变动记录
+Route::rule('inventoryShoppingList', 'admin/InventoryShopping/list');//获取商城商品库存列表
+Route::rule('inventoryShoppingAdd', 'admin/InventoryShopping/add');//添加商城商品库存
+Route::rule('inventoryShoppingRead', 'admin/InventoryShopping/read');//获取商城商品库存详情
+Route::rule('inventoryShoppingEdit', 'admin/InventoryShopping/edit');//编辑商城商品库存
+Route::rule('inventoryShoppingLog', 'admin/InventoryShopping/log');//商城商品库存变动记录
 
 
 //【账户】
-Route::rule('AccoountBatchAdd', 'admin/Account/batchAdd');//批量添加账户
-Route::rule('AccoountAdd', 'admin/Account/add');//添加
-Route::rule('AccoountList', 'admin/Account/list');//列表
-Route::rule('AccoountRead', 'admin/Account/read');//读取
-Route::rule('AccoountEdit', 'admin/Account/edit');//修改
-Route::rule('AccoountDelete', 'admin/Account/delete');//删除
+Route::rule('accoountBatchAdd', 'admin/Account/batchAdd');//批量添加账户
+Route::rule('accoountAdd', 'admin/Account/add');//添加
+Route::rule('accoountList', 'admin/Account/list');//列表
+Route::rule('accoountRead', 'admin/Account/read');//读取
+Route::rule('accoountEdit', 'admin/Account/edit');//修改
+Route::rule('accoountDelete', 'admin/Account/delete');//删除