kreta/Kreta.DataAccessManual/AdatszotarDAL.cs
2024-03-13 00:33:46 +01:00

626 lines
26 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
using Kreta.Core.Logic;
using Kreta.DataAccess.Interfaces;
using Kreta.DataAccessManual.Interfaces;
using Kreta.DataAccessManual.Util;
using Kreta.Enums;
using Kreta.Framework;
using Kreta.Framework.Entities;
using Kreta.Framework.Util;
using SDA.DataProvider;
using SDA.Kreta.Entities;
namespace Kreta.DataAccessManual
{
internal class AdatszotarDAL : DataAccessBase, IAdatszotarDAL
{
public AdatszotarDAL(DalHandler handler, GridParameters parameters)
: base(handler, parameters)
{
}
public AdatszotarDAL(DalHandler handler) : base(handler)
{
}
public DataSet GetAdatszotar(GeneratedAdatszotarTipusEnum type, int tanevId)
{
using (var sdaCommand = new SDACommand())
{
sdaCommand.Connection = UserContext.Instance.SDAConnection;
sdaCommand.Transaction = UserContext.Instance.SDATransaction;
sdaCommand.CommandType = CommandType.StoredProcedure;
sdaCommand.CommandText = "uspGetAdatszotar";
sdaCommand.Parameters.Add("pTanevId", tanevId);
sdaCommand.Parameters.Add("pTypeId", (int)type);
var dataSet = new DataSet();
using (var sdaDataAdapter = new SDADataAdapter())
{
sdaDataAdapter.SelectCommand = sdaCommand;
sdaDataAdapter.Fill(dataSet);
}
SetBoolFields(dataSet.Tables[0], "Lathato,Protected,IsSorszamozando,IsTanorai,IsTanorankivuli,IsLeNemKotottMunkaido");
return dataSet;
}
}
public int GetAdatszotarMaxSorszam(GeneratedAdatszotarTipusEnum type, int tanevId, int intezmenyId)
{
using (var command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspGetAdatszotarMaxSorszam";
command.Parameters.Add("pType", (int)type);
command.Parameters.Add("pTanevId", tanevId);
command.Parameters.Add("pIntezmenyId", intezmenyId);
var ds = new DataSet();
using (var sdaDataAdapter = new SDADataAdapter())
{
sdaDataAdapter.SelectCommand = command;
sdaDataAdapter.Fill(ds);
}
string maxSorszamStr = string.Empty;
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
maxSorszamStr = ds.Tables[0].Rows[0]["MaxSorszam"].ToString();
}
return Convert.ToInt32(maxSorszamStr);
}
}
public void GenerateNaptariNapok(int? intezmenyId, int? tanevId)
{
using (SDACommand command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandText = "sp_GenerateNaptariNapok";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("INTEZMENYID", intezmenyId ?? -1);
command.Parameters.Add("TANEVID", tanevId ?? -1);
command.Parameters.Add("hetNapjaTipusTypeId", (int)GeneratedAdatszotarTipusEnum.HetNapjaTipus);
command.ExecuteNonQuery();
DalHelper.Commit();
}
}
public void Insert(IDictionaryItemBase dto)
{
var entity = dto as DictionaryItemBase;
entity.Insert(true);
FollowUp(entity);
DalHelper.Commit();
}
public void UpdateSorszam(int id, int sorszam, int userId, int intezmenyId, int tanevId)
{
using (SDACommand command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandText = "uspUpdateSorszam";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("pSorszam", sorszam);
command.Parameters.Add("pUserId", userId);
command.Parameters.Add("pId", id);
command.Parameters.Add("pIntezmenyId", intezmenyId);
command.Parameters.Add("pTanevId", tanevId);
command.ExecuteNonQuery();
DalHelper.Commit();
}
}
public void Delete(int felhasznaloId, int id, int typeId, int intezmenyId, int tanevId)
{
switch (typeId)
{
case (int)GeneratedAdatszotarTipusEnum.HetiRendTipus:
var orarendiOraDal = DalHelper.OrarendiOra();
if (orarendiOraDal.GetHetirendDictionaryItemIsUsed(id))
{
throw new Exception(StringResourcesUtil.GetString(3152));
}
break;
case (int)GeneratedAdatszotarTipusEnum.TargyKategoriaTipus:
var tantargyDal = DalHelper.Tantargy();
if (tantargyDal.TantargyKategoriaHasznalatbanVan(id))
{
throw new Exception(StringResourcesUtil.GetString(3152));
}
break;
}
var tipusType = Type.GetType(string.Format("SDA.Kreta.Entities.{0}, Kreta.DataAccessGenerated", ((GeneratedAdatszotarTipusEnum)typeId).ToString()));
DictionaryItemBase entity = (DictionaryItemBase)tipusType.GetMethod("GiveAnInstance", BindingFlags.Public | BindingFlags.Static).Invoke(null, null);
// TODO ERTEKELES REFAKT2 ERTEKELESMOD: Ha töröljük a T_TANARIATLAGSULY táblát, akkor törölni kell az egész ErtekelesMod-os if-et!
// https://jira.ekreta.hu/browse/KRETA2-12367
if (entity is ErtekelesMod)
{
string commandText = @"SELECT ID FROM T_TANARIATLAGSULY_OSSZES WHERE C_ERTEKELESMODID = :pErtekelesModId AND C_TANEVID = :pTanevId AND C_INTEZMENYID = :pIntezmenyId";
var commandParameters = new List<CommandParameter>
{
new CommandParameter("pErtekelesModId", id),
new CommandParameter("pTanevId", tanevId),
new CommandParameter("pIntezmenyId", intezmenyId)
};
DataTable dataTable = GetData(commandText, commandParameters).Tables[0];
foreach (DataRow dataRow in dataTable.Rows)
{
var tanariAtlagSulyEntity = TanariAtlagSuly.GiveAnInstance();
tanariAtlagSulyEntity.LoadByID(Convert.ToInt32(dataRow["ID"]));
tanariAtlagSulyEntity.Delete();
DalHelper.Commit();
}
}
using (SDACommand command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandText = "uspDeleteDictionaryItemBase";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("pId", id);
command.Parameters.Add("pIntezmenyId", intezmenyId);
command.Parameters.Add("pTanevId", tanevId);
command.Parameters.Add("pFelhasznaloId", felhasznaloId);
command.ExecuteNonQuery();
DalHelper.Commit();
typeId.RemoveFromCache(tanevId);
}
}
public bool ModifyItemActivity(List<int> adatszotarIds, bool activ, int tanevId, out int szotartipusId)
{
szotartipusId = 0;
foreach (var id in adatszotarIds)
{
var oDictionaryItemBase = Get(id) as DictionaryItemBase;
szotartipusId = oDictionaryItemBase.DictionaryTypeId;
if (!oDictionaryItemBase.Protected)
{
oDictionaryItemBase.Visible = activ;
if (oDictionaryItemBase.DictionaryTypeId == (int)GeneratedAdatszotarTipusEnum.HetiRendTipus)
{
var dal = DalHelper.OrarendiOra();
if (dal.GetHetirendDictionaryItemIsUsed(oDictionaryItemBase.ID))
{
throw new Exception(StringResourcesUtil.GetString(3152));
}
}
oDictionaryItemBase.Update();
szotartipusId.RemoveFromCache(tanevId);
FollowUp(oDictionaryItemBase);
}
else
{
return false;
}
}
DalHelper.Commit();
return true;
}
public DataSet GetAdatszotarById(int id, int intezmenyId, int tanevId)
{
using (var command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspGetAdatszotarById";
command.Parameters.Add("pId", id);
command.Parameters.Add("pIntezmenyId", intezmenyId);
command.Parameters.Add("pTanevId", tanevId);
var dataSet = new DataSet();
using (var sdaDataAdapter = new SDADataAdapter())
{
sdaDataAdapter.SelectCommand = command;
sdaDataAdapter.Fill(dataSet);
}
return dataSet;
}
}
public void Update(int id, string megnevezes, string megnevezes1, string megnevezes2, string megnevezes3, string megnevezes4, bool? lathato, bool isProtected, string color, int userId, int intezmenyId, int tanevId)
{
using (SDACommand command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspUpdateDictionaryItem";
command.Parameters.Add("pMegnevezes", megnevezes);
command.Parameters.Add("pMegnevezes1", megnevezes1 ?? "");
command.Parameters.Add("pMegnevezes2", megnevezes2 ?? "");
command.Parameters.Add("pMegnevezes3", megnevezes3 ?? "");
command.Parameters.Add("pMegnevezes4", megnevezes4 ?? "");
command.Parameters.Add("pLathato", lathato == true ? "T" : "F");
command.Parameters.Add("pIsProtected", isProtected ? "T" : "F");
command.Parameters.Add("pColor", color ?? "");
command.Parameters.Add("pUserId", userId);
command.Parameters.Add("pId", id);
command.Parameters.Add("pIntezmenyId", intezmenyId);
command.Parameters.Add("pTanevId", tanevId);
command.ExecuteNonQuery();
DalHelper.Commit();
}
}
public void UpdateNapTipus(int id, bool isSorszamozando, bool isTanorai, bool isTanorankivuli, bool isLeNemKotottMunkaido, int intezmenyId, int tanevId, int userId)
{
using (SDACommand command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspUpdateNapTipus";
command.Parameters.Add("pIsSorszamozando", isSorszamozando ? "T" : "F");
command.Parameters.Add("pIsTanorai", isTanorai ? "T" : "F");
command.Parameters.Add("pIsTanorankivuli", isTanorankivuli ? "T" : "F");
command.Parameters.Add("pIsLeNemKotottMunkaido", isLeNemKotottMunkaido ? "T" : "F");
command.Parameters.Add("pId", id);
command.Parameters.Add("pIntezmenyId", intezmenyId);
command.Parameters.Add("pTanevId", tanevId);
command.Parameters.Add("pUserId", userId);
command.ExecuteNonQuery();
DalHelper.Commit();
}
}
public void UpdateSorolasOkaTipus(int id, bool isBizonyitvanybanMegjelenik, bool isNaplobanMegjelenik, bool isTorzslaponMegjelenik, int intezmenyId, int tanevId, int felhasznaloId)
{
using (SDACommand command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspUpdateSorolasOkaTipus";
command.Parameters.Add("pId", id);
command.Parameters.Add("pIntezmenyId", intezmenyId);
command.Parameters.Add("pTanevId", tanevId);
command.Parameters.Add("pFelhasznaloId", felhasznaloId);
command.Parameters.Add("pIsBizonyitvanybanMegjelenik", isBizonyitvanybanMegjelenik ? "T" : "F");
command.Parameters.Add("pIsNaplobanMegjelenik", isNaplobanMegjelenik ? "T" : "F");
command.Parameters.Add("pIsTorzslaponMegjelenik", isTorzslaponMegjelenik ? "T" : "F");
command.ExecuteNonQuery();
DalHelper.Commit();
}
}
public void UpdateRendszerbeallitasok(int intezmenyId, int tanevId, RendszerBeallitasTipusEnum beallitasTipus, GeneratedAdatszotarTipusEnum dictionaryType)
{
using (SDACommand command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspUpdateRendszerbeallitasokMultiSelectionList";
command.Parameters.Add("pIntezmenyId", intezmenyId);
command.Parameters.Add("pTanevId", tanevId);
command.Parameters.Add("pBeallitasTipusId", (int)beallitasTipus);
command.Parameters.Add("pDictionaryTypeId", (int)dictionaryType);
command.ExecuteNonQuery();
DalHelper.Commit();
}
}
/// INFO @DevKornel: Mobil használja
public DataSet GetAdatszotarLathatoElemek(GeneratedAdatszotarTipusEnum adatszotarTipus, int tanevId, List<int> removeIdList = null)
{
List<CommandParameter> parameters = new List<CommandParameter>
{
new CommandParameter("pAdatszotarTipusId", (int)adatszotarTipus),
new CommandParameter("pTanevId", tanevId)
};
var commandText = @"
SELECT
dib.ID AS ID
,dib.C_ORDER AS Sorszam
,dib.C_NAME AS Megnevezes
,dib.C_NAME_1 AS Megnevezes1
,dib.C_NAME_2 AS Megnevezes2
,dib.C_NAME_3 AS Megnevezes3
,dib.C_NAME_4 AS Megnevezes4
,dib.C_VISIBLE AS Lathato
,dib.C_PROTECTED AS Protected
,dib.C_TYPE AS Type
,dib.C_DICTIONARYTYPEID AS TypeId
,dib.C_COLOR AS Color
FROM T_DICTIONARYITEMBASE_OSSZES dib
WHERE dib.TOROLT = 'F'
AND dib.C_VISIBLE = 'T'
AND dib.C_DICTIONARYTYPEID = :pAdatszotarTipusId
AND dib.C_TANEVID = :pTanevId";
if (removeIdList != null)
{
string removeIds = SqlLogic.ParseListToParameter(removeIdList).ToString();
commandText += $@"
AND dib.ID IN({removeIds})
";
}
commandText += @"
ORDER BY dib.C_ORDER, dib.C_NAME
";
DataSet ds = GetData(commandText, parameters);
return ds;
}
public DataSet GetAdatszotarLathatoElemekNyelv(GeneratedAdatszotarTipusEnum type, int tanevId, int intezmenyId, int nyelvId)
{
using (SDACommand command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspGetAdatszotarLathatoElemekNyelv";
command.Parameters.Add("pType", (int)type);
command.Parameters.Add("pTanevId", tanevId);
command.Parameters.Add("pIntezmenyId", intezmenyId);
command.Parameters.Add("pNyelvId", (int)nyelvId);
var dataSet = new DataSet();
using (var sdaDataAdapter = new SDADataAdapter())
{
sdaDataAdapter.SelectCommand = command;
sdaDataAdapter.Fill(dataSet);
}
return dataSet;
}
}
public void UpdateCsoportTipus(int id, bool isTanoraiCelu, int oraPerc, int intezmenyId, int tanevId, int userId)
{
using (var command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspUpdateCsoportTipus";
command.Parameters.Add("pId", id);
command.Parameters.Add("pIsTanoraiCelu", isTanoraiCelu ? "T" : "F");
command.Parameters.Add("pOraPerc", oraPerc);
command.Parameters.Add("pIntezmenyId", intezmenyId);
command.Parameters.Add("pTanevId", tanevId);
command.Parameters.Add("pUserId", userId);
command.ExecuteNonQuery();
}
DalHelper.Commit();
}
public void FollowUpDictionaryItemBase(int intezmenyId, int aktTanevId, int kovetkezoTanevId, int id)
{
using (SDACommand command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandText = "uspFollowUpDictionaryItemBase";
command.Parameters.Add("IntezmenyId", intezmenyId);
command.Parameters.Add("pCurrentTanevId", aktTanevId);
command.Parameters.Add("kovetkezoTanevId", kovetkezoTanevId);
command.Parameters.Add("pItemId", id);
command.CommandType = CommandType.StoredProcedure;
command.ExecuteNonQuery();
DalHelper.Commit();
}
}
public Dictionary<int, List<EntityConnectionModel>> GetEntitiesConnections(int entityId, string entitasNev, int tanevId)
{
XDocument xmlDoc = new XDocument(new XElement("EntitasNevek"));
xmlDoc.Root.Add(new XElement("Entitas", entitasNev));
var EntitasNevek = xmlDoc.ToString();
xmlDoc = new XDocument(new XElement("Entitasok"));
xmlDoc.Root.Add(new XElement("EntitasId", entityId));
var EntitasIdk = xmlDoc.ToString();
var result = new Dictionary<int, List<EntityConnectionModel>>();
using (SDACommand command = UserContext.Instance.SDAConnection.CreateCommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandText = @"sp_GetEntitasAktivKapcsolatai";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("EntitasIDk", EntitasIdk);
command.Parameters.Add("EntitasNevek", EntitasNevek);
command.Parameters.Add("tanevId", tanevId);
using (SDADataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var entitasId = reader.GetInt32(0);
var targetTableName = reader.GetString(1);
var targetColumnName = reader.GetString(2);
var sorokSzama = reader.GetInt32(3);
if (!result.ContainsKey(entitasId))
{
result.Add(entitasId, new List<EntityConnectionModel>());
}
result[entitasId].Add(new EntityConnectionModel
{
TargetTableName = targetTableName,
TargetColumnName = targetColumnName,
RowsCount = sorokSzama
});
}
}
}
return result;
}
public bool IsProtected(int id, int tanevId)
{
var entity = DictionaryItemBase.LoadWithFilter($" AND T_DICTIONARYITEMBASE_OSSZES.ID = {id} AND T_DICTIONARYITEMBASE_OSSZES.C_TANEVID = {tanevId} ").SingleOrDefault();
return entity.Protected;
}
public List<ICsoportTipus> GetCsoportTipusEntityList(int intezmenyId, int tanevId)
{
var commandParameters = new Dictionary<string, object>()
{
{nameof(intezmenyId), intezmenyId },
{nameof(tanevId), tanevId }
};
var filter = new StringBuilder();
filter.Append($" AND C_VISIBLE = 'T'");
filter.Append($" AND T_CSOPORTTIPUS_OSSZES.TOROLT = 'F'");
filter.Append($" AND C_INTEZMENYID = @{nameof(intezmenyId)}");
filter.Append($" AND C_TANEVID = @{nameof(tanevId)}");
filter.Append($" AND C_ALINTEZMENYID = @{nameof(intezmenyId)}");
filter.Append($" AND C_ALTANEVID = @{nameof(tanevId)}");
return CsoportTipus.LoadWithFilter(filter.ToString(), commandParameters).OrderBy(x => x.Order).ToList<ICsoportTipus>();
}
public IDictionaryItemBase Get(int id)
{
var entity = DictionaryItemBase.GiveAnInstance();
entity.LoadByID(id);
return entity;
}
public IDictionaryItemBase Get(string type)
{
if (!string.IsNullOrWhiteSpace(type))
{
var tipusType = Type.GetType(string.Format("SDA.Kreta.Entities.{0}, Kreta.DataAccessGenerated", type));
DictionaryItemBase oDictionaryItemBase = (DictionaryItemBase)tipusType.GetMethod("GiveAnInstance").Invoke(null, null);
return oDictionaryItemBase;
}
return DictionaryItemBase.GiveAnInstance();
}
public void UpdateErtekelesModTipus(int intezmenyId, int tanevId, int userId, int id, bool isBold, int suly)
{
using (var command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspUpdateErtekelesModTipus";
command.Parameters.Add("pIntezmenyId", intezmenyId);
command.Parameters.Add("pTanevId", tanevId);
command.Parameters.Add("pUserId", userId);
command.Parameters.Add("pId", id);
command.Parameters.Add("pIsBold", isBold ? "T" : "F");
command.Parameters.Add("pSuly", suly);
command.ExecuteNonQuery();
DalHelper.Commit();
}
}
public List<(int id, string megnevezes)> GetEditableAdatszotarTipusok(int tanevId)
{
using (var command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspGetEditableAdatszotarTipusok";
command.Parameters.Add("pTanevId", tanevId);
var dataSet = new DataSet();
using (var sdaDataAdapter = new SDADataAdapter())
{
sdaDataAdapter.SelectCommand = command;
sdaDataAdapter.Fill(dataSet);
}
var result = new List<(int id, string megnevezes)>();
foreach (DataRow row in dataSet.Tables[0].Rows)
{
var id = row.Field<int>("Id");
var name = row.Field<string>("Name");
result.Add((id, name));
}
return result;
}
}
public int? GetEgyediKovTanevSzotarelemIdByKovTanevIdAndAktTanevSzotarelemId(int tanevId, int id)
{
using (var command = new SDACommand())
{
command.Connection = UserContext.Instance.SDAConnection;
command.Transaction = UserContext.Instance.SDATransaction;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspGetEgyediKovTanevSzotarelemIdByKovTanevIdAndAktTanevSzotarelemId";
command.Parameters.Add("pId", id);
command.Parameters.Add("pTanevId", tanevId);
return (int?)command.ExecuteScalar();
}
}
}
}