using $ProjectNamePrefix$.Core.Common.HttpContextUser;
using $ProjectNamePrefix$.Core.Extensions.Others;
using $ProjectNamePrefix$.Core.IServices;
using $ProjectNamePrefix$.Core.Model;
using $ProjectNamePrefix$.Core.Model.CommonModel;
using $ProjectNamePrefix$.Core.Model.Models;
using $ProjectNamePrefix$.Core.Model.ParamModels;
using $ProjectNamePrefix$.Core.Utility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
namespace $ProjectNamePrefix$.Core.Api.Controllers
{
///
/// $ChinaComment$
///
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class $EntityName$Controller : ControllerBase
{
private readonly I$EntityName$Services _$EntityName$Services;
private readonly IUser _user;
private readonly ILogger<$EntityName$Controller> _logger;
public $EntityName$Controller(I$EntityName$Services $EntityName$Services, IUser user, ILogger<$EntityName$Controller> logger)
{
_$EntityName$Services = $EntityName$Services;
_user = user;
_logger = logger;
}
///
/// ·ÖÒ³»ñÈ¡$ChinaComment$Áбí
///
/// ɸѡÌõ¼þ
/// »ñÈ¡½á¹û
[HttpPost]
public async Task>> Get([FromBody] $EntityName$Param param)
{
if (string.IsNullOrEmpty(param.searchVal) || string.IsNullOrWhiteSpace(param.searchVal))
{
param.searchVal = string.Empty;
}
var whereConditions = WhereConditionsExtensions.GetWhereConditions<$EntityName$, $EntityName$Param>(param);
if (!whereConditions.IsSuccess) {
return new MessageModel>()
{
msg = whereConditions.ErrMsg,
success = false,
response = null
};
}
var data = await _$EntityName$Services.QueryPage(whereConditions.data, param.page, param.pageSize, " $OrderByName$ desc ");
return new MessageModel>()
{
msg = "»ñÈ¡³É¹¦",
success = true,
response = data
};
}
///
/// »ñÈ¡µ¥¸ö$ChinaComment$
///
/// Ö÷¼ü
/// »ñÈ¡½á¹û
[HttpGet]
public async Task> Get(int id = 0)
{
return new MessageModel<$EntityName$>()
{
msg = "»ñÈ¡³É¹¦",
success = true,
response = await _$EntityName$Services.QueryById(id)
};
}
///
/// ÐÂÔö$ChinaComment$
///
/// ÒªÐÂÔöµÄ$ChinaComment$¶ÔÏó
///
[HttpPost]
public async Task> Post([FromBody] $EntityName$ request)
{
try
{
#region ÑéÖ¤
MessageModel retBody = null;
retBody = ValidateDataHelper.CommonValidateIsNULL<$EntityName$, string>(request);
if (retBody != null)
{
return retBody;
}
List columnsList = null;
columnsList = new List() {
$ValidateEmptyForInsert$
};
retBody = ValidateDataHelper.CommonValidate<$EntityName$, string>(request, columnsList);
if (retBody != null)
{
return retBody;
}
#endregion
request.CreateTime = request.ModifyTime = DateTime.Now;
request.CreateBy = request.ModifyBy = _user.Name;
request.CreateId = request.ModifyId = _user.ID;
request.OperationRemark = "Ìí¼Ó";
var id = await _$EntityName$Services.Add(request);
var success = id > 0;
if (success)
{
return MessageModel.Success("Ìí¼Ó³É¹¦", id.ObjToString());
}
return MessageModel.Fail("Ìí¼Óʧ°Ü");
}
catch (Exception ex)
{
_logger.LogError(ex,"ÐÂÔö$ChinaComment$³öÏÖÒì³£:" + ex.Message);
return MessageModel.Fail("ÐÂÔö$ChinaComment$³öÏÖÒì³£:" + ex.Message);
}
}
///
/// ¸üÐÂ$ChinaComment$
///
/// Òª¸üеÄ$ChinaComment$¶ÔÏó
/// ¸üнá¹û
[HttpPut]
public async Task> Put([FromBody] $EntityName$ request)
{
try
{
#region ÑéÖ¤
MessageModel retBody = null;
retBody = ValidateDataHelper.CommonValidateIsNULL<$EntityName$, string>(request);
if (retBody != null)
{
return retBody;
}
List columnsList = null;
columnsList = new List()
{
$ValidateEmptyForUpdate$
};
retBody = ValidateDataHelper.CommonValidate<$EntityName$, string>(request, columnsList);
if (retBody != null)
{
return retBody;
}
#endregion
var dbObj = await _$EntityName$Services.QueryById(request.Id);
if (dbObj == null)
{
return MessageModel.Fail("Êý¾Ý²»´æÔÚ!");
}
request.ModifyTime = DateTime.Now;
request.ModifyBy = _user.Name;
request.ModifyId = _user.ID;
request.OperationRemark = "¸üÐÂ";
var success = await _$EntityName$Services.Update(request);
if (success)
{
return MessageModel.Success("¸üгɹ¦", request.Id.ObjToString());
}
return MessageModel.Fail("¸üÐÂʧ°Ü");
}
catch (Exception ex)
{
_logger.LogError(ex,"¸üÐÂ$ChinaComment$³öÏÖÒì³£:" + ex.Message);
return MessageModel.Fail("¸üÐÂ$ChinaComment$³öÏÖÒì³£:" + ex.Message);
}
}
///
/// ɾ³ý$ChinaComment$
///
/// Ö÷¼ü
/// ɾ³ý½á¹û
[HttpDelete]
public async Task> Delete(int id = 0)
{
try
{
var data = new MessageModel();
if (id > 0)
{
var detail = await _$EntityName$Services.QueryById(id);
if (detail != null)
{
data.success = await _$EntityName$Services.DeleteById(detail.Id);
if (data.success)
{
data.msg = "ɾ³ý³É¹¦";
data.response = detail?.Id.ObjToString();
}
}
else {
return MessageModel.Fail("Êý¾Ý²»´æÔÚ!");
}
}
else
{
data.success = false;
data.msg = "ɾ³ýʧ°Ü,$ChinaComment$²»´æÔÚ";
}
return data;
}
catch (Exception ex)
{
_logger.LogError(ex,"ɾ³ý$ChinaComment$³öÏÖÒì³£:" + ex.Message);
return MessageModel.Fail("ɾ³ý$ChinaComment$³öÏÖÒì³£:" + ex.Message);
}
}
///
/// Â߼ɾ³ý$ChinaComment$
///
/// Ö÷¼ü
/// ɾ³ý½á¹û
[HttpDelete]
public async Task> LogicDelete(int id = 0)
{
try
{
var data = new MessageModel();
if (id > 0)
{
var detail = await _$EntityName$Services.QueryById(id);
if (detail != null)
{
detail.ModifyTime = DateTime.Now;
detail.ModifyBy = _user.Name;
detail.ModifyId = _user.ID;
detail.IsDeleted = true;
detail.OperationRemark = "ɾ³ý";
data.success = await _$EntityName$Services.Update(detail);
if (data.success)
{
data.msg = "ɾ³ý³É¹¦";
data.response = detail?.Id.ObjToString();
}
}
}
else
{
data.success = false;
data.msg = "ɾ³ýʧ°Ü,Êý¾Ý²»´æÔÚ";
}
return data;
}
catch (Exception ex)
{
_logger.LogError(ex,"Â߼ɾ³ý$ChinaComment$³öÏÖÒì³£:" + ex.Message);
return MessageModel.Fail("Â߼ɾ³ý$ChinaComment$³öÏÖÒì³£:" + ex.Message);
}
}
}
}