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));
        }
Beispiel #2
0
        public object GetProduction([FromQuery] int qId, int wId, bool menu, bool step)
        {
            if (step && qId != 0)
            {
                var data    = ProductionHelper.GetDetail(wId, qId);
                var flowIds = data.SelectMany(x => x.FlowIds);
                if (flowIds.Any())
                {
                    var flows = FlowCodeHelper.Instance.GetByIds <FlowCode>(flowIds).ToDictionary(x => x.Id);
                    foreach (var d in data)
                    {
                        d.Flows = d.FlowIds.Where(flowId => flows.ContainsKey(flowId)).Select(flowId => flows[flowId].Code).Join();
                    }
                }
                var pIds = data.Select(x => x.Id);
                if (pIds.Any())
                {
                    var args    = ProductionArgHelper.GetDetails(pIds, null);
                    var stepIds = data.SelectMany(x => x.Args.Select(y => y.StepId));
                    var steps   = stepIds.Any() ? FlowStepHelper.Instance.GetByIds <FlowStep>(stepIds).ToDictionary(x => x.Id) : new Dictionary <int, FlowStep>();
                    foreach (var d in data)
                    {
                        d.Args.AddRange(args.Where(x => x.ProductionId == d.Id).Select(x =>
                        {
                            var y      = ClassExtension.ParentCopyToChild <ProductionArg, ProductionArgFormat>(x);
                            y.FormatId = steps.ContainsKey(x.StepId) ? steps[x.StepId].FormatId : 0;
                            return(y);
                        }));
                    }
                }
                return(new
                {
                    errno = 0,
                    errmsg = "成功",
                    datas = data
                });
            }
            var result = new DataResult();

            if (menu)
            {
                result.datas.AddRange(ProductionHelper.GetMenu(wId, qId));
            }
            else
            {
                var data    = ProductionHelper.GetDetail(wId, qId);
                var flowIds = data.SelectMany(x => x.FlowIds);
                if (flowIds.Any())
                {
                    var flows = FlowCodeHelper.Instance.GetByIds <FlowCode>(flowIds).ToDictionary(x => x.Id);
                    foreach (var d in data)
                    {
                        d.Flows = d.FlowIds.Where(flowId => flows.ContainsKey(flowId)).Select(flowId => flows[flowId].Code).Join();
                    }
                }
                result.datas.AddRange(data);
            }
            if (qId != 0 && !result.datas.Any())
            {
                result.errno = Error.ProductionNotExist;
                return(result);
            }
            return(result);
        }