123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- 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;
- use think\facade\Db;
- use think\response\Json;
- class InventoryExchangeLogic extends BaseLogic
- {
- //获取兑换商品库存列表
- public static function list(array $data = []): Json
- {
- $db = InventoryExchangeModel::alias('a')
- ->leftJoin('account b', 'b.id=a.account_id AND b.is_del=' . CommonModel::$del_normal);
- 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,a.account_username,b.status,a.account_name,b.mobile,a.inventory,a.updatetime')
- ->page($data['page'], $data['size'])
- ->order(['a.id' => 'desc'])
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取兑换商品库存列表成功', ['count' => $count, 'list' => $list]);
- }
- //添加兑换商品库存
- public static function add(array $data = []): Json
- {
- Db::startTrans();
- try {
- $rs = InventoryExchangeModel::field('id')
- ->where([
- 'account_id' => $data['account_id'],
- 'good_id' => $data['good_id']
- ])
- ->findOrEmpty()
- ->isEmpty();
- if (!$rs) throw new Exception('该账号下的兑换商品库存已存在,不能重复添加');
- $account = AccountModel::field('id,username,name')
- ->where(['id' => $data['account_id'], 'is_del' => CommonModel::$del_normal])
- ->findOrEmpty();
- 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');
- $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('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();
- return json_show(CommonModel::$success, '添加兑换商品库存成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(CommonModel::$error_param, '添加兑换商品库存失败,' . $exception->getMessage());
- }
- }
- //获取兑换商品库存详情
- public static function read(int $id = 0): Json
- {
- $res = InventoryExchangeModel::field(true)
- ->where(['id' => $id])
- ->findOrEmpty()
- ->toArray();
- if (empty($res)) throw new ValidateException('该兑换商品库存为空');
- return json_show(CommonModel::$success, '获取兑换商品库存详情成功', $res);
- }
- //编辑兑换商品库存
- public static function edit(array $data = []): Json
- {
- Db::startTrans();
- try {
- $rs = InventoryExchangeModel::field('id,inventory,good_id,good_code,good_name')
- ->lock(true)
- ->where('id', $data['id'])
- ->findOrEmpty();
- if ($rs->isEmpty()) throw new Exception('该兑换商品库存不存在');
- $date = date('Y-m-d H:i:s');
- 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,
- ]);
- }
- InventoryExchangeModel::where('id', $data['id'])
- ->save(array_merge($data, ['updatetime' => $date, 'updaterid' => self::$uid, 'updater' => self::$uname]));
- Db::commit();
- return json_show(CommonModel::$success, '编辑兑换商品库存成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(CommonModel::$error_param, '编辑兑换商品库存失败,' . $exception->getMessage());
- }
- }
- //兑换商品库存变动记录
- public static function log(array $data = []): Json
- {
- $count = Db::name('inventory_exchange_log')
- ->where('inventory_exchange_id', $data['id'])
- ->count('id');
- $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, '获取兑换商品库存变动记录成功', ['count' => $count, 'list' => $res]) : json_show(CommonModel::$success, '获取兑换商品库存变动记录失败');
- }
- }
|