public class BaseApiController<T> : ApiController where T : class { BaseDal<T> baseDal = new BaseDal<T>(); #region 通用Api /// <summary> /// 新增 /// </summary> /// <param name="value"></param> /// <returns></returns> [System.Web.Http.HttpPost] public virtual HttpResponseMessage Insert([FromBody] JObject value) { T model = value.ToObject<T>(); int res = DbHelper.Insert<T>(model); JObject obj = new JObject(); obj.Add("code", "0"); obj.Add("msg", "成功"); obj.Add("data", res); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json") }; return result; } /// <summary> /// 批量新增 /// </summary> /// <param name="value"></param> /// <returns></returns> [System.Web.Http.HttpPost] public virtual HttpResponseMessage InsertBatch([FromBody] List<T> value) { bool res = DbHelper.InsertBatch<T>(value); JObject obj = new JObject(); obj.Add("code", "0"); obj.Add("msg", "成功"); obj.Add("data", res); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json") }; return result; } /// <summary> /// 修改 /// </summary> /// <param name="value"></param> /// <returns></returns> [System.Web.Http.HttpPost] public virtual HttpResponseMessage Update([FromBody] JObject value) { T model = value.ToObject<T>(); bool res = DbHelper.Update<T>(model); JObject obj = new JObject(); obj.Add("code", "0"); obj.Add("msg", "成功"); obj.Add("data", res); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json") }; return result; } /// <summary> /// 更新非空字段部分字段 默认第一个字段为更新主键 /// </summary> /// <param name="value"></param> /// <returns></returns> [System.Web.Http.HttpPost] public virtual HttpResponseMessage UpdateNotNull([FromBody] JObject value) { T model = value.ToObject<T>(); bool res = baseDal.UpdateNotNull(model); JObject obj = new JObject(); obj.Add("code", "0"); obj.Add("msg", "成功"); obj.Add("data", res); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json") }; return result; } /// <summary> /// 删除 /// </summary> /// <param name="value"></param> /// <returns></returns> [System.Web.Http.HttpPost] public virtual HttpResponseMessage Delete([FromBody] JObject value) { T model = value.ToObject<T>(); bool res = DbHelper.Delete<T>(model); JObject obj = new JObject(); obj.Add("code", "0"); obj.Add("msg", "成功"); obj.Add("data", res); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json") }; return result; } /// <summary> /// 批量删除 /// </summary> /// <param name="value"></param> /// <returns></returns> [System.Web.Http.HttpPost] //public virtual HttpResponseMessage DeleteBatch([FromBody] T value) public virtual HttpResponseMessage DeleteBatch([FromBody] List<T> value) { //List<T> model = value.ToObject<List<T>>(); bool res = DbHelper.DeleteBatch<T>(value); JObject obj = new JObject(); obj.Add("code", "0"); obj.Add("msg", "成功"); obj.Add("data", res); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json") }; return result; } /// <summary> /// 查询一个实体 /// </summary> /// <param name="id"></param> /// <returns></returns> [System.Web.Http.HttpGet] public virtual HttpResponseMessage Get(string id) { T res = DbHelper.Get<T>(id); JObject obj = new JObject(); obj.Add("code", "0"); obj.Add("msg", "成功"); obj.Add("data", res.ToJson()); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json") }; return result; } /// <summary> /// 查询全部 /// </summary> /// <returns></returns> [System.Web.Http.HttpGet] public virtual HttpResponseMessage GetListAll() { List<T> res = DbHelper.GetListAll<T>(); JObject obj = new JObject(); obj.Add("code", "0"); obj.Add("msg", "成功"); obj.Add("data", res.ToJson()); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json") }; return result; } #endregion #region 示例Api需重写 /// <summary> /// 以条件查询 /// </summary> /// <param name="where">条件</param> /// <returns></returns> [System.Web.Http.HttpPost] public virtual HttpResponseMessage GetList([FromBody] JObject where) { var v = new { UserID = 5 }; List<T> res = baseDal.GetList(where); JObject obj = new JObject(); obj.Add("code", "0"); obj.Add("msg", "成功"); obj.Add("data", res.ToJson()); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json") }; return result; } /// <summary> /// SQL分页示例 /// </summary> /// <returns></returns> [System.Web.Http.HttpPost] public virtual HttpResponseMessage GetPageListForSQL([FromBody] JObject value) { PageCriteria pageCriteria = new PageCriteria(); StringBuilder sb = new StringBuilder(); sb.Append("UserName like @UserName"); pageCriteria.ParameterList.Add(new ParameterDict() { ParamName = "UserName", ParamValue = "%jack%" }); sb.Append(" and State=@State"); pageCriteria.ParameterList.Add(new ParameterDict() { ParamName = "State", ParamValue = 0 }); pageCriteria.Condition = sb.ToString(); pageCriteria.CurrentPage = 5; pageCriteria.Fields = " * "; pageCriteria.PageSize = 10; //pageCriteria.PrimaryKey = " id"; pageCriteria.Sort = " UserID desc"; pageCriteria.TableName = "UserInfo"; PageDataView<T> res = baseDal.GetPageListForSQL(value); JObject obj = new JObject(); obj.Add("code", "0"); obj.Add("msg", "成功"); obj.Add("data", res.ToJson()); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json") }; return result; } /// <summary> /// Dapper扩展分页 /// </summary> /// <param name="value"></param> /// <returns></returns> [System.Web.Http.HttpPost] public virtual HttpResponseMessage GetPage([FromBody] Object value) { //Object model = value.ToObject<Object>(); int pageIndex = 0; int pageSize = 5; List<T> res = baseDal.GetPage(pageIndex, pageSize, out long allRowsCount); long t = allRowsCount; JObject obj = new JObject(); obj.Add("code", "0"); obj.Add("msg", "成功"); obj.Add("data", res.ToJson()); obj.Add("allRowsCount", allRowsCount); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json") }; return result; } #endregion }