184 lines
5.7 KiB
C#
184 lines
5.7 KiB
C#
using Model;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Web;
|
||
using System.Web.Http;
|
||
|
||
/// <summary>
|
||
/// AncestralTabletPositionController 的摘要描述
|
||
/// </summary>
|
||
public class AncestralTabletPositionController: ApiController
|
||
{
|
||
private Model.ezEntities _db = new Model.ezEntities();
|
||
|
||
[HttpGet]
|
||
[Route("api/ancestraltablet/position/getlist")]
|
||
public IHttpActionResult GetList([FromUri] int areaId)
|
||
{
|
||
var positions = _db.AncestralTabletPosition
|
||
.Where(p => p.AreaId == areaId)
|
||
.Select(p => new
|
||
{
|
||
PositionId = p.PositionId,
|
||
AreaId = p.AreaId,
|
||
PositionCode = p.PositionCode,
|
||
PositionName = p.PositionName,
|
||
Price = p.Price,
|
||
StatusCode = p.StatusCode,
|
||
Description = p.Description,
|
||
RowNo = p.RowNo,
|
||
ColumnNo = p.ColumnNo,
|
||
AncestralTabletRegistrant = _db.AncestralTabletRegistrant
|
||
.Where(r => r.PositionId == p.PositionId && r.IsActive == true)
|
||
.Select(r => new
|
||
{
|
||
r.RegistrantCode,
|
||
r.Name,
|
||
r.Phone,
|
||
r.Address,
|
||
r.RegisterDate,
|
||
r.Price,
|
||
r.StartDate,
|
||
r.EndDate,
|
||
r.IsLongTerm,
|
||
r.IsActive,
|
||
|
||
// 嵌套查询牌位记录(PositionRecord)
|
||
TabletRecord = r.AncestralTabletPositionRecord
|
||
.Select(pr => new
|
||
{
|
||
pr.RecordId,
|
||
pr.RegistrantCode,
|
||
pr.NPTitle,
|
||
pr.NPStandDate,
|
||
pr.NPYangShang,
|
||
pr.WPContent
|
||
})
|
||
.FirstOrDefault()
|
||
})
|
||
.FirstOrDefault()
|
||
})
|
||
.ToList();
|
||
|
||
return Ok(positions);
|
||
}
|
||
[HttpGet]
|
||
[Route("api/ancestraltablet/position/shortlist")]
|
||
public IHttpActionResult GetListWithShort([FromUri] int areaId)
|
||
{
|
||
//获取位置列表,简单信息
|
||
var positions = _db.AncestralTabletPosition
|
||
.Where(p => p.AreaId == areaId)
|
||
.Select(p => new
|
||
{
|
||
PositionId = p.PositionId,
|
||
AreaId = p.AreaId,
|
||
PositionCode = p.PositionCode,
|
||
PositionName = p.PositionName,
|
||
Price = p.Price,
|
||
StatusCode = p.StatusCode,
|
||
Description = p.Description,
|
||
RowNo = p.RowNo,
|
||
ColumnNo = p.ColumnNo,
|
||
isCanUse = p.StatusCode == "available" ? true : false,
|
||
})
|
||
.ToList();
|
||
|
||
return Ok(positions);
|
||
}
|
||
[HttpPost]
|
||
[Route("api/ancestraltablet/position/batchcreate")]
|
||
public IHttpActionResult BatchCreatePosition([FromBody] List<AncestralTabletPositionDto> positions)
|
||
{
|
||
if (positions == null || positions.Count == 0)
|
||
return BadRequest("未接收到任何位置数据");
|
||
|
||
try
|
||
{
|
||
foreach (var dto in positions)
|
||
{
|
||
var entity = new AncestralTabletPosition
|
||
{
|
||
AreaId = dto.AreaId,
|
||
PositionCode = dto.PositionCode,
|
||
PositionName = dto.PositionName,
|
||
Price = dto.Price,
|
||
StatusCode = dto.StatusCode,
|
||
Description = dto.Description,
|
||
RowNo = dto.RowNo,
|
||
ColumnNo = dto.ColumnNo
|
||
};
|
||
|
||
_db.AncestralTabletPosition.Add(entity);
|
||
}
|
||
|
||
_db.SaveChanges();
|
||
|
||
return Ok(new { message = "批量新增成功", count = positions.Count });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
string message = ex.InnerException?.InnerException?.Message ?? ex.Message;
|
||
|
||
return Content(HttpStatusCode.InternalServerError, new
|
||
{
|
||
message = "批量新增失败",
|
||
exceptionMessage = message
|
||
});
|
||
}
|
||
}
|
||
|
||
[HttpPost]
|
||
[Route("api/ancestraltablet/position/edit")]
|
||
public IHttpActionResult EditPosition([FromBody] AncestralTabletPositionDto pos)
|
||
{
|
||
var oldPos = _db.AncestralTabletPosition
|
||
.FirstOrDefault(p => p.AreaId == pos.AreaId && p.PositionCode == pos.PositionCode);
|
||
if(oldPos == null) return NotFound();
|
||
try
|
||
{
|
||
oldPos.PositionName = pos.PositionName;
|
||
oldPos.Price = pos.Price;
|
||
oldPos.StatusCode = pos.StatusCode;
|
||
|
||
// 保存到数据库
|
||
_db.SaveChanges();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return InternalServerError(ex);
|
||
}
|
||
return Ok(new { message="更新成功", code=200});
|
||
}
|
||
[HttpDelete]
|
||
[Route("api/ancestraltablet/position/delete/{positionId}")]
|
||
public IHttpActionResult DeletePosition(int positionId)
|
||
{
|
||
var pos = _db.AncestralTabletPosition.FirstOrDefault(p => p.PositionId == positionId);
|
||
|
||
if (pos == null)
|
||
{
|
||
return NotFound();
|
||
}
|
||
|
||
_db.AncestralTabletPosition.Remove(pos);
|
||
_db.SaveChanges();
|
||
|
||
return Ok("删除成功");
|
||
}
|
||
public class AncestralTabletPositionDto
|
||
{
|
||
public int PositionId { get; set; }
|
||
public int AreaId { get; set; }
|
||
public string PositionCode { get; set; }
|
||
public string PositionName { get; set; }
|
||
public int? Price { get; set; }
|
||
public string StatusCode { get; set; }
|
||
public string Description { get; set; }
|
||
public int? RowNo { get; set; }
|
||
public int? ColumnNo { get; set; }
|
||
}
|
||
|
||
} |