123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace app\admin\logic;
- use app\model\AccountModel;
- use app\model\CommonModel;
- use app\model\GoodModel;
- use app\model\GroupModel;
- 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)
- ->leftJoin('good c', 'c.id=a.good_id AND c.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'] . '%');
- if ($data['good_name'] != '') $db->whereLike('c.good_name', '%' . $data['good_name'] . '%');
- if ($data['good_code'] != '') $db->whereLike('c.good_code', '%' . $data['good_code'] . '%');
- $count = $db->count('a.id');
- $list = $db->field('a.id,a.account_username,b.status,a.account_name,b.mobile,a.inventory,a.updatetime,c.good_cover_img,c.good_code,c.good_name,c.moq,c.step,c.good_remark,c.unit_id,c.type,c.price')
- ->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 {
- $group = GroupModel::field('id,company_id,card_id')
- ->where(['id' => $data['group_id'], 'is_del' => CommonModel::$del_normal])
- ->findOrEmpty()
- ->toArray();
- if (empty($group)) throw new Exception('该分组不存在');
- $good_list = GoodModel::where(['is_del' => CommonModel::$del_normal, 'type' => GoodModel::$type_exchange])
- ->whereIn('id', array_column($data['list'], 'good_id'))
- ->column('id,good_code,good_name', 'id');
- if (empty($good_list)) throw new Exception('该商品不存在');
- $account_list = AccountModel::field('id,username,name')
- ->where(['is_del' => CommonModel::$del_normal, 'company_id' => $group['company_id'], 'card_id' => $group['card_id']])
- ->select()
- ->toArray();
- $temp = InventoryExchangeModel::field('id,account_username,good_name')
- ->whereIn('account_id', array_column($account_list, 'id'))
- ->whereIn('good_id', array_column($good_list, 'id'))
- ->findOrEmpty()
- ->toArray();
- if (!empty($temp)) throw new Exception('【' . $temp['account_username'] . '】账户的【' . $temp['good_name'] . '】商品库存记录已存在');
- $date = date('Y-m-d H:i:s');
- $inventory = array_column($data['list'], 'inventory', 'good_id');
- foreach ($account_list as $account) {
- foreach ($good_list as $good) {
- $inventory_exchange_id = Db::name('inventory_exchange')->insertGetId([
- 'account_id' => $account['id'],
- 'account_username' => $account['username'],
- 'account_name' => $account['name'],
- 'good_id' => $good['id'],
- 'good_code' => $good['good_code'],
- 'good_name' => $good['good_name'],
- 'inventory' => $inventory[$good['id']] ?? 0,
- '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' => $inventory[$good['id']] ?? 0,
- 'good_id' => $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::alias('a')
- ->field('a.*,b.good_cover_img,b.moq,b.step,b.type,b.price,b.good_banner_img,b.good_img,b.good_param,b.good_remark,c.unit,b.status as good_status')
- ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
- ->leftJoin('unit c','c.id=b.unit_id AND c.is_del='.CommonModel::$del_normal)
- ->withAttr('good_banner_img', function ($val) {
- return explode(',', $val);
- })->withAttr('good_img', function ($val) {
- return explode(',', $val);
- })->withAttr('good_param', function ($val) {
- return json_decode($val);
- })
- ->where(['a.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');
- $inventory = bcadd($rs->inventory, bcmul($data['flag'], $data['number']));
- if ($inventory < 0) throw new Exception('最终库存数少于0');
- Db::name('inventory_exchange_log')->insert([
- 'inventory_exchange_id' => $data['id'],
- 'before_inventory' => $rs->inventory,
- 'after_inventory' => $inventory,
- 'flag' => $data['flag'],
- '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(['updatetime' => $date, 'updaterid' => self::$uid, 'updater' => self::$uname, 'inventory' => $inventory]);
- 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,flag,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, '获取兑换商品库存变动记录失败');
- }
- }
|