using CommonFunc ;
using CommonFunc.Tools ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using XGL.Models.Model.FoamingMac ;
namespace XGL.Data.DBService
{
public class FormingMachineService
{
/// <summary>
/// 获取成型机在产物料信息
/// </summary>
/// <param name="bdt"></param>
/// <param name="edt"></param>
/// <returns></returns>
public DataTable GetFormingMachineInfo ( string devicecode , string workDate )
{
string sql = $ @ "SELECT DISTINCT
ord . workorder_id ,
ord . workorder_code ,
res . OrderCode ,
ord . product_name ,
product_code ,
isnull ( car_num , 0 ) AS car_num ,
isnull ( plan_time , ' 1900 - 01 - 01 08 : 00 : 00 ' ) AS plan_time ,
ord . status ,
ord . route_code ,
ord . attr1 ,
ord . shift_id , totalCount , shift . Shift_Desc
FROM
pro_order_workorder ord
LEFT JOIN mes_material_transfer_result res ON res . OrderCode = ord . workorder_id
LEFT JOIN pro_wet_material_plan_detail wet ON wet . workorder_id = res . OrderCode
LEFT JOIN pro_rfid_process_detail rfid ON rfid . workorder_code = ord . workorder_code
left join ( select count ( * ) as totalCount , ordercode from mes_material_transfer_result
where CONVERT ( VARCHAR ( 10 ) , update_time , 120 ) = CONVERT ( VARCHAR ( 10 ) , ' { workDate } ' , 120 ) and equipmentCode = ' { devicecode } ' group by OrderCode ) tt
on ( tt . ordercode = wet . workorder_id )
AND rfid . rfid = res . rfid
LEFT JOIN base_shifts_t shift on ord . shift_id = shift . Shift_Id
WHERE
CONVERT ( VARCHAR ( 10 ) , wet . plan_time , 120 ) = CONVERT ( VARCHAR ( 10 ) , ' { workDate } ' , 120 )
And ord . prod_line_code like ' % { devicecode } % ' and totalCount is not null
ORDER BY
( isnull ( plan_time , ' 1900 - 01 - 01 08 : 00 : 00 ' ) ) DESC ";
DataSet dtset = Utils . netClientDBHelper . getDataSet ( sql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 & & dtset . Tables [ 0 ] . Rows . Count > 0 )
{
return dtset . Tables [ 0 ] ;
}
return null ;
}
public DataTable GetShiftList ( )
{
string sql = $@"select shift_id,shift_code,shift_desc,Shift_Start_Time,Shift_End_Time from base_shifts_t where del_flag ='0' " ;
DataSet dtset = Utils . netClientDBHelper . getDataSet ( sql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 & & dtset . Tables [ 0 ] . Rows . Count > 0 )
{
return dtset . Tables [ 0 ] ;
}
return null ;
}
public DataTable GetTeamList ( )
{
string sql = $@"SELECT team_id,team_code,team_desc FROM [dbo].[base_team_t] where del_flag ='0' " ;
DataSet dtset = Utils . netClientDBHelper . getDataSet ( sql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 & & dtset . Tables [ 0 ] . Rows . Count > 0 )
{
return dtset . Tables [ 0 ] ;
}
return null ;
}
/// <summary>
/// 获取成型机状态
/// </summary>
/// <param name="bdt"></param>
/// <param name="edt"></param>
/// <returns></returns>
public string getnumber ( string order , string devicecode , string shetl )
{
string sql = $@"SELECT COUNT(*) FROM mes_material_transfer_result WHERE equipmentCode='{order}' and work_date='{devicecode}' and shift_id='{shetl}' and rfid_status='1'" ;
DataSet dtset = Utils . netClientDBHelper . getDataSet ( sql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 )
{
// 获取查询结果中的计数值并转换为字符串
string countAsString = dtset . Tables [ 0 ] . Rows [ 0 ] [ 0 ] . ToString ( ) ;
return countAsString ;
}
return null ;
}
/// <summary>
/// 获取成型机状态
/// </summary>
/// <param name="bdt"></param>
/// <param name="edt"></param>
/// <returns></returns>
public DataTable GetFormingMachineState ( string devicecode )
{
string sql = $@"select status from base_equipment where equipment_code = '{devicecode}'; " ;
DataSet dtset = Utils . netClientDBHelper . getDataSet ( sql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 & & dtset . Tables [ 0 ] . Rows . Count > 0 )
{
return dtset . Tables [ 0 ] ;
}
return null ;
}
/// <summary>
/// 查询出入记录
/// </summary>
/// <param name="bdt"></param>
/// <param name="edt"></param>
/// <returns></returns>
public DataTable GetInOutHistoryList ( string workOrderCode , string devicecode , string workDate )
{
string sql = $ @ "SELECT DISTINCT ord.workorder_id,ord.workorder_code,res.OrderCode,ord.product_name,product_code,isnull(car_num,0) as car_num,
isnull ( [ begin ] , ' 1900 - 01 - 01 08 : 00 : 00 ' ) as [ begin ] , ord . status , ord . route_code , ord . attr1 , work_batch_code , res . taskcode , res . rfid , plan_time , res . update_time
FROM [ dbo ] . [ mes_material_transfer_result ] res
LEFT JOIN pro_order_workorder ord on res . OrderCode = ord . workorder_id
left JOIN pro_wet_material_plan_detail wet on wet . workorder_id = ord . workorder_id
LEFT JOIN pro_rfid_process_detail rfid on rfid . workorder_code = ord . workorder_code and rfid . rfid = res . rfid
where equipmentCode = ' { devicecode } ' and work_date = ' { workDate } ' and ord . workorder_code = ' { workOrderCode } ' and res . OrderCode is not null ORDER BY update_time desc ; ";
DataSet dtset = Utils . netClientDBHelper . getDataSet ( sql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 & & dtset . Tables [ 0 ] . Rows . Count > 0 )
{
return dtset . Tables [ 0 ] ;
}
return null ;
}
/// <summary>
/// 查询完成记录
/// </summary>
/// <param name="bdt"></param>
/// <param name="edt"></param>
/// <returns></returns>
public DataTable GetCompleteHistoryList ( string devicecode )
{
//taskcode=1,当前机台完成, ord.status = 'w3'当前工单完成
// string sql = $@"SELECT workorder_code,product_name,product_code,isnull(car_num,0) as car_num,isnull([begin],'1900-00-00') as [begin],beout,ord.status,product_date,ord.create_time FROM [dbo].[mes_material_transfer_result] res
//LEFT JOIN pro_order_workorder ord on res.OrderCode = ord.workorder_id
//where res.taskcode = '1' and equipmentCode = '{devicecode}'; ";
string sql = $ @ "SELECT ord.workorder_id,ord.workorder_code,res.OrderCode,ord.product_name,product_code,isnull(car_num,0) as car_num,
isnull ( [ begin ] , ' 1900 - 01 - 01 08 : 00 : 00 ' ) as [ begin ] , ord . status , ord . route_code , ord . attr1 , work_batch_code , res . taskcode , res . rfid , plan_time , res . update_time
FROM [ dbo ] . [ mes_material_transfer_result ] res
LEFT JOIN pro_order_workorder ord on res . OrderCode = ord . workorder_id
left JOIN pro_wet_material_plan_detail wet on wet . workorder_id = ord . workorder_id
LEFT JOIN pro_rfid_process_detail rfid on rfid . workorder_code = ord . workorder_code and rfid . rfid = res . rfid
where res . taskcode = '0' and equipmentCode = ' C6 ' and res . OrderCode is not null ORDER BY plan_time ; ";
DataSet dtset = Utils . netClientDBHelper . getDataSet ( sql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 & & dtset . Tables [ 0 ] . Rows . Count > 0 )
{
return dtset . Tables [ 0 ] ;
}
return null ;
}
/// <summary>
/// 获取工单当前状态,弃用
/// </summary>
/// <param name="bdt"></param>
/// <param name="edt"></param>
/// <returns></returns>
public string GetWorkOrderStatus ( string orderCode )
{
string sql = $@"select status from pro_order_workorder where workorder_code = '{orderCode}'" ;
var obj = Utils . netClientDBHelper . getScalar ( sql , null ) ;
string issucc = obj = = null ? "" : obj . ToString ( ) ;
return issucc ;
}
/// <summary>
/// 更新工单状态
/// </summary>
/// <param name="bdt"></param>
/// <param name="edt"></param>
/// <returns></returns>
public bool UpdateWorkOrder ( string orderCode , string status , string remark , string type , string deviceCode )
{
List < string > sqlList = new List < string > ( ) ;
string logSql = "" ;
if ( status . Equals ( "w2" ) )
{
logSql = $ @ "INSERT INTO [dbo].[mes_operation_log]
( [ id ] , [ user_id ] , [ user_name ] , [ operation_type ] , [ operation_time ] ,
[operation_desc] , [ old_value ] , [ new_value ] , [ device_code ] , [ remark ] , [ order_code ] )
VALUES
( ' { CommonFunc . Common . GetUUID ( ) } ' , ' { LoginUser . UserId } ' , ' { LoginUser . UserName } ' , ' { type } ' , GetDate ( ) ,
' 工 单 状 态 更 新 - 暂 停 ' , ' w2 ' , ' { status } ' , ' { deviceCode } ',' { remark } ',' { orderCode } ' ) ; ";
}
else
{
logSql = $ @ "INSERT INTO [dbo].[mes_operation_log]
( [ id ] , [ user_id ] , [ user_name ] , [ operation_type ] , [ operation_time ] ,
[operation_desc] , [ old_value ] , [ new_value ] , [ device_code ] , [ remark ] , [ order_code ] )
VALUES
( ' { CommonFunc . Common . GetUUID ( ) } ' , ' { LoginUser . UserId } ' , ' { LoginUser . UserName } ' , ' { type } ' , GetDate ( ) ,
' 工 单 状 态 更 新 - 恢 复 ' , ' w4 ' , ' { status } ' , ' { deviceCode } ',' { remark } ',' { orderCode } ' ) ; ";
}
string sql = $@"update pro_order_workorder set status = '{status}' where workorder_code = '{orderCode}'" ;
sqlList . Add ( sql ) ;
sqlList . Add ( logSql ) ;
bool issucc = Utils . netClientDBHelper . executeBatchSql ( sqlList ) ;
return issucc ;
}
/// <summary>
/// 更新工单
/// </summary>
/// <param name="bdt"></param>
/// <param name="edt"></param>
/// <returns></returns>
public bool InsertData ( FoamingMacModel formingModel )
{
string sql = $ @ "INSERT INTO [mes_material_transfer_result] (
[id] ,
[begin] ,
[beout] ,
[equipmentCode] ,
[OrderCode] ,
[sku] ,
[planID] ,
[rfid] ,
[now_process_id] ,
[status] ,
[create_time] ,
[rfid_status] ,
[taskcode] ,
[state] ,
[update_time]
)
VALUES
(
' { CommonFunc . Common . GetUUID ( ) } ' ,
' GetDate ( ) ' ,
' GetDate ( ) ' ,
' { Utils . GetAppSetting ( "DeviceCode" ) } ' ,
' { formingModel . workorder_id } ' ,
' { formingModel . product_code } ' ,
' ' ,
' { formingModel . rfid } ' ,
' ' ,
'4' ,
' GetDate ( ) ' ,
'1' ,
'0' ,
'1' ,
' GetDate ( ) '
) ; ";
int issucc = Utils . netClientDBHelper . executeUpdate ( sql ) ;
return issucc > 0 ? true : false ;
}
/// <summary>
/// 报工
/// </summary>
/// <param name="bdt"></param>
/// <param name="edt"></param>
/// <returns></returns>
public bool ExecuteReportWork ( DataRowView selectRow , string workCount , string reportCode , string workTime , string userCount , string costCenter , string batchCode , string isEndReport , string deviceCode )
{
string getReportCodeSql = $@"SELECT equipment_code,equipment_name FROM [dbo].[base_equipment] WHERE equipment_code='{deviceCode}'" ;
string lineName = "" ;
DataSet dtset = Utils . netClientDBHelper . getDataSet ( getReportCodeSql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 & & dtset . Tables [ 0 ] . Rows . Count > 0 )
{
lineName = dtset . Tables [ 0 ] . Rows [ 0 ] [ "equipment_name" ] . ToString ( ) ;
}
List < string > sqlList = new List < string > ( ) ;
string sql = $ @ "INSERT INTO [dbo].[mes_report_work]
( [ id ] , [ report_type ] , [ report_code ] , [ workorder_code ] , [ product_code ] , [ product_name ] ,
[spec] , [ unit ] , [ quantity ] , [ quantity_feedback ] , [ quantity_qualified ] , [ quantity_unqualified ] ,
[user_name] , [ nick_name ] , [ feedback_channel ] , [ feedback_time ] , [ record_user ] , [ status ] ,
[remark] , [ work_time ] , [ machine_code ] , [ machine_name ] , [ team_code ] , [ shift_code ] ,
[attr1] , [ attr2 ] , [ attr3 ] , [ attr4 ] , [ create_by ] , [ create_time ] ,
[update_time] , [ update_by ] , [ upload_time ] , [ upload_status ] , [ upload_msg ] , [ prod_type ] ,
[use_man] , [ work_center ] , [ del_flag ] , [ batch ] , [ end_report ] )
VALUES
( ' { Common . GetUUID ( ) } ' , ' SELF ' , ' { reportCode } ' , ' { selectRow [ "workorder_code" ] } ' , ' { selectRow [ "product_code" ] } ' , ' { selectRow [ "product_name" ] } ' ,
' { selectRow [ "product_spc" ] } ' , ' { selectRow [ "unit" ] } ' , { workCount } , { workCount } , 0 , 0 ,
' { LoginUser . UserCode } ' , ' { LoginUser . UserName } ' , ' PC ' , ' { DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) } ' , ' { LoginUser . UserName } ' , ' PREPARE ' ,
'无' , ' { workTime } ' , ' { deviceCode } ' , ' { lineName } ' , N ' { LoginUser . TeamCode } ' , ' { selectRow [ "shift_id" ] } ' ,
NULL , NULL , NULL , NULL , ' { LoginUser . UserName } ' , ' { DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) } ' ,
' { DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) } ' , ' { LoginUser . UserName } ' , GetDate ( ) , '0' , NULL , ' prod ' ,
{ userCount } , ' { costCenter } ',' 0 ',' { batchCode } ',' { isEndReport } ' ) ; ";
//赵啸林说不用上位机更新报工标识
//string updateOrderSql = $@"update pro_order_workorder set status = 'w3', update_by = '{LoginUser.UserName}',update_time = GETDATE() where workorder_id = '{selectRow["workorder_id"]}' ";
sqlList . Add ( sql ) ;
//sqlList.Add(updateOrderSql);
bool issucc = Utils . netClientDBHelper . executeBatchSql ( sqlList ) ;
return issucc ; //> 0 ? true : false;
}
/// <summary>
/// 插入消耗表
/// </summary>
/// <param name="qty"></param>
/// <param name="remark"></param>
/// <param name="selectedRow"></param>
/// <param name="newReportCode"></param>
/// <param name="siteCode"></param>
public bool InsertConsumeInfo ( string qty , string remark , DataRowView selectedRow , string newReportCode , string siteCode )
{
string sql = $ @ " INSERT INTO [dbo].[mes_report_work_consume]
( [ record_id ] , [ workorder_code ] , [ material_code ] , [ material_name ] , [ material_spc ] ,
[quantity] , [ unit ] , [ remark ] , [ attr1 ] , [ attr2 ] ,
[attr3] , [ attr4 ] , [ create_by ] , [ create_time ] , [ update_by ] ,
[update_time] , [ factory_code ] , [ report_code ] , [ del_flag ] )
VALUES
( ' { Common . GetUUID ( ) } ' , ' { selectedRow [ "workorder_code" ] } ' , ' { selectedRow [ "product_code" ] } ' , ' { selectedRow [ "product_name" ] } ' , ' { selectedRow [ "product_spc" ] } ' ,
' { qty } ',' { selectedRow [ "unit" ] } ' , ' { remark } ' , NULL , NULL ,
NULL , NULL , ' { LoginUser . UserName } ' , GetDate ( ) , ' { LoginUser . UserName } ' ,
GetDate ( ) , ' { siteCode } ' , ' { newReportCode } ' , '0' ) ; ";
int ret = Utils . netClientDBHelper . executeUpdate ( sql ) ;
return ret > 0 ? true : false ;
}
/// <summary>
/// 根据设备编码获取当前机台最新的吊满的RFID信息
/// </summary>
/// <param name="deviceCode"></param>
/// <returns></returns>
public DataTable GetRfidInfo ( string deviceCode )
{
string sql = $ @ "select top 2 rfid.workorder_code, rfid, machine_code,order_num, rfid.create_time, rfid.update_by, rfid.update_time, work_batch_code, bind_status ,workorder.product_code
from pro_rfid_process_detail rfid
LEFT JOIN pro_order_workorder workorder on rfid . workorder_code = workorder . workorder_code
where machine_code = ' X1 ' and bind_status = '0' order by update_time DESC ";
DataSet dtset = Utils . netClientDBHelper . getDataSet ( sql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 & & dtset . Tables [ 0 ] . Rows . Count > 0 )
{
return dtset . Tables [ 0 ] ;
}
return null ;
}
public DataTable GetNewRFID ( )
{
string sql = $@"select max(rfid) as rfid from mes_rfidandsfc_bind where CONVERT(VARCHAR(10), create_time , 120) = CONVERT(VARCHAR(10),Getdate(), 120)" ;
DataSet dtset = Utils . netClientDBHelper . getDataSet ( sql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 & & dtset . Tables [ 0 ] . Rows . Count > 0 )
{
return dtset . Tables [ 0 ] ;
}
return null ;
}
public DataTable GetReportCode ( )
{
string getReportCodeSql = $@"select max(report_code) as report_code from mes_report_work where CONVERT(VARCHAR(10), create_time , 120) = CONVERT(VARCHAR(10),Getdate(), 120)" ;
DataSet dtset = Utils . netClientDBHelper . getDataSet ( getReportCodeSql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 & & dtset . Tables [ 0 ] . Rows . Count > 0 )
{
return dtset . Tables [ 0 ] ;
}
return null ;
}
public string InterfaceUrl ( string v )
{
string getReportCodeSql = $@"SELECT url FROM [dbo].[mes_interface_url] where url_type = 'reportWork' and del_flag = '0'" ;
DataSet dtset = Utils . netClientDBHelper . getDataSet ( getReportCodeSql ) ;
if ( dtset ! = null & & dtset . Tables . Count > 0 & & dtset . Tables [ 0 ] . Rows . Count > 0 )
{
return dtset . Tables [ 0 ] . Rows [ 0 ] [ 0 ] . ToString ( ) ;
}
return "" ;
}
}
}