博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
web api+递归树型结构
阅读量:4677 次
发布时间:2019-06-09

本文共 11223 字,大约阅读时间需要 37 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Http;using System.Web.Http;using SqlSugar;using Models;using WebApplication.Dao;using System.Text;namespace WebApplication.Controllers{    ///     /// 分类接口    ///     public class classificationController : ApiController    {        ///         /// 获取分类列表(条件查询,分页) http://192.168.2.177:1222/api/classification/GetclassificationListPage?pageIndex=1&pageSize=10&status=2&title=        ///         /// 分页索引        /// 分页大小        /// 状态:0:下线 1:上线  -1:失效,默认传入2获取所有数据        ///         /// 
[Route("api/classification/GetclassificationListPage")] [HttpGet] public string GetclassificationListPage(int pageIndex, int pageSize, int status, string title) { try { using (var db = SugarDao.GetInstance()) { var qable = db.Queryable
(); var dataCountTable = db.Queryable
(); if (status != 2) { qable = qable.Where(i => i.status == status); dataCountTable = dataCountTable.Where(i => i.status == status); } if (status == 2) { dataCountTable = dataCountTable.Where(i => i.status != status); } if (!string.IsNullOrEmpty(title)) { qable = qable.Where(i => i.title.Contains(title)); dataCountTable = dataCountTable.Where(i => i.title.Contains(title)); } var data = qable.OrderBy(it => it.createtime, OrderByType.Asc).ToPageList(pageIndex, pageSize); var dataCount = dataCountTable.ToList().Count; if (data.Count > 0) { return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "获取数据成功", Data = data, Count = dataCount }); } else { return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "获取数据失败", Data = data, Count = dataCount }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize(new { Result = 0, Msg = "获取数据失败,原因为:" + ex.Message }); } } ///
/// 获取分类数据,不分页 http://192.168.2.177:1222/api/classification/GetclassificationList?status=2&title= /// ///
///
///
[Route("api/classification/GetclassificationList")] [HttpGet] public string GetclassificationList(int status, string title) { try { using (var db = SugarDao.GetInstance()) { var qable = db.Queryable
(); if (status != 2) { qable = qable.Where(i => i.status == status); } if (!string.IsNullOrEmpty(title)) { qable = qable.Where(i => i.title.Contains(title)); } var data = qable.OrderBy(it => it.createtime, OrderByType.Asc).ToList(); var dataCount = db.Queryable
().Where(it => it.status != status).ToList().Count; if (data.Count > 0) { return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "获取数据成功", Data = data, Count = dataCount }); } else { return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "获取数据失败", Data = data, Count = dataCount }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize(new { Result = 0, Msg = "获取数据失败,原因为:" + ex.Message }); } } ///
/// 根据id得到对象 http://192.168.2.177:1222/api/classification/GetclassificationModel?id=1 /// ///
///
[Route("api/classification/GetclassificationModel")] [HttpGet, HttpPost] public string GetclassificationModel(int id) { try { using (var db = SugarDao.GetInstance()) { if (id != 0) { var classification = db.Queryable
().Single(it => it.id == id); if (classification != null) { return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "获取数据成功", Data = classification }); } else { return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "获取数据失败", Data = classification }); } } else { return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "获取数据失败,无法找到id为:" + id + "的数据", }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize(new { Result = 0, Msg = "获取数据失败,原因为:" + ex.Message }); } } ///
/// 新增分类 http://192.168.2.177:1222/api/classification/Postclassification /// ///
[Route("api/classification/Postclassification")] [HttpPost] public string Postclassification(tb_classification classification) { try { classification.createtime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); using (var db = SugarDao.GetInstance()) { object result = db.Insert
(classification); if (Convert.ToInt64(result) > 0) { return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "添加成功" }); } else { return SqlSugar.JsonConverter.Serialize(new { Result = 0, Msg = "添加失败" }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize(new { Result = 0, Msg = "添加失败,原因为:" + ex.Message }); } } ///
/// 修改分类 /// ///
///
[Route("api/classification/Putclassification")] [HttpPost] public string Putclassification(tb_classification classification) { try { classification.createtime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); using (var db = SugarDao.GetInstance()) { bool result = db.Update
(classification, it => it.id == classification.id); if (result == true) { return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "更新成功" }); } else { return SqlSugar.JsonConverter.Serialize(new { Result = 0, Msg = "更新失败" }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize(new { Result = 0, Msg = "更新失败,原因为:" + ex.Message }); } } ///
/// 删除 http://192.168.2.177:1222/api/classification/Deleteclassification?ids=4,5 /// ///
///
[Route("api/classification/Deleteclassification")] [HttpGet, HttpPost] public string Deleteclassification(string ids) { try { string[] input = ids.Split(','); int[] output = Array.ConvertAll
(input, delegate (string s) { return int.Parse(s); }); using (var db = SugarDao.GetInstance()) { bool result = db.Delete
(output); if (result == true) { return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "删除成功" }); } else { return SqlSugar.JsonConverter.Serialize(new { Result = 0, Msg = "删除失败" }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize(new { Result = 0, Msg = "删除失败,原因为:" + ex.Message }); } } ///
/// 更新状态 http://192.168.2.177:1222/api/classification/UpdateStatus?status=0&ids=2,7 /// ///
状态:0:下线 1:上线 -1:失效 ///
///
[Route("api/classification/UpdateStatus")] [HttpGet] public string UpdateStatus(int status, string ids) { try { string[] input = ids.Split(','); int[] output = Array.ConvertAll
(input, delegate (string s) { return int.Parse(s); }); using (var db = SugarDao.GetInstance()) { bool result = db.Update
(new { status = status }, output); if (result == true) { return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "状态更改成功" }); } else { return SqlSugar.JsonConverter.Serialize(new { Result = 0, Msg = "状态更改失败" }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize(new { Result = 0, Msg = "状态更改失败,原因为:" + ex.Message }); } } string result = string.Empty; ///
/// 分类树形结构 http://192.168.2.177:1222/api/classification/GetclassificationTree /// ///
[Route("api/classification/GetclassificationTree")] [HttpGet] public string GetclassificationTree() { WebApplication.Controllers.TreeMethod tm = new TreeMethod(); // 找到所有的父节点 List
treeList1 = tm.findAllParents(); if (treeList1 != null) { for (int i = 0; i < treeList1.Count; i++) { TreeEntity tree = treeList1[i]; // 打印父节点 result += "|--" + tree.name; // 绑定孩子 result+=tm.BindChildByParent(tree.id, ""); } } else { result += "没有数据!"; } return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "获取数据成功",Data= result }); } } public class TreeEntity { public string id { get; set; } public string name { get; set; } public string pid { get; set; } } internal class TreeMethod { ///
/// 找到所有的父节点 /// ///
public List
findAllParents() { List
treeList = new List
(); using (var db = SugarDao.GetInstance()) { var list = db.Queryable
().Where(it => it.pid == 0 && it.status==1).ToList(); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { TreeEntity myTree = new TreeEntity(); myTree.id = list[i].id.ToString(); myTree.name = list[i].title; myTree.pid = list[i].pid.ToString(); treeList.Add(myTree); } } } return treeList; } ///
/// 根据父节点找到所有的子节点 /// ///
///
public List
findChildByPid(string pid) { int p_id = Convert.ToInt32(pid); List
treeList = new List
(); using (var db = SugarDao.GetInstance()) { var list = db.Queryable
().Where(it => it.pid == p_id&&it.status==1).ToList(); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { TreeEntity myTree = new TreeEntity(); myTree.id = list[i].id.ToString(); myTree.name = list[i].title; myTree.pid = list[i].pid.ToString(); treeList.Add(myTree); } } } return treeList; } ///
/// 查看是否存在子节点 /// ///
///
public bool HasChild(string pid) { int p_id = Convert.ToInt32(pid); int count = 0; bool flag = false; using (var db = SugarDao.GetInstance()) { var list = db.Queryable
().Where(it => it.pid == p_id&it.status==1).ToList(); for (int i = 0; i < list.Count; i++) { count++; } if (count > 0) { flag = true; } } return flag; } string Tree = string.Empty; ///
/// 使用递归拼接父节点的子节点 /// ///
///
public string BindChildByParent(string pid, string prefix) { if (this.HasChild(pid)) { // 得到当前父节点下的所有孩子 List
list = this.findChildByPid(pid); // 循环打印当前父节点下的孩子 for (int i = 0; i < list.Count; i++) { Tree += "|----" + prefix + list[i].name; if (this.HasChild(list[i].id)) { this.BindChildByParent(list[i].id, "--"); } } } return Tree; } }}
View Code

 

转载于:https://www.cnblogs.com/Blog-Yang/p/6237321.html

你可能感兴趣的文章
linux 如何释放缓存
查看>>
经典,程序员笑话
查看>>
Linux 抓取网站命令
查看>>
浪叫兽的自我介绍 (完整版) 讲述一段如何进入大数据行业
查看>>
Qt ui在程序中的使用
查看>>
datatables.js 简单使用--弹出编辑框或添加数据框
查看>>
前端--3、jQuery
查看>>
最小化的 Google Analytics 代码
查看>>
服务器监控相关
查看>>
转: 环信联合创始人:App主流反垃圾服务难点和技术实现全解析
查看>>
关于类型转换这件事
查看>>
[转]30分钟,让你成为一个更好的程序员
查看>>
《使用Hibernate开发租房系统》内部测试笔试题
查看>>
矩阵的奇异值与特征值
查看>>
窗体切换中的小技巧
查看>>
day10作业
查看>>
2013-5-11 湘潭多省程序设计 赛后总结
查看>>
SEO页面优化
查看>>
读构建之法第一天
查看>>
Redis(二)、Redis持久化RDB和AOF
查看>>