using Gs.Toolbox; using Gs.Toolbox.ApiCore.Abstract.Mvc; using Gs.Toolbox.ApiCore.Common.Mvc; using Gs.Toolbox.ApiCore.Group; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using System.Data; using System.Data.SqlClient; using System.Dynamic; using System.Security.Cryptography.X509Certificates; using System.Text; using static Gs.Toolbox.UtilityHelper; namespace GS.QC.Service { [ApiGroup(ApiGroupNames.QC)] public class FqcItemsManager : IRomteService { private readonly IHttpContextAccessor _http; private readonly string _userCode, _userGuid, _orgFids; public FqcItemsManager(IHttpContextAccessor httpContextAccessor) { _http = httpContextAccessor; (_userCode, _userGuid, _orgFids) = GetUserGuidAndOrgGuid(_http); } /// /// 读取列表,支持分页 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto> GetListPage([FromBody] dynamic model) { int currentPage = model.currentPage; int everyPageSize = model.everyPageSize; string sortName = model.sortName; string keyWhere = model.keyWhere; SqlParameter[] parameters = { new("@inCurrentPage", currentPage), new("@inEveryPageSize", everyPageSize), new("@inSortName", sortName), new("@inSortOrder", ""), new("@inQueryWhere", keyWhere), new("@inFid", ""), new("@inP1", ""), new("@inP2", ""), new("@inP3", ""), new("@inP4", "") }; var dset = new DataSet(); var _pglist = new PageList { total = 0, everyPageSize = 0, pages = 0, list = new List() }; try { dset = DbHelperSQL.RunProcedure("prc_fqc_items_lst", parameters, "0"); if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0) //有数据 { var intTotal = int.Parse(dset.Tables[1].Rows[0]["intTotal"].ToString()); var pages = intTotal % everyPageSize != 0 ? intTotal / everyPageSize + 1 : intTotal / everyPageSize; _pglist.total = intTotal; _pglist.everyPageSize = everyPageSize; _pglist.pages = pages; var _dy = dset.Tables[0].TableToDynamicList(); _pglist.list = _dy; } } catch (Exception ex) { LogHelper.Debug(ToString(), ex.Message); return ReturnDto>.QuickReturn(_pglist, ReturnCode.Exception, ex.Message); } return ReturnDto>.QuickReturn(_pglist, ReturnCode.Success, "读取成功"); } /// /// 读取单个实体 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto GetModel([FromBody] dynamic model) { string guid = model.guid.ToString(); dynamic m = new ExpandoObject(); m.list = new List(); m.list2 = new List(); try { // 查询主表数据 string sqlMain = $"SELECT A.*,B.Name as OrgName FROM MES_FQC_ITEMS A LEFT JOIN SYS_ORGANIZATION b ON A.OrgId = b.FID WHERE a.GUID='{guid}'"; var dsMain = DbHelperSQL.Query(sqlMain); if (dsMain != null && dsMain.Tables.Count > 0 && dsMain.Tables[0].Rows.Count > 0) { var dr = dsMain.Tables[0].Rows[0]; m = dr.RowToDynamic(); } // 查询子表1数据 string sqlDetail1 = $"SELECT * FROM MES_FQC_ITEM_DETAIL1 WHERE PID='{guid}' ORDER BY Seq"; var dsDetail1 = DbHelperSQL.Query(sqlDetail1); if (dsDetail1 != null && dsDetail1.Tables.Count > 0) { var _tb = dsDetail1.Tables[0].TableToDynamicList(); m.list = _tb; } // 查询子表2数据 string sqlDetail2 = $"SELECT * FROM MES_FQC_ITEM_DETAIL2 WHERE PID='{guid}' ORDER BY Seq"; var dsDetail2 = DbHelperSQL.Query(sqlDetail2); if (dsDetail2 != null && dsDetail2.Tables.Count > 0) { var _tb2 = dsDetail2.Tables[0].TableToDynamicList(); m.list2 = _tb2; } } catch (Exception ex) { LogHelper.Debug(ToString(), ex.Message); } if (m != null) return ReturnDto.QuickReturn(m, ReturnCode.Success, "读取成功!"); return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!"); } /// /// 增加或编辑实体 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto EditModel([FromBody] dynamic model) { Guid? guid = model.guid; //主键 string fqcNo = model.fqcNo; // 检验单号 string workShop = model.workShop; // 生产车间 string itemName = model.itemName; // 产品名称 string itemModel = model.itemModel; // 规格型号 string brand = model.brand; // 商标 // 处理生产日期,允许为空 DateTime? productionDate = null; if (model.productionDate != null && !string.IsNullOrEmpty(model.productionDate.ToString())) { if (DateTime.TryParse(model.productionDate.ToString(), out DateTime parsedDate)) { productionDate = parsedDate; } } string acRe_A = model.acRe_A; // AC/Re A类 string acRe_B = model.acRe_B; // AC/Re B类 string acRe_C = model.acRe_C; // AC/Re C类 string sampleMethod = model.sampleMethod; // 抽样 string sampleSize1 = model.sampleSize1; // 匀速抽样速 string sampleSize2 = model.sampleSize2; // 随机抽样数 string sampleSize3 = model.sampleSize3; // 样本数 string temperature = model.temperature; // 室温 string voltage = model.voltage; // 电压 string classes = model.classes; // 班次 string lineNo = model.lineNo; // 线号 string batch = model.batch; // 批量 string checkResult = model.checkResult; // 检验结果 string processResults = model.processResults; // 处理结果 // 添加检验人、审核人、审批人及对应的日期 string jyUser = model.jyUser; // 检验人 string checkUser = model.checkUser; // 审核人 string spUser = model.spUser; // 审批人 // 处理检验日期,允许为空 DateTime? jyDate = null; if (model.jyDate != null && !string.IsNullOrEmpty(model.jyDate.ToString())) { if (DateTime.TryParse(model.jyDate.ToString(), out DateTime parsedJyDate)) { jyDate = parsedJyDate; } } // 处理审核日期,允许为空 DateTime? checkDate = null; if (model.checkDate != null && !string.IsNullOrEmpty(model.checkDate.ToString())) { if (DateTime.TryParse(model.checkDate.ToString(), out DateTime parsedCheckDate)) { checkDate = parsedCheckDate; } } // 处理审批日期,允许为空 DateTime? spDate = null; if (model.spDate != null && !string.IsNullOrEmpty(model.spDate.ToString())) { if (DateTime.TryParse(model.spDate.ToString(), out DateTime parsedSpDate)) { spDate = parsedSpDate; } } // 构建子表1数据字符串 var _sb1 = new StringBuilder(); var _split = "|"; foreach (var m in model.list1) { string _guid = m.Guid.ToString(); var _line = m.RPB001 + _split + m.RPB003 + _split + m.RPB004 + _split + m.RPB005 + _split + m.RPB006 + _split + m.RPB007 + _split + m.RPB008 + _split + m.RPB009 + _split + m.RPB010 + _split + m.RPB011 + _split + m.RPB012 + _split + m.RPB013 + _split + m.RPB014 + _split + m.RPB015 + _split + m.RPB016 + _split + m.RPB017 + _split + m.RPB018 + _split + m.RPB019 + _split + m.Remark + _split + (CheckGuid(_guid) ? _guid : Guid.Empty.ToString()); if (_sb1.Length > 0) _sb1.Append("~"); _sb1.Append(_line); } // 构建子表2数据字符串 var _sb2 = new StringBuilder(); foreach (var m in model.list2) { string _guid = m.Guid.ToString(); var _line = m.Seq + _split + m.Description + _split + m.AClass + _split + m.BClass + _split + m.CClass + _split + m.Point + _split + m.Remark + _split + (CheckGuid(_guid) ? _guid : Guid.Empty.ToString()); if (_sb2.Length > 0) _sb2.Append("~"); _sb2.Append(_line); } dynamic mObj = new ExpandoObject(); mObj.outMsg = ""; mObj.outSum = -1; mObj.outGuid = ""; mObj.outNo = ""; using (var conn = new SqlConnection(DbHelperSQL.strConn)) { using (var cmd = new SqlCommand("[prc_fqc_items_edt]", conn)) { try { conn.Open(); cmd.CommandType = CommandType.StoredProcedure; SqlParameter[] parameters = { new("@outMsg", SqlDbType.NVarChar, 300), new("@outSum", SqlDbType.Int), new("@outGuid", SqlDbType.UniqueIdentifier), new("@outNo", SqlDbType.NVarChar, 300), new("@inOrderGuid", CheckGuid(guid) ? guid : DBNull.Value), new("@inFQCNo", fqcNo), new("@inWorkShop", workShop), new("@inItemName", itemName), new("@inItemModel", itemModel), new("@inBrand", brand), new("@inProductionDate", productionDate.HasValue ? (object)productionDate.Value : DBNull.Value), new("@inAcRe_A", acRe_A), new("@inAcRe_B", acRe_B), new("@inAcRe_C", acRe_C), new("@inSampleMethod", sampleMethod), new("@inSampleSize1", sampleSize1), new("@inSampleSize2", sampleSize2), new("@inSampleSize3", sampleSize3), new("@inTemperature", temperature), new("@inVoltage", voltage), new("@inClasses", classes), new("@inLineNo", lineNo), new("@inBatch", batch), new("@inCheckResult", checkResult), new("@inProcessResults", processResults), // 添加检验人、审核人、审批人及对应的日期参数 new("@inJyUser", jyUser), new("@inCheckUser", checkUser), new("@inSpUser", spUser), new("@inJyDate", jyDate.HasValue ? (object)jyDate.Value : DBNull.Value), new("@inCheckDate", checkDate.HasValue ? (object)checkDate.Value : DBNull.Value), new("@inSpDate", spDate.HasValue ? (object)spDate.Value : DBNull.Value), new("@inEdtUserGuid", _userGuid), new("@inDetail1List", _sb1.ToString()), new("@inDetail2List", _sb2.ToString()) }; parameters[0].Direction = ParameterDirection.Output; parameters[1].Direction = ParameterDirection.Output; parameters[2].Direction = ParameterDirection.Output; parameters[3].Direction = ParameterDirection.Output; foreach (var parameter in parameters) cmd.Parameters.Add(parameter); cmd.ExecuteNonQuery(); mObj.outMsg = parameters[0].Value.ToString(); mObj.outSum = int.Parse(parameters[1].Value.ToString()); mObj.outGuid = parameters[2].Value.ToString(); mObj.outNo = parameters[3].Value.ToString(); } catch (Exception ex) { LogHelper.Debug(ToString(), "prc_fqc_items_edt error:" + ex.Message); mObj.outMsg = ex.Message; mObj.outSum = -1; } finally { conn.Close(); } } } if (mObj.outSum <= 0) return ReturnDto.QuickReturn(mObj, ReturnCode.Exception, mObj.outMsg); return ReturnDto.QuickReturn(mObj, ReturnCode.Success, mObj.outMsg); } /// /// 删除主表及明细 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto DeleteModel([FromBody] dynamic model) { int? rtnInt = (int)ReturnCode.Default; Guid? guid = model.guid; var _outMsg = ""; var _outSum = -1; try { if (CheckGuid(guid)) { string sql = $@" DELETE FROM MES_FQC_ITEM_DETAIL1 WHERE PID='{guid}'; DELETE FROM MES_FQC_ITEM_DETAIL2 WHERE PID='{guid}'; DELETE FROM MES_FQC_ITEMS WHERE GUID='{guid}';"; _outSum = DbHelperSQL.ExecuteSql(sql); _outMsg = _outSum > 0 ? "删除成功!" : "未删除任何数据"; } else { _outMsg = "主键不能为空!"; _outSum = -1; } } catch (Exception ex) { LogHelper.Debug(ToString(), "DeleteModel error:" + ex.Message); _outMsg = ex.Message; _outSum = -1; } if (_outSum <= 0) return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, _outMsg); return ReturnDto.QuickReturn(rtnInt, ReturnCode.Success, _outMsg); } /// /// 成品检验报表 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto> GetCPMX([FromBody] dynamic model) { int currentPage = model.currentPage; int everyPageSize = model.everyPageSize; string sortName = model.sortName; string keyWhere = model.keyWhere; SqlParameter[] parameters = { new("@inCurrentPage", currentPage), new("@inEveryPageSize", everyPageSize), new("@inSortName", sortName), new("@inSortOrder", ""), new("@inQueryWhere", keyWhere), }; var dset = new DataSet(); var _pglist = new PageList { total = 0, everyPageSize = 0, pages = 0, list = new List() }; try { dset = DbHelperSQL.RunProcedure("report_cprkjd", parameters, "0"); if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0) //有数据 { var intTotal = int.Parse(dset.Tables[1].Rows[0]["intTotal"].ToString()); var pages = intTotal % everyPageSize != 0 ? intTotal / everyPageSize + 1 : intTotal / everyPageSize; _pglist.total = intTotal; _pglist.everyPageSize = everyPageSize; _pglist.pages = pages; var _dy = dset.Tables[0].TableToDynamicList(); _pglist.list = _dy; } } catch (Exception ex) { LogHelper.Debug(ToString(), ex.Message); } return ReturnDto>.QuickReturn(_pglist, ReturnCode.Success, "读取成功"); } [RequestMethod(RequestMethods.POST)] public ReturnDto GetCPMXDetail([FromBody] dynamic model) { dynamic m = new ExpandoObject(); m.list = new List(); string daa001 = model.daa001; SqlParameter[] parameters = { new("@daa001", daa001) }; try { var dsMain = DbHelperSQL.RunProcedure("report_cprkjdDetail", parameters, "0"); if (dsMain != null && dsMain.Tables.Count > 0 && dsMain.Tables[0].Rows.Count > 0) { m.list = dsMain.Tables[0].TableToDynamicList(); } } catch (Exception ex) { LogHelper.Debug(ToString(), ex.Message); } if (m != null) return ReturnDto.QuickReturn(m, ReturnCode.Success, "读取成功!"); return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!"); } } }