CommunicationController.cs 5.84 KB
using HHECS.DAQWebClient.Dtos;
using HHECS.DAQWebClient.Model;
using HHECS.DAQWebClient.Models;
using HHECS.DAQWebClient.ViewModel;
using LinqKit;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;

namespace HHECS.DAQWebClient.Controllers
{
    [Authorize]
    public class CommunicationController : Controller
    {
        private readonly IFreeSql _freeSql;
        private readonly ILogger<CommunicationController> _logger;

        public CommunicationController(IFreeSql freeSql, ILogger<CommunicationController> logger)
        {
            _freeSql = freeSql;
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public async Task<IActionResult> Load(CommunicationQueryModel model, int page, int limit)
        {
            try
            {
                var query = _freeSql.Queryable<CommunicationConfig>().Where(GetFilter(model));
                var result = await query.Skip((page - 1) * limit).Take(limit).ToListAsync();
                var total = await query.CountAsync();
                return Ok(new ResultDto<List<CommunicationConfig>>
                {
                    Code = 0,
                    Count = total,
                    Data = result
                });
            }
            catch (Exception ex)
            {
                return Ok(new ResultDto<List<CommunicationConfig>>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }

        private static Expression<Func<CommunicationConfig, bool>> GetFilter(CommunicationQueryModel model)
        {
            var filter = PredicateBuilder.New<CommunicationConfig>(true);
            if (!string.IsNullOrWhiteSpace(model.Code))
            {
                filter = filter.And(x => x.Code.Contains(model.Code));
            }
            if (!string.IsNullOrWhiteSpace(model.Name))
            {
                filter = filter.And(x => x.Name.Contains(model.Name));
            }
            if (model.CommunicationType != null)
            {
                filter = filter.And(x => x.CommunicationType == model.CommunicationType);
            }
            return filter;
        }

        [HttpPost]
        [Authorize(Roles = $"{nameof(RoleType.Admin)}")]
        public async Task<IActionResult> Add(CommunicationConfig model)
        {
            try
            {
                model.CreatedBy = HttpContext.User.Identity?.Name ?? string.Empty;
                model.Created = DateTime.Now;
                await _freeSql.Insert<CommunicationConfig>(model).ExecuteAffrowsAsync();
                return Ok(new ResultDto
                {
                    Code = 0,
                    Msg = "新增成功"
                });
            }
            catch (Exception ex)
            {
                return Ok(new ResultDto
                {
                    Code = 500,
                    Msg = $"新增失败:{ex.Message}"
                });
            }
        }

        [HttpPost]
        public async Task<IActionResult> UpdateStatus(CommunicationConfig model)
        {
            try
            {
                var user = HttpContext.User.Identity?.Name ?? string.Empty;
                await _freeSql.Update<CommunicationConfig>(model.Id).Set(x => new CommunicationConfig
                {
                    Disable = model.Disable,
                    UpdatedBy = user,
                    Updated = DateTime.Now
                }).ExecuteAffrowsAsync();

                return Ok(new ResultDto
                {
                    Code = 0,
                    Msg = $"更新状态成功"
                });
            }
            catch (Exception ex)
            {
                return Ok(new ResultDto
                {
                    Code = 500,
                    Msg = $"更新状态失败:{ex.Message}"
                });
            }
        }

        [HttpPost]
        [Authorize(Roles = $"{nameof(RoleType.Admin)}")]
        public async Task<IActionResult> Update(CommunicationConfig model)
        {
            try
            {
                var user = HttpContext.User.Identity?.Name ?? string.Empty;
                await _freeSql.Update<CommunicationConfig>(model.Id).Set(x => new CommunicationConfig
                {
                    Code = model.Code,
                    Name = model.Name,
                    CommunicationType = model.CommunicationType,
                    IpAddress = model.IpAddress,
                    Port = model.Port,
                    Disable = model.Disable,
                    Remark = model.Remark,
                    UpdatedBy = user,
                    Updated = DateTime.Now
                }).ExecuteAffrowsAsync();

                return Ok(new ResultDto
                {
                    Code = 0,
                    Msg = $"更新成功"
                });
            }
            catch (Exception ex)
            {
                return Ok(new ResultDto
                {
                    Code = 500,
                    Msg = $"更新失败:{ex.Message}"
                });
            }
        }

        [HttpPost]
        [Authorize(Roles = $"{nameof(RoleType.Admin)}")]
        public async Task<IActionResult> Delete(int[] ids)
        {
            try
            {
                await _freeSql.Delete<CommunicationConfig>().Where(x => ids.Contains(x.Id)).ExecuteAffrowsAsync();
                return Ok(new ResultDto
                {
                    Code = 0,
                    Msg = "删除成功"
                });
            }
            catch (Exception ex)
            {
                return Ok(new ResultDto
                {
                    Code = 500,
                    Msg = $"删除失败:{ex.Message}"
                });
            }
        }
    }
}