wanshenmean
2 天以前 5e851678cc02257bbbd179446de36082430ca5bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using Autofac.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WIDESEA_Core;
using WIDESEA_Core.BaseController;
using WIDESEA_DTO.MES;
using WIDESEA_IBasicService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_WMSServer.Controllers.Mes
{
    /// <summary>
    /// MES接口日志控制器
    /// </summary>
    [Route("api/MesLog")]
    [ApiController]
    [Authorize]
    public class MesLogController : ApiBaseController<IMesLogService, Dt_MesApiLog>
    {
        private readonly IMesLogService _mesLogService;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public MesLogController(IMesLogService mesLogService) : base(mesLogService)
        {
            _mesLogService = mesLogService;
        }
 
        /// <summary>
        /// 分页查询MES日志
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="page">页码,默认1</param>
        /// <param name="pageSize">每页数量,默认20</param>
        /// <returns>分页结果</returns>
        [HttpPost("page")]
        public async Task<WebResponseContent> GetPage([FromBody] MesLogQueryDto query, [FromQuery] int page = 1, [FromQuery] int pageSize = 20)
        {
            var response = new WebResponseContent();
            try
            {
                var (items, total) = await _mesLogService.GetPageAsync(query, page, pageSize);
                return response.OK(null, new { rows = items, total = total });
            }
            catch (Exception ex)
            {
                return response.Error($"查询失败: {ex.Message}");
            }
        }
 
        /// <summary>
        /// 获取日志详情
        /// </summary>
        /// <param name="id">日志ID</param>
        /// <returns>日志详情</returns>
        [HttpGet("{id}")]
        public async Task<WebResponseContent> GetDetail(long id)
        {
            var response = new WebResponseContent();
            try
            {
                var detail = await _mesLogService.GetDetailAsync(id);
                if (detail == null)
                {
                    return response.Error("日志不存在");
                }
                return response.OK(null, detail);
            }
            catch (Exception ex)
            {
                return response.Error($"查询失败: {ex.Message}");
            }
        }
 
        /// <summary>
        /// 获取统计数据
        /// </summary>
        /// <param name="query">查询条件(可选)</param>
        /// <returns>统计数据</returns>
        [HttpGet("statistics")]
        public async Task<WebResponseContent> GetStatistics([FromQuery] MesLogQueryDto query)
        {
            var response = new WebResponseContent();
            try
            {
                var statistics = await _mesLogService.GetStatisticsAsync(query ?? new MesLogQueryDto());
                return response.OK(null, statistics);
            }
            catch (Exception ex)
            {
                return response.Error($"查询失败: {ex.Message}");
            }
        }
 
        /// <summary>
        /// 导出日志
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>CSV文件</returns>
        [HttpPost("export")]
        public async Task<IActionResult> Export([FromBody] MesLogQueryDto query)
        {
            try
            {
                var data = await _mesLogService.ExportAsync(query ?? new MesLogQueryDto());
                var fileName = $"MES接口日志_{DateTime.Now:yyyyMMdd_HHmmss}.csv";
                return File(data, "text/csv; charset=utf-8", fileName);
            }
            catch (Exception ex)
            {
                return BadRequest(new { error = ex.Message });
            }
        }
    }
}