BaseQualitystencilService.cs 7.85 KB
using Hh.Mes.Common.Infrastructure;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
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 Hh.Mes.Pojo.System;
using System.Text;

namespace Hh.Mes.Service.Quality
{
    public class BaseQualitystencilService : RepositorySqlSugar<base_qualityStencil_head>
    {
        public dynamic Load(PageReq pageReq, base_qualityStencil_head entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var expression = LinqWhere(entity);
                //先组合查询表达式(多表查询查看IOT 设备列表案例)
                var query = Context.Queryable<base_qualityStencil_head>().Where(expression);
                //Exel false 分页
                if (!entity.Exel)
                {
                    int total = 0;
                    result.Result = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
                    result.Count = total;
                }
                else
                {
                    result.Result = query.ToList();
                    result.Count = result.Result.Count();
                }
                return result;
            }, catchRetrunValue: "list");
        }

        public dynamic GetQualitystencilDetailList(PageReq pageReq, base_qualityStencil_head entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var expression = LinqDetailWhere(entity);
                //先组合查询表达式(多表查询查看IOT 设备列表案例)
                var query = Context.Queryable<base_qualityStencil_detail>().Where(expression);
                //Exel false 分页
                if (!entity.Exel)
                {
                    int total = 0;
                    result.Result = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
                    result.Count = total;
                }
                else
                {
                    result.Result = query.ToList();
                    result.Count = result.Result.Count();
                }
                return result;
            }, catchRetrunValue: "list");
        }


        public dynamic Ins(base_qualityStencil_head entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                //entity.createBy = sysWebUser.Account;
                entity.createTime = DateTime.Now;
                entity.keys = Guid.NewGuid();
                response.Status = Add(entity);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic InsDesc(base_qualityStencil_detail entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                //entity.createBy = sysWebUser.Account;
                entity.createTime = DateTime.Now;
                entity.keys = Guid.NewGuid();
                response.Status = Add(entity);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic Upd(base_qualityStencil_head entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                //entity.updateBy = sysWebUser.Account;
                entity.updateTime = DateTime.Now;
                response.Status = Update(entity);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic UpdDesc(base_qualityStencil_detail entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                entity.updateTime = DateTime.Now;
                response.Status = Context.Updateable(entity)
                                                        .Where(x => x.headKey == entity.headKey)
                                                        .IgnoreColumns(x => new { x.createTime })
                                                        .ExecuteCommand() > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic DelByIds(Guid[] keys)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                Context.Deleteable<base_qualityStencil_head>(t => keys.Contains(t.keys)).AddQueue();
                Context.Deleteable<base_qualityStencil_detail>(t => keys.Contains(t.headKey)).AddQueue();
                response.Status = Context.SaveQueues() > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic DelByIdsDesc(int[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Context.Deleteable<base_qualityStencil_detail>().In(x => x.id, ids).AddQueue();
                response.Status = Context.SaveQueues() > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public Response ExportData(base_qualityStencil_head entity)
        {
            return Load(null, entity);
        }

        public Expression<Func<base_qualityStencil_head, bool>> LinqWhere(base_qualityStencil_head model)
        {
            try
            {
                var exp = Expressionable.Create<base_qualityStencil_head>();
                //数据过滤条件
                //if (!string.IsNullOrWhiteSpace(model.XXX)) exp.And(x => x.XXX.Contains(model.XXX));
                if (!string.IsNullOrWhiteSpace(model.stencilName))
                {
                    exp.And(x => x.stencilName.Contains(model.stencilName));
                }
                if (!string.IsNullOrWhiteSpace(model.remark))
                {
                    exp.And(x => x.remark.Contains(model.remark));
                }
                if (!string.IsNullOrWhiteSpace(model.temp1))
                {
                    exp.And(x => x.temp1.Contains(model.temp1));
                }
                return exp.ToExpression();//拼接表达式
            }
            catch (Exception ex)
            {
                throw new Exception($"{ex.Message}");
            }
        }

        public Expression<Func<base_qualityStencil_detail, bool>> LinqDetailWhere(base_qualityStencil_head model)
        {
            try
            {
                var exp = Expressionable.Create<base_qualityStencil_detail>();
                //数据过滤条件
                //if (!string.IsNullOrWhiteSpace(model.XXX)) exp.And(x => x.XXX.Contains(model.XXX));
                if (model.keys != Guid.Empty)
                {
                    exp.And(x => x.headKey == model.keys);
                }
                if (!string.IsNullOrWhiteSpace(model.remark))
                {
                    exp.And(x => x.remark.Contains(model.remark));
                }
                if (!string.IsNullOrWhiteSpace(model.temp1))
                {
                    exp.And(x => x.temp1.Contains(model.temp1));
                }
                return exp.ToExpression();//拼接表达式
            }
            catch (Exception ex)
            {
                throw new Exception($"{ex.Message}");
            }
        }
    }
}