WorkOrderJobService.cs 23.1 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
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 NPOI.SS.Formula.Functions;
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).
                Take(100).ToList();

            if (orderTaskList.Count > 0)
            {
                List<sys_dict_data> dicList = Context.Queryable<sys_dict_data>().With(SqlWith.NoLock).Where(x => x.dictType == "SpecialCharacters").ToList();
                var code = DateTime.Now.ToString("yyyyMMddHHmmss");
                int i = 1;
                List<sys_dict_data> pipePrintCode = Context.Queryable<sys_dict_data>().With(SqlWith.NoLock).Where(x => x.dictType == "PipePrintCodeSeting").ToList();
                foreach (var orderTask in orderTaskList)
                {
                    // 条码和喷码
                    string barCode = orderTask.pipePartsCode + "-" + orderTask.paintCode;
                    string paintPrintCode = string.Empty;
                    //是否打印长码    
                    bool isLenthCode = true;
                    try
                    {
                        isLenthCode = pipePrintCode.First(x => x.dictLabel.ToUpper() == orderTask.lineCode.ToUpper()).dictValue == "1";
                    }
                    catch (Exception)
                    {
                        //默认没有配置也是长码
                        isLenthCode = true;
                    }
                    if (isLenthCode)
                    {
                        paintPrintCode = GetPaintPrintCode(orderTask, dicList);
                    }
                    else
                    {
                        paintPrintCode = orderTask.pipePartsCode;
                    }
                    string outMsg = string.Empty;
                    string taskCode = $"{code}-{i}";
                    try
                    {
                        Context.Ado.BeginTran();
                        if (CreateProjectAndWorkOrder(orderTask, ref outMsg, paintPrintCode, taskCode))
                        {

                            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();
                            Context.Ado.CommitTran();
                        }
                        else
                        {
                            Context.Ado.RollbackTran();
                            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")
                                .Where(t => t.keys == orderTask.keys)
                                .ExecuteCommand();
                        }
                        i += 1;

                    }
                    catch (Exception ex)
                    {
                        Log4NetHelper.Instance.Error($"工单生成报错:{ex.Message}");
                    }
                }
            }

        }

        /// <summary>
        /// 创建工单任务
        /// </summary>
        /// <param name="work_order"></param>
        /// <returns></returns>
        public bool CreateProjectAndWorkOrder(base_work_order_head work_order, ref string outMsg, string paintPrintCode, string taskCode)
        {
            try
            {
                outMsg = "生成成功!"; 
                //获取工艺路线
                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.workOrderName = work_order.workOrderCode;
                bus_WorkOrder_Head.orderType = "1";
                bus_WorkOrder_Head.state = 0;
                bus_WorkOrder_Head.productName = "CZYY工单";
                bus_WorkOrder_Head.planStartTime = work_order.startWorkDate;
                bus_WorkOrder_Head.planEndTime = work_order.startWorkDate;
                bus_WorkOrder_Head.nowOprSequenceCode = "LoadMaterial";
                bus_WorkOrder_Head.productHeaderCode = "CZYY";
                bus_WorkOrder_Head.headKeys = work_order.keys;
                bus_WorkOrder_Head.planCode = work_order.planCode;
                Context.Insertable(bus_WorkOrder_Head).ExecuteCommand();

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

                return true;

            }
            catch (Exception ex)
            {
                outMsg = $"生成失败!{ex.Message}";
                return false;
            }
        }



        private bool CreateRouteTaskInfo(Guid guid, bus_workOrder_head busWorkOrderHead, base_work_order_head order, string paintPrintCode, string taskCode)
        {
            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>();
                 
                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.extend1, 

                    barCode = order.workPieceNo,
                    partCode = order.workPieceNo, 

                    serialNumber = item.oprSequence,
                    oprSequenceCode = item.oprSequenceCode,
                    oprSequenceName = item.oprSequenceName,
                    workCenterCode = item.workCenterCode,
                    planStartTime = busWorkOrderHead.planStartTime,
                    planEndTime = busWorkOrderHead.planEndTime,
                    //钢印码 九位码-涂装号
                    extendComp1 = $"{busWorkOrderHead.pipePartsCode}-{busWorkOrderHead.paintCode}",
                    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";
                //提前打码都要写
                nowWorkDetail.weldMaterCode = order.attachment1;
                //如果是组对或焊接需要补充焊口信息
                if (item.workCenterCode == WorkCenterCode.组对 || item.workCenterCode == WorkCenterCode.焊接 || item.workCenterCode == WorkCenterCode.组焊 || item.workCenterCode == WorkCenterCode.小组焊接)
                {
                    nowWorkDetail.weldNo = order.instructions1;

                    busWorkOrderDetailList.Add(nowWorkDetail);

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

                            weldNo = order.instructions2,
                            weldMaterCode = order.attachment2,
                            cutMaterCode = order.materielCode,
                            cuttingLength = order.cutLength,

                            lineCode = busWorkOrderHead.lineCode,
                            //扬子江没有
                            designNo = order.extend1,
                            //  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
                { 
                    //坡口
                    if (item.oprSequenceCode == "Bevel" && !string.IsNullOrEmpty(order.bevels1) && order.bevels1 != "0")
                    {
                        nowWorkDetail.oprSequenceName = "坡口1";
                        nowWorkDetail.extendComp2 = order.bevels1;
                        nowWorkDetail.extendComp3 = order.bevels2;
                        busWorkOrderDetailList.Add(nowWorkDetail);

                    }
                    //坡口要区分1和2 

                    if (item.oprSequenceCode == "Bevel" && !string.IsNullOrEmpty(order.bevels2) && order.bevels2 != "0")
                    {
                        bus_workOrder_detail detail2 = new bus_workOrder_detail();
                        CopyModel(detail2, nowWorkDetail);
                        detail2.oprSequenceName = "坡口2";
                        detail2.serialNumberName = nowWorkDetail.serialNumber + "_2";
                        detail2.keys = Guid.NewGuid();
                        detail2.extendComp2 = order.bevels1;
                        detail2.extendComp3 = order.bevels2;
                        detail2.weldMaterCode = order.attachment2;
                        busWorkOrderDetailList.Add(detail2);
                    }
                    if (item.oprSequenceCode != "Bevel" && item.oprSequenceCode != "Beve2")
                    {
                        busWorkOrderDetailList.Add(nowWorkDetail);
                    }

                    if (item.oprSequenceCode == "BentPipe")
                    {
                        bus_pipebends_job bendPipe = new bus_pipebends_job();
                        bendPipe.workOrderCode = order.workOrderCode;
                        bendPipe.lineCode = order.lineCode;
                        bendPipe.workPieceNo = order.workPieceNo;
                        bendPipe.state = "0";
                        bendPipe.createBy = "Job";
                        bendPipe.createTime = DateTime.Now;
                        bendPipe.updateby = "Job";
                        bendPipe.updateTime = DateTime.Now;
                        Context.Insertable(bendPipe).ExecuteCommand();
                    }
                }
                //赋值打印码
                foreach (var item1 in busWorkOrderDetailList)
                {
                    item1.extend1 = paintPrintCode;
                } 
                Context.Insertable(busWorkOrderDetailList).ExecuteCommand(); 
            } 
            return true;
        }


        /// <summary>
        /// 获取工艺路线,没有就新增
        /// </summary>
        /// <param name="work_order"></param>
        /// <returns></returns>
        public Guid InitRoute(base_work_order_head work_order)
        {
            //获取短管判断长度
            int shortPipeLength = int.Parse(GetDictionaryDictValue("ShortPipeLength", "ShortPipeLength"));
            Guid processKeys = Guid.NewGuid();
            int orderNo = 0;
            List<base_process_route_detail> detailList = new List<base_process_route_detail>(); 
            if (work_order.cutLength >= 0)
            {
                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";
                d2.remarks = "工单工序";
                d2.createBy = "System";
                d2.createTime = DateTime.Now;
                detailList.Add(d2);

                orderNo += 1;
                base_process_route_detail dl = new base_process_route_detail();
                dl.LineCode = work_order.lineCode;
                dl.oprSequenceCode = "Cut";
                dl.oprSequenceName = "切割";
                dl.oprSequence = orderNo;
                dl.isDelete = 1;
                dl.workCenterCode = "Cut";
                dl.remarks = "工单工序";
                dl.createBy = "System";
                dl.createTime = DateTime.Now;
                detailList.Add(dl);
            } 

            // 坡口
            if (!string.IsNullOrEmpty(work_order.bevels1) || !string.IsNullOrEmpty(work_order.bevels2))
            {
                if (work_order.bevels1 != "0" || work_order.bevels2 != "0")
                {

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

                orderNo += 1;
                base_process_route_detail d = new base_process_route_detail();
                d.LineCode = work_order.lineCode;
                d.oprSequenceCode = "Weld";
                d.oprSequenceName = "焊接";
                d.oprSequence = orderNo;
                d.isDelete = 1;
                d.workCenterCode = "Weld";
                d.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).OrderBy(t => t.oprSequenceCode).ToList();

                if (ArePropertiesEqual(detailList.OrderBy(t => t.oprSequenceCode).ToList(), 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("yyyyMMddHHmmssfff");
            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();
            string s1 = string.Join(",", value1);
            string s2 = string.Join(",", value2);
            return s1 == s2;
        }

        public string GetPaintPrintCode(base_work_order_head order_head, List<sys_dict_data> dic)
        { 
            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; //附件2打印信息
            string turningAngle = string.IsNullOrEmpty(order_head.turningAngle) ? "_" : order_head.turningAngle; //转角 
            string bevels1 = string.IsNullOrEmpty(order_head.bevels1) ? "0" : order_head.bevels1; // 坡口
            string bevels2 = string.IsNullOrEmpty(order_head.bevels2) ? "0" : order_head.bevels2;
            string assemblageInfo = order_head.assemblageInfo;
             
            bevels1 = bevels1 == "0" ? "0" : "1";
            bevels2 = bevels2 == "0" ? "0" : "1";  

            string range = order_head.range;
            string pageNo = string.IsNullOrEmpty(order_head.extend1) ? "0" : order_head.extend1;
            string paintCode = string.IsNullOrEmpty(order_head.paintCode) ? "0" : order_head.paintCode;
  
            //管件号+ 坡口+ 切割长度
            paintPrintCode = $"{order_head.workPieceNo}-{bevels1}/ {bevels2}-{order_head.cutLength}";

            //替换特殊字符
            if (dic.Count > 0)
            {
                foreach (var item in dic)
                {
                    paintPrintCode = paintPrintCode.Replace(item.dictLabel, item.dictValue);
                }
            }
            return paintPrintCode;
        }
    }
}