public Result PostFlowCardLibrary([FromBody] FlowCardLibrary flowCardLibrary) { var cnt = ServerConfig.FlowCardDb.Query <int>("SELECT COUNT(1) FROM `flowcard_library` WHERE FlowCardName = @FlowCardName AND MarkedDelete = 0;", new { flowCardLibrary.FlowCardName }).FirstOrDefault(); if (cnt > 0) { return(Result.GenError <Result>(Error.FlowCardLibraryIsExist)); } cnt = ServerConfig.FlowCardDb.Query <int>("SELECT COUNT(1) FROM `production_library` WHERE `Id` = @ProductionProcessId AND MarkedDelete = 0;", new { flowCardLibrary.ProductionProcessId }).FirstOrDefault(); if (cnt == 0) { return(Result.GenError <Result>(Error.ProductionProcessLibraryNotExist)); } cnt = ServerConfig.FlowCardDb.Query <int>("SELECT COUNT(1) FROM `raw_materia` WHERE `Id` = @RawMateriaId AND MarkedDelete = 0;", new { flowCardLibrary.RawMateriaId }).FirstOrDefault(); if (cnt == 0) { return(Result.GenError <Result>(Error.RawMateriaNotExist)); } var productionProcessId = flowCardLibrary.ProductionProcessId; var processSteps = ServerConfig.FlowCardDb.Query <ProductionProcessStep>("SELECT * FROM `production_process_step` WHERE ProductionProcessId = @ProductionProcessId AND MarkedDelete = 0;", new { ProductionProcessId = productionProcessId }); var productionProcessSpecifications = ServerConfig.FlowCardDb.Query <ProductionSpecification>("SELECT * FROM `production_specification` WHERE ProductionProcessId = @ProductionProcessId AND MarkedDelete = 0;", new { ProductionProcessId = productionProcessId }); var createUserId = Request.GetIdentityInformation(); var time = DateTime.Now; flowCardLibrary.CreateUserId = createUserId; flowCardLibrary.MarkedDateTime = time; var index = ServerConfig.FlowCardDb.Query <int>( "INSERT INTO flowcard_library (`CreateUserId`, `MarkedDateTime`, `MarkedDelete`, `ModifyId`, `FlowCardName`, `ProductionProcessId`, `RawMateriaId`, `RawMaterialQuantity`, `Sender`, `InboundNum`, `Remarks`, `Priority`) " + "VALUES (@CreateUserId, @MarkedDateTime, @MarkedDelete, @ModifyId, @FlowCardName, @ProductionProcessId, @RawMateriaId, @RawMaterialQuantity, @Sender, @InboundNum, @Remarks, @Priority);SELECT LAST_INSERT_ID();", flowCardLibrary).FirstOrDefault(); flowCardLibrary.Id = index; foreach (var productionSpecification in productionProcessSpecifications) { flowCardLibrary.Specifications.Add(new FlowCardSpecification { CreateUserId = createUserId, MarkedDateTime = time, FlowCardId = flowCardLibrary.Id, SpecificationName = productionSpecification.SpecificationName, SpecificationValue = productionSpecification.SpecificationValue, }); } foreach (var processStep in processSteps) { flowCardLibrary.ProcessSteps.Add(new FlowCardProcessStepDetail { CreateUserId = createUserId, MarkedDateTime = time, FlowCardId = flowCardLibrary.Id, ProcessStepOrder = processStep.ProcessStepOrder, ProcessStepName = processStep.ProcessStepName, ProcessStepRequirements = processStep.ProcessStepRequirements, }); } ServerConfig.FlowCardDb.Execute( "INSERT INTO flowcard_specification (`CreateUserId`, `MarkedDateTime`, `MarkedDelete`, `ModifyId`, `FlowCardId`, `SpecificationName`, `SpecificationValue`) " + "VALUES (@CreateUserId, @MarkedDateTime, @MarkedDelete, @ModifyId, @FlowCardId, @SpecificationName, @SpecificationValue);", flowCardLibrary.Specifications.OrderBy(x => x.FlowCardId)); ServerConfig.FlowCardDb.Execute( "INSERT INTO flowcard_process_step (`CreateUserId`, `MarkedDateTime`, `MarkedDelete`, `ModifyId`, `FlowCardId`, `ProcessStepOrder`, `ProcessStepName`, `ProcessStepRequirements`) " + "VALUES (@CreateUserId, @MarkedDateTime, @MarkedDelete, @ModifyId, @FlowCardId, @ProcessStepOrder, @ProcessStepName, @ProcessStepRequirements);", flowCardLibrary.ProcessSteps.OrderBy(x => x.ProcessStepOrder)); return(Result.GenError <Result>(Error.Success)); }
public Result PutFlowCardLibrary([FromRoute] int id, [FromBody] FlowCardLibrary flowCardLibrary) { var cnt = ServerConfig.FlowCardDb.Query <int>("SELECT COUNT(1) FROM `flowcard_library` WHERE Id = @id AND MarkedDelete = 0;", new { id }).FirstOrDefault(); if (cnt == 0) { return(Result.GenError <Result>(Error.FlowCardLibraryNotExist)); } var createUserId = Request.GetIdentityInformation(); var time = DateTime.Now; flowCardLibrary.Id = id; flowCardLibrary.CreateUserId = createUserId; flowCardLibrary.MarkedDateTime = time; ServerConfig.FlowCardDb.Execute( "UPDATE flowcard_library SET `CreateUserId` = @CreateUserId, `MarkedDateTime` = @MarkedDateTime, `MarkedDelete` = @MarkedDelete, `ModifyId` = @ModifyId, `RawMaterialQuantity` = @RawMaterialQuantity, `Sender` = @Sender, `InboundNum` = @InboundNum, " + "`Remarks` = @Remarks, `Priority` = @Priority WHERE `Id` = @Id;", flowCardLibrary); var specifications = flowCardLibrary.Specifications; if (specifications.Any()) { foreach (var specification in specifications) { specification.FlowCardId = id; specification.CreateUserId = createUserId; specification.MarkedDateTime = time; } var exist = ServerConfig.FlowCardDb.Query <FlowCardSpecification>("SELECT * FROM `flowcard_specification` " + "WHERE MarkedDelete = 0 AND FlowCardId = @FlowCardId;", new { FlowCardId = id }); ServerConfig.FlowCardDb.Execute( "INSERT INTO flowcard_specification (`CreateUserId`, `MarkedDateTime`, `MarkedDelete`, `ModifyId`, `FlowCardId`, `SpecificationName`, `SpecificationValue`) " + "VALUES (@CreateUserId, @MarkedDateTime, @MarkedDelete, @ModifyId, @FlowCardId, @SpecificationName, @SpecificationValue);", specifications.Where(x => x.Id == 0)); var update = specifications.Where(x => x.Id != 0 && exist.Any(y => y.Id == x.Id && (y.SpecificationName != x.SpecificationName || y.SpecificationValue != x.SpecificationValue))).ToList(); update.AddRange(exist.Where(x => specifications.All(y => x.Id != y.Id)).Select(x => { x.MarkedDateTime = DateTime.Now; x.MarkedDelete = true; return(x); })); ServerConfig.FlowCardDb.Execute( "UPDATE flowcard_specification SET `CreateUserId` = @CreateUserId, `MarkedDateTime` = @MarkedDateTime, `MarkedDelete` = @MarkedDelete, " + "`ModifyId` = @ModifyId, `SpecificationName` = @SpecificationName, `SpecificationValue` = @SpecificationValue WHERE `Id` = @Id;", update); } var processSteps = flowCardLibrary.ProcessSteps; if (processSteps.Any()) { foreach (var processStep in processSteps) { processStep.FlowCardId = id; processStep.CreateUserId = createUserId; processStep.MarkedDateTime = time; } var exist = ServerConfig.FlowCardDb.Query <FlowCardProcessStepDetail>("SELECT * FROM `flowcard_process_step` " + "WHERE MarkedDelete = 0 AND FlowCardId = @FlowCardId;", new { FlowCardId = id }); ServerConfig.FlowCardDb.Execute( "INSERT INTO flowcard_process_step (`CreateUserId`, `MarkedDateTime`, `MarkedDelete`, `ModifyId`, `FlowCardId`, `ProcessStepOrder`, `ProcessStepName`, `ProcessStepRequirements`) " + "VALUES (@CreateUserId, @MarkedDateTime, @MarkedDelete, @ModifyId, @FlowCardId, @ProcessStepOrder, @ProcessStepName, @ProcessStepRequirements);", processSteps.Where(x => x.Id == 0).OrderBy(x => x.ProcessStepOrder)); var update = processSteps.Where(x => x.Id != 0 && exist.Any(y => y.Id == x.Id && (y.ProcessStepOrder != x.ProcessStepOrder || y.ProcessStepName != x.ProcessStepName || y.ProcessStepRequirements != x.ProcessStepRequirements))).ToList(); update.AddRange(exist.Where(x => processSteps.All(y => x.Id != y.Id)).Select(x => { x.MarkedDateTime = DateTime.Now; x.MarkedDelete = true; return(x); })); ServerConfig.FlowCardDb.Execute( "UPDATE flowcard_process_step SET `CreateUserId` = @CreateUserId, `MarkedDateTime` = @MarkedDateTime, `MarkedDelete` = @MarkedDelete, `ModifyId` = @ModifyId, " + "`ProcessStepOrder` = @ProcessStepOrder, `ProcessStepName` = @ProcessStepName, `ProcessStepRequirements` = @ProcessStepRequirements " + "WHERE `Id` = @Id;", update); } return(Result.GenError <Result>(Error.Success)); }