UpstreamSendProcessRoute.cs 9.42 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;
using System.Linq;

namespace Hh.Mes.Service.ApiService
{
    public partial class UpstreamService
    {    
        #region 属性

        /// <summary>
        /// 华恒线体code
        /// </summary>
        base_line line = null;
        #endregion

        public dynamic SendProcessRoute(MomProcessRouteHeadEntity entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new ResponseUpstream<string>(entity.plmeid);
                if (!typeValidation(entity.type, response).Status) return response;

                int resultCount = 0;
                var validation = ExecuteQueuesBefore(entity, response);
                if (!validation.Status) return validation;

                #region 工艺路线  赋值

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

                    isDelete = AddOrUpdateFlag,
                    createBy = SystemVariable.DefaultCreated,
                    createTime = DateTime.Now
                };
                var details = new List<base_process_route_detail>();
                foreach (var item in entity.details)
                {
                    var processRouteDetail = new base_process_route_detail
                    {
                        headkeys = head.keys,
                        bodyKeys = Guid.NewGuid(),
                        oprSequenceCode = item.proced_code,
                        oprSequenceName = item.proced_name,
                        oprSequence = item.proced_num,
                        oprsequenceTime=item.oprsequenceTime,//工序耗时

                        workCenterCode = item.workcenter_code,
                        workCenterName = item.workcenter_name,
                        LineCode = line.lineCode,
                        plmeid=head.plmeid,
                        remarks = item.remark,

                        isDelete = AddOrUpdateFlag,
                        createBy = SystemVariable.DefaultCreated,
                        createTime = DateTime.Now
                    };
                    details.Add(processRouteDetail);
                }
                #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

                #region 工艺路线和产品关系
                var bpprel = new base_process_product_rel
                {
                    keys = Guid.NewGuid(),
                    processHeadKeys = head.keys,
                    processCode = head.processCode,
                    processName = head.processName,
                    productCode = entity.product_code,
                    lineCode = line.lineCode,

                    createTime = DateTime.Now,
                    createBy = SystemVariable.DefaultCreated
                }; 
                #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($"【上位系统】【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();
                    }

                    Context.Insertable(head).AddQueue();
                    Context.Insertable(details).AddQueue();
                    Context.Insertable(bpprel).AddQueue();
                }
                else if (entity.type == EnumAction.U.ToString())
                {
                    if (!existHead) return response.ResponseError($"【上位系统】【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].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.productName, x.updateBy, x.updateTime })
                           .Where(x => x.productCode == productHead.productCode && 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 = $"【上位系统】工艺路线的工序顺序【product_code】不能为空,请核对数据!";
                return response;
            }

            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;
            }
            foreach (var item in entity.details)
            {
                if (string.IsNullOrEmpty(item.workcenter_code))
                {
                    response.ResponseError($"【上位系统】工艺路线的 工作中心【workcenter_code】【{item.workcenter_code}】不能为空,请核对数据!");
                    return response;
                }
                var isAngWkCen = Context.Queryable<base_work_center>().Any(x => x.workCenterCode == item.workcenter_code);
                if (!isAngWkCen)
                {
                    response.ResponseError($"【上位系统】工艺路线的 工作中心【workcenter_code】【{item.workcenter_code}】和华恒工作中心数据不一致的数据,请核对数据!");
                    return response;
                }
                 
            }

            var repeatProcedcode =  entity.details.GroupBy(x => new { x.proced_code }).Any(c => c.Count() > 1);
            if (repeatProcedcode)
            {
                response.ResponseError($"【上位系统】工艺路线的工序【proced_code】存在相同工序编码,请核对数据!");
                return response;
            }
            return response;
        }
    }
}