ProductionOrderService.cs 29.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Hh.Mes.Service.Material
{
    public class ProductionOrderService : RepositorySqlSugar<bus_pro_plan_head>
    {
        /// <summary>
        /// 初始化
        /// </summary>
        private int initState { get; set; }

        /// <summary>
        /// 已确认
        /// </summary>
        private int confirmState { get; set; }

        public ProductionOrderService()
        {

            initState = (int)EnumProPlan.初始化;
            confirmState = (int)EnumProPlan.已确认;
        }

        #region 列表

        /// <summary>
        /// 获取工单列表
        /// </summary>
        public Response GetProductionOrderList(PageReq pageReq, bus_pro_plan_head entity)
        {
            var result = new Response();
            string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " id desc" : $"{pageReq.field} {pageReq.order} ";
            string sqlWhere = SqlProductionOrderWhere(entity);
            var stringBuilder = new StringBuilder();
            //页码,页数
            //Exel ture 不分页
            if (!entity.Exel && pageReq != null)
            {
                stringBuilder.Append("declare @pageIndex int,@pageSize int,@offset int");
                stringBuilder.AppendLine($"  select @pageIndex={pageReq.page}, @pageSize={pageReq.limit}, @offset=(@pageIndex - 1) * @pageSize");
            }

            stringBuilder.AppendLine($@" select * from bus_pro_plan_head where {sqlWhere} order by {orderBy} ");

            //Exel ture 不分页
            if (!entity.Exel)
            {
                stringBuilder.AppendLine("  offset @offset row fetch next @pageSize row only ");
                stringBuilder.Append($@" select rowTotal= count(*) from bus_pro_plan_head where {sqlWhere}");
            }

            var parameters = new List<SugarParameter>(){
                new SugarParameter("@planCode",entity.planCode),
                new SugarParameter("@planName",entity.planName),
                new SugarParameter("@productCode",entity.productCode),
                new SugarParameter("@productName",entity.productName)
            };

            var ds = base.Context.Ado.GetDataSetAll(stringBuilder.ToString(), parameters);
            result.Result = ds.Tables[0];
            result.Count = entity.Exel ? (int)result.Result.Rows.Count : (int)ds.Tables[1].Rows[0]["rowTotal"];
            return result;
        }

        /// <summary>
        /// 获取工单列表明细
        /// </summary>
        public Response GetProductionOrderDetailList(PageReq pageReq, bus_pro_plan_head entity)
        {
            var result = new Response();
            string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " id asc" : $"{pageReq.field} {pageReq.order} ";
            string sqlWhere = SqlProductionOrderDetailWhere(entity);
            var stringBuilder = new StringBuilder();
            //页码,页数
            //Exel ture 不分页
            if (!entity.Exel && pageReq != null)
            {
                stringBuilder.Append("declare @pageIndex int,@pageSize int,@offset int");
                stringBuilder.AppendLine($"  select @pageIndex={pageReq.page}, @pageSize={pageReq.limit}, @offset=(@pageIndex - 1) * @pageSize");
            }

            stringBuilder.AppendLine($@" select * from bus_pro_plan_detail where {sqlWhere} order by {orderBy} ");

            //Exel ture 不分页
            if (!entity.Exel)
            {
                stringBuilder.AppendLine("  offset @offset row fetch next @pageSize row only ");
                stringBuilder.Append($@" select rowTotal= count(*) from bus_pro_plan_detail where {sqlWhere}");
            }

            var parameters = new List<SugarParameter>(){
                new SugarParameter("@proPlanHeadKeys",entity.keys),
            };

            var ds = base.Context.Ado.GetDataSetAll(stringBuilder.ToString(), parameters);
            result.Result = ds.Tables[0];
            result.Count = entity.Exel ? (int)result.Result.Rows.Count : (int)ds.Tables[1].Rows[0]["rowTotal"];
            return result;
        }

        /// <summary>
        /// 获取工单列表明细Bom
        /// </summary>
        public Response GetProductionOrderDetailBomList(PageReq pageReq, bus_pro_plan_detail entity)
        {
            var result = new Response();
            string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " id asc" : $"{pageReq.field} {pageReq.order} ";
            string sqlWhere = SqlProductionOrderDetailBomWhere();
            var stringBuilder = new StringBuilder();
            //页码,页数
            //Exel ture 不分页
            if (!entity.Exel && pageReq != null)
            {
                stringBuilder.Append("declare @pageIndex int,@pageSize int,@offset int");
                stringBuilder.AppendLine($"  select @pageIndex={pageReq.page}, @pageSize={pageReq.limit}, @offset=(@pageIndex - 1) * @pageSize");
            }

            stringBuilder.AppendLine($@" select * from bus_pro_plan_detail_bom where {sqlWhere} order by {orderBy} ");

            //Exel ture 不分页
            if (!entity.Exel)
            {
                stringBuilder.AppendLine("  offset @offset row fetch next @pageSize row only ");
                stringBuilder.Append($@" select rowTotal= count(*) from bus_pro_plan_detail_bom where {sqlWhere}");
            }

            var parameters = new List<SugarParameter>(){
                new SugarParameter("@proPlanDetailKeys",entity.bodyKeys),
            };

            var ds = base.Context.Ado.GetDataSetAll(stringBuilder.ToString(), parameters);
            result.Result = ds.Tables[0];
            result.Count = entity.Exel ? (int)result.Result.Rows.Count : (int)ds.Tables[1].Rows[0]["rowTotal"];
            return result;
        }


        public string SqlProductionOrderWhere(bus_pro_plan_head model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(" 1=1 ");
            if (!string.IsNullOrEmpty(model.planCode))
            {
                stringBuilder.Append(" and  planCode = @planCode ");
            }
            if (!string.IsNullOrEmpty(model.planName))
            {
                stringBuilder.Append(" and  planName like '%'+@planName+'%' ");
            }
            if (!string.IsNullOrEmpty(model.productCode))
            {
                stringBuilder.Append(" and  productCode = @productCode ");
            }
            if (!string.IsNullOrEmpty(model.productName))
            {
                stringBuilder.Append(" and  productName like '%'+@productName+'%' ");
            }
            //下拉弹出搜索框
            //if (!string.IsNullOrEmpty(model.codeOrName))
            //{
            //    stringBuilder.Append($" and  (t1.materialCode like '%'+@codeOrName+'%' or  t1.materialName like '%'+@codeOrName+'%' )   ");
            //}
            return stringBuilder.ToString();
        }

        public string SqlProductionOrderDetailWhere(bus_pro_plan_head model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(" 1=1 ");
            stringBuilder.Append(" and  proPlanHeadKeys = @proPlanHeadKeys ");
            return stringBuilder.ToString();
        }

        public string SqlProductionOrderDetailBomWhere()
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(" 1=1 ");
            stringBuilder.Append(" and  proPlanDetailKeys = @proPlanDetailKeys ");
            return stringBuilder.ToString();
        }
        #endregion

        /// <summary>
        /// 新增生产计划 工单
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public dynamic Ins(bus_pro_plan_head entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();

                var isExistCount = Context.Queryable<bus_pro_plan_head>().Where(x => x.planCode == entity.planCode).Count();
                if (isExistCount > 0) return response.ResponseError("生产计划 已存在相同的计划号【" + entity.planCode + "】的记录!请勿重复!");
                if (entity.productHeadKeys== Guid.Empty) return response.ResponseError("生产计划 选择的产品名称【" + entity.productName + "】没有关联工艺路线,请重新绑定!");
                if (entity.planEndTime < entity.planStartTime) return response.ResponseError("计划结束时间不能小于计划开始时间!");

                entity.keys = Guid.NewGuid();
                entity.createBy = sysWebUser.Account;
                entity.createTime = DateTime.Now;
                entity.status = initState;

                var materialCodeList = Context.Queryable<base_product_detail>().Where(x=>x.headKeys== entity.productHeadKeys).Select(x => x.materialCode).ToList();
                if (materialCodeList.Count==0) return response.ResponseError("生产计划 选择的产品【" + entity.productName + "】没有产品明细信息!");
                var materialList = Context.Queryable<base_material_rate>().Where(x => materialCodeList.Contains(x.materialCode)).ToList();
                if(materialList.Count==0) return response.ResponseError("生产计划 选择的产品【" + entity.productName + "】明细物料信息【"+ string.Join(",", materialCodeList) + "】没有绑定物料比例,请在物料信息=>编辑物料BOM比例 绑定!");

                Context.Insertable(entity).AddQueue();
                for (int index = 0; index < entity.quantity; index++)
                {
                    var detail = new bus_pro_plan_detail();
                    detail.bodyKeys = Guid.NewGuid();
                    detail.proPlanHeadKeys = entity.keys;
                    detail.workOrderCode = DateTime.Now.ToString("yyyyMMddHHmmss") + getNumber((index + 1).ToString());
                    detail.workOrderName = entity.planName + getNumber((index + 1).ToString());
                    detail.lineCode = entity.lineCode;
                    detail.productCode = entity.productCode;
                    detail.productName = entity.productName;
                    detail.createBy = sysWebUser?.Account;
                    detail.createTime = DateTime.Now;

                    int count = 0;
                    int i = 1;
                    foreach (var material in materialList)
                    {
                        count += material.quantityRate.Value;
                        var bom = new bus_pro_plan_detail_bom();
                        bom.bodyKeys = Guid.NewGuid();
                        bom.proPlanHeadKeys = entity.keys;
                        bom.proPlanDetailKeys = detail.bodyKeys;
                        bom.materialCode = material.materialCode;
                        bom.materialName = material.materialName;
                        bom.oprSequenceCode = material.oprSequenceCode;
                        bom.detailSN = DateTime.Now.ToString("yyyyMMddHHmmss") + getNumber((i++).ToString());
                        bom.spec = material.specifications;
                        bom.unit = material.unitCode;
                        bom.productCode = entity.productCode;
                        bom.quantity = material.quantityRate.Value;
                        bom.createBy = sysWebUser.Account;
                        bom.createTime = DateTime.Now;
                        Context.Insertable(bom).AddQueue();

                        var rel = new bus_pro_plan_detail_bom_rel();
                        rel.proPlanHeadKeys = entity.keys;
                        rel.proPlanDetailBodyKeys = detail.bodyKeys;
                        rel.proPlanDetailBomBodyKeys = bom.bodyKeys;
                        rel.createBy = sysWebUser.Account;
                        rel.createTime = DateTime.Now;
                        Context.Insertable(rel).AddQueue();
                    }
                    detail.quantity = count;
                    Context.Insertable(detail).AddQueue();
                }

                var resultCount = Context.SaveQueues();
                return resultCount > 0 ? response.ResponseSuccess() : response.ResponseError();
            });
        }

        private string getNumber(string index)
        {
            while (index.Length < 5)
            {
                index = "0" + index;
            }
            return index;
        }

        /// <summary>
        /// 更新 生产计划工单
        /// </summary>
        public dynamic Upd(bus_pro_plan_head entity)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var isExistCount = Context.Queryable<bus_pro_plan_head>().Where(x => x.planCode == entity.planCode && x.id != entity.id).Count();
                if (isExistCount > 0)
                {
                    response.ResponseErr("已存在工单为【" + entity.planCode + "】的记录!请勿重复!");
                    return response;
                }

                if (entity.planEndTime < entity.planStartTime)
                {
                    response.ResponseErr("计划结束时间不能小于计划开始时间!");
                    return response;
                }

                entity.updateBy = sysWebUser.Account;
                entity.updateTime = DateTime.Now;
                entity.status = (int)EnumProPlan.初始化;
                response.Status = Context.Updateable(entity).Where(u => u.id == entity.id).ExecuteCommand() > 0;
                if (!response.Status) response.Message = "更新失败";

                if (response.Status)
                {
                    var countResult = Context.Queryable<bus_pro_plan_detail>().Where(x => x.proPlanHeadKeys == entity.keys).Count();
                    if (countResult != entity.quantity)
                    {
                        Context.Deleteable<bus_pro_plan_detail>().Where(x => x.proPlanHeadKeys == entity.keys).AddQueue();
                        Context.Deleteable<bus_pro_plan_detail_bom>().Where(x => x.proPlanHeadKeys == entity.keys).AddQueue();
                        Context.Deleteable<bus_pro_plan_detail_bom_rel>().Where(x => x.proPlanHeadKeys == entity.keys).AddQueue();

                        var productCodeList = Context.Queryable<base_product_header>().Where(x => x.productCode == entity.productCode).Select(x => x.keys).ToList();
                        var materialCodeList = Context.Queryable<base_product_detail>().Where(x => productCodeList.Contains(x.headKeys)).Select(x => x.materialCode).ToList();
                        var materialList = Context.Queryable<base_material_rate>().Where(x => materialCodeList.Contains(x.materialCode)).ToList();

                        if (materialList.Count > 0)
                        {
                            for (int index = 0; index < entity.quantity; index++)
                            {
                                var detail = new bus_pro_plan_detail();
                                detail.bodyKeys = Guid.NewGuid();
                                detail.proPlanHeadKeys = entity.keys;
                                detail.workOrderCode = DateTime.Now.ToString("yyyyMMddHHmmss") + getNumber((index + 1).ToString());
                                detail.workOrderName = entity.planName + getNumber((index + 1).ToString());
                                detail.lineCode = entity.lineCode;
                                detail.productCode = entity.productCode;
                                detail.productName = entity.productName;
                                detail.createBy = sysWebUser?.Account;
                                detail.createTime = DateTime.Now;

                                int count = 0;
                                int i = 1;
                                foreach (var material in materialList)
                                {
                                    count += material.quantityRate.Value;
                                    var bom = new bus_pro_plan_detail_bom();
                                    bom.bodyKeys = Guid.NewGuid();
                                    bom.proPlanHeadKeys = entity.keys;
                                    bom.proPlanDetailKeys = detail.bodyKeys;
                                    bom.materialCode = material.materialCode;
                                    bom.materialName = material.materialName;
                                    bom.oprSequenceCode = material.oprSequenceCode;
                                    bom.detailSN = DateTime.Now.ToString("yyyyMMddHHmmss") + getNumber((i++).ToString());
                                    bom.spec = material.specifications;
                                    bom.unit = material.unitCode;
                                    bom.productCode = entity.productCode;
                                    bom.quantity = material.quantityRate.Value;
                                    bom.createBy = sysWebUser?.Account;
                                    bom.createTime = DateTime.Now;
                                    Context.Insertable(bom).AddQueue();

                                    var rel = new bus_pro_plan_detail_bom_rel();
                                    rel.proPlanHeadKeys = entity.keys;
                                    rel.proPlanDetailBodyKeys = detail.bodyKeys;
                                    rel.proPlanDetailBomBodyKeys = bom.bodyKeys;
                                    rel.createBy = sysWebUser?.Account;
                                    rel.createTime = DateTime.Now;
                                    Context.Insertable(rel).AddQueue();
                                }
                                detail.quantity = count;
                                Context.Insertable(detail).AddQueue();
                            }

                            var resultCount = Context.SaveQueues();
                            return resultCount > 0 ? response.ResponseSuccess() : response.ResponseError();
                        }
                        else
                        {
                            return response.ResponseError("查不到产品物料列表,修改错误!");
                        }
                    }
                }

                return response;
            });
        }

        /// <summary>
        /// 更新 生产计划工单
        /// </summary>
        public dynamic UpdDetail(bus_pro_plan_detail entity)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                entity.updateBy = sysWebUser?.Account;
                entity.updateTime = DateTime.Now;
                response.Status = Context.Updateable(entity).Where(u => u.id == entity.id).ExecuteCommand() > 0;
                if (!response.Status) response.Message = "更新失败";

                return response;
            });
        }

        /// <summary>
        /// 更新 生产计划工单
        /// </summary>
        public dynamic UpdDetailBom(bus_pro_plan_detail_bom entity)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                entity.updateBy = sysWebUser.Account;
                entity.updateTime = DateTime.Now;
                response.Status = Context.Updateable(entity).Where(u => u.id == entity.id).ExecuteCommand() > 0;
                if (!response.Status) response.Message = "更新失败";

                return response;
            });
        }

        /// <summary>
        /// 删除生产计划
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public dynamic DelByIds(int[] ids)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Response response = new Response();
                var dataList = Context.Queryable<bus_pro_plan_head>().Where(x => ids.Contains(x.id)).ToList();
                bool isSuccess = true;
                int isDelete = 0;
                for (int index = 0; index < dataList.Count; index++)
                {
                    if (dataList[index].status == initState)
                    {
                        isDelete++;
                        Context.Deleteable<bus_pro_plan_head>().Where(x => x.keys == dataList[index].keys).AddQueue();
                        Context.Deleteable<bus_pro_plan_detail>().Where(x => x.proPlanHeadKeys == dataList[index].keys).AddQueue();
                        Context.Deleteable<bus_pro_plan_detail_bom>().Where(x => x.proPlanHeadKeys == dataList[index].keys).AddQueue();
                        Context.Deleteable<bus_pro_plan_detail_bom_rel>().Where(x => x.proPlanHeadKeys == dataList[index].keys).AddQueue();
                    }
                    else
                    {
                        isSuccess = false;
                    }
                }
                var resultCount = Context.SaveQueues();
                if (!isSuccess)
                {
                    response.ResponseErr("已确认的无法删除!!!!");
                }
                else if (resultCount > 0)
                {
                    response.ResponseSuccess("删除成功!已删除【" + isDelete + "】条");
                }
                return response;
            });
        }

        public dynamic DelAllByIds(Guid[] ids)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Response response = new Response();
                var dataList = Context.Queryable<bus_pro_plan_head>().Where(x => ids.Contains(x.keys)).ToList();
                bool isSuccess = true;
                int isDelete = 0;
                for (int index = 0; index < dataList.Count; index++)
                {
                    isDelete++;
                    Context.Deleteable<bus_pro_plan_head>().Where(x => x.keys == dataList[index].keys).AddQueue();
                    Context.Deleteable<bus_pro_plan_detail>().Where(x => x.proPlanHeadKeys == dataList[index].keys).AddQueue();
                    Context.Deleteable<bus_pro_plan_detail_bom>().Where(x => x.proPlanHeadKeys == dataList[index].keys).AddQueue();
                    Context.Deleteable<bus_pro_plan_detail_bom_rel>().Where(x => x.proPlanHeadKeys == dataList[index].keys).AddQueue();
                    Context.Deleteable<bus_workOrder_head>().Where(x => x.keys == dataList[index].keys).AddQueue();
                    Context.Deleteable<bus_workOrder_detail>().Where(x => x.headKeys == dataList[index].keys).AddQueue();

                }
                var resultCount = Context.SaveQueues();

                return response.ResponseSuccess("已删除【" + isDelete + "】条");
            });
        }

        /// <summary>
        /// 完成 生产计划工单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public dynamic Complete(bus_pro_plan_head entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                var dataObj = Context.Queryable<bus_pro_plan_head>().Where(x => x.planCode == entity.planCode).First();
                if (dataObj == null)
                {
                    response.ResponseErr("查询不到计划工单!无法完成工单编制!");
                    return response;
                }

                dataObj.status = confirmState;
                Context.Updateable(dataObj).AddQueue();

                //找工序为1的
                var oprSequenceCode = "";
                var oprSequenceName = "";
                var processDetailList = Context.Queryable<base_process_route_detail>().Where(x => x.headkeys == dataObj.processRouteKeys).OrderBy(t => t.oprSequence).ToList();
                if (processDetailList.Count > 0)
                {
                    oprSequenceCode = processDetailList[0].oprSequenceCode;
                    oprSequenceName = processDetailList[0].oprSequenceName;
                }
                //循环工单明细 保存工单
                var planDetailList = Context.Queryable<bus_pro_plan_detail>().Where(x => x.proPlanHeadKeys == dataObj.keys).OrderBy(x => x.workOrderCode, OrderByType.Asc).ToList();
                foreach (var planDetail in planDetailList)
                {
                    var head = new bus_workOrder_head();
                    head.keys = entity.keys;//取生产计划主表的key
                    head.processHeadKeys = dataObj.processRouteKeys;
                    head.productHeaderCode = dataObj.productCode;
                    head.productName = dataObj.productName;
                    head.orderType = "3";//通过生产计划生成
                    head.lineCode = dataObj.lineCode;
                    head.nowOprSequenceCode = oprSequenceCode;
                    head.workOrderCode = dataObj.planCode;
                    head.planCode = planDetail.workOrderCode;
                    head.state = (int)EnumOrderHeadStatus.初始化;
                    head.planStartTime = dataObj.planStartTime;
                    head.planEndTime = dataObj.planEndTime;
                    head.isDelete = (int)EnumDeleteOrAdd.新增或更新;
                    head.projectCode = dataObj.projectCode;
                    head.projectName = dataObj.projectName;
                    head.lineNo = dataObj.lineNo;
                    head.edition = dataObj.edition;
                    head.factoryCode = dataObj.factoryCode;
                    head.createBy = sysWebUser?.Account;
                    head.createTime = DateTime.Now;
                    Context.Insertable(head).AddQueue();

                    //保存工单明细
                    foreach (var processDetail in processDetailList)
                    {
                        var list = Context.Queryable<base_work_center_station_rel>().Where(x => x.workCenterCode.Equals(processDetail.workCenterCode)).ToList();
                        var i = 0;
                        var bomList = Context.Queryable<bus_pro_plan_detail_bom>().Where(x => x.proPlanDetailKeys == planDetail.bodyKeys && x.oprSequenceCode == processDetail.oprSequenceCode).ToList();
                        if (bomList.Count == 0)
                        {
                            response.ResponseErr("工序【" + processDetail.oprSequenceCode + "】无法匹配,请确定是否配置正确!");
                            return response;
                        }
                        foreach (var bomObject in bomList)
                        {
                            for (int index = 0; index < bomObject.quantity; index++)
                            {
                                var detail = new bus_workOrder_detail();
                                detail.bodyKeys = Guid.NewGuid();
                                detail.headKeys = head.keys;
                                detail.serialNumber = processDetail.oprSequence;
                                detail.lineCode = head.lineCode;
                                detail.oprSequenceCode = processDetail.oprSequenceCode;
                                detail.oprSequenceName = processDetail.oprSequenceName;
                                detail.state = (int)EnumOrderBodyStatus.初始化;
                                detail.isRework = false;
                                detail.planStartTime = dataObj.planStartTime;
                                detail.planEndTime = dataObj.planEndTime;
                                detail.productHeaderCode = dataObj.productCode;
                                detail.isDelete = (int)EnumDeleteOrAdd.新增或更新;
                                detail.workOrderCode = head.workOrderCode;
                                detail.barCode = GetBarCode();
                                detail.designUrl = dataObj.designUrl;
                                detail.designNo = dataObj.designNo;
                                if (list.Count == 1)
                                {
                                    detail.stationCode = list[0].workStationCode;
                                }
                                detail.workReportStatus = (int)EnumWorkReportStatus.未报工;
                                detail.workCenterCode = processDetail.workCenterCode;
                                detail.createBy = sysWebUser?.Account;
                                detail.createTime = DateTime.Now;

                                detail.cutMaterCode = bomObject.materialCode;
                                detail.weldMaterCode = bomObject.materialCode;
                                detail.serialNumberName = processDetail.oprSequence.ToString() + "-" + (++i);

                                Context.Insertable(detail).AddQueue();
                            }
                        }
                    }
                }

                var resultCount = Context.SaveQueues();
                return resultCount > 0 ? response.ResponseSuccess() : response.ResponseError();
            });
        }

        public string GetBarCode()
        {
            var id = Context.Queryable<bus_identify_config>().Max(x => x.id);
            if (id == 0)
            {
                //第一次加载 添加一行数据 默认从1001开始
                Context.Insertable(new bus_identify_config { id = 1001 }).ExecuteCommand();
                id = Context.Queryable<bus_identify_config>().Max(x => x.id);//取值
            }
            return id.ToString();
        }
    }
}