Backend/TCM_API/Controllers/manage/Health_detail_tableController.cs
2025-02-22 19:38:02 +08:00

207 lines
6.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using TCM_API.Entities;
using TCM_API.Models;
using TCM_API.Models.manage;
using TCM_API.Services;
using TCM_API.ViewModels;
namespace TCM_API.Controllers.manage
{
[Route("api/[controller]")]
[ApiController]
public class Health_detail_tableController : Controller
{
private readonly SqlContext _context;
public Health_detail_tableController(SqlContext context)
{
_context = context;
}
#region
/// <summary>
/// 獲取所有健管師
/// </summary>
[HttpGet("get_all_health")]
public async Task<ActionResult<object>> GetHealth_all_deatil()
{
var data = await (from c in _context.health_detail_table
orderby c.data_create_time
select new
{
health_name = c.health_name,
guid = c.guid
}).ToListAsync();
return Ok(data);
}
#endregion
#region
/// <summary>
/// 獲取指定企業健管師
/// </summary>
[HttpGet("get_company_all_health-{guid}")]
public async Task<ActionResult<object>> GetCompany_Health_all_deatil(string guid)
{
var data = await (from a in _context.health_detail_table
join b in _context.user_table on a.guid equals b.guid into joined
from b in joined.DefaultIfEmpty() // LEFT JOIN
where a.company_guid == guid
orderby a.data_create_time
select new
{
health_name = a.health_name,
guid = a.guid,
level = b.level
}).ToListAsync();
return Ok(data);
}
#endregion
#region
/// <summary>
/// 獲取指定企業健管師
/// </summary>
[HttpGet("get_company_all_health_count-{guid}")]
public async Task<ActionResult<object>> GetCompany_Health_all_count(string guid)
{
var data = await (from a in _context.health_detail_table
join b in _context.user_table on a.guid equals b.guid into userGroup
from b in userGroup.DefaultIfEmpty() // 使用 DefaultIfEmpty() 實現 LEFT JOIN
where a.company_guid == guid
group a by b.level into g
select new
{
level = g.Key,
level_count = g.Count() // 計算每個 level 的數量
}).ToListAsync();
return Ok(data);
}
#endregion
#region
[HttpGet("get_health_person-{guid}")]
public async Task<ActionResult<object>> GetCompany_deatil(string guid)
{
var data = await (from a in _context.health_detail_table
join b in _context.user_table on a.guid equals b.guid into joined
from b in joined.DefaultIfEmpty()
where b.guid == guid && a.guid == guid
select new
{
guid = a.guid,
level = b.level,
health_name = a.health_name,
user_name = b.username,
email = b.email
}).FirstOrDefaultAsync();
if (data == null)
{
return NotFound();
}
return data;
}
#endregion
#region
/// <summary>
/// 編輯企業啟用
/// </summary>
[HttpPost("healther_enable")]
public async Task<IActionResult> UpdateEnable(EnableDto enableDto)
{
var edit_data = await (from a in _context.user_table
where a.guid == enableDto.Guid
select a).FirstOrDefaultAsync();
if (edit_data == null)
{
return NotFound("找不到該使用者");
}
// 更新 level 屬性
edit_data.level = enableDto.level;
// 標記為已修改
_context.user_table.Update(edit_data);
await _context.SaveChangesAsync();
return Ok();
}
#endregion
#region
/// <summary>
/// 新增健管師
/// </summary>
[HttpPost("Add_health")]
public async Task<ActionResult<Health_detail_table>> PostHealth_detail_table_1(Add_health add_health)
{
// 創建Guid
Guid new_guid = Guid.NewGuid();
// 創建 健管師詳細
Health_detail_table health_detail_table = new Health_detail_table();
health_detail_table.guid = new_guid.ToString();
health_detail_table.company_guid = add_health.company_guid;
health_detail_table.data_create_time = DateTime.Now;
health_detail_table.health_name = add_health.username;
//創建健管師帳號
User user = new User();
user.firstname = "healther";
user.lastname = add_health.lastname;
user.email = add_health.email;
user.password = add_health.password;
user.username = add_health.username;
user.level = "6";
user.guid = new_guid.ToString();
//儲存
_context.health_detail_table.Add(health_detail_table);
_context.user_table.Add(user);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Health_detail_tableExists(health_detail_table.guid))
{
return Conflict();
}
else
{
throw;
}
}
return Ok();
}
#endregion
private bool Health_detail_tableExists(string id)
{
return _context.health_detail_table.Any(e => e.guid == id);
}
}
}