Beispiel #1
0
        public object PutProduction([FromBody] IEnumerable <ProductionDetail> details)
        {
            if (details == null || !details.Any())
            {
                return(Result.GenError <Result>(Error.ParamError));
            }
            if (details.Any(x => x.Id == 0))
            {
                return(Result.GenError <Result>(Error.ProductionNotExist));
            }
            if (details.Any(x => x.Name.IsNullOrEmpty()))
            {
                var ids = details.Select(x => x.Id);
                var cnt = ProductionHelper.Instance.GetCountByIds(ids);
                if (cnt != details.Count())
                {
                    return(Result.GenError <Result>(Error.ProductionNotExist));
                }
                var markedDateTime = DateTime.Now;
                foreach (var detail in details)
                {
                    detail.MarkedDateTime = markedDateTime;
                }
                ProductionHelper.Enable(details);
            }
            else
            {
                if (details.GroupBy(x => x.Name).Any(y => y.Count() > 1))
                {
                    return(Result.GenError <Result>(Error.ProductionDuplicate));
                }
                if (details.Any(x => x.StepIds.Count != x.Rates.Count ||
                                x.StepIds.Count != x.Costs.Count))
                {
                    return(Result.GenError <Result>(Error.ProductionCapacityListError));
                }
                var ids  = details.Select(x => x.Id);
                var data = ProductionHelper.Instance.GetByIds <Production>(ids);
                if (data.Count() != details.Count())
                {
                    return(Result.GenError <Result>(Error.ProductionNotExist));
                }

                var wId            = details.FirstOrDefault()?.WorkshopId ?? 0;
                var userId         = Request.GetIdentityInformation();
                var markedDateTime = DateTime.Now;
                foreach (var detail in details)
                {
                    detail.CreateUserId   = userId;
                    detail.MarkedDateTime = markedDateTime;
                    detail.Remark         = detail.Remark ?? "";
                    detail.FlowList       = detail.Args?.Select(x => x.FlowId).Distinct().Join() ?? "";
                    foreach (var arg in detail.Args)
                    {
                        arg.WorkshopId     = wId;
                        arg.CreateUserId   = userId;
                        arg.MarkedDateTime = markedDateTime;
                        arg.ProductionId   = detail.Id;
                        arg.Para           = arg.Para ?? "";
                        arg.Other          = arg.Other ?? "";
                    }
                }
                var newDetails = details.Where(x => ClassExtension.HaveChange(x, data.First(y => y.Id == x.Id)));
                if (newDetails.Any())
                {
                    var tIds      = newDetails.Select(x => x.TypeId).Distinct();
                    var flowTypes = FlowTypeHelper.Instance.GetByIds <FlowType>(tIds).ToDictionary(x => x.Id);
                    if (tIds.Count() != flowTypes.Count)
                    {
                        return(Result.GenError <Result>(Error.FlowTypeNotExist));
                    }

                    var cIds = newDetails.Select(x => x.CapacityId).Distinct();
                    var cnt  = CapacityHelper.Instance.GetCountByIds(cIds);
                    if (cIds.Count() != cnt)
                    {
                        return(Result.GenError <Result>(Error.CapacityNotExist));
                    }

                    var aTypeFlows = FlowCodeHelper.GetDetails(wId, null, tIds);
                    var flowIds    = newDetails.SelectMany(x => x.FlowIds).Distinct();
                    if (flowIds.Any(x => aTypeFlows.All(y => y.Id != x)))
                    {
                        return(Result.GenError <Result>(Error.FlowCodeNotExist));
                    }

                    var typeFlows = aTypeFlows.Where(x => flowIds.Contains(x.Id));
                    var flowCodes = typeFlows.ToDictionary(x => x.Id);
                    if (flowIds.Count() != flowCodes.Count)
                    {
                        return(Result.GenError <Result>(Error.FlowCodeNotExist));
                    }

                    if (newDetails.Count(detail =>
                    {
                        var fIds = detail.Args.Select(arg => arg.FlowId).Distinct();
                        return(fIds.Any(fId =>
                        {
                            var mFcDic = detail.Args.Where(x => x.FlowId == fId).GroupBy(x => x.StepId).ToDictionary(y => y.Key, y => y.Count());
                            var fcDic = flowCodes[fId].StepIds.GroupBy(y => y).ToDictionary(y => y.Key, y => y.Count());
                            return detail.Args.Count(x => x.FlowId == fId) != flowCodes[fId].StepIds.Count ||
                            mFcDic.Any(x => x.Value != fcDic[x.Key]);
                        }));
                    }) > 0)
                    {
                        return(Result.GenError <Result>(Error.ProductionFlowCodeStepCountError));
                    }

                    var sames = newDetails.Select(x => x.Name);
                    if (ProductionHelper.GetHaveSame(wId, sames, ids))
                    {
                        return(Result.GenError <Result>(Error.ProductionIsExist));
                    }

                    var stepIds = newDetails.SelectMany(x => x.StepIds).Distinct();
                    if (stepIds.Any())
                    {
                        var steps     = FlowStepHelper.Instance.GetByIds <FlowStep>(stepIds).ToDictionary(x => x.Id);
                        var formatIds = steps.Values.Select(x => x.FormatId).Distinct();
                        if (formatIds.Any())
                        {
                            var formats = ArgsFormatHelper.Instance.GetByIds <ArgsFormat>(formatIds).ToDictionary(x => x.Id);
                            if (newDetails.Any(x => x.Args.Any(arg =>
                            {
                                if (steps.ContainsKey(arg.StepId) && steps[arg.StepId].FormatId != 0 && formats.ContainsKey(steps[arg.StepId].FormatId))
                                {
                                    var format = formats[steps[arg.StepId].FormatId];
                                    return(ArgsFormatHelper.CheckPara(format, arg.Para));
                                }
                                return(false);
                            })))
                            {
                                return(Result.GenError <Result>(Error.ProductionFlowCodeStepFormatError));
                            }
                        }
                    }
                    ProductionHelper.Instance.Update(newDetails);
                }

                var args    = ProductionArgHelper.GetMenus(ids, null);
                var argTmps = details.SelectMany(x => x.Args);
                var add     = new List <ProductionArg>();
                var up      = new List <ProductionArg>();
                var del     = new List <int>();
                add.AddRange(argTmps.Where(x => x.Id == 0));
                up.AddRange(argTmps.Where(x => x.Id != 0).Where(x => argTmps.Any(y => ClassExtension.HaveChange(y, x))));
                del.AddRange(args.Where(x => !argTmps.Any(y => y.Id == x.Id)).Select(x => x.Id));
                if (add.Any())
                {
                    ProductionArgHelper.Instance.Add(add);
                }
                if (up.Any())
                {
                    ProductionArgHelper.Instance.Update <ProductionArg>(up);
                }
                if (del.Any())
                {
                    ProductionArgHelper.Instance.Delete(del);
                }
            }
            return(Result.GenError <Result>(Error.Success));
        }