索引
简述
简单的客户管理
项目准备
我们用的工具是:VS 2013 + SqlServer 2012 + IIS7.5
希望大家对ASP.NET MVC有一个初步的理解,理论性的东西我们不做过多解释,有些地方不理解也没关系,会用就行了,用的多了,用的久了,自然就理解了。
项目开始
一、数据库设计
表名:SYS_BUSSINESSCUSTOMER
|
二、创建接口和实现类
1、我们在Service类库下IService文件夹的SysManage文件夹下新建一个接口 IBussinessCustomerManage
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Service.IService 8 { 9 ///10 /// 客户管理接口11 /// add yuangang by 2016-06-1712 /// 13 public interface IBussinessCustomerManage:IRepository14 {15 }16 }
2、我们在ServiceImp文件夹的SysManage文件夹下面新建一个实现类 BussinessCustomerManage
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Service.ServiceImp 8 { 9 public class BussinessCustomerManage:RepositoryBase,IService.IBussinessCustomerManage10 {11 }12 }
3、然后呢?对了,千万别忘记,我们在Service类库下面的Service.xml配置一下
三、控制器和视图
1、我们在/Areas/SysManage/Controllers 下新建一个控制器 BussinessCustomerController 继承基础控制器 BaseController
2、我们配置一下/Config/Controllers.xml 实现spring的注入
3、我们声明一下这些容器
1 #region 声明容器 2 ///3 /// 公司客户管理 4 /// 5 IBussinessCustomerManage BussinessCustomerManage { get; set; } 6 ///7 /// 省市区管理 8 /// 9 ICodeAreaManage CodeAreaManage { get; set; }10 ///11 /// 大数据字段管理12 /// 13 IContentManage ContentManage { get; set; }14 ///15 /// 编码管理16 /// 17 ICodeManage CodeManage { get; set; }18 #endregion
4、我们为Index添加视图,并且加入验证模块 模块别名为 BussinessCustomer 操作验证为 View
1 ///2 /// 客户管理加载主页3 /// 4 ///5 [UserAuthorizeAttribute(ModuleAlias = "BussinessCustomer", OperaAction = "View")]6 public ActionResult Index()7 {8 9 }
5、我们添加菜单连接
5.1 我们打开 系统管理中心 → 系统管理 → 模块管理 添加一个顶部分类 【客户管理中心】
5.2 我们在【客户管理中心】 下新建一个 右侧顶级菜单 【客户管理】
5.3 我们在【客户管理】菜单下 新建一个菜单 【客户中心】 模块别名: BussinessCustomer 模块类型: 列表页 模块路径: /Sys/BussinessCustomer
5.4 这样我们是否就有菜单了呢?答案是否定的。因为我们为Index添加了权限验证 View 而我们这个模块还没有权限。所以我们要创建权限
为了快速,我们可以直接选择 【初始化权限】 会自动为我们添加 基本的操作权限 如果我们需要其它权限 比如:审核、发布、下载、导出等,我们可以通过【创建新权限】 来添加自定义操作权限
OK,现在可以打开页面了。
6、修改Index 输出客户列表
6.1 输出省份列表和客户类型列表
1 ViewData["ProvinceList"] = CodeAreaManage.LoadListAll(p => p.LEVELS == 1);2 ViewBag.KHLX = this.CodeManage.LoadAll(p => p.CODETYPE == "LXRLX").OrderBy(p => p.SHOWORDER).ToList();
6.2 处理查询参数 输出客户列表
1 ///2 /// 客户管理加载主页 3 /// 4 ///5 [UserAuthorizeAttribute(ModuleAlias = "BussinessCustomer", OperaAction = "View")] 6 public ActionResult Index() 7 { 8 try 9 {10 11 #region 处理查询参数12 //接收省份13 string Province = Request.QueryString["Province"];14 ViewData["Province"] = Province;15 //接收客户类型16 string CustomerStyle = Request.QueryString["CustomerStyle"];17 ViewData["CustomerStyle"] = CustomerStyle;18 //文本框输入查询关键字19 ViewBag.Search = base.keywords; 20 #endregion21 22 ViewData["ProvinceList"] = CodeAreaManage.LoadListAll(p => p.LEVELS == 1);23 ViewBag.KHLX = this.CodeManage.LoadAll(p => p.CODETYPE == "LXRLX").OrderBy(p => p.SHOWORDER).ToList();24 25 //输出客户分页列表26 return View(BindList(Province, CustomerStyle));27 }28 catch (Exception e)29 {30 WriteLog(Common.Enums.enumOperator.Select, "客户管理加载主页:", e);31 throw e.InnerException;32 }33 }
6.3 分页查询客户列表方法
1 #region 帮助方法及其他控制器调用 2 ///3 /// 分页查询公司客户列表 4 /// 5 private Common.PageInfo BindList(string Province, string CustomerStyle) 6 { 7 //基础数据(缓存) 8 var query = this.BussinessCustomerManage.LoadAll(null); 9 10 //非超级管理员只允许查看用户所在部门客户11 if(!CurrentUser.IsAdmin)12 {13 query = query.Where(p => p.Fk_DepartId == CurrentUser.DptInfo.ID);14 }15 16 //客户所在省份17 if (!string.IsNullOrEmpty(Province))18 { 19 query = query.Where(p => p.CompanyProvince == Province);20 }21 22 //客户类型23 if (!string.IsNullOrEmpty(CustomerStyle))24 {25 int styleId = int.Parse(CustomerStyle);26 query = query.Where(p => p.CustomerStyle == styleId);27 }28 29 //查询关键字30 if (!string.IsNullOrEmpty(keywords))31 {32 keywords = keywords.ToLower();33 query = query.Where(p => p.CompanyName.Contains(keywords) || p.ChargePersionName.Contains(keywords));34 }35 //排序36 query = query.OrderByDescending(p => p.UpdateDate).OrderByDescending(p => p.ID);37 //分页38 var result = this.BussinessCustomerManage.Query(query, page, pagesize);39 40 var list = result.List.Select(p => new41 {42 p.ID,43 p.CompanyName,44 p.IsValidate,45 CompanyProvince = this.CodeAreaManage.Get(m => m.ID == p.CompanyProvince).NAME,46 CompanyCity = this.CodeAreaManage.Get(m => m.ID == p.CompanyCity).NAME,47 CompanyArea = this.CodeAreaManage.Get(m => m.ID == p.CompanyArea).NAME,48 p.CompanyTel,49 p.ChargePersionName,50 p.CreateUser,51 CreateDate=p.CreateDate.ToString("yyyy-MM-dd"),52 p.CustomerStyle53 54 55 }).ToList();56 57 return new Common.PageInfo(result.Index, result.PageSize, result.Count, Common.JsonConverter.JsonClass(list));58 }59 #endregion
6.4 页面输出
7、添加、修改、删除客户
7.1 加载客户详情 生成表单
1 ///2 /// 加载详情 3 /// 4 /// 5 ///6 [UserAuthorizeAttribute(ModuleAlias = "BussinessCustomer", OperaAction = "Detail")] 7 public ActionResult Detail(int? id) 8 { 9 //初始化客户10 var entity = new Domain.SYS_BUSSINESSCUSTOMER() { ChargePersionSex = 1 };11 12 if(id!=null && id>0)13 {14 //客户实体15 entity = BussinessCustomerManage.Get(p => p.ID == id);16 //公司介绍17 ViewData["CompanyInstroduce"] = ContentManage.Get(p => p.FK_RELATIONID == entity.FK_RELATIONID && p.FK_TABLE == "SYS_BUSSINESSCUSTOMER") ?? new Domain.COM_CONTENT();18 }19 20 //客户类型21 ViewBag.KHLX = this.CodeManage.LoadAll(p => p.CODETYPE == "LXRLX").OrderBy(p=>p.SHOWORDER).ToList();22 23 return View(entity);24 }
7.2 Detail.cshtml
1 @{ 2 ViewBag.Title = "Detail"; 3 Layout = "~/Views/Shared/_Layout.cshtml"; 4 var content = ViewData["CompanyInstroduce"] == null ? new Domain.COM_CONTENT() : ViewData["CompanyInstroduce"] as Domain.COM_CONTENT; 5 } 6 @model Domain.SYS_BUSSINESSCUSTOMER 7 12 @using (Ajax.BeginForm("Save", null, new AjaxOptions() 13 { 14 HttpMethod = "Post", 15 OnBegin = "SubAjax.Loading", 16 OnComplete = "SubAjax.Complate", 17 OnFailure = "SubAjax.Failure", 18 OnSuccess = "SubAjax.Success" 19 }, 20 new { @class = "form-horizontal dig-from", @role = "form" })) 21 { 22 @Html.HiddenFor(p => p.ID) 23 @Html.HiddenFor(p=>p.FK_RELATIONID) 24 @Html.HiddenFor(p=>p.Fk_DepartId) 25 @Html.HiddenFor(p => p.CreateUser) 26 @Html.HiddenFor(p => p.CreateDate) 27 @Html.Hidden("ContentId", content.ID) 2829185 }186 @section scripts{187 188 189 190 299 }3018431 添加/修改客户 3233341833560 61364337 384239 @Html.TextBoxFor(p => p.CompanyName, new { @class = "form-control", @placeholder = "请输入客户名称", @datatype = "*", @nullmsg = "请输入客户名称!", @errormsg = "请输入客户名称!" }) 4041445945 465847 55 56576279 80637064 656966 @Html.TextBoxFor(p => p.CompanyTel, new { @class = "form-control", @placeholder = "请输入公司电话" }) 6768717872 737774 @Html.TextBoxFor(p => p.CompanyWebSite, new { @class = "form-control", @placeholder = "请输入公司网址" }) 757681 8297 9883 @Html.HiddenFor(p => p.CompanyProvince) 84 @Html.HiddenFor(p => p.CompanyCity) 85 @Html.HiddenFor(p => p.CompanyArea) 86 89 92 959699 100104 105101 @Html.TextBoxFor(p => p.CompanyAddress, new { @class = "form-control", @placeholder = "请输入通讯地址" })102103106128 129107114108 109113110 @Html.TextBoxFor(p => p.ChargePersionName, new { @class = "form-control", @placeholder = "请输入负责人姓名", @datatype = "*", @nullmsg = "请输入负责人姓名!", @errormsg = "请输入负责人姓名!" })111112115127116 117126118 121 124125130147 148131138132 133137134 @Html.TextBoxFor(p => p.ChargePersionPhone, new { @class = "form-control", @placeholder = "请输入负责人电话" })135136139146140 141145142 @Html.TextBoxFor(p => p.ChargePersionQQ, new { @class = "form-control", @placeholder = "请输入负责人QQ" })143144149168 169150157151 152156153 @Html.TextBoxFor(p => p.ChargePersionEmail, new { @class = "form-control", @placeholder = "请输入负责人邮箱" })154155158167159 160166161 164165170 171177 178172 175176179 180 181182
7.3 保存客户
1 ///2 /// 保存客户信息 3 /// 4 [ValidateInput(false)] 5 [UserAuthorizeAttribute(ModuleAlias = "BussinessCustomer", OperaAction = "Add,Edit")] 6 public ActionResult Save(Domain.SYS_BUSSINESSCUSTOMER entity) 7 { 8 bool isEdit = false; 9 var FK_RELATIONID = ""; 10 var json = new JsonHelper() { Msg = "保存成功", Status = "n" }; 11 try 12 { 13 if (entity != null) 14 { 15 //公司简介数据ID 16 var contentId = Request["ContentId"] == null ? 0 : Int32.Parse(Request["ContentId"].ToString()); 17 18 if (entity.ID <= 0) //添加 19 { 20 FK_RELATIONID = Guid.NewGuid().ToString(); 21 entity.FK_RELATIONID = FK_RELATIONID; 22 entity.Fk_DepartId = this.CurrentUser.DptInfo == null ? "" : this.CurrentUser.DptInfo.ID; 23 entity.CreateUser = CurrentUser.Name; 24 entity.CreateDate = DateTime.Now; 25 entity.UpdateUser = CurrentUser.Name; 26 entity.UpdateDate = DateTime.Now; 27 28 } 29 else //修改 30 { 31 FK_RELATIONID = entity.FK_RELATIONID; 32 entity.UpdateUser = CurrentUser.Name; 33 entity.UpdateDate = DateTime.Now; 34 isEdit = true; 35 } 36 //同部门下 客户名称不能重复 37 if (!this.BussinessCustomerManage.IsExist(p => p.CompanyName.Equals(entity.CompanyName) && p.ID != entity.ID && p.Fk_DepartId == entity.Fk_DepartId)) 38 { 39 using (TransactionScope ts = new TransactionScope()) 40 { 41 try 42 { 43 if (this.BussinessCustomerManage.SaveOrUpdate(entity, isEdit)) 44 { 45 if (contentId <= 0) 46 { 47 this.ContentManage.Save(new Domain.COM_CONTENT() 48 { 49 CONTENT = Request["Content"], 50 FK_RELATIONID = FK_RELATIONID, 51 FK_TABLE = "SYS_BUSSINESSCUSTOMER", 52 CREATEDATE = DateTime.Now 53 }); 54 } 55 else 56 { 57 this.ContentManage.Update(new Domain.COM_CONTENT() 58 { 59 ID = contentId, 60 CONTENT = Request["Content"], 61 FK_RELATIONID = FK_RELATIONID, 62 FK_TABLE = "SYS_BUSSINESSCUSTOMER", 63 CREATEDATE = DateTime.Now 64 }); 65 } 66 json.Status = "y"; 67 68 } 69 70 ts.Complete(); 71 72 } 73 catch(Exception e) 74 { 75 json.Msg = "保存客户信息发生内部错误!"; 76 WriteLog(Common.Enums.enumOperator.None, "保存客户错误:", e); 77 } 78 79 } 80 } 81 else 82 { 83 json.Msg = "客户已经存在,请不要重复添加!"; 84 } 85 } 86 else 87 { 88 json.Msg = "未找到要操作的客户记录"; 89 } 90 if (isEdit) 91 { 92 WriteLog(Common.Enums.enumOperator.Edit, "修改客户[" + entity.CompanyName + "],结果:" + json.Msg, Common.Enums.enumLog4net.INFO); 93 } 94 else 95 { 96 WriteLog(Common.Enums.enumOperator.Add, "添加客户[" + entity.CompanyName + "],结果:" + json.Msg, Common.Enums.enumLog4net.INFO); 97 } 98 } 99 catch (Exception e)100 {101 json.Msg = "保存客户信息发生内部错误!";102 WriteLog(Common.Enums.enumOperator.None, "保存客户错误:", e);103 }104 return Json(json);105 106 }
7.3 删除客户
1 ///2 /// 删除客户 3 /// 删除原则:1、删除客户信息 4 /// 2、删除客户公司简介数据 5 /// 6 [UserAuthorizeAttribute(ModuleAlias = "User", OperaAction = "Remove")] 7 public ActionResult Delete(string idList) 8 { 9 var json = new JsonHelper() { Status = "n", Msg = "删除客户成功" };10 try11 {12 //是否为空13 if (string.IsNullOrEmpty(idList)) { json.Msg = "未找到要删除的客户"; return Json(json); }14 15 var id = idList.Trim(',').Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToList();16 17 using (TransactionScope ts = new TransactionScope())18 {19 try20 {21 foreach(var item in id)22 {23 //删除客户公司简介24 var entity = BussinessCustomerManage.Get(p => p.ID == item);25 ContentManage.Delete(p => p.FK_RELATIONID == entity.FK_RELATIONID && p.FK_TABLE == "SYS_BUSSINESSCUSTOMER");26 }27 //删除客户信息28 BussinessCustomerManage.Delete(p => id.Contains(p.ID));29 30 WriteLog(Common.Enums.enumOperator.Remove, "删除客户:" + json.Msg, Common.Enums.enumLog4net.WARN); 31 32 ts.Complete();33 34 }35 catch (Exception e)36 {37 json.Msg = "删除客户发生内部错误!";38 WriteLog(Common.Enums.enumOperator.Remove, "删除客户:", e);39 }40 41 } 42 }43 catch (Exception e)44 {45 json.Msg = "删除客户发生内部错误!";46 WriteLog(Common.Enums.enumOperator.Remove, "删除客户:", e);47 }48 return Json(json);49 }
原创文章 转载请尊重劳动成果