UpstreamSendWorkOrder.cs 22.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
using Hh.Mes.Common.Json;
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.ApiEntity;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Request;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.WebEntity.bus;
using Hh.Mes.Service.Logs;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;


namespace Hh.Mes.Service.ApiService
{
    public partial class UpstreamService
    {
        #region 属性
        /// <summary>
        /// 产品表
        /// </summary>
        private base_product_header Sys_ProductHeader { get; set; }

        /// <summary>
        /// 工作中心映射华恒线体code
        /// </summary>
        private string Sys_LineCode { get; set; }

        /// <summary>
        /// 工艺路线头
        /// </summary>
        private base_process_route_head Sys_ProcessHead { get; set; }

        /// <summary>
        /// 工艺路线明细
        /// </summary>
        private List<base_process_route_detail> Sys_ProcessDetail { get; set; }

        /// <summary>
        /// 输出返回实体
        /// </summary>
        private ResponseUpstream<string> ResponseOrder { get; set; }

        /// <summary>
        /// Json 接受实体
        /// </summary>
        private MomWorkOrderHead MomEntity { get; set; }

        #region 切割、组队、焊接工序
        /// <summary>
        /// 切割工序
        /// </summary>
        private base_process_route_detail Sys_process_cut { get; set; }

        /// <summary>
        /// 组队工序
        /// </summary>
        private base_process_route_detail Sys_process_fitUp { get; set; }

        /// <summary>
        /// 焊接工序
        /// </summary>
        private base_process_route_detail Sys_process_weld { get; set; }
        #endregion

        //工序任务 管段号 同一组工序明细的值 一样
        private string Part_Code { get; set; }

        /// <summary>
        /// 更新数据使用 缓存工序任务头变量
        /// </summary>
        private bus_workOrder_head busWkHead { get; set; }

        /// <summary>
        /// 订单类型名称
        /// </summary>
        private string proOrderTypeName { get; set; }
		#endregion

		public dynamic SendWorkOrder(MomWorkOrderHead requestData)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                this.MomEntity = requestData;
                MomWorkOrderHead entity = requestData;
                var plmeid = entity == null ? "" : entity.plmeid;
                ResponseOrder = new ResponseUpstream<string>(plmeid);
				proOrderTypeName = Enum.GetName(typeof(EnumProOrderType), int.Parse(MomEntity.order_type));
				if (!typeValidation(MomEntity.type, ResponseOrder).Status) return ResponseOrder;

				ResponseOrder = HandleAcceptOrderHeadBefore();
                if (!ResponseOrder.Status)
                {
                    QueueInterLog.GetInstance.EnqueueInterLog(null, null, EnumLog.MOM订单下发失败.ToString(), ResponseOrder.Message, 0, user: "AOP", sysTitle: EnumLog.MOM业务问题.ToString());
                    return ResponseOrder;
                }
                //生成订单数据
                var orderDataResult = new ValueTuple<bus_workOrder_head, List<bus_workOrder_detail>>(null, null);
                var proOrder = (int)EnumProOrderType.正常订单;

                if (proOrder.ToString() == requestData.order_type)
                {
                    ResponseOrder = HandleAcceptOrderBodyBefore();
                    if (!ResponseOrder.Status)
                    {
                        QueueInterLog.GetInstance.EnqueueInterLog(null, null, EnumLog.MOM订单下发失败.ToString(), ResponseOrder.Message, 0, user: "AOP", sysTitle: EnumLog.MOM业务问题.ToString());
                        return ResponseOrder;
                    }
                    //生成订单数据
                    orderDataResult = HandleAcceptOrderData();
                }
                else
                {
                    orderDataResult = HandleAcceptOhterOrderData();
                }
				if (!ResponseOrder.Status)
				{
					QueueInterLog.GetInstance.EnqueueInterLog(null, null, EnumLog.MOM订单下发失败.ToString(), ResponseOrder.Message, 0, user: "AOP", sysTitle: EnumLog.MOM业务问题.ToString());
					return ResponseOrder;
				}
				//新增生产清单记录
				AddApiWorkOrder(entity);
                //设置 BarCode 管段条码
                ResponseOrder = SetBarCode(orderDataResult.Item2, ResponseOrder);

                if (!ResponseOrder.Status)
                {
                    QueueInterLog.GetInstance.EnqueueInterLog(null, null, EnumLog.MOM订单下发失败.ToString(), ResponseOrder.Message, 0, user: "AOP", sysTitle: EnumLog.MOM业务问题.ToString());
                    return ResponseOrder;
                }

                //保存数据
                var saveResult = SaveHandleAcceptOrderData(orderDataResult.Item1, orderDataResult.Item2);

                return saveResult;
            });
        }


		/// <summary>
		/// 新增 生产清单记录
		/// </summary>
		private void AddApiWorkOrder(MomWorkOrderHead entity)
        {
            var head = new api_workOrder_head
            {
                keys = Guid.NewGuid(),
                plmeId = entity.plmeid,
                workOrderCode = entity.pro_order_code,
                planCode = entity.plan_code,
                productCode = entity.product_code,
                productName = entity.product_name,

                factoryCode = entity.factory_code,
                lineCode = entity.line_code,
                orderType = entity.order_type,
                edition = entity.edition,
                projectCode = entity.project_code,
                projectName = entity.project_name,

                lineNo = entity.line_no,

                planStartTime = entity.oprplan_start_time,
                planEndTime = entity.oprplan_end_time,

                extend1 = entity.extend1,
                extend2 = entity.extend2,
                extend3 = entity.extend3,
                extend4 = entity.extend4,
                isDelete = AddOrUpdateFlag,
                createTime = DateTime.Now,
                createBy = SystemVariable.DefaultCreated
            };

            var details = new List<api_workOrder_detail>();
            foreach (var item in entity.details)
            {
                foreach (var itemCom in item.componentDetails)
                {
                    var detail = new api_workOrder_detail();
                    detail.headKeys = head.keys;
                    detail.oprSequenceCode = item.proced_code;
                    detail.isEndProduct = item.isend_product;
                    detail.extend1 = item.extend1;
                    detail.extend2 = item.extend2;
                    detail.createTime = DateTime.Now;
                    detail.createBy = SystemVariable.DefaultCreated;
                    if (string.IsNullOrEmpty(Part_Code)) Part_Code = itemCom.part_code;
                    detail.cutMaterCode = itemCom.cut_mater_code;
                    detail.cuttingLength = itemCom.cutting_len;

                    detail.weldType = itemCom.weld_type;
                    detail.weldNo = itemCom.weld_no;
                    detail.weldMaterCode = itemCom.weld_mater_code;

                    detail.extendComp1 = itemCom.extendComp1;
                    detail.extendComp2 = itemCom.extendComp2;
                    detail.extendComp3 = itemCom.extendComp3;
                    detail.extendComp4 = itemCom.extendComp4;
                    details.Add(detail);
                }

            }
            Context.Insertable(head).AddQueue();
            Context.Insertable(details).AddQueue();
        }

        /// <summary>
        /// 生成数据
        /// </summary>
        private ValueTuple<bus_workOrder_head, List<bus_workOrder_detail>> HandleAcceptOrderData()
        {
            #region  生成工序任务头 bus_workOrder_head
            var busWorkOrderHead = new bus_workOrder_head
            {
                keys = Guid.NewGuid(),
                plmeId = MomEntity.plmeid,
                workOrderCode = MomEntity.pro_order_code,
                workOrderName = MomEntity.pro_order_code + "_订单名称",
                planCode = MomEntity.plan_code,
                processHeadKeys = Sys_ProcessHead.keys,//工艺路线keys
                processCode = Sys_ProductHeader.processCode,        //工艺路线编码
                productHeaderCode = MomEntity.product_code,     //产品编码
                productName = MomEntity.product_name,

                factoryCode = MomEntity.factory_code,
                lineCode = Sys_LineCode,
                orderType = MomEntity.order_type,
                edition = MomEntity.edition,

                projectCode = MomEntity.project_code,   //项目编码
                projectName = MomEntity.project_name,
                lineNo = MomEntity.line_no,               //管线号/图纸号

                planStartTime = MomEntity.oprplan_start_time,
                planEndTime = MomEntity.oprplan_end_time,

                extend1 = MomEntity.extend1,
                extend2 = MomEntity.extend2,
                extend3 = MomEntity.extend3,
                extend4 = MomEntity.extend4,

                otherOrderCode = MomEntity.plan_code,
                state = (int)EnumOrderHeadStatus.初始化,
                isDelete = AddOrUpdateFlag,
                createTime = DateTime.Now,
                createBy = SystemVariable.DefaultCreated
            };
            #endregion

            #region  生成工序任务明细 busWorkOrderDetailList
            var busWorkOrderDetailList = new List<bus_workOrder_detail>();
            foreach (var item in Sys_ProcessDetail)
            {
                if (item.oprSequenceCode == Sys_process_cut.oprSequenceCode)
                {
                    SetWorkOrderDetailCut(busWorkOrderDetailList, item, busWorkOrderHead);
                }
                else if (item.oprSequenceCode == Sys_process_fitUp.oprSequenceCode)
                {
                    SetWorkOrderDetailFitUpOrWeld(busWorkOrderDetailList, item, busWorkOrderHead);
                }
                else if (item.oprSequenceCode == Sys_process_weld.oprSequenceCode)
                {
                    SetWorkOrderDetailFitUpOrWeld(busWorkOrderDetailList, item, busWorkOrderHead);
                }
                else
                {
                    var jsonMomItem = MomEntity.details.FirstOrDefault(x => x.proced_code == item.oprSequenceCode);
                    for (int i = 0; i < jsonMomItem.componentDetails.Count; i++)
                    {
                        var nowWorkDetail = new bus_workOrder_detail
                        {
                            headKeys = busWorkOrderHead.keys,
                            productHeaderCode = busWorkOrderHead.productHeaderCode,
                            workOrderCode = busWorkOrderHead.workOrderCode,

                            cutMaterCode = jsonMomItem.componentDetails[i].cut_mater_code,
                            cuttingLength = (decimal)jsonMomItem.componentDetails[i].cutting_len,

                            lineCode = Sys_LineCode,
                            designNo = jsonMomItem.componentDetails[i].design_no,
                            designUrl = jsonMomItem.componentDetails[i].design_url,
                            partCode = Part_Code,

                            serialNumber = item.oprSequence,
                            oprSequenceCode = item.oprSequenceCode,
                            oprSequenceName = item.oprSequenceName,
                            workCenterCode = item.workCenterCode,
                            planStartTime = jsonMomItem.oprplan_start_time,
                            planEndTime = jsonMomItem.oprplan_end_time,

                            isEndProduct = jsonMomItem.isend_product,
                            isDelete = AddOrUpdateFlag,
                            state = (int)EnumOrderBodyStatus.初始化,
                            workReportStatus = (int)EnumWorkReportStatus.未报工,
                            createTime = DateTime.Now,
                            createBy = SystemVariable.DefaultCreated
                        };
                        nowWorkDetail.bodyKeys = Guid.NewGuid();
                        nowWorkDetail.serialNumberName = nowWorkDetail.serialNumber + "_1";
                        busWorkOrderDetailList.Add(nowWorkDetail);
                    }
                }
            }

            #endregion

            // 设置工序任务头的当前工序,为第一个工序任务明细的工序
            if (busWorkOrderDetailList.Count > 0)
            {
                busWorkOrderHead.nowOprSequenceCode = busWorkOrderDetailList[0].oprSequenceCode;
            }
            return (busWorkOrderHead, busWorkOrderDetailList);
        }

        //生成管段条码 
        public ResponseUpstream<string> SetBarCode(List<bus_workOrder_detail> details, ResponseUpstream<string> response)
        {
            var barCodeList = new List<string>();
            for (int i = 0; i < Sys_ProcessDetail.Count; i++)
            {
                var oprSequenceCode = Sys_ProcessDetail[i].oprSequenceCode;
                var oprList = details.Where(x => x.oprSequenceCode == oprSequenceCode).ToList();
                //第一个工序
                if (i == 0)
                {
                    for (int j = 0; j < oprList.Count; j++)
                    {
                        details[j].barCode = base.GetWorkOrderBarCode();
                        barCodeList.Add(details[j].barCode);
                    }
                }
                else
                {
                    //if (barCodeList.Count != oprList.Count)
                    //{
                    //    response.ResponseError($"【上位系统】订单下发:工艺路线工序【{oprSequenceCode}】个数【{barCodeList.Count}】和工序任务明细个数【{oprList.Count}】不一致,请检查!");
                    //    return ResponseOrder;
                    //}

                    for (int j = 0; j < oprList.Count; j++)
                    {
                        if (barCodeList.Count > j) {
                            var index = details.FindIndex(x => x.bodyKeys == oprList[j].bodyKeys);
                            details[index].barCode = barCodeList[j];
                        }
                    }
                }
            }

            return response;
        }

        /// <summary>
        /// 切割工序 工序任务设置值
        /// </summary>
        public void SetWorkOrderDetailCut(List<bus_workOrder_detail> busWorkOrderDetailList, base_process_route_detail oprDetail, bus_workOrder_head head)
        {
            var cutOprList = MomEntity.details.Where(x => x.proced_code == oprDetail.oprSequenceCode).ToList();
            var serialNumberNameIndex = 1;
            foreach (var itemCut in cutOprList)
            {
                for (int i = 0; i < itemCut.componentDetails.Count; i++)
                {
                    var nowWorkDetail = new bus_workOrder_detail
                    {
                        headKeys = head.keys,
                        bodyKeys = Guid.NewGuid(),
                        productHeaderCode = head.productHeaderCode,
                        workOrderCode = head.workOrderCode,
                        lineCode = Sys_LineCode,

                        designNo = itemCut.componentDetails[i].design_no,
                        designUrl = itemCut.componentDetails[i].design_url,
                        partCode = Part_Code,

                        serialNumber = oprDetail.oprSequence,
                        oprSequenceCode = oprDetail.oprSequenceCode,
                        oprSequenceName = oprDetail.oprSequenceName,
                        workCenterCode = oprDetail.workCenterCode,
                        planStartTime = itemCut.oprplan_start_time,
                        planEndTime = itemCut.oprplan_end_time,

                        isDelete = AddOrUpdateFlag,
                        state = (int)EnumOrderBodyStatus.初始化,
                        workReportStatus = (int)EnumWorkReportStatus.未报工,
                        createTime = DateTime.Now,
                        createBy = SystemVariable.DefaultCreated
                    };

                    nowWorkDetail.isEndProduct = itemCut.isend_product;
                    nowWorkDetail.serialNumberName = oprDetail.oprSequence + "_" + serialNumberNameIndex++;
                    nowWorkDetail.cutMaterCode = itemCut.componentDetails[i].cut_mater_code;
                    nowWorkDetail.cuttingLength = (decimal)itemCut.componentDetails[i].cutting_len;

                    nowWorkDetail.extend1 = itemCut.componentDetails[i].extend1;
                    nowWorkDetail.extend2 = itemCut.componentDetails[i].extend2;
                    nowWorkDetail.extendComp1 = itemCut.componentDetails[i].extendComp1;
                    nowWorkDetail.extendComp2 = itemCut.componentDetails[i].extendComp2;
                    nowWorkDetail.extendComp3 = itemCut.componentDetails[i].extendComp3;
                    nowWorkDetail.extendComp4 = itemCut.componentDetails[i].extendComp4;
                    busWorkOrderDetailList.Add(nowWorkDetail);
                }
            }

        }

        /// <summary>
        /// 组队或者焊接工序 工序任务设置值
        /// </summary>
        public void SetWorkOrderDetailFitUpOrWeld(List<bus_workOrder_detail> busWorkOrderDetailList, base_process_route_detail oprDetail, bus_workOrder_head head)
        {
            var oprList = MomEntity.details.Where(x => x.proced_code == oprDetail.oprSequenceCode).ToList();
            foreach (var item in oprList)
            {
                for (int i = 0; i < item.componentDetails.Count; i++)
                {
                    //焊接工序物料
                    var weldNoList = item.componentDetails[i].weld_no.Split(',', StringSplitOptions.RemoveEmptyEntries);
                    var weldMaterCodeList = item.componentDetails[i].weld_mater_code.Split(',', StringSplitOptions.RemoveEmptyEntries);
                    for (int xp = 0; xp < weldNoList.Length; xp++)
                    {
                        var nowWorkDetail = new bus_workOrder_detail
                        {
                            headKeys = head.keys,
                            bodyKeys = Guid.NewGuid(),
                            productHeaderCode = head.productHeaderCode,
                            workOrderCode = head.workOrderCode,

                            lineCode = Sys_LineCode,
                            designNo = item.componentDetails[i].design_no,
                            designUrl = item.componentDetails[i].design_url,
                            partCode = Part_Code,

                            serialNumber = oprDetail.oprSequence,
                            oprSequenceCode = oprDetail.oprSequenceCode,
                            oprSequenceName = oprDetail.oprSequenceName,
                            workCenterCode = oprDetail.workCenterCode,
                            planStartTime = item.oprplan_start_time,
                            planEndTime = item.oprplan_end_time,
                            isEndProduct = item.isend_product,

                            isDelete = AddOrUpdateFlag,
                            state = (int)EnumOrderBodyStatus.初始化,
                            workReportStatus = (int)EnumWorkReportStatus.未报工,
                            createTime = DateTime.Now,
                            createBy = SystemVariable.DefaultCreated,

                            weldType = item.componentDetails[i].weld_type
                        };

                        nowWorkDetail.serialNumberName = oprDetail.oprSequence + "_" + (xp + 1);
                        nowWorkDetail.weldNo = weldNoList[xp];
                        nowWorkDetail.weldMaterCode = weldMaterCodeList[xp];
                        nowWorkDetail.cutMaterCode = item.componentDetails[i].cut_mater_code;

                        nowWorkDetail.extend1 = item.componentDetails[i].extend1;
                        nowWorkDetail.extend2 = item.componentDetails[i].extend2;
                        nowWorkDetail.extendComp1 = item.componentDetails[i].extendComp1;
                        nowWorkDetail.extendComp2 = item.componentDetails[i].extendComp2;
                        nowWorkDetail.extendComp3 = item.componentDetails[i].extendComp3;
                        nowWorkDetail.extendComp4 = item.componentDetails[i].extendComp4;
                        busWorkOrderDetailList.Add(nowWorkDetail);
                    }

                }
            }

        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private ResponseUpstream<string> SaveHandleAcceptOrderData(bus_workOrder_head busWorkOrderHead, List<bus_workOrder_detail> busWorkOrderDetailList)
        {
            int resultCount = 0;
            if (busWorkOrderDetailList == null || busWorkOrderDetailList.Count == 0)
            {
                ResponseOrder.ResponseError($"【上位系统】订单下发:工序任务明细生成失败,请确认请先发送工艺路线接口数据,反复出现请联系管理员!");
                return ResponseOrder;
            }

            //新增
            if (MomEntity.type == EnumAction.I.ToString())
            {
                Context.Insertable(busWorkOrderHead).AddQueue();
                Context.Insertable(busWorkOrderDetailList).AddQueue();
            }
            //更新
            else if (MomEntity.type == EnumAction.U.ToString())
            {
                busWorkOrderHead.keys = busWkHead.keys;
                busWorkOrderHead.updateTime = DateTime.Now;
                busWorkOrderDetailList.ForEach(x =>
                {
                    x.headKeys = busWkHead.keys;
                    x.updateTime = DateTime.Now;
                });

                Context.Updateable(busWorkOrderHead)
                       .IgnoreColumns(x => new { x.createBy, x.createTime, x.keys })
                       .Where(x => x.keys == busWorkOrderHead.keys && x.isDelete == AddOrUpdateFlag).AddQueue();
                Context.Updateable(busWorkOrderDetailList)
                       .IgnoreColumns(x => new { x.createBy, x.createTime, x.bodyKeys, x.headKeys })
                       .Where(x => x.bodyKeys == busWorkOrderHead.keys && x.isDelete == AddOrUpdateFlag).AddQueue();
            }

            resultCount = ExecuteQueues(base.Context);
            return (ResponseUpstream<string>)(resultCount > 0 ? ResponseOrder.ResponseSuccess() : ResponseOrder.ResponseError());
        }
    }
}