diff --git a/src/KopSoftWms/Controllers/DeviceController.cs b/src/KopSoftWms/Controllers/DeviceController.cs index e847e6a..5f28270 100644 --- a/src/KopSoftWms/Controllers/DeviceController.cs +++ b/src/KopSoftWms/Controllers/DeviceController.cs @@ -1,176 +1 @@ -using IServices; -using Microsoft.AspNetCore.Mvc; -using SqlSugar; -using System; -using System.Linq; -using YL.Core.Dto; // 包含数据传输对象(如分页参数) -using YL.Core.Entity; // 包含实体类(如Wms_device设备实体) -using YL.Core.Entity.Fluent.Validation; // 设备实体的验证规则 -using YL.NetCore.Attributes; // 自定义特性(如权限检查、操作日志) -using YL.NetCore.NetCoreApp; // 基础控制器及响应处理 -using YL.Utils.Extensions; // 扩展方法(如字符串判空) -using YL.Utils.Pub; // 公共常量、枚举(如操作结果提示、字典类型) - -namespace KopSoftWms.Controllers -{ - /// - /// 设备管理控制器 - /// 处理设备的列表展示、添加、修改、删除等操作 - /// - public class DeviceController : BaseController - { - // 设备服务(注入,处理设备相关业务逻辑) - private readonly IWms_deviceServices _deviceServices; - // 部门服务(注入,处理部门相关数据查询) - private readonly ISys_deptServices _deptServices; - // 字典服务(注入,处理数据字典相关查询) - private readonly ISys_dictServices _dictServices; - - /// - /// 构造函数(依赖注入) - /// - /// 字典服务 - /// 部门服务 - /// 设备服务 - public DeviceController(ISys_dictServices dictServices, ISys_deptServices deptServices, IWms_deviceServices deviceServices) - { - _dictServices = dictServices; - _deviceServices = deviceServices; - _deptServices = deptServices; - } - - /// - /// 设备管理首页 - /// - /// 视图 - [HttpGet] // 接受GET请求 - [CheckMenu] // 应用菜单检查过滤器(加载用户菜单和页面信息) - public IActionResult Index() - { - // 向视图传递部门列表(仅查询未删除的部门,IsDel=1表示未删除) - ViewBag.Dept = _deptServices.QueryableToList(c => c.IsDel == 1); - // 向视图传递设备类型字典(从数据字典中查询设备类型,PubDictType.device为设备类型枚举) - ViewBag.Device = _dictServices.Queryable() - .Where(c => c.IsDel == 1 && c.DictType == SqlFunc.ToString(Convert.ToInt32(PubDictType.device))) - .ToList(); - // 向视图传递设备属性字典(从数据字典中查询设备属性,PubDictType.property为属性枚举) - ViewBag.Property = _dictServices.Queryable() - .Where(c => c.IsDel == 1 && c.DictType == SqlFunc.ToString(Convert.ToInt32(PubDictType.property))) - .ToList(); - return View(); // 返回首页视图 - } - - /// - /// 设备列表数据(分页查询) - /// - /// 分页参数(包含页码、页大小、排序等) - /// JSON格式的分页数据 - [HttpPost] // 接受POST请求 - [OperationLog(LogType.select)] // 记录查询类型的操作日志 - public ContentResult List([FromForm] PubParams.DeviceBootstrapParams bootstrap) - { - // 调用设备服务获取分页数据(返回JSON字符串) - var sd = _deviceServices.PageList(bootstrap); - return Content(sd); // 返回JSON格式的内容 - } - - /// - /// 设备添加/编辑页面 - /// - /// 设备ID(为空则为添加,不为空则为编辑) - /// 视图 - [HttpGet] // 接受GET请求 - public IActionResult Add(string id) - { - var model = new Wms_device(); // 初始化设备实体 - if (id.IsEmpty()) // 若ID为空,返回空模型(添加操作) - { - return View(model); - } - else // 若ID不为空,查询设备信息(编辑操作) - { - model = _deviceServices.QueryableToEntity(c => c.DeviceId == SqlFunc.ToInt64(id)); - return View(model); - } - } - - /// - /// 设备添加或更新操作 - /// - /// 设备实体数据 - /// 设备ID(为空则添加,不为空则更新) - /// 操作结果(JSON) - [HttpPost] // 接受POST请求 - [FilterXss] // 应用XSS过滤(防止跨站脚本攻击) - [OperationLog(LogType.addOrUpdate)] // 记录新增或更新类型的操作日志 - public IActionResult AddOrUpdate([FromForm] Wms_device device, [FromForm] string id) - { - // 使用FluentValidation验证设备实体数据 - var validator = new DeviceFluent(); - var results = validator.Validate(device); - var success = results.IsValid; - - // 若验证失败,返回错误信息 - if (!success) - { - // 拼接所有验证错误信息(换行分隔) - string msg = results.Errors.Aggregate("", (current, item) => (current + item.ErrorMessage + "
")); - return BootJsonH((PubEnum.Failed.ToInt32(), msg)); // 返回失败结果 - } - - // ID为空或0:执行添加操作 - if (id.IsEmptyZero()) - { - // 检查设备序列号是否已存在(防止重复) - if (_deviceServices.IsAny(c => c.SerialNo == device.SerialNo)) - { - return BootJsonH((false, PubConst.Device1)); // 返回序列号已存在的提示 - } - - // 初始化新增设备的基础信息 - device.DeviceId = PubId.SnowflakeId; // 生成雪花算法唯一ID - device.CreateBy = UserDtoCache.UserId; // 创建人ID(从缓存的当前用户信息获取) - bool flag = _deviceServices.Insert(device); // 执行插入操作 - - // 返回添加结果(成功/失败提示) - return BootJsonH(flag ? (flag, PubConst.Add1) : (flag, PubConst.Add2)); - } - else // ID不为空:执行更新操作 - { - // 初始化更新设备的基础信息 - device.DeviceId = id.ToInt64(); // 设备ID(从参数获取) - device.ModifiedBy = UserDtoCache.UserId; // 修改人ID - device.ModifiedDate = DateTimeExt.DateTime; // 修改时间(当前时间) - var flag = _deviceServices.Update(device); // 执行更新操作 - - // 返回更新结果(成功/失败提示) - return BootJsonH(flag ? (flag, PubConst.Update1) : (flag, PubConst.Update2)); - } - } - - /// - /// 设备删除操作(逻辑删除) - /// - /// 设备ID - /// 操作结果(JSON) - [HttpGet] // 接受GET请求 - [OperationLog(LogType.delete)] // 记录删除类型的操作日志 - public IActionResult Delete(string id) - { - // 执行逻辑删除:更新IsDel为0(表示已删除),并记录修改人/时间 - var flag = _deviceServices.Update( - new Wms_device - { - DeviceId = SqlFunc.ToInt64(id), - IsDel = 0, - ModifiedBy = UserDtoCache.UserId, - ModifiedDate = DateTimeExt.DateTime - }, - c => new { c.IsDel, c.ModifiedBy, c.ModifiedDate } // 指定仅更新这三个字段 - ); - - // 返回删除结果(成功/失败提示) - return BootJsonH(flag ? (flag, PubConst.Delete1) : (flag, PubConst.Delete2)); - } - } -} \ No newline at end of file + \ No newline at end of file