Пример #1
0
        public object PostProduction([FromBody] IEnumerable <ProductionDetail> details)
        {
            if (details == null || !details.Any())
            {
                return(Result.GenError <Result>(Error.ParamError));
            }
            if (details.Any(x => x.Name.IsNullOrEmpty()))
            {
                return(Result.GenError <Result>(Error.ProductionNotEmpty));
            }
            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 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.Para           = arg.Para ?? "";
                    arg.Other          = arg.Other ?? "";
                }
            }
            var tIds      = details.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 = details.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    = details.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 (details.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 = details.Select(x => x.Name);

            if (ProductionHelper.GetHaveSame(wId, sames))
            {
                return(Result.GenError <Result>(Error.ProductionIsExist));
            }

            var stepIds = details.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 (details.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.Add(details);
            var productions = ProductionHelper.GetMenus(wId, sames).ToDictionary(x => x.Name);

            foreach (var detail in details)
            {
                if (productions.ContainsKey(detail.Name))
                {
                    foreach (var pro in detail.Args)
                    {
                        pro.CreateUserId   = userId;
                        pro.MarkedDateTime = markedDateTime;
                        pro.WorkshopId     = wId;
                        pro.ProductionId   = productions[detail.Name].Id;
                        pro.Other          = pro.Other ?? "";
                    }
                }
            }
            ProductionArgHelper.Instance.Add(details.SelectMany(x => x.Args));
            return(Result.GenError <Result>(Error.Success));
        }