JobService.cs 5.23 KB
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.Text;

namespace Hh.Mes.Service.WebService.Job
{
    /// <summary>
    /// 定时器
    /// </summary>
    public class JobService : RepositorySqlSugar<SysJob>
    {
        /// <summary>
        /// 新增  code = 字典前缀+对应表标识最大值
        /// </summary>
        /// <returns></returns>
        public dynamic Ins(SysJob model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.CreateBy = base.sysWebUser?.Account;
                model.CreateTime = DateTime.Now;
                response.Status = Add(model);
                if (!response.Status) response.Message = "新增失败";
                return response;
            });
        }

        /// <summary>
        /// 根据主键数组 删除
        /// </summary>
        public dynamic DelByIds(int id)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response { Message = "" };
                var result = base.DeleteById(id);
                if (!result)
                {
                    response.Code = 500;
                    response.Message = "删除失败,反复出现请联系管理员";
                }
                return response;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(SysJob model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.UpdateBy = sysWebUser?.Account;
                model.UpdateTime = DateTime.Now;
                response.Status = Update(model);
                if (!response.Status) response.Message = "更新失败";
                return response;
            });
        }

        /// <summary>
        ///  列表
        /// </summary>
        public dynamic Load(PageReq pageReq, SysJob entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " id desc" : $"{pageReq.field} {pageReq.order} ";
                string sqlWhere = LoadSqlWhere(entity);
                var stringBuilder = new StringBuilder();
                //页码,页数
                //Exel ture 不分页
                if (!entity.Exel && pageReq != null)
                {
                    stringBuilder.Append("declare @pageIndex int,@pageSize int,@offset int");
                    stringBuilder.AppendLine($"  select @pageIndex={pageReq.page}, @pageSize={pageReq.limit}, @offset=(@pageIndex - 1) * @pageSize");
                }

                stringBuilder.AppendLine($@" select t1.* from sys_job t1 
                                         where {sqlWhere}
                                         order by {orderBy} ");

                //Exel false 分页
                if (!entity.Exel)
                {
                    stringBuilder.AppendLine("  offset @offset row fetch next @pageSize row only ");
                    stringBuilder.Append($" select rowTotal= count(*) from sys_job  t1  with(nolock) where {sqlWhere}");
                }
                var parameter = new List<SugarParameter>(){
                    new SugarParameter("@jobName", $"%{entity.JobName}%"),
                    new SugarParameter("@methodName", $"%{entity.MethodName}%"),
                };
                var ds = base.Context.Ado.GetDataSetAll(stringBuilder.ToString(), parameter);
                result.Result = ds.Tables[0];
                result.Count = entity.Exel ? (int)result.Result.Rows.Count : (int)ds.Tables[1].Rows[0]["rowTotal"];
                return result;
            }, catchRetrunValue: "list");
        }

        private string LoadSqlWhere(SysJob entity)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(" 1=1 ");
            if (!string.IsNullOrEmpty(entity.JobName))
            {
                stringBuilder.Append(" and  t1.jobName like  @jobName");
            }
            if (!string.IsNullOrEmpty(entity.MethodName))
            {
                stringBuilder.Append(" and  t1.methodName like  @methodName");
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// 导出 
        /// </summary>
        public dynamic ExportData(SysJob entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var ds = Load(null, entity);
                if (ds == null || ds.Result.Tables[0].Rows.Count == 0)
                {
                    result.Result = "[]";
                    result.Count = 0;
                }
                else
                {
                    result.Result = ds.Result.Tables[0];
                    result.Count = result.Result.Rows.Count;
                }
                return result;
            }, catchRetrunValue: "list");
        }
    }
}