public Result PostProductionPlan([FromBody] OpProductPlan productionPlan) { if (productionPlan.Plan.IsNullOrEmpty()) { return(Result.GenError <Result>(Error.ProductionPlanNotEmpty)); } var cnt = ServerConfig.ApiDb.Query <int>("SELECT COUNT(1) FROM `production_plan` WHERE Plan = @Plan AND MarkedDelete = 0;", new { productionPlan.Plan }).FirstOrDefault(); if (cnt > 0) { return(Result.GenError <Result>(Error.ProductionPlanIsExist)); } IEnumerable <int> planBill = null; if (productionPlan.Bill != null && productionPlan.Bill.Any()) { planBill = productionPlan.Bill.GroupBy(x => x.BillId).Select(y => y.Key); cnt = ServerConfig.ApiDb.Query <int>("SELECT COUNT(1) FROM `material_bill` WHERE Id IN @ids AND MarkedDelete = 0;", new { ids = planBill }).FirstOrDefault(); if (cnt != planBill.Count()) { return(Result.GenError <Result>(Error.MaterialBillNotExist)); } } var createUserId = Request.GetIdentityInformation(); var markedDateTime = DateTime.Now; productionPlan.CreateUserId = createUserId; productionPlan.MarkedDateTime = markedDateTime; var id = ServerConfig.ApiDb.Query <int>( "INSERT INTO production_plan (`CreateUserId`, `MarkedDateTime`, `Plan`, `Remark`) VALUES (@CreateUserId, @MarkedDateTime, @Plan, @Remark);SELECT LAST_INSERT_ID();", productionPlan).FirstOrDefault(); if (planBill != null) { var pBill = new List <ProductPlanBill>(); foreach (var billId in planBill) { var bill = productionPlan.Bill.First(x => x.BillId == billId); bill.PlannedConsumption = productionPlan.Bill.Where(x => x.BillId == billId).Sum(y => y.PlannedConsumption); bill.PlanId = id; bill.CreateUserId = createUserId; bill.MarkedDateTime = markedDateTime; pBill.Add(bill); } ServerConfig.ApiDb.Execute( "INSERT INTO production_plan_bill (`CreateUserId`, `MarkedDateTime`, `PlanId`, `BillId`, `PlannedConsumption`) " + "VALUES (@CreateUserId, @MarkedDateTime, @PlanId, @BillId, @PlannedConsumption);", pBill); } return(Result.GenError <Result>(Error.Success)); }
public Result PutProductionPlan([FromRoute] int id, [FromBody] OpProductPlan productionPlan) { var cnt = ServerConfig.ApiDb.Query <int>("SELECT COUNT(1) FROM `production_plan` WHERE Id = @id AND `MarkedDelete` = 0;", new { id }).FirstOrDefault(); if (cnt == 0) { return(Result.GenError <Result>(Error.ProductionPlanNotExist)); } var createUserId = Request.GetIdentityInformation(); var markedDateTime = DateTime.Now; productionPlan.Id = id; productionPlan.MarkedDateTime = markedDateTime; if (productionPlan.Bill == null || !productionPlan.Bill.Any()) { ServerConfig.ApiDb.Execute( "UPDATE `production_plan_bill` SET `MarkedDateTime`= @MarkedDateTime, `MarkedDelete`= @MarkedDelete WHERE `PlanId` = @PlanId;", new { MarkedDateTime = DateTime.Now, MarkedDelete = true, PlanId = id }); } else { var planBill = ServerConfig.ApiDb.Query <ProductPlanBill>("SELECT * FROM `production_plan_bill` WHERE `PlanId` = @PlanId AND MarkedDelete = 0;", new { PlanId = id }); productionPlan.Bill = productionPlan.Bill.OrderByDescending(x => x.Id); var plBill = new Dictionary <int, ProductPlanBill>(); foreach (var bill in productionPlan.Bill) { if (!plBill.ContainsKey(bill.BillId)) { if (bill.Id == 0) { bill.CreateUserId = createUserId; bill.MarkedDateTime = markedDateTime; bill.PlanId = id; } if (planBill != null && planBill.Any()) { bill.Extra = planBill.FirstOrDefault(x => x.BillId == bill.BillId)?.Extra ?? false; } plBill.Add(bill.BillId, bill); } else { plBill[bill.BillId].MarkedDateTime = markedDateTime; plBill[bill.BillId].PlannedConsumption += bill.PlannedConsumption; } } //productionPlan.Bill = productionPlan.Bill.Where(x => !x.Extra); var pBill = plBill.Values.Where(z => z.BillId != 0).GroupBy(x => x.BillId).Select(y => y.Key); cnt = ServerConfig.ApiDb.Query <int>("SELECT COUNT(1) FROM `material_bill` WHERE Id IN @ids;", new { ids = pBill }).FirstOrDefault(); if (cnt != pBill.Count()) { return(Result.GenError <Result>(Error.MaterialBillNotExist)); } var updateBill = new List <ProductPlanBill>(); #region 更新 updateBill.AddRange(plBill.Values.Where(x => planBill.Any(y => y.Id == x.Id))); #endregion #region 除 var deleteBill = planBill.Where(x => plBill.Values.All(y => y.Id != x.Id)); if (deleteBill.Any(x => x.ActualConsumption > 0)) { return(Result.GenError <Result>(Error.ProductionPlanBillConsumed)); } updateBill.AddRange(deleteBill.Select(x => { x.MarkedDelete = true; return(x); })); #endregion #region 添加 var addBill = plBill.Values.Where(x => planBill.All(y => y.Id != x.Id)); #endregion ServerConfig.ApiDb.Execute( "UPDATE production_plan_bill SET `MarkedDateTime` = @MarkedDateTime, `MarkedDelete`= @MarkedDelete, `BillId` = @BillId, `PlannedConsumption` = @PlannedConsumption, `Extra` = @Extra WHERE `Id` = @Id;", updateBill); if (addBill.Any()) { ServerConfig.ApiDb.Execute( "INSERT INTO production_plan_bill (`CreateUserId`, `MarkedDateTime`, `PlanId`, `BillId`, `PlannedConsumption`) " + "VALUES (@CreateUserId, @MarkedDateTime, @PlanId, @BillId, @PlannedConsumption);", addBill); } } ServerConfig.ApiDb.Execute( "UPDATE production_plan SET `MarkedDateTime` = @MarkedDateTime, `Plan` = @Plan, `Remark` = @Remark WHERE `Id` = @Id;", productionPlan); return(Result.GenError <Result>(Error.Success)); }