コード例 #1
0
ファイル: JobSubmit.cs プロジェクト: moling3650/Smart.MES.POD
        public static string JobUpload(string json)
        {
            string[] str1      = json.Split('|');
            string   strJson   = str1[0];
            string   strStrict = str1[1];
            string   strRes;

            ILE.IJob job = JsonConvert.DeserializeObject <ILE.Model.Job>(strJson);

            ILE.IResult res = CheckJob(job, strStrict);
            if (!res.Result)
            {
                strRes = JsonConvert.SerializeObject(res);
                return(strRes);
            }

            try
            {
                #region 数据提交

                using (var context = LEDAO.APIGateWay.GetEntityContext())
                {
                    //用于删除
                    //var context1 = LEDAO.APIGateWay.GetEntityContext();
                    //结果
                    string strResult      = job.Result == true ? "OK" : "NG";
                    string strProcessCode = job.ProcessCode;
                    string strSFC         = job.SFC;
                    //工艺流程
                    B_Process_Flow_Detail flowDetail = context.B_Process_Flow_Detail.Where(c => c.process_from == strProcessCode && c.flow_code == job.FlowCode && c.process_result == strResult).First();
                    //提交时间
                    DateTime?newDate = context.NewDate().First();
                    //判断是否为最后一步工艺流程
                    bool BEnd = flowDetail.process_next == "END" ? true : false;
                    //查询SFC状态
                    List <LEDAO.P_SFC_State> sfcList = context.P_SFC_State.Where(c => c.SFC == strSFC).ToList();
                    //获取班别
                    List <B_WorkGroup_Class> Class_codeList = context.sp_Count_Class(job.group_code).ToList();
                    string Class_Code = string.Empty;
                    if (Class_codeList.Count != 0)
                    {
                        Class_Code = Class_codeList[0].class_code.ToString();
                    }
                    else
                    {
                        Class_Code = "";
                    }
                    //获取P_Date
                    List <sp_Count_PDate_Result> P_Date1 = context.sp_Count_PDate(job.group_code).ToList();
                    DateTime P_Date = Convert.ToDateTime(P_Date1[0].P_date);

                    //根据工单号获取父工单及主工单
                    P_WorkOrder ListOrder = context.P_WorkOrder.Where(c => c.order_no == job.OrderNO).First();
                    //根据Product_code获取mat_code
                    //V_Bom_Detail ListBom_Detail = context.V_Bom_Detail.Where(c => c.product_code == job.Product).First();

                    //SFC状态P_SFC_STATE
                    LEDAO.P_SFC_State sfcstate;
                    if (sfcList.Count > 0)
                    {
                        sfcstate = sfcList.First();
                        //流程NG
                        if (strResult == "NG")
                        {
                            sfcstate.fail_times++;
                        }
                        //工艺是否结束
                        if (BEnd && job.Result)
                        {
                            sfcstate.state    = 2;
                            sfcstate.end_time = newDate;
                            sfcstate.end_Date = P_Date;
                        }
                        sfcstate.from_process = sfcstate.now_process;
                        sfcstate.now_process  = flowDetail.process_next;
                        sfcstate.process_time = newDate;
                        context.ObjectStateManager.ChangeObjectState(sfcstate, System.Data.EntityState.Modified);
                    }
                    else
                    {
                        sfcstate              = new LEDAO.P_SFC_State();
                        sfcstate.order_no     = job.OrderNO;
                        sfcstate.SFC          = job.SFC;
                        sfcstate.initqty      = job.QTY;
                        sfcstate.qty          = job.QTY;
                        sfcstate.is_tray      = job.IsPack;
                        sfcstate.fail_times   = strResult == "OK" ? 0 : 1;
                        sfcstate.state        = 1;
                        sfcstate.from_process = job.ProcessCode;
                        sfcstate.now_process  = flowDetail.process_next;
                        //车间别
                        sfcstate.workshop = job.workshop;
                        //班别,P_Date
                        sfcstate.begin_classcode = Class_Code;
                        sfcstate.begin_Date      = P_Date;
                        sfcstate.end_classcode   = Class_Code;
                        sfcstate.end_Date        = P_Date;
                        //父工单及主工单
                        sfcstate.parent_order = ListOrder.parent_order;
                        sfcstate.main_order   = ListOrder.main_order;
                        //mat_code
                        sfcstate.mat_code = ListOrder.product_code;  //ListBom_Detail.mat_code;

                        //工艺是否结束
                        if (BEnd && job.Result)
                        {
                            sfcstate.state    = 2;
                            sfcstate.end_time = newDate;
                            sfcstate.end_Date = P_Date;
                            #region
                            //P_WorkOrder orderlst = context.P_WorkOrder.Where(c => c.order_no == job.OrderNO).FirstOrDefault();
                            //decimal orderCount = 0; //工单总完成数
                            //var lst = context.P_GetOrderCompCount(job.OrderNO).FirstOrDefault();
                            //if (lst!=null)
                            //{
                            //    orderCount = (decimal)(lst.qty);
                            //}
                            //if (orderlst!=null)
                            //{
                            //    if (orderlst.qty <= orderCount + job.QTY)
                            //    {
                            //        orderlst.state = 2;
                            //        //context.ObjectStateManager.ChangeObjectState(orderlst, System.Data.EntityState.Modified);
                            //    }
                            //}
                            #endregion
                        }

                        else
                        {
                            sfcstate.state = 1;
                        }
                        sfcstate.input_station = job.StationCode;
                        //sfcstate.begin_time = newDate;
                        sfcstate.begin_time   = job.start_time;
                        sfcstate.process_time = newDate;
                        context.P_SFC_State.AddObject(sfcstate);
                    }


                    //过站记录
                    P_SFC_Process_IOLog sfcIOLog;
                    int max = context.P_SFC_Process_IOLog.Where(c => c.SFC == strSFC && c.process_code == job.ProcessCode && c.output_time != null).Count();
                    List <LEDAO.P_SFC_Process_IOLog> sfcIOLogList1 = context.P_SFC_Process_IOLog.Where(c => c.SFC == strSFC && c.process_code == job.ProcessCode && c.output_time == null).ToList();
                    if (sfcIOLogList1.Count > 0)
                    {
                        //将station_code 和 output_time为NULL 的数据删掉(next站记录)
                        var var = from u in context.P_SFC_Process_IOLog where u.station_code == null && u.output_time == null && u.SFC == job.SFC select u;
                        foreach (var del in var)
                        {
                            context.P_SFC_Process_IOLog.DeleteObject(del);
                            //context.SaveChanges();
                        }
                    }
                    List <LEDAO.P_SFC_Process_IOLog> sfcIOLogList2 = context.P_SFC_Process_IOLog.Where(c => c.SFC == strSFC && c.output_time != null).ToList();
                    List <LEDAO.P_SFC_Process_IOLog> sfcIOLogList  = context.P_SFC_Process_IOLog.Where(c => c.SFC == strSFC && c.process_code == job.ProcessCode && c.output_time != null).ToList();
                    if (sfcIOLogList.Count > 0)
                    {
                        #region
                        //sfcIOLog = sfcIOLogList.First();
                        //sfcIOLog.fail_times = sfcstate.fail_times;
                        //sfcIOLog.pass = max + 1;
                        //sfcIOLog.output_time = newDate;
                        //sfcIOLog.station_code = job.StationCode;
                        //sfcIOLog.emp_code = job.EmpCode;
                        //context.ObjectStateManager.ChangeObjectState(sfcIOLog, System.Data.EntityState.Modified);
                        #endregion
                        //将station_code 和 output_time为NULL 的数据删掉
                        var var = from u in context.P_SFC_Process_IOLog where u.station_code == null && u.output_time == null && u.SFC == job.SFC select u;
                        foreach (var del in var)
                        {
                            context.P_SFC_Process_IOLog.DeleteObject(del);
                        }
                        //context.SaveChanges();

                        List <P_SFC_Process_IOLog> modle = context.P_SFC_Process_IOLog.Where(X => X.station_code != null && X.output_time != null && X.SFC == job.SFC && X.process_code == job.ProcessCode).ToList();
                        foreach (var mod in modle)
                        {
                            sfcIOLog      = mod;
                            sfcIOLog.pass = sfcIOLog.pass - 1;
                            context.ObjectStateManager.ChangeObjectState(sfcIOLog, System.Data.EntityState.Modified);
                        }
                        sfcIOLog              = new P_SFC_Process_IOLog();
                        sfcIOLog.order_no     = job.OrderNO;
                        sfcIOLog.SFC          = job.SFC;
                        sfcIOLog.initqty      = job.QTY; // 需要验证数据正确性
                        sfcIOLog.qty          = job.QTY;
                        sfcIOLog.fail_times   = sfcstate.fail_times;
                        sfcIOLog.group_code   = flowDetail.process_from_group;
                        sfcIOLog.process_code = job.ProcessCode;
                        sfcIOLog.station_code = job.StationCode;
                        if (sfcIOLogList2.Count > 0)
                        {
                            sfcIOLog.input_time = sfcIOLogList2[sfcIOLogList2.Count - 1].output_time;
                        }
                        else
                        {
                            sfcIOLog.input_time = newDate;
                        }
                        sfcIOLog.output_time = newDate;
                        sfcIOLog.emp_code    = job.EmpCode;
                        //sfcIOLog.pass = max + 1;
                        sfcIOLog.pass = 1;
                        //车间别
                        sfcIOLog.workshop   = job.workshop;
                        sfcIOLog.class_code = Class_Code;
                        sfcIOLog.p_date     = P_Date;
                        sfcIOLog.start_time = job.start_time;
                        //sfcIOLog.p_date 没确定
                        context.P_SFC_Process_IOLog.AddObject(sfcIOLog);
                    }
                    else
                    {
                        //SFC记录
                        sfcIOLog              = new P_SFC_Process_IOLog();
                        sfcIOLog.order_no     = job.OrderNO;
                        sfcIOLog.SFC          = job.SFC;
                        sfcIOLog.initqty      = job.QTY;
                        sfcIOLog.qty          = job.QTY;
                        sfcIOLog.fail_times   = sfcstate.fail_times;
                        sfcIOLog.group_code   = flowDetail.process_from_group;
                        sfcIOLog.process_code = job.ProcessCode;
                        sfcIOLog.station_code = job.StationCode;
                        if (sfcIOLogList2.Count > 0)
                        {
                            sfcIOLog.input_time = sfcIOLogList2[sfcIOLogList2.Count - 1].output_time;
                        }
                        else
                        {
                            sfcIOLog.input_time = job.start_time;
                        }
                        sfcIOLog.output_time = newDate;
                        sfcIOLog.emp_code    = job.EmpCode;
                        sfcIOLog.pass        = 1;
                        //车间别
                        sfcIOLog.workshop   = job.workshop;
                        sfcIOLog.class_code = Class_Code;
                        sfcIOLog.p_date     = P_Date;
                        sfcIOLog.start_time = job.start_time;
                        //sfcIOLog.p_date 没确定
                        context.P_SFC_Process_IOLog.AddObject(sfcIOLog);
                    }

                    //传建下一个过站记录
                    P_SFC_Process_IOLog sfcIOLogNext;
                    if (!string.IsNullOrEmpty(flowDetail.process_next) && flowDetail.process_next != "END")
                    {
                        //SFC记录
                        sfcIOLogNext              = new P_SFC_Process_IOLog();
                        sfcIOLogNext.order_no     = job.OrderNO;
                        sfcIOLogNext.SFC          = job.SFC;
                        sfcIOLogNext.qty          = job.QTY;
                        sfcIOLogNext.group_code   = flowDetail.process_next_group;
                        sfcIOLogNext.process_code = flowDetail.process_next;
                        sfcIOLogNext.fail_times   = sfcstate.fail_times;
                        sfcIOLogNext.pass         = 0;
                        sfcIOLogNext.input_time   = newDate;
                        //车间别
                        sfcIOLogNext.workshop   = job.workshop;
                        sfcIOLogNext.class_code = Class_Code;
                        sfcIOLogNext.p_date     = P_Date;
                        sfcIOLogNext.start_time = job.start_time;
                        context.P_SFC_Process_IOLog.AddObject(sfcIOLogNext);
                    }


                    //工步字段
                    LEDAO.P_SFC_ProcessData    sfcdata;
                    LEDAO.P_Mat_WIP_Seed       wip_seed;
                    LEDAO.P_Material_WIP       material_wip;
                    LEDAO.P_SFC_ProcessSonData processSonData;
                    LEDAO.P_SFC_Jude           sfc_jude;
                    for (int i = job.IndexBack; i < job.StepList.Count; i++)
                    {
                        ILE.Step step = job.StepList[i];
                        if (step.IsRecord != 1 | step.StepDetail == null)
                        {
                            continue;
                        }
                        //保存子工步数据
                        if (step.IsRecord != 1 | step.StepSonDetail != null)
                        {
                            foreach (var stepd in step.StepSonDetail)
                            {
                                processSonData              = new P_SFC_ProcessSonData();
                                processSonData.orderno      = job.OrderNO;
                                processSonData.sfc          = job.SFC;
                                processSonData.stepid       = stepd.stepsoncode;
                                processSonData.stepval      = stepd.StepVal;
                                processSonData.steppre      = stepd.stepSonPre;
                                processSonData.parentstepid = step.StepCode;
                                processSonData.stepconclude = stepd.stepconclude;
                                processSonData.isfedbatch   = 0;
                                processSonData.inputtime    = DateTime.Now;
                                context.P_SFC_ProcessSonData.AddObject(processSonData);
                            }
                        }
                        //手动耗料
                        string lot_no   = job.StepList[i].StepValue;
                        string mat_code = job.StepList[i].Matcode;
                        string product  = job.Product;
                        string drive    = job.StepList[i].DriveName;
                        var    res2     = (from a in context.P_Material_WIP.Where(X => X.lot_no == lot_no) select new { lot_no = a.lot_no }).ToList();
                        if (drive == "DP_Expend" && res2.Count != 0)
                        {
                            //如果所消耗物料是单件,把WIP表中此批次的STATE改为2
                            var    res1   = (from a in context.V_ProductMaterial.Where(X => X.p_code == mat_code) select new { product_code = a.p_code, max_qty = a.max_qty }).ToList();
                            string maxqty = res1[0].max_qty.ToString();
                            if (maxqty == "1.000")
                            {
                                var model = context.P_Material_WIP.Where(X => X.lot_no == lot_no).FirstOrDefault();
                                model.state = 2;
                            }
                        }


                        string flowcode = job.FlowCode;


                        var res3 = (from a in context.B_Process_Flow.Where(X => X.flow_code == flowcode) select new { flow_name = a.flow_name, bom_id = a.bom_id }).ToList();

                        int bomid = Convert.ToInt32(res3[0].bom_id);

                        var res4 = (from a in context.B_Bom.Where(X => X.bom_id == bomid) select new { bom_code = a.bom_code }).ToList();

                        string bomcode = res4[0].bom_code;

                        var bom_detail = (from a in context.V_Bom_Detail.Where(X => X.mat_code == mat_code && X.product_code == product && X.bom_code == bomcode) select new { qty = a.qty, baseqty = a.base_qty }).ToList();


                        decimal mat_qty = 0;
                        int     baseqty = 0;
                        if (bom_detail != null && bom_detail.Count > 0)
                        {
                            for (int m = 0; m < bom_detail.Count; m++)
                            {
                                mat_qty = (decimal)(bom_detail[m].qty);
                                baseqty = (int)bom_detail[m].baseqty;
                            }
                        }
                        else
                        {
                            mat_qty = 1;
                            baseqty = 1;
                        }
                        float   percentum = job.StepList[i].consume_percent;
                        decimal qty       = job.QTY * (mat_qty / baseqty) * Convert.ToDecimal(percentum);
                        foreach (var stepdata in step.StepDetail)
                        {
                            #region 扣料
                            //自动投料时扣料
                            if (stepdata.seed_id != "" && !string.IsNullOrEmpty(stepdata.seed_id))
                            {
                                string Usedqty = "0";
                                //int seed_id = 0;
                                int     seed_id    = Convert.ToInt32(stepdata.seed_id.ToString());
                                decimal lot_qty    = decimal.Parse((stepdata.lotqty).ToString());
                                decimal surplusqty = 0;
                                if (step.StepDetail.Count > 1)
                                {
                                    //1:平均消耗 2:顺序消耗
                                    if (stepdata.consume_type == 1)
                                    {
                                        List <ILE.StepData> stepDetail = new List <ILE.StepData>();
                                        bool    isPositive             = false;;
                                        decimal qtySum = 0;
                                        foreach (var stepdata2 in step.StepDetail)
                                        {
                                            if (stepdata2.lotqty > 0 && stepdata2 == step.StepDetail[step.StepDetail.Count - 1])
                                            {
                                                isPositive = true;
                                                break;
                                            }
                                        }
                                        if (isPositive)
                                        {
                                            foreach (var stepdata3 in step.StepDetail)
                                            {
                                                if (stepdata3.lotqty > 0)
                                                {
                                                    stepDetail.Add(stepdata3);
                                                }
                                            }
                                            foreach (var stepdata1 in stepDetail)
                                            {
                                                qtySum += Math.Abs(decimal.Parse((stepdata1.lotqty).ToString()));
                                            }
                                        }
                                        else
                                        {
                                            foreach (var stepdata1 in step.StepDetail)
                                            {
                                                qtySum += Math.Abs(decimal.Parse((stepdata1.lotqty).ToString()));
                                            }
                                        }
                                        Usedqty = (lot_qty * (qty / qtySum)).ToString("#0.0000");
                                    }
                                    else if (stepdata.consume_type == 2)
                                    {
                                        if (lot_qty > 0)
                                        {
                                            surplusqty = (qty - Math.Abs(lot_qty));
                                        }
                                        else
                                        {
                                            surplusqty = qty;
                                        }
                                        if (surplusqty >= 0)
                                        {
                                            Usedqty = lot_qty.ToString();
                                            if (lot_qty > 0)
                                            {
                                                qty -= Math.Abs(lot_qty);
                                            }
                                            else
                                            {
                                                qty = qty;
                                            }
                                        }
                                        if (stepdata != step.StepDetail[step.StepDetail.Count - 1] && (decimal.Parse(Usedqty) < 0 || decimal.Parse(Usedqty) == 0))
                                        {
                                            Usedqty = "0";
                                        }
                                        //允许超越时算法(做的多,投的料少,且允许超越)
                                        if (stepdata == step.StepDetail[step.StepDetail.Count - 1] && surplusqty > 0)
                                        {
                                            //Usedqty = (Math.Abs(surplusqty)).ToString();
                                            Usedqty = ((job.QTY * (mat_qty / baseqty))).ToString();
                                        }
                                        //做的少,投的料多,最后一个批次扣料算法
                                        if (surplusqty < 0 && qty != 0)
                                        {
                                            Usedqty = (lot_qty - Math.Abs(surplusqty)).ToString();
                                            qty     = 0;
                                        }
                                    }
                                }
                                else
                                {
                                    Usedqty = (job.QTY * (mat_qty / baseqty) * Convert.ToDecimal(percentum)).ToString();
                                }
                                List <P_Mat_WIP_Seed> modle = context.P_Mat_WIP_Seed.Where(X => X.wip_id == seed_id && X.station_code == job.StationCode).ToList();
                                foreach (var mod in modle)
                                {
                                    wip_seed = mod;
                                    if (stepdata == step.StepDetail[step.StepDetail.Count - 1] && (stepdata.lotqty < 0 || stepdata.lotqty == 0) && stepdata.consume_type == 2)
                                    {
                                        wip_seed.lot_qty -= (job.QTY * (mat_qty / baseqty)) * decimal.Parse(percentum.ToString());
                                    }
                                    else
                                    {
                                        if (stepdata == step.StepDetail[step.StepDetail.Count - 1] && surplusqty > 0 && stepdata.consume_type == 2)
                                        {
                                            wip_seed.lot_qty -= decimal.Parse(Usedqty) * decimal.Parse(percentum.ToString());
                                        }
                                        else
                                        {
                                            wip_seed.lot_qty -= Math.Abs(decimal.Parse(Usedqty));
                                        }
                                    }
                                    context.ObjectStateManager.ChangeObjectState(wip_seed, System.Data.EntityState.Modified);
                                }
                            }

                            //手动投料时扣料
                            if (stepdata.wip_id != "" && !string.IsNullOrEmpty(stepdata.wip_id))
                            {
                                decimal usedqty             = job.QTY * (mat_qty / baseqty);
                                int     wip_id              = Convert.ToInt32(stepdata.wip_id);
                                List <P_Material_WIP> modle = context.P_Material_WIP.Where(X => X.id == wip_id).ToList();
                                foreach (var mod in modle)
                                {
                                    material_wip          = mod;
                                    material_wip.lot_qty -= decimal.Parse(usedqty.ToString()) * decimal.Parse(percentum.ToString());
                                    context.ObjectStateManager.ChangeObjectState(material_wip, System.Data.EntityState.Modified);
                                }
                            }
                            #endregion

                            #region 保存待判数据
                            string lot_sfc = stepdata.StepVal.ToString();
                            List <LEDAO.P_SFC_Jude> Listjude = context.P_SFC_Jude.Where(c => c.sfc == lot_sfc && c.state == 0).ToList();
                            if (Listjude != null)
                            {
                                foreach (var jude in Listjude)
                                {
                                    sfc_jude           = new P_SFC_Jude();
                                    sfc_jude.jude_code = jude.jude_code;
                                    sfc_jude.jude_name = jude.jude_name;
                                    sfc_jude.sfc       = job.SFC;
                                    sfc_jude.state     = 0;
                                    context.P_SFC_Jude.AddObject(sfc_jude);
                                }
                            }
                            #endregion

                            //保存P_SFC_ProcessData
                            int stepid = Convert.ToInt32(job.StepList[i].StepID);
                            List <LEDAO.P_SFC_ProcessData> sfcDataList = context.P_SFC_ProcessData.Where(c => c.SFC == job.SFC && c.step_id == stepid).ToList();
                            if (sfcDataList.Count > 0)
                            {
                                #region
                                List <P_SFC_ProcessData> model = context.P_SFC_ProcessData.Where(c => c.SFC == job.SFC && c.step_id == stepid).ToList();
                                foreach (var mod in model)
                                {
                                    sfcdata      = mod;
                                    sfcdata.pass = sfcdata.pass - 1;
                                    context.ObjectStateManager.ChangeObjectState(sfcdata, System.Data.EntityState.Modified);
                                }
                                #endregion

                                //判断当前数据库中有几条数据,pass则+1
                                sfcdata              = new LEDAO.P_SFC_ProcessData();
                                sfcdata.idx          = step.Idx;
                                sfcdata.order_no     = job.OrderNO;
                                sfcdata.pid          = job.Pid;
                                sfcdata.step_id      = step.StepID;
                                sfcdata.SFC          = job.SFC;
                                sfcdata.step_type    = step.StepType;
                                sfcdata.station_code = job.StationCode;
                                sfcdata.fail_times   = sfcstate.fail_times;
                                sfcdata.mat_code     = stepdata.matCode;
                                //sfcdata.pass = sfcDataList.Count + 1;
                                sfcdata.pass       = 1;
                                sfcdata.qty        = stepdata.qty;
                                sfcdata.val        = stepdata.StepVal;
                                sfcdata.input_time = stepdata.InPutDate;
                                sfcdata.unit       = step.Unit;
                                //车间别
                                sfcstate.workshop  = job.workshop;
                                sfcdata.class_code = Class_Code;
                                sfcdata.P_Date     = P_Date;
                                sfcdata.emp_code   = job.EmpCode;
                                context.P_SFC_ProcessData.AddObject(sfcdata);
                            }
                            else
                            {
                                sfcdata              = new LEDAO.P_SFC_ProcessData();
                                sfcdata.idx          = step.Idx;
                                sfcdata.order_no     = job.OrderNO;
                                sfcdata.pid          = job.Pid;
                                sfcdata.step_id      = step.StepID;
                                sfcdata.SFC          = job.SFC;
                                sfcdata.step_type    = step.StepType;
                                sfcdata.station_code = job.StationCode;
                                sfcdata.fail_times   = sfcstate.fail_times;
                                sfcdata.mat_code     = stepdata.matCode;
                                //sfcdata.pass = sfcIOLog.pass;
                                sfcdata.pass       = 1;
                                sfcdata.qty        = stepdata.qty;
                                sfcdata.val        = stepdata.StepVal;
                                sfcdata.input_time = stepdata.InPutDate;
                                sfcdata.unit       = step.Unit;
                                sfcdata.emp_code   = job.EmpCode;
                                //车间别
                                sfcdata.workshop   = job.workshop;
                                sfcdata.class_code = Class_Code;
                                sfcdata.P_Date     = P_Date;
                                context.P_SFC_ProcessData.AddObject(sfcdata);
                            }
                        }
                    }

                    if (job.QTYOrder + job.QTY >= job.MaxQTYOrder)
                    {
                        LEDAO.P_WorkOrder order = context.P_WorkOrder.Where(c => c.order_no == job.OrderNO).First();
                        order.state = 2;
                        context.ObjectStateManager.ChangeObjectState(order, System.Data.EntityState.Modified);
                    }
                    LEDAO.P_Material_WIP wip;
                    if (BEnd && job.Result)
                    {
                        try
                        {
                            List <LEDAO.P_Material_WIP> wip1 = context.P_Material_WIP.Where(c => c.lot_no == job.SFC && c.workshop == job.workshop && c.mat_code == job.Product).ToList();
                            if (wip1.Count < 1)
                            {
                                wip                = new P_Material_WIP();
                                wip.lot_no         = job.SFC;
                                wip.input_qty      = job.QTY;
                                wip.lot_qty        = job.QTY;
                                wip.mat_code       = job.Product;
                                wip.input_time     = newDate;
                                wip.Parent_order   = job.FatherOrderNO;
                                wip.order_no       = job.OrderNO;
                                wip.bill_no        = job.OrderNO;
                                wip.parent_station = job.StationCode;
                                wip.state          = 0;
                                //车间别
                                wip.workshop = job.workshop;
                                context.P_Material_WIP.AddObject(wip);
                            }
                        }
                        catch
                        {
                        }
                    }

                    //LEDAO.P_FailLog failLog;
                    //LEDAO.P_Fail_Detail failDetail;
                    //if (job.NGPheno.Count > 0)
                    //{
                    //    failLog = new P_FailLog();
                    //    failLog.fail_times = sfcstate.fail_times;
                    //    failLog.from_emp = job.EmpCode;
                    //    failLog.from_process = job.ProcessCode;
                    //    failLog.from_station = job.StationCode;
                    //    failLog.input_time = newDate;
                    //    failLog.order_no = job.OrderNO;
                    //    failLog.sfc = job.SFC;
                    //    failLog.state = job.Result ? 0 : 1;
                    //    failLog.Disposal_Process = flowDetail.disposal_code;
                    //    failLog.process_code = flowDetail.process_next;
                    //    failLog.fguid = System.Guid.NewGuid().ToString();
                    //    context.P_FailLog.AddObject(failLog);

                    //    for (int i = 0; i < job.NGPheno.Count; i++)
                    //    {
                    //        failDetail = new P_Fail_Detail();
                    //        failDetail.fguid = failLog.fguid;
                    //        failDetail.input_time = newDate;
                    //        failDetail.ng_code = job.NGPheno[i];
                    //        failDetail.order_no = job.OrderNO;
                    //        failDetail.sfc = job.SFC;
                    //        context.P_Fail_Detail.AddObject(failDetail);
                    //    }
                    //}
                    context.SaveChanges();
                }
                #endregion

                res.ExtMessage = "提交完成";
                res.Result     = true;
                strRes         = JsonConvert.SerializeObject(res);
                return(strRes);
            }
            catch (Exception exc)
            {
                res.ExtMessage = "提交异常:" + exc.Message;
                res.Result     = false;
                strRes         = JsonConvert.SerializeObject(res);
                return(strRes);
            }
        }
コード例 #2
0
        //测试API1
        //public static string InsertDate(string json)
        //{
        //    string StrJson = json.Replace("\\", "\\\\");
        //    DataTable tb = JsonToTable(StrJson);
        //    string order=tb.Rows[0]["order_no"].ToString();
        //    //判断获取数据在本地数据库是否存在
        //    var context1 = LEDAO.APIGateWay.GetEntityContext();
        //    var var = from b in context1.P_SSW_PrintList_test where b.order_no == order select b;
        //    string isAdd = null;
        //    if (var.Count() > 0)
        //    {
        //        isAdd = "2";
        //    }
        //    else
        //    {
        //        isAdd = "1";
        //    }
        //    //根据获取数据生成新的Json
        //    P_SSW_PrintList_test obj = new P_SSW_PrintList_test();
        //    obj.order_no = tb.Rows[0]["order_no"].ToString();
        //    obj.inputTime = Convert.ToDateTime(tb.Rows[0]["inputTime"].ToString());
        //    obj.product_code = tb.Rows[0]["product_code"].ToString();
        //    obj.product_name = tb.Rows[0]["product_name"].ToString();
        //    obj.qty = Convert.ToDecimal(tb.Rows[0]["qty"].ToString());
        //    obj.state = tb.Rows[0]["state"].ToString();
        //    obj.Template_id = tb.Rows[0]["Template_id"].ToString();
        //    if (tb.Rows[0]["finishTime"].ToString() == "")
        //    {
        //        obj.finishTime = null;
        //    }
        //    else
        //    {
        //        obj.finishTime = Convert.ToDateTime(tb.Rows[0]["finishTime"].ToString());
        //    }
        //    obj.completed = Convert.ToInt32(tb.Rows[0]["completed"].ToString());
        //    string strJson1 = JsonToolsNet.ObjectToJson(obj);
        //    //添加或修改
        //    string STR = "";
        //    //添加
        //    if (isAdd == "1")
        //    {
        //        LEDAO.P_SSW_PrintList_test tem = JsonConvert.DeserializeObject<LEDAO.P_SSW_PrintList_test>(strJson1);
        //        var context = LEDAO.APIGateWay.GetEntityContext();
        //        using (context)
        //        {
        //            context.P_SSW_PrintList_test.AddObject(tem);
        //            context.SaveChanges();
        //        }
        //        STR = "1";
        //    }
        //    //修改
        //    if (isAdd == "2")
        //    {
        //        LEDAO.P_SSW_PrintList_test tem = JsonConvert.DeserializeObject<LEDAO.P_SSW_PrintList_test>(strJson1);
        //        using (var context2 = LEDAO.APIGateWay.GetEntityContext())
        //        {
        //            string No = tb.Rows[0]["order_no"].ToString();
        //            var model = context2.P_SSW_PrintList_test.Where(X => X.order_no == No).FirstOrDefault();
        //            model.Template_id = tem.Template_id;
        //            model.inputTime = tem.inputTime;
        //            model.product_code = tem.product_code;
        //            model.order_no = tem.order_no;
        //            model.product_name = tem.product_name;
        //            model.qty = tem.qty;
        //            model.state = tem.state;
        //            model.finishTime = tem.finishTime;
        //            model.completed = tem.completed;
        //            context2.SaveChanges();
        //        }
        //        STR = "2";
        //    }
        //    return STR;
        //}
        //工单同步
        public static string WorkOrderSyncAPI(string json)
        {
            string    StrJson = json.Replace("\\", "\\\\");
            DataTable tb      = JsonToTable(StrJson);
            string    order   = tb.Rows[0]["FBillNo"].ToString();
            //判断获取数据在本地数据库是否存在
            var    context1 = LEDAO.APIGateWay.GetEntityContext();
            var    var      = from b in context1.P_WorkOrder where b.order_no == order select b;
            string isAdd    = null;

            if (var.Count() > 0)
            {
                isAdd = "2";
            }
            else
            {
                isAdd = "1";
            }
            //根据获取数据生成新的Json
            P_WorkOrder obj = new P_WorkOrder();

            obj.order_no     = tb.Rows[0]["FBillNo"].ToString();
            obj.input_time   = Convert.ToDateTime(tb.Rows[0]["FCheckDate"].ToString());
            obj.planned_time = Convert.ToDateTime(tb.Rows[0]["FPlanCommitDate"].ToString());
            obj.product_code = tb.Rows[0]["FItemID"].ToString();
            obj.qty          = Convert.ToDecimal(tb.Rows[0]["FQty"].ToString());
            obj.parent_order = tb.Rows[0]["FParentInterID"].ToString();
            obj.CO           = tb.Rows[0]["FOrderInterID"].ToString();
            obj.state        = Convert.ToInt32(tb.Rows[0]["FStatus"].ToString());
            obj.flow_code    = tb.Rows[0]["FRoutingID"].ToString();
            string strJson1 = JsonToolsNet.ObjectToJson(obj);
            //添加或修改
            string STR = "";

            //添加
            if (isAdd == "1")
            {
                LEDAO.P_WorkOrder tem = JsonConvert.DeserializeObject <LEDAO.P_WorkOrder>(strJson1);
                var context           = LEDAO.APIGateWay.GetEntityContext();
                using (context)
                {
                    context.P_WorkOrder.AddObject(tem);
                    context.SaveChanges();
                }
                STR = "1";
            }
            //修改
            if (isAdd == "2")
            {
                LEDAO.P_WorkOrder tem = JsonConvert.DeserializeObject <LEDAO.P_WorkOrder>(strJson1);
                using (var context2 = LEDAO.APIGateWay.GetEntityContext())
                {
                    string No    = tb.Rows[0]["FBillNo"].ToString();
                    var    model = context2.P_WorkOrder.Where(X => X.order_no == No).FirstOrDefault();
                    model.order_no     = tem.order_no;
                    model.input_time   = tem.input_time;
                    model.product_code = tem.product_code;
                    model.order_no     = tem.order_no;
                    model.qty          = tem.qty;
                    model.state        = tem.state;
                    model.planned_time = tem.planned_time;
                    model.parent_order = tem.parent_order;
                    model.CO           = tem.CO;
                    model.property     = tem.property;
                    model.urgency      = tem.urgency;
                    model.flow_code    = tem.flow_code;
                    context2.SaveChanges();
                }
                STR = "2";
            }
            return(STR);
        }