public DataResult GetProcessManagementByProductionNameToDeviceId([FromBody] Request1 request)
        {
            var result             = new DataResult();
            var processManagements = ProcessManagementHelper.Instance.GetAll <ProcessManagementDetail>();

            if (request.Pid != 0 && processManagements.All(x => x.Id != request.Pid))
            {
                return(Result.GenError <DataResult>(Error.ProcessManagementNotExist));
            }

            try
            {
                var workshopId = 1;
                if (request.RawMaterial.IsNullOrEmpty())
                {
                    var productionIdList  = request.ProductionList;
                    var deviceModelIdList = request.DeviceModelList;
                    processManagements = processManagements.Where(x => x.RawMaterial.IsNullOrEmpty());
                    var pms      = processManagements.Where(x => x.ProductionList.Any(y => productionIdList.Contains(y)));
                    var existIds = pms.Select(x => x.DeviceIdList).SelectMany(x => x).Distinct();
                    var devices  = DeviceHelper.GetDetailsByModelIds(workshopId, deviceModelIdList);
                    result.datas.AddRange(
                        devices.Where(x => !existIds.Contains(x.Id)).Select(x => new { x.Id, x.Code, ModelId = x.ModelId }));
                }
                else
                {
                    var rawMaterial       = request.RawMaterial;
                    var deviceModelIdList = request.DeviceModelList;
                    var pms      = processManagements.Where(x => x.RawMaterial == rawMaterial);
                    var existIds = pms.Select(x => x.DeviceIdList).SelectMany(x => x).Distinct();
                    var devices  = DeviceHelper.GetDetailsByModelIds(workshopId, deviceModelIdList);
                    result.datas.AddRange(
                        devices.Where(x => !existIds.Contains(x.Id)).Select(x => new { x.Id, x.Code, ModelId = x.ModelId }));
                }
            }
            catch (Exception)
            {
                // ignored
            }

            return(result);
        }
        public Result PostProcessManagement([FromBody] Models.ProcessManagement.ProcessManagement processManagement)
        {
            var pms = ProcessManagementHelper.Instance.GetAll <Models.ProcessManagement.ProcessManagement>();

            if (pms.Any(x => x.ProcessNumber == processManagement.ProcessNumber))
            {
                return(Result.GenError <DataResult>(Error.ProcessManagementIsExist));
            }

            try
            {
                var workshopId      = 1;
                var deviceModelList = processManagement.DeviceModelList;
                if (deviceModelList.Any())
                {
                    var cnt = DeviceModelHelper.Instance.GetCountByIds(deviceModelList);
                    if (deviceModelList.Count() != cnt)
                    {
                        return(Result.GenError <Result>(Error.DeviceModelNotExist));
                    }
                }
                else
                {
                    return(Result.GenError <Result>(Error.DeviceModelNotExist));
                }

                var deviceIdList = processManagement.DeviceIdList;
                if (deviceIdList.Any())
                {
                    var deviceLibraries = DeviceHelper.GetDetailsByModelIds(workshopId, processManagement.DeviceModelList).ToDictionary(x => x.Id);
                    if (deviceIdList.Any(x => !deviceLibraries.ContainsKey(x)))
                    {
                        return(Result.GenError <Result>(Error.DeviceNotExist));
                    }

                    var rawMaterial = processManagement.RawMaterial;
                    if (rawMaterial.IsNullOrEmpty())
                    {
                        var productionList = processManagement.ProductionList;
                        pms = pms.Where(x => x.RawMaterial.IsNullOrEmpty());
                        if (productionList.Any())
                        {
                            var cnt = ProductionHelper.Instance.GetCountByIds(processManagement.ProductionList);
                            if (productionList.Count() != cnt)
                            {
                                return(Result.GenError <Result>(Error.ProductionNotExist));
                            }
                            var pManagements = pms.Where(x => x.ProductionList.Any(y => productionList.Contains(y)));
                            if (pManagements.Any(x => x.DeviceIdList.Any(y => deviceIdList.Contains(y))))
                            {
                                return(Result.GenError <Result>(Error.ProcessManagementAddError));
                            }
                        }
                        else
                        {
                            return(Result.GenError <Result>(Error.ProductionNotExist));
                        }
                    }
                    else
                    {
                        pms = pms.Where(x => !x.RawMaterial.IsNullOrEmpty());
                        var pManagements = pms.Where(x => x.RawMaterial == rawMaterial);
                        if (pManagements.Any(x => x.DeviceIdList.Any(y => deviceIdList.Contains(y))))
                        {
                            return(Result.GenError <Result>(Error.ProcessManagementAddRawMaterialError));
                        }
                    }
                }
                else
                {
                    processManagement.DeviceIds = "";
                }
            }
            catch (Exception)
            {
                return(Result.GenError <Result>(Error.Fail));
            }

            var createUserId = Request.GetIdentityInformation();
            var time         = DateTime.Now;

            processManagement.CreateUserId   = createUserId;
            processManagement.MarkedDateTime = time;
            var pId         = ProcessManagementHelper.Instance.AddBackId(processManagement);
            var processData = processManagement.ProcessData;
            var i           = 1;

            foreach (var pd in processData)
            {
                pd.ProcessOrder        = i++;
                pd.ProcessManagementId = pId;
                pd.CreateUserId        = createUserId;
                pd.MarkedDateTime      = time;
            }
            ProcessDataHelper.Instance.Add(processData);
            return(Result.GenError <Result>(Error.Success));
        }
        public Result PutProcessManagement([FromBody] Models.ProcessManagement.ProcessManagement processManagement)
        {
            var pms = ProcessManagementHelper.Instance.GetAll <Models.ProcessManagement.ProcessManagement>();

            if (pms.All(x => x.Id != processManagement.Id))
            {
                return(Result.GenError <DataResult>(Error.ProcessManagementNotExist));
            }

            if (pms.Any(x => x.Id != processManagement.Id && x.ProcessNumber == processManagement.ProcessNumber))
            {
                return(Result.GenError <DataResult>(Error.ProcessManagementIsExist));
            }

            var old    = pms.First(x => x.Id == processManagement.Id);
            var change = ClassExtension.HaveChange(old, processManagement);

            if (!change)
            {
                return(Result.GenError <Result>(Error.Success));
            }
            pms = pms.Where(x => x.Id != processManagement.Id);
            try
            {
                var workshopId      = 1;
                var deviceModelList = processManagement.DeviceModelList;
                if (deviceModelList.Any())
                {
                    var cnt = DeviceModelHelper.Instance.GetCountByIds(deviceModelList);
                    if (deviceModelList.Count() != cnt)
                    {
                        return(Result.GenError <Result>(Error.DeviceModelNotExist));
                    }
                }
                else
                {
                    return(Result.GenError <Result>(Error.DeviceModelNotExist));
                }

                var deviceIdList = processManagement.DeviceIdList;
                if (deviceIdList.Any())
                {
                    var deviceLibraries = DeviceHelper.GetDetailsByModelIds(workshopId, processManagement.DeviceModelList).ToDictionary(x => x.Id);
                    if (deviceIdList.Any(x => !deviceLibraries.ContainsKey(x)))
                    {
                        return(Result.GenError <Result>(Error.DeviceNotExist));
                    }

                    var rawMaterial = processManagement.RawMaterial;
                    if (rawMaterial.IsNullOrEmpty())
                    {
                        var productionList = processManagement.ProductionList;
                        pms = pms.Where(x => x.RawMaterial.IsNullOrEmpty());
                        if (productionList.Any())
                        {
                            var cnt = ProductionHelper.Instance.GetCountByIds(processManagement.ProductionList);
                            if (productionList.Count() != cnt)
                            {
                                return(Result.GenError <Result>(Error.ProductionNotExist));
                            }
                            var pManagements = pms.Where(x => x.ProductionList.Any(y => productionList.Contains(y)));
                            if (pManagements.Any(x => x.DeviceIdList.Any(y => deviceIdList.Contains(y))))
                            {
                                return(Result.GenError <Result>(Error.ProcessManagementAddError));
                            }
                        }
                        else
                        {
                            return(Result.GenError <Result>(Error.ProductionNotExist));
                        }
                    }
                    else
                    {
                        pms = pms.Where(x => !x.RawMaterial.IsNullOrEmpty());
                        var pManagements = pms.Where(x => x.RawMaterial == rawMaterial);
                        if (pManagements.Any(x => x.DeviceIdList.Any(y => deviceIdList.Contains(y))))
                        {
                            return(Result.GenError <Result>(Error.ProcessManagementAddRawMaterialError));
                        }
                    }
                }
                else
                {
                    processManagement.DeviceIds = "";
                }
            }
            catch (Exception)
            {
                return(Result.GenError <Result>(Error.Fail));
            }

            var createUserId = Request.GetIdentityInformation();
            var time         = DateTime.Now;

            processManagement.CreateUserId   = createUserId;
            processManagement.MarkedDateTime = time;
            ProcessManagementHelper.Instance.Update(processManagement);

            var processData = processManagement.ProcessData;
            var i           = 1;

            foreach (var pd in processData)
            {
                pd.ProcessOrder        = i++;
                pd.ProcessManagementId = processManagement.Id;
                pd.CreateUserId        = createUserId;
                pd.MarkedDateTime      = time;
            }

            var exist = ProcessDataHelper.Instance.GetFromParent <ProcessData>(processManagement.Id);
            var add   = processData.Where(x => x.Id == 0);

            if (add.Any())
            {
                ProcessDataHelper.Instance.Add(add);
            }
            var update = processData.Where(x => x.Id != 0 && exist.Any(y => y.Id == x.Id && ClassExtension.HaveChange(x, y)));

            if (update.Any())
            {
                ProcessDataHelper.Instance.Update(update);
            }
            var del = exist.Where(x => processData.All(y => y.Id != x.Id)).Select(x => x.Id);

            if (del.Any())
            {
                ProcessDataHelper.Instance.Delete(del);
            }
            return(Result.GenError <Result>(Error.Success));
        }