ProcessRouteService.cs 10 KB
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Hh.Mes.Service.Configure
{
    public class ProcessRouteService : RepositorySqlSugar<base_process_route_head>
    {
        #region 头表
        /// <summary>
        /// 
        /// </summary>
        public Response Load(PageReq pageReq, base_process_route_head model)
        {
            var totalCount = 0;
            var result = new Response();
            var query = Context.Queryable<base_process_route_head, base_line>((x, li) => new JoinQueryInfos(
                                        JoinType.Left, x.lineCode == li.lineCode)).
                        Where(Wheres(model)).
                        Select((x, li) => new
                        {
                            x.id,
                            x.keys,
                            x.processCode,
                            x.processName,
                            x.lineCode,
                            li.lineName,

                            x.edition,
                            x.otherCode,
                            x.isDelete,
                            x.plmeid,

                            x.createBy,
                            x.createTime,
                            x.updateBy,
                            x.updateTime
                        });
            dynamic data;
            if (pageReq != null)
            {
                data = query.OrderBy(x => x.createTime, OrderByType.Desc).ToOffsetPage(pageReq.page, pageReq.limit, ref totalCount);
            }
            else
            {
                data = query.ToList();
                totalCount = data.Count;
            }

            result.Result = data;
            result.Count = totalCount;
            return result;
        }

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        public Expression<Func<base_process_route_head, bool>> Wheres(base_process_route_head model)
        {
            var exp = Expressionable.Create<base_process_route_head>();
            if (!string.IsNullOrWhiteSpace(model.lineCode)) exp.And(x => x.lineCode.Contains(model.lineCode));
            if (!string.IsNullOrWhiteSpace(model.processCode)) exp.And(x => x.processCode.Contains(model.processCode));
            if (model.keys!= Guid.Empty) exp.And(x => x.keys== model.keys);
            if (!string.IsNullOrWhiteSpace(model.plmeid)) exp.And(x => x.plmeid == model.plmeid);
            var wheres = exp.ToExpression();//拼接表达式
            return wheres;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        public dynamic Ins(base_process_route_head model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.createBy = sysWebUser?.Account;
                model.createTime = DateTime.Now;
                model.keys = Guid.NewGuid();
                response.Status = Add(model);
                if (!response.Status)
                {
                    response.Message = SystemVariable.dataActionError;
                }
                return response;
            });
        }

        /// <summary>
        /// 编辑方法
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>

        public dynamic Upd(base_process_route_head model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.updateBy = sysWebUser?.Account;
                model.updateTime = DateTime.Now;
                response.Status = base.Context.Updateable(model).IgnoreColumns(it => new { it.createBy, it.createTime }).ExecuteCommand() > 0;
                if (!response.Status)
                {
                    response.Message = SystemVariable.dataActionError;
                }
                return response;
            });
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="ids">需要删除的id</param>
        /// <returns></returns>
        public dynamic DelByIds(Guid[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                base.Context.Deleteable<base_process_route_head>().In(x => x.keys, ids).AddQueue();
                base.Context.Deleteable<base_process_route_detail>().In(x => x.headkeys, ids).AddQueue();

                //删除工序工位
                Guid[] processRouteDetailId = Context.Queryable<base_process_route_detail>().In(x => x.headkeys, ids).Select(x => x.bodyKeys).ToArray();
                //base.Context.Deleteable<base_process_route_station>().In(x => x.bodyKeys, processRouteDetailId).AddQueue();

                //删除工艺路线和 产品关系
                base.Context.Deleteable<base_process_product_rel>().In(x => x.processHeadKeys, ids).AddQueue();

                response.Status = Context.SaveQueues() > 0;
                if (!response.Status)
                {
                    response.Message = SystemVariable.dataActionError;
                }
                return response;

            });
        }

        /// <summary>
        /// 导出方法
        /// </summary>
        /// <returns></returns>
        public dynamic Export(base_process_route_head Table_Entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Response result = Load(null, Table_Entity);
                if (result.Count == 0)
                {
                    result.Result = null;
                    result.Message = SystemVariable.queryNotData;
                    result.Code = -1;
                    return result;
                }
                var guids = new List<Guid>();
                foreach (var item in result.Result)
                {
                    guids.Add((Guid)item.keys);
                }
                var data = LoadDesc(null, new base_process_route_detail{ headkeysList= guids });
                result.Result = new
                {
                    head = result.Result,
                    body = data.Result
                };
                return result;
            }, catchRetrunValue: "list");
        }
        #endregion

        #region 明细
        /// <summary>
        /// 查询方法明细  支持导出查询
        /// </summary>
        public Response LoadDesc(PageReq pageReq, base_process_route_detail model)
        {
            var totalCount = 0;
            var result = new Response();
            var query =  base.Context.Queryable<base_process_route_detail>();
            var data = model.headkeysList!=null ? query.In(x => x.headkeys, model.headkeysList).ToList() :
                       query.Where(x => x.headkeys == model.headkeys).OrderBy(x=>x.oprSequence).ToOffsetPage(pageReq.page, pageReq.limit, ref totalCount);
            //页码,页数
            result.Result = data;
            result.Count = totalCount;
            return result;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        public dynamic InsDesc(base_process_route_detail model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.bodyKeys = Guid.NewGuid();
                model.createBy = sysWebUser?.Account;
                model.createTime = DateTime.Now;
                response.Status = Add(model);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        /// <summary>
        /// 编辑方法
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>

        public dynamic UpdDesc(base_process_route_detail model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                response.Status = base.Context.Updateable(model).Where(x=>x.bodyKeys==model.bodyKeys).IgnoreColumns(it => new { it.createBy, it.createTime }).ExecuteCommand() > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="ids">需要删除的id</param>
        /// <returns></returns>
        public dynamic DelByIdsDesc(Guid[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Context.Deleteable<base_process_route_detail>().In(x => x.bodyKeys, ids).AddQueue();
                //删除工序工位
                //Context.Deleteable<base_process_route_station>().In(x => x.bodyKeys, ids).AddQueue();
                response.Status = Context.SaveQueues() > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }
        #endregion


        /// <summary>
        /// //获取列表
        /// </summary>
        public Response GetProcessDetail()
        {
            var result = new Response();
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine($@"  select  distinct oprSequenceCode ,oprSequenceName
                                          from  [dbo].[base_process_route_detail] ");

           
            var ds = base.Context.Ado.GetDataSetAll(stringBuilder.ToString());
            result.Result = ds.Tables[0];
            result.Count = result.Result.Rows.Count;
            return result;
        }

    }
}