Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        //提交操作
        private void btn_reback_Click(object sender, EventArgs e)
        {
            ServiceReference.ServiceClient client = new ServiceReference.ServiceClient();
            try
            {
                if (string.IsNullOrWhiteSpace(richTextBox_faildesc.Text.Trim()))
                {
                    MessageBox.Show("请给出维修说明,在记录中填写", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (string.IsNullOrWhiteSpace(kComboBox_Process.Text.Trim()))
                {
                    MessageBox.Show("请选择责任工序", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                //修改数据库信息,1.P_SFC_State(如果P_SFC_State表中存在记录才修改),
                //更新P_SFC_State 当前工序和过站时间 +P_SFC_Process_IOLog 当前工序
                int    ishebeifail = 0;
                string str         = "";
                List <P_Fail_Detail> lstpFailDetail = new List <P_Fail_Detail>();
                for (int i = 0; i < dataGridView_faildetail.Rows.Count; i++)
                {
                    P_Fail_Detail onepfaildetail = new P_Fail_Detail();
                    onepfaildetail.id = int.Parse(dataGridView_faildetail.Rows[i].Cells["detail_id"].Value.ToString());
                    if ((this.dataGridView_faildetail.Rows[i].Cells["failReasoncode"] as DataGridViewComboBoxCell).Value == null)
                    {
                        MessageBox.Show("原因代码为空,请选择", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    onepfaildetail.reason_code = dicReason[(string)(this.dataGridView_faildetail.Rows[i].Cells["failReasoncode"] as DataGridViewComboBoxCell).Value];
                    if (onepfaildetail.reason_code.Contains("EEE"))  //判断是否为设备不良原因代码
                    {
                        ishebeifail = 1;
                    }
                    if ((this.dataGridView_faildetail.Rows[i].Cells["failtype"] as DataGridViewComboBoxCell).Value == null)
                    {
                        MessageBox.Show("原因类型代码为空,请选择", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    onepfaildetail.reasontype_code = dicTypeReason[(string)(this.dataGridView_faildetail.Rows[i].Cells["failtype"] as DataGridViewComboBoxCell).Value];
                    lstpFailDetail.Add(onepfaildetail);
                }
                //order,sfc,nowprocess,state,failtimes,pass,grade_id,grade_type,iofailtimes
                if (checkBox1.Checked == true)
                {
                    if (checkedListBox1.CheckedItems.Count > 0)
                    {
                        for (int items = 0; items < checkedListBox1.CheckedItems.Count; items++)
                        {
                            string judecode     = ((ComboboxItem)checkedListBox1.CheckedItems[items]).Value;
                            string judename     = ((ComboboxItem)checkedListBox1.CheckedItems[items]).Text;
                            string SFC          = textBox_sfc.Text.ToString();
                            string JudeCodeJson = client.RunServerAPI("BLL.SFC", "GetJudeSFC", judecode + "," + SFC);
                            if (string.IsNullOrEmpty(JudeCodeJson))
                            {
                                P_SFC_Jude sfc_jude = new P_SFC_Jude()
                                {
                                    jude_code = judecode,
                                    jude_name = judename,
                                    sfc       = SFC,
                                    state     = 0
                                };
                                string judejson = JsonToolsNet.ObjectToJson(sfc_jude);
                                client.RunServerAPI("BLL.SFC", "InsertSFCJude", judejson);
                            }
                            else
                            {
                                MessageBox.Show("该批次已存在[ " + judename + " ]的待判,不可重复", "提示");
                                return;
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("请勾选待判名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                string      strJson        = "";
                P_SFC_State clpsfcstateTmp = new P_SFC_State();
                clpsfcstateTmp.order_no   = strorderno;
                clpsfcstateTmp.SFC        = strsfc;
                clpsfcstateTmp.fail_times = 0;
                if (iRepairProcc == 1)  //降级
                {
                    if (string.IsNullOrWhiteSpace(kComboBox_ProductGrade.Text.Trim()))
                    {
                        MessageBox.Show("请先设置级别", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (string.IsNullOrWhiteSpace(kComboBox_rebackprocess.Text.Trim()))
                    {
                        MessageBox.Show("请先设置返回工序", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    clpsfcstateTmp.from_process = strfromprocess_code;
                    clpsfcstateTmp.now_process  = diczrprocess[this.kComboBox_rebackprocess.Text.ToString().Trim()];
                    if (clpsfcstateTmp.now_process == "END")
                    {
                        clpsfcstateTmp.state = 2;//该批次已完成,可以流向下一工序
                    }
                    else
                    {
                        clpsfcstateTmp.state = 1;  //设置状态为 1 ,未完成,,正常加工
                    }

                    B_Product_Grade clBProductGrade = new B_Product_Grade();
                    clBProductGrade           = dicProductGrade[kComboBox_ProductGrade.Text.Trim()];
                    clpsfcstateTmp.grade_id   = clBProductGrade.grade_id.ToString();
                    clpsfcstateTmp.grade_type = clBProductGrade.grade_type;
                    //order,sfc,nowprocess,state,failtimes,pass,grade_id,grade_type,iofailtimes
                    strJson = strorderno + "," + strsfc + "," + diczrprocess[this.kComboBox_rebackprocess.Text.ToString().Trim()]
                              + "," + "1" + "," + "0" + "," + "0" + "," + clBProductGrade.grade_id + "," + clBProductGrade.grade_type + "," + "0" + "," + strfromprocess_code;
                }
                else if (iRepairProcc == 2) //待判
                {
                    //待完善???
                }
                else if (iRepairProcc == 3)    //报废
                {
                    clpsfcstateTmp.state = -1; //设置状态为 -1 ,已报废

                    clpsfcstateTmp.now_process = "END";
                    //order,sfc,nowprocess,state,failtimes,pass,grade_id,grade_type,iofailtimes
                    strJson = strorderno + "," + strsfc + "," + "END" + "," + "-1" + "," + "0" + "," + "0" + "," + "grade_id" + "," + "grade_type" + "," + "0" + "," + strfromprocess_code;
                }
                else  //不做操作,直接返回指定工序
                {
                    if (string.IsNullOrWhiteSpace(kComboBox_rebackprocess.Text.Trim()))
                    {
                        MessageBox.Show("请先设置返回工序", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    clpsfcstateTmp.state       = 1; //设置状态为 1 ,未完成,,正常加工
                    clpsfcstateTmp.now_process = diczrprocess[this.kComboBox_rebackprocess.Text.ToString().Trim()];
                    //order,sfc,nowprocess,state,failtimes,pass,grade_id,grade_type,iofailtimes
                    strJson = strorderno + "," + strsfc + "," + diczrprocess[this.kComboBox_rebackprocess.Text.ToString().Trim()]
                              + "," + "1" + "," + "0" + "," + "0" + "," + "grade_id" + "," + "grade_type" + "," + "0" + "," + strfromprocess_code;
                }

                str = client.RunServerAPI("BLL.SFC", "UpDataSFCInfoAndSFCIOLogData", strJson);
                if (!str.Contains("OK"))
                {
                    MessageBox.Show("更新P_SFC_State 失败 Or P_SFC_Process_IOLog 失败," + str, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                //更新维修结果  P_FailLog  P_Fail_Detail
                P_FailLog clpfaillog = new P_FailLog();
                clpfaillog.fid = int.Parse(strfid);
                if (!string.IsNullOrWhiteSpace(kComboBox_Process.Text.Trim()))
                {
                    clpfaillog.process_code     = strfromprocess_code;
                    clpfaillog.Disposal_Process = strfromprocess_code;
                    #region
                    //if (diczrprocess.Keys.Contains(kComboBox_Process.Text.Trim()))
                    //{
                    //    clpfaillog.process_code = diczrprocess[kComboBox_Process.Text.Trim()];
                    //}
                    //else
                    //{
                    //    MessageBox.Show("请选择责任工序!");
                    //    return;
                    //}
                    //clpfaillog.Disposal_Process = diczrprocess[kComboBox_Process.Text.Trim()];
                    #endregion
                    //根据责任工序,找到责任人和工位
                    strJson = "";
                    strJson = strsfc + "," + clpfaillog.process_code;
                    str     = client.RunServerAPI("BLL.SFC", "GetSFCIOLogInfo", strJson);
                    if (string.IsNullOrWhiteSpace(str))
                    {
                        //
                    }
                    else
                    {
                        List <P_SFC_Process_IOLog> lstsfciolog = JsonConvert.DeserializeObject <List <P_SFC_Process_IOLog> >(str);
                        if (lstsfciolog.Count > 0)
                        {
                            clpfaillog.emp_code = lstsfciolog[0].emp_code;
                        }
                        if (ishebeifail == 1) //存在设备不良,需要根据工位代码找到设备代码
                        {
                            //根据工位,找到设备
                            str = client.RunServerAPI("BLL.Station", "GetStationEquipmentByStation", lstsfciolog[0].station_code);
                            if (string.IsNullOrWhiteSpace(str))
                            {
                                //
                            }
                            else
                            {
                                List <B_StationMachine> lststationequipment = JsonConvert.DeserializeObject <List <B_StationMachine> >(str);
                                if (lstsfciolog.Count > 0)
                                {
                                    clpfaillog.equipment_code = lststationequipment[0].machine_code;
                                }
                            }
                        }
                    }
                }
                if (iRepairProcc == 1) //降级
                {
                    clpfaillog.state = 1;
                }
                else if (iRepairProcc == 2) //待判
                {
                    //待完善???
                }
                else if (iRepairProcc == 3) //报废
                {
                    clpfaillog.state = 9;   //报废状态
                }
                else
                {
                    clpfaillog.state = 1;
                }
                if (!string.IsNullOrWhiteSpace(richTextBox_faildesc.Text.Trim()))
                {
                    clpfaillog.repair_remark = richTextBox_faildesc.Text.Trim();
                }
                str = "";
                str = client.RunServerAPI("BLL.Faillog", "UpdateFailLog", JsonConvert.SerializeObject(clpfaillog));
                if (!str.Contains("1"))
                {
                    MessageBox.Show("更新P_FailLog 失败" + str, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                str = "";
                str = client.RunServerAPI("BLL.Fail_Detail", "UpdateFail_Detail", JsonConvert.SerializeObject(lstpFailDetail));
                if (!str.Contains("1"))
                {
                    MessageBox.Show("更新P_Fail_Detail 失败" + str, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("提交失败:" + exp.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //提交完成
            MessageBox.Show("维修完成", "OK", MessageBoxButtons.OK, MessageBoxIcon.None);
            this.Close();
        }