361 lines
11 KiB
C#
361 lines
11 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Web;
|
||
using System.Web.Http;
|
||
using System.Web.UI.WebControls;
|
||
|
||
/// <summary>
|
||
/// AncestralTabletRecordController 的摘要描述
|
||
/// </summary>
|
||
public class AncestralTabletRecordController: ApiController
|
||
{
|
||
private Model.ezEntities _db = new Model.ezEntities();
|
||
[HttpGet]
|
||
[Route("api/ancestraltablet/registrant/getlist")]
|
||
public IHttpActionResult GetRegistrantList()
|
||
{
|
||
//获取登记人列表
|
||
var data = _db.AncestralTabletRegistrant.Select(x => new
|
||
{
|
||
x.RegistrantCode,
|
||
x.Name,
|
||
x.Phone,
|
||
x.Address,
|
||
x.RegisterDate,
|
||
x.Price,
|
||
x.PositionId,
|
||
x.StartDate,
|
||
x.EndDate,
|
||
x.IsLongTerm,
|
||
x.IsActive,
|
||
x.CreatedAt,
|
||
x.UpdatedAt
|
||
})
|
||
.Take(1000) // 取前1000条
|
||
.ToList(); ;
|
||
return Ok(data);
|
||
}
|
||
[HttpPost]
|
||
[Route("api/ancestraltablet/registrant/getlistbypage")]
|
||
public IHttpActionResult GetRegistrantListByPage([FromBody] RegistrantSearchDto searchDto)
|
||
{
|
||
//获取登记人列表
|
||
var query = _db.AncestralTabletRegistrant.AsQueryable();
|
||
if( !string.IsNullOrEmpty(searchDto.searchName))
|
||
{
|
||
query = query.Where(r => r.Name == searchDto.searchName);
|
||
}
|
||
var tatol = query.Count();
|
||
var data = query.Select(x => new
|
||
{
|
||
x.RegistrantCode,
|
||
x.Name,
|
||
x.Phone,
|
||
x.Address,
|
||
x.RegisterDate,
|
||
x.Price,
|
||
x.PositionId,
|
||
x.StartDate,
|
||
x.EndDate,
|
||
x.IsLongTerm,
|
||
x.IsActive,
|
||
x.CreatedAt,
|
||
x.UpdatedAt
|
||
})
|
||
.OrderByDescending(a => a.CreatedAt)
|
||
.Skip((searchDto.page - 1) * searchDto.pageSize)
|
||
.Take(searchDto.pageSize) // 取前1000条
|
||
.ToList();
|
||
return Ok(new
|
||
{
|
||
data = data,
|
||
total = tatol
|
||
|
||
});
|
||
}
|
||
|
||
[HttpGet]
|
||
[Route("api/ancestraltablet/registrant/getbycode")]
|
||
public IHttpActionResult GetRegistrantByCode([FromUri] string registrantCode)
|
||
{
|
||
var r = _db.AncestralTabletRegistrant.Find(registrantCode);
|
||
if (r == null)
|
||
{
|
||
return NotFound();
|
||
}
|
||
var rDto = new
|
||
{
|
||
r.RegistrantCode,
|
||
r.Name,
|
||
r.Phone,
|
||
r.Address,
|
||
r.RegisterDate,
|
||
r.Price,
|
||
r.StartDate,
|
||
r.EndDate,
|
||
r.IsLongTerm,
|
||
r.IsActive,
|
||
r.PositionId,
|
||
positionName = r.AncestralTabletPosition?.PositionName,
|
||
|
||
|
||
// 嵌套查询牌位记录(PositionRecord)
|
||
TabletRecord = r.AncestralTabletPositionRecord
|
||
.Select(pr => new
|
||
{
|
||
pr.RecordId,
|
||
pr.RegistrantCode,
|
||
pr.NPTitle,
|
||
pr.NPStandDate,
|
||
pr.NPYangShang,
|
||
pr.WPContent,
|
||
})
|
||
.FirstOrDefault()
|
||
};
|
||
return Ok(rDto);
|
||
}
|
||
|
||
[HttpPost]
|
||
[Route("api/ancestraltablet/registrant/create")]
|
||
public IHttpActionResult CreateRegistrant([FromBody] AncestralTabletRegistrantDto dto)
|
||
{
|
||
//新增登记人api
|
||
if (dto == null)
|
||
{
|
||
return BadRequest("请求体不能为空");
|
||
}
|
||
if(!string.IsNullOrEmpty(dto.RegistrantCode))
|
||
{
|
||
return BadRequest("RegistrantCode 应传递空");
|
||
}
|
||
try
|
||
{
|
||
dto.RegistrantCode = GenerateRegistrantCode();
|
||
|
||
// 设置默认创建时间
|
||
dto.CreatedAt = DateTime.Now;
|
||
var entity = new Model.AncestralTabletRegistrant
|
||
{
|
||
RegistrantCode = dto.RegistrantCode,
|
||
Name = dto.Name,
|
||
Phone = dto.Phone,
|
||
Address = dto.Address,
|
||
RegisterDate = dto.RegisterDate,
|
||
Price = dto.Price,
|
||
PositionId = dto.PositionId,
|
||
StartDate = dto.startDate,
|
||
EndDate = dto.endDate,
|
||
IsLongTerm = dto.isLongTerm,
|
||
IsActive = dto.isActive,
|
||
CreatedAt = dto.CreatedAt,
|
||
UpdatedAt = dto.UpdatedAt
|
||
};
|
||
|
||
// 假设你有一个 EF DbContext(如 _db)
|
||
_db.AncestralTabletRegistrant.Add(entity);
|
||
if (dto.PositionId != null)
|
||
{
|
||
var position = _db.AncestralTabletPosition
|
||
.FirstOrDefault(p => p.PositionId == dto.PositionId);
|
||
|
||
if (position != null)
|
||
{
|
||
position.StatusCode = "used"; // 或者根据你的枚举/字段设置
|
||
}
|
||
}
|
||
|
||
_db.SaveChanges();
|
||
|
||
return Ok(new { message = "登记成功", registrantCode = entity.RegistrantCode });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return InternalServerError(ex);
|
||
}
|
||
}
|
||
[HttpPost]
|
||
[Route("api/ancestraltablet/registrant/update")]
|
||
public IHttpActionResult UpdateRegistrant([FromBody] AncestralTabletRegistrantDto dto)
|
||
{
|
||
if (dto == null)
|
||
{
|
||
return BadRequest("请求体不能为空");
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(dto.RegistrantCode))
|
||
{
|
||
return BadRequest("缺少 RegistrantCode,无法进行更新操作");
|
||
}
|
||
|
||
try
|
||
{
|
||
// 查找原始资料
|
||
var entity = _db.AncestralTabletRegistrant
|
||
.FirstOrDefault(r => r.RegistrantCode == dto.RegistrantCode);
|
||
|
||
if (entity == null)
|
||
{
|
||
return NotFound(); // 没有对应记录
|
||
}
|
||
|
||
// ===== 处理安位状态 =====
|
||
if (entity.PositionId != dto.PositionId)
|
||
{
|
||
// 1. 原来的安位设置为可用
|
||
if (entity.PositionId != null)
|
||
{
|
||
var oldPosition = _db.AncestralTabletPosition
|
||
.FirstOrDefault(p => p.PositionId == entity.PositionId);
|
||
|
||
if (oldPosition != null)
|
||
{
|
||
oldPosition.StatusCode = "available"; // 可用
|
||
}
|
||
}
|
||
|
||
// 2. 新的安位设置为已使用
|
||
if (dto.PositionId != null)
|
||
{
|
||
var newPosition = _db.AncestralTabletPosition
|
||
.FirstOrDefault(p => p.PositionId == dto.PositionId);
|
||
|
||
if (newPosition != null)
|
||
{
|
||
newPosition.StatusCode = "used"; // 已使用
|
||
}
|
||
}
|
||
|
||
// 更新登记人安位
|
||
entity.PositionId = dto.PositionId;
|
||
}
|
||
|
||
// 更新其它字段
|
||
entity.Name = dto.Name;
|
||
entity.Phone = dto.Phone;
|
||
entity.Address = dto.Address;
|
||
entity.RegisterDate = dto.RegisterDate;
|
||
entity.Price = dto.Price;
|
||
entity.StartDate = dto.startDate;
|
||
entity.EndDate = dto.endDate;
|
||
entity.IsLongTerm = dto.isLongTerm;
|
||
entity.IsActive = dto.isActive;
|
||
entity.UpdatedAt = DateTime.Now;
|
||
|
||
_db.SaveChanges();
|
||
|
||
return Ok(new { message = "登记人更新成功", registrantCode = entity.RegistrantCode });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return InternalServerError(ex);
|
||
}
|
||
}
|
||
|
||
[HttpPost]
|
||
[Route("api/ancestraltablet/pw/create")]
|
||
public IHttpActionResult CreatePW([FromBody] AncestralTabletPositionRecordDto dto)
|
||
{
|
||
if (dto == null)
|
||
return BadRequest("请求体不能为空");
|
||
|
||
if (string.IsNullOrEmpty(dto.RegistrantCode))
|
||
return BadRequest("登记人编号(RegistrantCode)不能为空");
|
||
|
||
try
|
||
{
|
||
// 映射到数据库实体
|
||
var entity = new Model.AncestralTabletPositionRecord
|
||
{
|
||
RegistrantCode = dto.RegistrantCode,
|
||
NPTitle = dto.NPTitle,
|
||
NPStandDate = dto.NPStandDate,
|
||
NPYangShang = dto.NPYangShang,
|
||
WPContent = dto.WPContent,
|
||
CreatedAt = DateTime.Now,
|
||
UpdatedAt = null
|
||
};
|
||
|
||
_db.AncestralTabletPositionRecord.Add(entity);
|
||
_db.SaveChanges();
|
||
|
||
return Ok(new { message = "牌位登记成功", recordId = entity.RecordId });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return InternalServerError(ex);
|
||
}
|
||
}
|
||
[HttpPost]
|
||
[Route("api/ancestraltablet/pw/update")]
|
||
public IHttpActionResult UpdatePW([FromBody] AncestralTabletPositionRecordDto dto)
|
||
{
|
||
if (dto == null)
|
||
return BadRequest("请求体不能为空");
|
||
try
|
||
{
|
||
var entity = _db.AncestralTabletPositionRecord.Find(dto.RecordId);
|
||
// 映射到数据库实体
|
||
if (entity == null)
|
||
return BadRequest("牌位不存在,更新失败");
|
||
entity.RegistrantCode = dto.RegistrantCode;
|
||
entity.NPTitle = dto.NPTitle;
|
||
entity.NPStandDate = dto.NPStandDate;
|
||
entity.NPYangShang = dto.NPYangShang;
|
||
entity.WPContent = dto.WPContent;
|
||
entity.UpdatedAt = DateTime.Now;
|
||
_db.SaveChanges();
|
||
|
||
return Ok(new { message = "牌位更新成功", recordId = entity.RecordId });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return InternalServerError(ex);
|
||
}
|
||
}
|
||
|
||
public string GenerateRegistrantCode(string prefix = "REG", int randomLength = 6)
|
||
{
|
||
string datePart = DateTime.Now.ToString("yyyyMMdd");
|
||
|
||
string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||
var random = new Random();
|
||
var suffix = new string(Enumerable.Repeat(chars, randomLength)
|
||
.Select(s => s[random.Next(s.Length)]).ToArray());
|
||
|
||
return $"{prefix}{datePart}{suffix}";
|
||
}
|
||
public class AncestralTabletRegistrantDto
|
||
{
|
||
public string RegistrantCode { get; set; }
|
||
public string Name { get; set; }
|
||
public string Phone { get; set; }
|
||
public string Address { get; set; }
|
||
public DateTime RegisterDate { get; set; }
|
||
public int? Price { get; set; }
|
||
public int? PositionId { get; set; }
|
||
public DateTime startDate { get; set; }
|
||
public DateTime? endDate { get; set; }
|
||
public bool isLongTerm { get; set; } = false;
|
||
public bool isActive { get; set; } = true;
|
||
public DateTime CreatedAt { get; set; }
|
||
public DateTime? UpdatedAt { get; set; }
|
||
}
|
||
public class AncestralTabletPositionRecordDto
|
||
{
|
||
public int? RecordId { get; set; }
|
||
public string RegistrantCode { get; set; }
|
||
public string NPTitle { get; set; }
|
||
public DateTime NPStandDate { get; set; }
|
||
public string NPYangShang { get; set; }
|
||
public string WPContent { get; set; }
|
||
}
|
||
public class RegistrantSearchDto
|
||
{
|
||
public int page { get; set; } = 1;
|
||
public int pageSize { get; set; } = 10;
|
||
public string searchName { get; set; }
|
||
}
|
||
|
||
|
||
} |