using WcsMain.ApiServe.Controllers.Dto.WcsDto.DB; using WcsMain.Common; using WcsMain.DataBase.TableEntity; using WcsMain.WcsAttribute.AutoFacAttribute; namespace WcsMain.DataBase.Dao; /// /// tbl_app_db 表的增删改查 /// [Component] public class AppDBDao { /// /// 插入数据 /// /// /// public int Insert(AppDB appDB) { try { int insertResult = CommonTool.DbServe.Insertable(appDB).ExecuteCommand(); return insertResult; } catch (Exception ex) { _ = ex; return 0; } } /// /// 更新数据,以主键为条件,主键 : DBName /// /// /// public int Update(AppDB appDB) { try { var sqlFuc = CommonTool.DbServe.Updateable(appDB).IgnoreColumns(ignoreAllNullColumns: true); return sqlFuc.ExecuteCommand(); } catch (Exception ex) { _ = ex; return 0; } } /// /// 删除数据,以主键为条件 /// /// /// public int Delete(AppDB appDB) { try { var sqlFuc = CommonTool.DbServe.Deleteable(appDB); return sqlFuc.ExecuteCommand(); } catch (Exception ex) { _ = ex; return 0; } } /// /// 查找数据 /// /// /// public List? Select(AppDB appDB) { try { var sqlFuc = CommonTool.DbServe.Queryable(); if (appDB.DBName != null) { sqlFuc = sqlFuc.Where(w => w.DBName == appDB.DBName); } if (appDB.DBAddress != null) { sqlFuc = sqlFuc.Where(w => w.DBAddress == appDB.DBAddress); } if (appDB.IsSystem != null) { sqlFuc = sqlFuc.Where(w => w.IsSystem == appDB.IsSystem); } if (appDB.Remark != null) { sqlFuc = sqlFuc.Where(w => w.Remark == appDB.Remark); } return sqlFuc.OrderBy(o => new { o.PlcId, o.DBName }).ToList(); } catch (Exception ex) { _ = ex; return default; } } /// /// 查找所有数据 /// /// public List? Select() => Select(new AppDB()); /// /// 查询所有DB地址,同时返回PLC名称 /// /// /// public List? QueryWithPlcName(AppDB appDB) { try { var sqlFuc = CommonTool.DbServe.Queryable() .LeftJoin((db, plc) => db.PlcId == plc.PLCId) .WhereIF(!string.IsNullOrEmpty(appDB.DBName), db => db.DBName!.Contains(appDB.DBName!)) .WhereIF(appDB.PlcId != default, db => db.PlcId == appDB.PlcId) .OrderBy(db => new { db.PlcId, db.DBName }) .Select((db, plc) => new GetDBWithPlcNameResponse { PlcId = db.PlcId, PlcName = plc.PLCName, DBName = db.DBName, DBAddress = db.DBAddress, IsSystem = db.IsSystem, Remark = db.Remark, }); return sqlFuc.ToList(); } catch (Exception ex) { _ = ex; return default; } } }