WorkOrderJobService.cs 24.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
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.Service.Repository;
using NetTaste;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Hh.Mes.Service.QuartzJobService
{
    public class WorkOrderJobService : RepositorySqlSugar<base_work_order_head>
    {
        public void Execute()
        {
            Log4NetHelper.Instance.Info("开始生成工单任务");
            var orderTaskList = base.Context.Queryable<base_work_order_head>().Where(t => t.processStatus == ((int)EnumWorkOrderProessStatus.未生成).ToString()).OrderBy(x => x.createTime,SqlSugar.OrderByType.Asc).ToList();
            Log4NetHelper.Instance.Info($"待处理工单数量:{orderTaskList.Count}");

            if (orderTaskList.Count>0)
            {
                foreach (var orderTask in orderTaskList)
                {
                    string outMsg =string.Empty;
                    if (CreateProjectAndWorkOrder(orderTask,ref outMsg))
                    {
                        // 条码和喷码
                        string barCode = orderTask.pipePartsCode + "-" + orderTask.paintCode;
                        string paintPrintCode = GetPaintPrintCode(orderTask);
                        Context.Updateable<base_work_order_head>().SetColumns(x =>x.processStatus == ((int)EnumWorkOrderProessStatus.生成成功).ToString())
                            .SetColumns(x=> x.updateTime==DateTime.Now)
                            .SetColumns(x => x.processMsg == outMsg)
                            .SetColumns(x => x.barCode == barCode)
                            .SetColumns(x => x.paintPrintCode == paintPrintCode) 
                            .SetColumns(x=> x.updateby == "Api")
                            .Where(t => t.keys  ==orderTask.keys)
                            .ExecuteCommand();
                    }
                    else
                    {
                        Context.Updateable<base_work_order_head>().SetColumns(x => x.processStatus == ((int)EnumWorkOrderProessStatus.生成失败).ToString())
                            .SetColumns(x => x.updateTime == DateTime.Now)
                            .SetColumns(x => x.processMsg == outMsg)
                            .SetColumns(x => x.updateby == "Api")
                            .WhereColumns(t => t.keys == orderTask.keys)
                            .ExecuteCommand();
                    }
                }
            }
             
        }

        /// <summary>
        /// 创建工单任务
        /// </summary>
        /// <param name="work_order"></param>
        /// <returns></returns>
        public bool CreateProjectAndWorkOrder(base_work_order_head work_order,ref  string outMsg)
        {
            try
            {
                outMsg = "生成成功!";
                #region 现在不需要项目相关信息了
                /*  base_project entity =   Context.Queryable<base_project>().Where(x => x.projectNo.Equals(work_order.projectNo)).First();
                 // 如果项目不存在创建项目及工艺路线
                 if (entity == null)
                 {
                     entity = new base_project();
                     Guid projectKeys = Guid.NewGuid();
                     entity.projectNo = work_order.projectNo;
                     entity.projectName = work_order.projectNo;
                     //  entity.createBy = sysWebUser.Account;
                     entity.createTime = DateTime.Now;
                     entity.keys = projectKeys;
                     entity.state = 0;//默认 未开始状态
                     entity.scheduledStartTime = work_order.startWorkDate;
                     entity.scheduledEntTime = work_order.startWorkDate;
                     // entity.processKeys = work_order.keys;
                     Context.Insertable(entity).ExecuteCommand();
                     List<base_work_center> work_Centers = Context.Queryable<base_work_center>().Where(x => x.isDelete.Equals("1")).ToList();
                 }

                 base_project_production_order base_Project_Production_Order = new base_project_production_order();
                 base_Project_Production_Order.projectKeys = entity.keys;
                 base_Project_Production_Order.productionOrderKey = work_order.keys;
                 base_Project_Production_Order.createTime = DateTime.Now;
                 base_Project_Production_Order.updateTime = DateTime.Now;
                 base_Project_Production_Order.keys = Guid.NewGuid();
                 Context.Insertable(base_Project_Production_Order).ExecuteCommand();
                */
                #endregion

                //生成工单信息
                //base_productionOrder_EP3D order = new base_productionOrder_EP3D();
                //CopyModel(order, work_order);
                //order.pipeSectionNo = work_order.pipePartsNo;
                //order.pipelineGrade = work_order.level;
                //order.weldingMaterial1 = work_order.instructions1;
                //order.weldingPointNo1 = !string.IsNullOrEmpty(work_order.instructions1) ? "HK01" : "";
                //order.weldingMaterial2 = work_order.instructions2;
                //order.weldingPointNo2 = !string.IsNullOrEmpty(work_order.instructions2) ? "HK02" : "";
                //order.keys = work_order.keys;
                //order.state = 0;
                //order.pipeLength = work_order.cutLength;
                //order.pipeMaterialCode = work_order.materielCode;
                //order.curvedAngle = string.IsNullOrEmpty(work_order.turningAngle) ? 0 : decimal.Parse(work_order.turningAngle);
                //Context.Insertable(order).ExecuteCommand();

                //获取工艺路线
                Guid guid = InitRoute(work_order);

                bus_workOrder_head bus_WorkOrder_Head = new bus_workOrder_head();
                CopyModel(bus_WorkOrder_Head, work_order);
                bus_WorkOrder_Head.isDelete = 1; 
                bus_WorkOrder_Head.isScrap = false;
                bus_WorkOrder_Head.processHeadKeys = guid;
                bus_WorkOrder_Head.projectCode = work_order.projectNo;
                bus_WorkOrder_Head.projectName = work_order.projectNo;
                bus_WorkOrder_Head.planCode = DateTime.Now.ToString("yyyyMMddHHmmss");
                bus_WorkOrder_Head.workOrderName = work_order.workOrderCode;
                bus_WorkOrder_Head.orderType = "1";
                bus_WorkOrder_Head.state = 0;
                bus_WorkOrder_Head.productName = "YZJ工单";
                bus_WorkOrder_Head.planStartTime = work_order.startWorkDate;
                bus_WorkOrder_Head.planEndTime = work_order.startWorkDate;
                bus_WorkOrder_Head.nowOprSequenceCode = "LoadMaterial";
                bus_WorkOrder_Head.productHeaderCode = "YZJ";
                bus_WorkOrder_Head.headKeys = work_order.keys;
                Context.Insertable(bus_WorkOrder_Head).ExecuteCommand();

                bool b = CreateRouteTaskInfo(guid, bus_WorkOrder_Head, work_order);

                return true;

            }
            catch (Exception ex)
            {
                outMsg = $"生成失败!{ex.Message}";
                Log4NetHelper.Instance.Info($" 工单任务生成失败:{work_order.workOrderCode} 错误信息{ex.Message}");
                return false;
            }
        }



        private bool CreateRouteTaskInfo(Guid guid, bus_workOrder_head busWorkOrderHead, base_work_order_head order)
        {
            var processDetail = Context.Queryable<base_process_route_detail>()
                    .Where(x => x.headkeys == guid).OrderBy(x => x.oprSequence, OrderByType.Asc).ToList();
            foreach (var item in processDetail)
            {
                var qualityDetail = new List<base_qualityStencil_detail>();


                #region  生成工序任务明细 busWorkOrderDetailList
                var busWorkOrderDetailList = new List<bus_workOrder_detail>();
                var nowWorkDetail = new bus_workOrder_detail
                {
                    headKeys = busWorkOrderHead.keys,
                    productHeaderCode = busWorkOrderHead.productHeaderCode,
                    workOrderCode = busWorkOrderHead.workOrderCode,

                    cutMaterCode = order.materielCode,
                    cuttingLength = order.cutLength,

                    lineCode = busWorkOrderHead.lineCode,
                    // designNo = order.pagination,
                    // designUrl = order.pipelineNo + ".pdf", 扬子江没有该信息

                    barCode = order.workPieceNo,
                    partCode = order.workPieceNo,  // order.pipelineNo.Trim(),

                    serialNumber = item.oprSequence,
                    oprSequenceCode = item.oprSequenceCode,
                    oprSequenceName = item.oprSequenceName,
                    workCenterCode = item.workCenterCode,
                    planStartTime = busWorkOrderHead.planStartTime,
                    planEndTime = busWorkOrderHead.planEndTime,

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

                //如果是组对或焊接需要补充焊口信息
                if (item.workCenterCode == WorkCenterCode.组对 || item.workCenterCode == WorkCenterCode.焊接 || item.workCenterCode == WorkCenterCode.组焊)
                {
                    nowWorkDetail.weldNo = order.instructions1;
                    nowWorkDetail.weldMaterCode = order.attachment1;
                    busWorkOrderDetailList.Add(nowWorkDetail);

                    if (!string.IsNullOrEmpty(order.instructions2))
                    {
                        var nowWorkDetai2 = new bus_workOrder_detail
                        {
                            headKeys = busWorkOrderHead.keys,
                            productHeaderCode = busWorkOrderHead.productHeaderCode,
                            workOrderCode = busWorkOrderHead.workOrderCode,

                            cutMaterCode = order.materielCode,
                            cuttingLength = order.cutLength,

                            lineCode = busWorkOrderHead.lineCode,
                            //扬子江没有
                            //  designNo = order.pagination,
                            //  designUrl = order.pipelineNo + ".pdf",

                            barCode = order.workPieceNo,
                            partCode = order.workPieceNo,  // order.pipelineNo.Trim(),

                            serialNumber = item.oprSequence,
                            oprSequenceCode = item.oprSequenceCode,
                            oprSequenceName = item.oprSequenceName,
                            workCenterCode = item.workCenterCode,
                            planStartTime = busWorkOrderHead.planStartTime,
                            planEndTime = busWorkOrderHead.planEndTime,

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

                        nowWorkDetai2.bodyKeys = Guid.NewGuid();
                        nowWorkDetai2.serialNumberName = nowWorkDetail.serialNumber + "_2";
                        nowWorkDetai2.weldNo = order.instructions2;
                        nowWorkDetai2.weldMaterCode = order.attachment1;
                        busWorkOrderDetailList.Add(nowWorkDetai2);
                    }
                }
                else
                {
                    busWorkOrderDetailList.Add(nowWorkDetail);
                }

                Context.Insertable(busWorkOrderDetailList).ExecuteCommand();//   .AddQueue();
                #endregion

                #region  生成质检任务
                if (item.inspectionFlag == (int)EnumQualityisExecute.)
                {
                    foreach (var detail in qualityDetail)
                    {
                        var bqualityExecute = new base_qualityStencil_Execute
                        {
                            keys = Guid.NewGuid(),
                            projectKey = guid,
                            workOrderDetailKey = nowWorkDetail.bodyKeys,
                            qualityDetailKey = detail.keys,
                            isExecute = (int)EnumQualityisExecute.,//目前直接设置为是,正式项目需要改为否,需要上一工序完成后修改
                            executeResult = (int)EnumQualityExecute.未执行,
                            state = (int)EnumQuality.启用,
                            createTime = DateTime.Now,
                            updateTime = DateTime.Now
                        };

                        Context.Insertable(bqualityExecute).ExecuteCommand();// AddQueue();
                    }
                }
                #endregion
            }

            return true;
        }


        /// <summary>
        /// 获取工艺路线,没有就新增
        /// </summary>
        /// <param name="work_order"></param>
        /// <returns></returns>
        public Guid InitRoute(base_work_order_head work_order)
        {
            Guid processKeys = Guid.NewGuid();
            int orderNo = 1;
            List<base_process_route_detail> detailList = new List<base_process_route_detail>();
            base_process_route_detail d1 = new base_process_route_detail();
            d1.LineCode = work_order.lineCode;
            d1.oprSequenceCode = "LoadMaterial";
            d1.oprSequenceName = "上料";
            d1.oprSequence = orderNo;
            d1.isDelete = 1;
            d1.workCenterCode = "LoadMaterial";
            d1.remarks = "工单工序";
            d1.createBy = "System";
            d1.createTime = DateTime.Now;
            detailList.Add(d1);

            orderNo += 1;
            base_process_route_detail d2 = new base_process_route_detail();
            d2.LineCode = work_order.lineCode;
            d2.oprSequenceCode = "Nesting";
            d2.oprSequenceName = "套料";
            d2.oprSequence = orderNo;
            d2.isDelete = 1;
            d2.workCenterCode = "Nesting";
            d1.remarks = "工单工序";
            d2.createBy = "System";
            d2.createTime = DateTime.Now;
            detailList.Add(d2);

            var cuts = base.Context.Queryable<base_work_order_flameCut>().Where(x => x.headKeys == work_order.keys).ToList();
            if (cuts.Count > 0)
            {
                orderNo += 1;
                base_process_route_detail dl = new base_process_route_detail();
                d2.LineCode = work_order.lineCode;
                dl.oprSequenceCode = "Cut";
                dl.oprSequenceName = "切割";
                dl.oprSequence = orderNo;
                dl.isDelete = 1;
                dl.workCenterCode = "Cut";
                d1.remarks = "工单工序";
                dl.createBy = "System"; 
                dl.createTime = DateTime.Now;
                detailList.Add(dl);
            }

            // 坡口
            if (!string.IsNullOrEmpty(work_order.bevels1))
            {
                orderNo += 1;
                base_process_route_detail dl = new base_process_route_detail();
                d2.LineCode = work_order.lineCode;
                dl.oprSequenceCode = "Bevel";
                dl.oprSequenceName = "坡口";
                dl.oprSequence = orderNo;
                dl.isDelete = 1;
                dl.workCenterCode = "Bevel";
                d1.remarks = "工单工序";
                dl.createBy = "System";
                dl.createTime = DateTime.Now;
                detailList.Add(dl);
            }
            // 组队,焊接
            if (!string.IsNullOrEmpty(work_order.attachment1) || !string.IsNullOrEmpty(work_order.attachment2))
            {
                orderNo += 1;
                base_process_route_detail dl = new base_process_route_detail();
                d2.LineCode = work_order.lineCode;
                dl.oprSequenceCode = "FitUp";
                dl.oprSequenceName = "组队";
                dl.oprSequence = orderNo;
                dl.isDelete = 1;
                dl.workCenterCode = "FitUp";
                d1.remarks = "工单工序";
                dl.createBy = "System";
                dl.createTime = DateTime.Now;
                detailList.Add(dl);

                orderNo += 1;
                base_process_route_detail d = new base_process_route_detail();
                d2.LineCode = work_order.lineCode;
                d.oprSequenceCode = "Weld";
                d.oprSequenceName = "焊接";
                d.oprSequence = orderNo;
                d.isDelete = 1;
                d.workCenterCode = "Weld";
                d1.remarks = "工单工序";
                d.createBy = "System";
                d.createTime = DateTime.Now;
                detailList.Add(d);
            }
            var bends = base.Context.Queryable<base_work_order_bends>().Where(x => x.headKeys == work_order.keys).ToList();
            //弯管
            if (bends.Count > 0)
            {
                orderNo += 1;
                base_process_route_detail d = new base_process_route_detail();
                d2.LineCode = work_order.lineCode;
                d.oprSequenceCode = "bentPipe";
                d.oprSequenceName = "弯管";
                d.oprSequence = orderNo;
                d.isDelete = 1;
                d.workCenterCode = "bentPipe";
                d1.remarks = "工单工序";
                d.createBy = "System";
                d.createTime = DateTime.Now;
                detailList.Add(d);
            }

            // 如果有装配信息需要进行组队焊接
            var mounting  = base.Context.Queryable<base_work_order_mounting>().Where(x => x.headKeys == work_order.keys).ToList();
            if (mounting.Count > 0)
            {
                orderNo += 1;
                base_process_route_detail dl = new base_process_route_detail();
                d2.LineCode = work_order.lineCode;
                dl.oprSequenceCode = "FitUp";
                dl.oprSequenceName = "组队";
                dl.oprSequence = orderNo;
                dl.isDelete = 1;
                dl.workCenterCode = "FitUp";
                d1.remarks = "装配工序";
                dl.createBy = "System";
                dl.createTime = DateTime.Now;
                detailList.Add(dl);

                orderNo += 1;
                base_process_route_detail d = new base_process_route_detail();
                d2.LineCode = work_order.lineCode;
                d.oprSequenceCode = "Weld";
                d.oprSequenceName = "焊接";
                d.oprSequence = orderNo;
                d.isDelete = 1;
                d.workCenterCode = "Weld";
                d1.remarks = "装配工序";
                d.createBy = "System";
                d.createTime = DateTime.Now;
                detailList.Add(d);
            }
            GetProcessKeys(detailList, ref processKeys, work_order);
            return processKeys;
        }

        //比对是否已经存在工艺路线,没有就创建
        private void GetProcessKeys(List<base_process_route_detail> detailList, ref Guid processKeys, base_work_order_head work_order)
        {

            var route_head = Context.Queryable<base_process_route_head>().Where(x => x.lineCode == work_order.lineCode).OrderBy(x => x.edition, SqlSugar.OrderByType.Desc).ToList();
            var route_detail = Context.Queryable<base_process_route_detail>().ToList();
            foreach (var item in route_head)
            {
                List<base_process_route_detail> curList = route_detail.Where(t => t.headkeys == item.keys).ToList();

                if (ArePropertiesEqual(detailList, curList, "oprSequenceCode"))
                {
                    processKeys = item.keys;
                    return;
                }
            }
            CreateRoutInfo(detailList, processKeys, work_order);
            return;
        }

        private void CreateRoutInfo(List<base_process_route_detail> detailList, Guid processKeys, base_work_order_head work_order)
        {
            base_process_route_head head = new base_process_route_head();
            head.edition = 1;
            head.keys = processKeys;
            head.processName = work_order.projectNo + "-" + work_order.workOrderCode;
            head.isDelete = 1;
            head.lineCode = work_order.lineCode;
            head.processCode = work_order.workOrderCode+DateTime.Now.ToString();
            head.productHeaderCode = "YZJ" + work_order.projectNo;
            head.createBy = "System";

            head.createTime = DateTime.Now;

            foreach (var item in detailList)
            {
                item.createBy = "System";
                item.createTime = DateTime.Now;
                item.headkeys = processKeys;
                item.inspectionFlag = 0;
                item.LineCode = work_order.lineCode;
            }
            Context.Insertable(detailList).ExecuteCommand();
            Context.Insertable(head).ExecuteCommand();
        }


        /// <summary>
        /// 模型赋值
        /// </summary>
        /// <param name="target">目标</param>
        /// <param name="source">数据源</param>
        public static void CopyModel<T, M>(T target, M source)
        {
            Type targetType = target.GetType();
            Type sourceType = source.GetType();
            foreach (var mi in sourceType.GetProperties())
            {
                var des = targetType.GetProperty(mi.Name);
                if (des != null)
                {
                    des.SetValue(target, mi.GetValue(source));
                }
            }
        }

        /// <summary>
        /// 通过列比对是否一致
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static bool ArePropertiesEqual<T>(List<T> obj1, List<T> obj2, string propertyName)
        {
            if (obj1.Count != obj2.Count) return false;
            PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
            if (propertyInfo == null)
            {
                throw new ArgumentException("Property not found");
            }

            //object value1 = propertyInfo.GetValue(obj1);
            //object value2 = propertyInfo.GetValue(obj2);
            List<string> value1 = new List<string>();
            List<string> value2 = new List<string>();
            foreach (var item in obj1)
            {
                value1.Add(propertyInfo.GetValue(item).ToString());
            }
            foreach (var item in obj2)
            {
                value2.Add(propertyInfo.GetValue(item).ToString());
            }
            value1.Sort();
            value2.Sort();
            return value1.Except(value2).ToList().Count < 1;

            //return Equals(value1, value2);
        }

        public string GetPaintPrintCode(base_work_order_head order_head)
        {

            string paintPrintCode = string.Empty;
            string attachment1PrintInfo = string.IsNullOrEmpty(order_head.attachment1PrintInfo) ? "_" : order_head.attachment1PrintInfo; //附件1打印信息
            string attachment2PrintInfo = string.IsNullOrEmpty(order_head.attachment2PrintInfo) ? "_" : order_head.attachment2PrintInfo; //附件1打印信息
            string turningAngle = string.IsNullOrEmpty(order_head.turningAngle) ? "_" : order_head.turningAngle; //转角 
            string bevels1 = string.IsNullOrEmpty(order_head.bevels1) ? "_" : order_head.bevels1; // 坡口
            //喷码:条形码 附件1+转角 / 坡口 + 九位码 + 系列 + 组立 + 坡口 / 附件2 + 转角 (条形码信息 = 九位码)										
            paintPrintCode = $"{attachment1PrintInfo}-{turningAngle}/{bevels1}-{order_head.pipePartsCode}-{order_head.range}-{order_head.assemblageInfo}-{bevels1}/{attachment2PrintInfo}-{turningAngle} ";
            return paintPrintCode;
        }
    }
}