Blame view

sys/Hh.Mes.Service/WebService/Job/JobService.cs 5.28 KB
赖素文 authored
1
2
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
3
using Hh.Mes.Pojo.System;
赖素文 authored
4
5
6
7
8
9
10
11
12
13
14
15
16
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>
17
    public class JobService : RepositorySqlSugar<sys_job>
赖素文 authored
18
19
20
21
22
    {
        /// <summary>
        /// 新增  code = 字典前缀+对应表标识最大值
        /// </summary>
        /// <returns></returns>
23
        public dynamic Ins(sys_job model)
赖素文 authored
24
25
26
27
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
28
29
                model.createBy = base.sysWebUser?.Account;
                model.createTime = DateTime.Now;
赖素文 authored
30
                response.Status = Add(model);
31
                if (!response.Status) response.Message = SystemVariable.dataActionError;
赖素文 authored
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
                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;
48
                    response.Message = SystemVariable.dataActionError;
赖素文 authored
49
50
51
52
53
54
55
56
                }
                return response;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
57
        public dynamic Upd(sys_job model)
赖素文 authored
58
59
60
61
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
62
63
                model.updateBy = base.sysWebUser?.Account;
                model.updateTime = DateTime.Now;
赖素文 authored
64
                response.Status = Update(model);
65
                if (!response.Status) response.Message = SystemVariable.dataActionError;
赖素文 authored
66
67
68
69
70
71
72
                return response;
            });
        }

        /// <summary>
        ///  列表
        /// </summary>
73
        public dynamic Load(PageReq pageReq, sys_job entity)
赖素文 authored
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        {
            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>(){
100
101
                    new SugarParameter("@jobName", $"%{entity.jobName}%"),
                    new SugarParameter("@methodName", $"%{entity.methodName}%"),
赖素文 authored
102
103
104
105
106
107
108
109
                };
                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");
        }
110
        private string LoadSqlWhere(sys_job entity)
赖素文 authored
111
112
113
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(" 1=1 ");
114
            if (!string.IsNullOrEmpty(entity.jobName))
赖素文 authored
115
116
117
            {
                stringBuilder.Append(" and  t1.jobName like  @jobName");
            }
118
            if (!string.IsNullOrEmpty(entity.methodName))
赖素文 authored
119
120
121
122
123
124
125
126
127
            {
                stringBuilder.Append(" and  t1.methodName like  @methodName");
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// 导出 
        /// </summary>
128
        public dynamic ExportData(sys_job entity)
赖素文 authored
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
        {
            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");
        }
    }
}