UpstreamSendProcessRoute.cs 13.6 KB
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using System;
using System.Collections.Generic;
using Hh.Mes.POJO.ApiEntity;
using Hh.Mes.POJO.WebEntity;
using Hh.Mes.POJO.WebEntity.bus;
using Hh.Mes.POJO.Response;
using static Microsoft.AspNetCore.Hosting.Internal.HostingApplication;

namespace Hh.Mes.Service.ApiService
{
    public partial class UpstreamService
    {
        public dynamic SendProcessRoute(MomProcessRouteHeadEntity entity)
        {
            int resultCount = 0;
            var response = new ResponseUpstream<string>(entity.plmeid);
            var validation = ExecuteQueuesbefore(entity, response);
            if (!validation.Status)
            {
                return validation;
            }
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                #region 工艺路线  赋值
                var line = Context.Queryable<base_line>().First(x => x.otherCode == entity.line_code);
                if (line == null || string.IsNullOrEmpty(line.lineCode))
                {
                    response.ResponseError($"工艺路线下发:没有查询到线体【line_code】【{entity.line_code}】和华恒线体一致的数据,请通知管理员在【工厂模型->线体设置】维护外部编码!");
                    return response;
                }
                var busEqList = new List<bus_equipment_process_template>();
                var details = new List<base_process_route_detail>();

                var head = new base_process_route_head
                {
                    keys = Guid.NewGuid(),
                    plmeid = entity.plmeid,
                    processCode = Guid.NewGuid().ToString(),
                    productHeaderCode = entity.product_code,
                    processName = entity.product_code + "工艺名称",
                    otherCode = entity.product_code,
                    lineCode = line.lineCode,
                    edition = entity.edition,

                    isDelete = AddOrUpdateFlag,
                    createBy = SystemVariable.DefaultCreated,
                    createTime = DateTime.Now
                };
                var workCenterList = new List<base_work_center>();
                foreach (var item in entity.details)
                {
                    if (!int.TryParse(item.proced_num, out int sequence))
                    {
                        response.plmeid = item.plmeid;
                        return response.ResponseError($"【MOM】工艺路线的工序顺序【proced_num】【{item.proced_num}】不能转换为数字,请核对数据!");
                    }
                    var processRouteDetail = new base_process_route_detail
                    {
                        headkeys = head.keys,
                        bodyKeys = Guid.NewGuid(),
                        oprSequenceCode = item.proced_code,
                        oprSequenceName = item.proced_name,
                        oprSequence = sequence,
                        plmeid = item.plmeid,
                        isDelete = false,
                        workCenterCode = item.station_code,
                        workCenterName = item.station_name,
                        LineCode = item.line_code,
                        createBy = item.CREATEUNAME,
                        createTime = DateTime.Now
                    };
                    details.Add(processRouteDetail);

                    #region 工作中心 赋值 默认外部编码就是工作中心编码
                    var temp = new base_work_center
                    {
                        key = Guid.NewGuid(),
                        workCenterCode = item.station_code,
                        workCenterName = item.station_name,
                        otherCode = item.station_code,

                        isDelete = AddOrUpdateFlag,
                        createBy = SystemVariable.DefaultCreated,
                        createTime = DateTime.Now
                    };
                    var isExt = workCenterList.Exists(x => x.workCenterCode == temp.workCenterCode);
                    if (!isExt) workCenterList.Add(temp);


                    #endregion

                    #region 设备工艺参数  赋值 工作中心默认设备工艺参数模板
                    var tempEq = new bus_equipment_process_template
                    {
                        keys = Guid.NewGuid(),
                        device_information = item.station_code,
                        device_code = item.station_code,

                        processing_steps = "",
                        processing_parameters = "",
                        processing_results = "",

                        remarks = $"编码【{item.station_code}】{item.station_name},工作中心默认设备工艺参数模板",

                        createBy = SystemVariable.DefaultCreated,
                        createTime = DateTime.Now
                    };
                    var isExtEq = busEqList.Exists(x => x.device_code == item.station_code);
                    if (!isExtEq) busEqList.Add(tempEq);


                    #endregion
                }
                #endregion

                #region base_product_header 赋值
                var productHead = new base_product_header
                {
                    keys = Guid.NewGuid(),
                    productCode = entity.product_code,
                    productName = entity.product_name,
                    productCodeOrProductName = entity.product_code,
                    processCode = head.processCode,
                    processHeadKeys = head.keys,

                    isDelete = AddOrUpdateFlag,
                    createBy = SystemVariable.DefaultCreated,
                    createTime = DateTime.Now
                };

                #endregion

                var existHead = Context.Queryable<base_process_route_head>().Any(t => t.plmeid == head.plmeid && t.isDelete == AddOrUpdateFlag);
                if (entity.type == EnumAction.I.ToString())
                {
                    if (existHead) return response.ResponseError($"【MOM】【plmeid】[{head.plmeid}]工艺路线信息已经存在,请勿重复发送!");
                    //如果没有产品信息就增加,如果有了就更新
                    if (Context.Queryable<base_product_header>().Any(t => t.productCode == productHead.productCode))
                    {
                        productHead.updateBy = SystemVariable.DefaultCreated;
                        productHead.updateTime = DateTime.Now;
                        Context.Updateable(productHead)
                               .IgnoreColumns(x => new { x.createBy, x.createTime })
                               .Where(x => x.productCode == productHead.productCode).AddQueue();
                    }
                    else
                    {
                        Context.Insertable(productHead).AddQueue();
                    }

                    #region 如果没有工作中心信息就增加,如果有了就更新
                    foreach (var item in workCenterList)
                    {
                        if (Context.Queryable<base_work_center>().Any(t => t.otherCode == item.workCenterCode))
                        {
                            item.updateBy = SystemVariable.DefaultCreated;
                            item.updateTime = DateTime.Now;
                            Context.Updateable(item)
                                .IgnoreColumns(x => new { x.createBy, x.createTime, x.otherCode })
                                .Where(x => x.otherCode == item.workCenterCode).AddQueue();
                        }
                        else
                        {
                            Context.Insertable(item).AddQueue();
                        }
                    }

                    #endregion

                    #region 设备工艺参数模板 
                    foreach (var item in busEqList)
                    {
                        if (!Context.Queryable<bus_equipment_process_template>().Any(t => t.device_code == item.device_code))
                        {
                            Context.Insertable(item).AddQueue();
                        }
                    }

                    #endregion

                    Context.Insertable(head).AddQueue();
                    Context.Insertable(details).AddQueue();
                }
                else if (entity.type == EnumAction.U.ToString())
                {
                    if (!existHead) return response.ResponseError($"【MOM】【plmeid】[{head.plmeid}]工艺路线信息不存在,修改失败!");
                    #region 更新工艺路线
                    head.updateTime = DateTime.Now;
                    head.updateBy = SystemVariable.DefaultCreated;
                    Context.Updateable(head)
                           .IgnoreColumns(it => new { it.createBy, it.createTime, it.keys })
                           .Where(x => x.plmeid == head.plmeid && x.isDelete == AddOrUpdateFlag).AddQueue();
                    for (var i = 0; i < details.Count; i++)
                    {
                        details[i].isDelete = false;
                        details[i].updateBy = SystemVariable.DefaultCreated;
                        details[i].updateTime = DateTime.Now;

                        var index = i;
                        Context.Updateable(details[index])
                               .IgnoreColumns(it => new { it.createBy, it.createTime, it.headkeys, it.bodyKeys })
                               .Where(x => x.plmeid == details[index].plmeid).AddQueue();
                    }
                    #endregion

                    #region 更新产品
                    productHead.updateBy = SystemVariable.DefaultCreated;
                    productHead.updateTime = DateTime.Now;
                    Context.Updateable(productHead)
                           .UpdateColumns(x => new { x.isDelete, x.productName, x.updateBy, x.updateTime })
                           .Where(x => x.productCode == productHead.productCode && x.isDelete == AddOrUpdateFlag).AddQueue();
                    #endregion

                    #region 工作中心

                    foreach (var item in workCenterList)
                    {
                        item.updateBy = SystemVariable.DefaultCreated;
                        item.updateTime = DateTime.Now;
                        Context.Updateable(item)
                            .UpdateColumns(x => new { x.isDelete, x.workCenterName, x.updateBy, x.updateTime })
                            .Where(x => x.otherCode == item.otherCode && x.isDelete == AddOrUpdateFlag).AddQueue();
                    }
                    #endregion
                }
                else if (entity.type == EnumAction.D.ToString())
                {
                    if (!existHead) return response.ResponseError($"【MOM】【plmeid】[{head.plmeid}]工艺路线信息不存在,修改失败!");
                    #region 删除工艺路线(逻辑删除)
                    head.isDelete = DeleteFlag;
                    Context.Updateable(head)
                           .UpdateColumns(x => new { x.isDelete })
                           .Where(x => x.plmeid == head.plmeid && x.isDelete == AddOrUpdateFlag).AddQueue();
                    for (var i = 0; i < details.Count; i++)
                    {
                        details[i].isDelete = false;
                        details[i].updateBy = SystemVariable.DefaultCreated;
                        details[i].updateTime = DateTime.Now;

                        var index = i;
                        Context.Updateable(details[index])
                               .UpdateColumns(x => new { x.isDelete })
                               .Where(x => x.plmeid == details[index].plmeid && x.isDelete.Value).AddQueue();
                    }
                    #endregion

                    #region 删除产品(逻辑删除)
                    productHead.updateBy = SystemVariable.DefaultCreated;
                    productHead.updateTime = DateTime.Now;
                    productHead.isDelete = DeleteFlag;
                    Context.Updateable(productHead)
                           .UpdateColumns(x => new { x.isDelete, x.updateBy, x.updateTime })
                           .Where(x => x.productCode == productHead.productCode && x.isDelete == AddOrUpdateFlag).AddQueue();
                    #endregion

                    #region 删除工作中心(逻辑删除)
                    foreach (var item in workCenterList)
                    {
                        item.updateBy = SystemVariable.DefaultCreated;
                        item.updateTime = DateTime.Now;
                        item.isDelete = DeleteFlag;
                        Context.Updateable(item)
                               .UpdateColumns(x => new { x.isDelete, x.updateBy, x.updateTime })
                               .Where(x => x.otherCode == item.otherCode && x.isDelete == AddOrUpdateFlag).AddQueue();
                    }
                    #endregion
                }
                resultCount = ExecuteQueues(Context);
                return resultCount > 0 ? response.ResponseSuccess() : response.ResponseError();
            });
        }

        public ResponseUpstream<string> ExecuteQueuesbefore(MomProcessRouteHeadEntity entity, ResponseUpstream<string> response)
        {
            if (string.IsNullOrWhiteSpace(entity.product_code))
            {
                response.Status = false;
                response.Message = $"【MOM】工艺路线的工序顺序【product_code】不能为空,请核对数据!";
            }
            return response;
        }
    }
}