SettingController.cs 6.91 KB
using HHECS.DAQWebClient.Dtos;
using HHECS.DAQWebClient.Models;
using HHECS.DAQWebClient.Services;
using HHECS.DAQWebClient.ViewModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace HHECS.DAQWebClient.Controllers
{
    [Authorize]
    public class SettingController : Controller
    {
        private readonly IFreeSql _freeSql;
        private readonly CommonService _commonService;
        private readonly IOTCloudService _cloudService;
        private readonly ILogger<SettingController> _logger;

        public SettingController(IFreeSql freeSql, CommonService commonService, IOTCloudService cloudService, ILogger<SettingController> logger)
        {
            _freeSql = freeSql;
            _commonService = commonService;
            _cloudService = cloudService;
            _logger = logger;
        }

        public IActionResult Index()
        {
            try
            {
                var codes = new List<ConfigType>
                {
                    ConfigType.ClientId,
                    ConfigType.CommitCount,
                    ConfigType.Area,
                    ConfigType.IsProductionEnvironment,
                    ConfigType.IOTCloundAPI,
                    ConfigType.IOTCloundDevelopmentAPI,
                    ConfigType.MqttWebSocketServer,
                    ConfigType.MqttWebSocketDevelopmentServer,
                }.Select(x => x.ToString()).ToList();

                var configs = _freeSql.Queryable<LocalConfig>().Where(x => codes.Contains(x.Code)).ToList(x => new LocalConfig
                {
                    Code = x.Code,
                    Value = x.Value,
                });
                var clientIdValue = configs.Where(x => x.Code == ConfigType.ClientId.ToString()).Select(x => x.Value).FirstOrDefault();
                var commitCountValue = configs.Where(x => x.Code == ConfigType.CommitCount.ToString()).Select(x => x.Value).FirstOrDefault();
                var areaValue = configs.Where(x => x.Code == ConfigType.Area.ToString()).Select(x => x.Value).FirstOrDefault();
                var isProductionEnvironmentValue = configs.Where(x => x.Code == ConfigType.IsProductionEnvironment.ToString()).Select(x => x.Value).FirstOrDefault();
                var iotCloundAPIValue = configs.Where(x => x.Code == ConfigType.IOTCloundAPI.ToString()).Select(x => x.Value).FirstOrDefault();
                var iotCloundDevelopmentAPIValue = configs.Where(x => x.Code == ConfigType.IOTCloundDevelopmentAPI.ToString()).Select(x => x.Value).FirstOrDefault();
                var mqttWebSocketServerValue = configs.Where(x => x.Code == ConfigType.MqttWebSocketServer.ToString()).Select(x => x.Value).FirstOrDefault();
                var mqttWebSocketDevelopmentServerValue = configs.Where(x => x.Code == ConfigType.MqttWebSocketDevelopmentServer.ToString()).Select(x => x.Value).FirstOrDefault();

                _ = Guid.TryParse(clientIdValue, out var clientId);
                _ = int.TryParse(commitCountValue, out var commitCount);
                _ = bool.TryParse(isProductionEnvironmentValue, out var isProductionEnvironment);

                var model = new SettingModel
                {
                    ClientId = clientId,
                    CommitCount = commitCount,
                    Area = areaValue ?? string.Empty,
                    IsProductionEnvironment = isProductionEnvironment,
                    IOTCloundAPI = iotCloundAPIValue ?? string.Empty,
                    IOTCloundDevelopmentAPI = iotCloundDevelopmentAPIValue ?? string.Empty,
                    MqttWebSocketServer = mqttWebSocketServerValue ?? string.Empty,
                    MqttWebSocketDevelopmentServer = mqttWebSocketDevelopmentServerValue ?? string.Empty
                };
                return View(model);
            }
            catch (Exception)
            {
                return View();
            }
        }

        [HttpPost]
        [Authorize(Roles = $"{nameof(RoleType.Admin)}")]
        public IActionResult UpdateConfig(LocalConfig model)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(model.Code))
                {
                    return Ok(new ResultDto
                    {
                        Code = 500,
                        Msg = $"Code不能为空"
                    });
                }

                var user = HttpContext.User.Identity?.Name ?? string.Empty;
                var config = _freeSql.Queryable<LocalConfig>().Where(x => x.Code == model.Code).First();
                if (config == null)
                {
                    config = new LocalConfig
                    {
                        Code = model.Code,
                        Name = model.Name,
                        Value = model.Value,
                        Remark = model.Remark,
                        Created = DateTime.Now,
                        CreatedBy = user,
                    };
                    _freeSql.Insert(config).ExecuteAffrows();
                }
                else
                {
                    _freeSql.Update<LocalConfig>(config.Id).Set(x => new LocalConfig
                    {
                        Value = model.Value,
                        Updated = DateTime.Now,
                        UpdatedBy = user,
                    }).ExecuteAffrows();
                }
                var msg = $"更新成功";
                _logger.LogInformation(msg);
                return Ok(new ResultDto
                {
                    Code = 0,
                    Msg = msg
                });
            }
            catch (Exception ex)
            {
                var msg = $"更新失败:{ex.Message}";
                _logger.LogError(msg);
                return Ok(new ResultDto
                {
                    Code = 0,
                    Msg = msg
                });
            }
        }

        [HttpPost]
        [Authorize(Roles = $"{nameof(RoleType.Admin)}")]
        public IActionResult UpdateEnvironment(bool isProductionEnvironment)
        {
            try
            {
                var result = _commonService.UpdateEnvironment(isProductionEnvironment);
                if (!result.Success)
                {
                    _logger.LogError(result.Msg);
                    return Ok(new ResultDto
                    {
                        Code = 500,
                        Msg = result.Msg
                    });
                }
                var msg = $"更新成功";
                _logger.LogInformation(msg);
                return Ok(new ResultDto
                {
                    Code = 0,
                    Msg = msg
                });
            }
            catch (Exception ex)
            {
                var msg = $"更新失败:{ex.Message}";
                _logger.LogError(msg);
                return Ok(new ResultDto
                {
                    Code = 0,
                    Msg = msg
                });
            }
        }
    }
}