using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Runtime.CompilerServices ;
using Mesnac.Action.ChemicalWeighing.LjMaterial ;
namespace Mesnac.Action.ChemicalWeighing.LjMixFormula
{
public class MixDb : DBHelp
{
public static IList < MyNameValue > GetActionByDeviceUnit ( int unitId )
{
IList < MyNameValue > myNameValues = new List < MyNameValue > ( ) ;
string sql = $"select Id,ActionName from ActionCode where DeviceUnitId={unitId}" ;
var dataTable = GetTable ( sql ) ;
foreach ( DataRow dataTableRow in dataTable . Rows )
{
myNameValues . Add ( new MyNameValue ( )
{
Id = Convert . ToInt32 ( dataTableRow [ 0 ] ) ,
Name = dataTableRow [ 1 ] . ToString ( )
} ) ;
}
return myNameValues ;
}
public static DataTable GetDetailByMId ( string id )
{
string sql = $"select Id, MixId, ActionId, ActionName, SecondTime, Temperature, Speed, Weight, Difference, Remark from lj_mix_detail where MixId='{id}'" ;
return GetTable ( sql ) ;
}
/ /
public static IList < MyNameValue > GetDeviceUnit ( )
{
IList < MyNameValue > myNameValues = new List < MyNameValue > ( ) ;
string sql = $"select Id,Name from DeviceUnit" ;
var dataTable = GetTable ( sql ) ;
foreach ( DataRow dataTableRow in dataTable . Rows )
{
myNameValues . Add ( new MyNameValue ( )
{
Id = Convert . ToInt32 ( dataTableRow [ 0 ] ) ,
Name = dataTableRow [ 1 ] . ToString ( )
} ) ;
}
return myNameValues ;
}
public static void SoftDel ( string id )
{
string sql = $"update lj_mix Set IsEnable=0 where id='{id}'" ;
ExecuteNonQuery ( sql ) ;
}
public static LjMixView GetById ( string id )
{
string sql =
$"select Id, Name, CreateTime, UpdateTime, Remark, DeviceUnitId, DeviceUnitName from lj_mix where Id='{id}'" ;
var dataTable = GetTable ( sql ) ;
LjMixView view = new LjMixView ( ) ;
view . Data = new List < LjMixDetailView > ( ) ;
if ( dataTable . Rows . Count = = 1 )
{
var datarow = dataTable . Rows [ 0 ] ;
view . Id = id ;
view . DeviceUnitId = Convert . ToInt32 ( datarow [ "DeviceUnitId" ] ) ;
view . DeviceUnitName = datarow [ "DeviceUnitName" ] . ToString ( ) ;
view . CreateTime = Convert . ToDateTime ( datarow [ "CreateTime" ] ) ;
view . UpdateTime = Convert . ToDateTime ( datarow [ "UpdateTime" ] ) ;
view . Name = datarow [ "Name" ] . ToString ( ) ;
}
sql = $"select Id, MixId, ActionId, ActionName, SecondTime, Temperature, Speed, Weight, Difference, Remark from lj_mix_detail where MixId='{id}'" ;
dataTable = GetTable ( sql ) ;
foreach ( DataRow dataTableRow in dataTable . Rows )
{
LjMixDetailView v = new LjMixDetailView ( )
{
Id = Convert . ToInt32 ( dataTableRow [ "Id" ] ) ,
MixId = id ,
ActionId = Convert . ToInt32 ( dataTableRow [ "ActionId" ] ) ,
ActionName = dataTableRow [ "ActionName" ] . ToString ( ) ,
SecondTime = Convert . ToInt32 ( dataTableRow [ "SecondTime" ] ) ,
Temperature = Convert . ToSingle ( dataTableRow [ "Temperature" ] ) ,
Speed = Convert . ToSingle ( dataTableRow [ "Speed" ] ) ,
Weight = Convert . ToSingle ( dataTableRow [ "Weight" ] ) ,
Difference = Convert . ToSingle ( dataTableRow [ "Difference" ] ) ,
Remark = dataTableRow [ "Remark" ] . ToString ( )
} ;
view . Data . Add ( v ) ;
}
return view ;
}
/// <summary>
/// 获取返回混合配方的下拉框
/// </summary>
/// <param name="deviceUnitId"></param>
/// <returns></returns>
public static List < MyNameValueStr > GetMyNameValueStrs ( int deviceUnitId )
{
List < MyNameValueStr > myNameValues = new List < MyNameValueStr > ( ) ;
string sql = $"select Id,Name,DeviceUnitName from lj_mix where DeviceUnitId={deviceUnitId} and IsEnable=1" ;
var dataTable = GetTable ( sql ) ;
foreach ( DataRow dataTableRow in dataTable . Rows )
{
myNameValues . Add ( new MyNameValueStr ( )
{
Id = dataTableRow [ 0 ] . ToString ( ) ,
Name = dataTableRow [ 1 ] . ToString ( )
} ) ;
}
return myNameValues ;
}
public static void Save ( LjMixView view )
{
string mixId = view . Id ;
string sql = $"select count(id) as a from lj_mix where Id='{mixId}'" ;
var dataTable = GetTable ( sql ) ;
var count = Convert . ToInt32 ( dataTable . Rows [ 0 ] [ 0 ] ) ;
if ( count = = 0 )
{
//添加
sql = "insert into lj_mix (Id, Name, CreateTime, UpdateTime, Remark, DeviceUnitId, DeviceUnitName,IsEnable) " +
$" values ('{mixId}','{view.Name}',getdate(),getdate(),'',{view.DeviceUnitId},'{view.DeviceUnitName}',1);" ;
ExecuteNonQuery ( sql ) ;
}
else
{
sql = $"update lj_mix set Name='{view.Name}',UpdateTime=getdate(),DeviceUnitId={view.DeviceUnitId},DeviceUnitName='{view.DeviceUnitName}' where id='{view.Id}'" ;
ExecuteNonQuery ( sql ) ;
}
sql = $"delete from lj_mix_detail where MixId='{mixId}'" ;
ExecuteNonQuery ( sql ) ;
foreach ( var ljMixDetailView in view . Data )
{
sql =
"insert into lj_mix_detail (MixId, ActionId, ActionName, SecondTime, Temperature, Speed, Weight, Difference, Remark)"
+ $" values ('{mixId}',{ljMixDetailView.ActionId},'{ljMixDetailView.ActionName}',{ljMixDetailView.SecondTime},{ljMixDetailView.Temperature},{ljMixDetailView.Speed},{ljMixDetailView.Weight},{ljMixDetailView.Difference},'');" ;
ExecuteNonQuery ( sql ) ;
}
}
}
}