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); if ($data['good_name'] != '') $db = $db->whereLike('a.good_name', '%' . $data['good_name'] . '%'); if (($data['inventory_start'] != '') && ($data['inventory_end'] != '')) $db = $db->whereBetween('a.inventory', [$data['inventory_start'], $data['inventory_end']]); $count = $db->count('a.id'); $list = $db->field('a.*,b.good_cover_img,b.moq,b.step,b.type,b.price,b.good_remark,c.unit') ->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 { $good_list = GoodModel::where(['is_del' => CommonModel::$del_normal, 'type' => GoodModel::$type_shopping]) ->whereIn('id', array_column($data['list'], 'good_id')) ->column('id,good_code,good_name', 'id'); if (empty($good_list)) throw new Exception('该商品不存在'); $rs = InventoryShoppingModel::field('id,good_code') ->whereIn('good_id', array_column($good_list, 'id')) ->findOrEmpty() ->toArray(); if (!empty($rs)) throw new Exception($rs['good_code'] . '该商城商品库存已存在,不能重复添加'); $date = date('Y-m-d H:i:s'); $inventory = array_column($data['list'], 'inventory', 'good_id'); foreach ($good_list as $good) { $inventory_shopping_id = Db::name('inventory_shopping') ->insertGetId([ 'good_id' => $good['id'], 'good_code' => $good['good_code'], 'good_name' => $good['good_name'], 'inventory' => $inventory[$good['id']] ?? 0, 'addtime' => $date, 'updatetime' => $date, ]); Db::name('inventory_shopping_log')->insert([ 'inventory_shopping_id' => $inventory_shopping_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 = InventoryShoppingModel::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 = InventoryShoppingModel::field(true) ->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_shopping_log')->insert([ 'inventory_shopping_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, ]); InventoryShoppingModel::where('id', $data['id'])->save(['updatetime' => $date, '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_shopping_log') ->where('inventory_shopping_id', $data['id']) ->count('id'); $res = Db::name('inventory_shopping_log') ->field(true) ->where('inventory_shopping_id', $data['id']) ->order('id', 'desc') ->page($data['page'], $data['size']) ->select() ->toArray(); return json_show(CommonModel::$success, '获取商城商品库存变动记录', ['count' => $count, 'list' => $res]); } }