using Backend.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Repository.BackendRepository.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; namespace Backend.ApiControllers { public class DeviceApiController : MyBaseApiController { private readonly IBackendRepository backendRepository; public DeviceApiController(IBackendRepository backendRepository) { this.backendRepository = backendRepository; } /// /// 修改設備名稱 /// /// /// [HttpPost] [Route("api/Device/SaveChangeName")] public async Task>> SaveChangeName(ChangeName change) { ApiResult apiResult = new ApiResult(); if (!jwtlife) { apiResult.Code = "5000"; return BadRequest(apiResult); } try { if (change.ChooseTable == 0) { //一般設備 var GetOne = await backendRepository.GetOneAsync("device", $" device_number = '{change.TagName}'"); if (GetOne == null) { apiResult.Data = "查無資料"; apiResult.Code = "0001"; } else { //修改設備名稱 Dictionary ChangeN = new Dictionary(); ChangeN = new Dictionary() { { "@full_name", change.ChangeN}, { "@updated_at",DateTime.Now}, }; await backendRepository.UpdateOneByCustomTable(ChangeN, "device", $" device_number = '{change.TagName}'"); //記錄使用者操作紀錄 Dictionary userOperatorLog = new Dictionary() { { "@user_guid", myUser.userinfo_guid }, { "@operation_type", 1 }, //1:名稱修改 { "@building_guid", GetOne.building_tag }, { "@main_system_guid", GetOne.device_system_tag }, { "@sub_system_guid", GetOne.device_name_tag }, { "@floor_guid", GetOne.floor_tag }, { "@device_guid", GetOne.Device_guid }, { "@action_name", "修改名稱" }, { "@parameter", JsonConvert.SerializeObject(change) }, }; await backendRepository.AddOneByCustomTable(userOperatorLog, "operation_log"); apiResult.Data = "更改成功"; apiResult.Code = "0000"; } } else { //燈控設備 var GetOne = await backendRepository.GetOneAsync("device_master", $" device_master_number = '{change.TagName}'"); if (GetOne == null) { apiResult.Data = "查無資料"; apiResult.Code = "0001"; } else { Dictionary ChangeN = new Dictionary(); ChangeN = new Dictionary() { { "@device_master_full_name", change.ChangeN}, { "@updated_at",DateTime.Now}, }; await backendRepository.UpdateOneByCustomTable(ChangeN, "device_master", $" device_master_number = '{change.TagName}'"); ////記錄使用者操作紀錄 //Dictionary userOperatorLog = new Dictionary() //{ // { "@user_guid", myUser }, // { "@operation_type", 1 }, //1:名稱修改 // { "@building_guid", GetOne.Building_guid }, // { "@main_system_guid", GetOne.Main_system_guid }, // { "@sub_system_guid", GetOne.Sub_system_guid }, // { "@floor_guid", GetOne.Floor_guid }, // { "@device_guid", GetOne.Device_guid }, // { "@action_name", "修改名稱" }, // { "@parameter", JsonConvert.SerializeObject(change) }, //}; //await backendRepository.AddOneByCustomTable(userOperatorLog, "operation_log"); apiResult.Data = "更改成功"; apiResult.Code = "0000"; } } } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); return Ok(apiResult); } return Ok(apiResult); } } }