using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Dapper;
using dccdc.Models;
namespace dccdc.DAL
{
public class AreaBll
{
///
/// 获取地区
///
///
public List GetList()
{
using (SqlConnection con = CommHelper.GetSqlConnection())
{
return con.Query("select * from area1").ToList();
}
}
///
/// 根据当前区域CODE查询下级区域
///
///
///
public List GetSubordinate(string code)
{
using (SqlConnection con = CommHelper.GetSqlConnection())
{
if (string.IsNullOrWhiteSpace(code) || code == "0")
{
return con.Query("select * from area1 where pcode='0'").ToList();
}
return con.Query("select * from area1 where pcode=@code", new { code }).ToList();
}
}
///
/// 获取省份列表
///
///
public List GetProvince()
{
using (SqlConnection con = CommHelper.GetSqlConnection())
{
return con.Query("select * from area1 where pcode='0'").ToList();
}
}
///
/// 获取单独省份
///
///
///
public List GetProvince(string code)
{
using (SqlConnection con = CommHelper.GetSqlConnection())
{
return con.Query("select * from area1 where code=@code", new { code }).ToList();
}
}
///
/// 根据省份获取市
///
///
///
public List GetProvinceByPid(string code)
{
using (SqlConnection conn = CommHelper.GetSqlConnection())
{
AreaModel m = GetTopLevel(code);
List list = new List { m };
string pcode = m.pCode;
while (pcode != "0")
{
AreaModel m1 = GetTopLevel(pcode);
list.Add(m1);
pcode = m1.pCode;
}
return list;
}
}
///
/// 根据当前区域code查询上级区域
///
///
///
public AreaModel GetTopLevel(string code)
{
using (SqlConnection conn = CommHelper.GetSqlConnection())
{
return conn.Query("select * from area1 where code=@code", new { code }).FirstOrDefault();
}
}
///
/// 根据code查找所有父级
///
///
///
public List GetTopListByCode(string code)
{
List list = new List();
using (SqlConnection conn = CommHelper.GetSqlConnection())
{
while (true)
{
AreaModel area = conn.Query("select * from area1 where code=@code", new { code }).FirstOrDefault();
if (null == area)
{
break;
}
list.Add(area);
code = area.pCode;
}
//AreaModel am = conn.Query("select * from area where code=@code", new { code })
// .FirstOrDefault();
//AreaModel topam = conn.Query("select * from area where code=@pcode", new { @pcode = am.pCode }).FirstOrDefault();
//AreaModel p = conn.Query("select * from area where code=@pcode",
// new { @pcode = topam.pCode }).FirstOrDefault();
//list.Add(p);
//list.Add(topam);
//list.Add(am);
return list;
}
}
}
}