using CallCenter.Utility; using CallCenterApi.Interface.Controllers.Base; using CallCenterApi.Interface.Models.Input; using Newtonsoft.Json; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; namespace CallCenterApi.Interface.Controllers.customer { public class CustomerNewController : BaseController { BLL.T_Cus_CustomerBaseNew cusbll = new BLL.T_Cus_CustomerBaseNew(); BLL.T_Cus_ContactPersonNew conbll = new BLL.T_Cus_ContactPersonNew(); BLL.T_Sys_Department deptbll = new BLL.T_Sys_Department(); /// /// 获取客户列表 /// /// 客户编号 /// 公司名称 /// 公司地址 /// 法人姓名 /// 法人电话 /// 所属行业 /// 客户信誉等级 /// 客户归属部门 /// 客户归属涉税会计 /// 客户归属审核会计 /// 客户归属做账会计 /// 联系人姓名 /// 联系人电话 /// public ActionResult GetList(string keywords, string code, string cmpname, string cmpaddress, string legname, string legtel, string subtrade, string layer, string bedept, string berelacc, string beaudacc, string bemakeacc, string conname, string contel, int pageindex = 1, int pagesize = 10) { string sql = ""; DataTable dt = new DataTable(); #region 筛选条件 if (!string.IsNullOrWhiteSpace(keywords))//关键字搜索 sql += $" and F_KeyWords like '%" + keywords.Trim() + "%'"; if (!string.IsNullOrWhiteSpace(code))//客户编号 sql += $" and F_CustomerCode like '%" + code.Trim() + "%'"; if (!string.IsNullOrWhiteSpace(cmpname))//公司名称 sql += $" and F_KeyWords like '%" + cmpname.Trim() + "%'"; if (!string.IsNullOrWhiteSpace(cmpaddress))//公司地址 sql += $" and F_CompanyAddress like '%" + cmpaddress.Trim() + "%'"; if (!string.IsNullOrWhiteSpace(legname))//法人姓名 sql += $" and F_LegalName like '%" + legname.Trim() + "%'"; if (!string.IsNullOrWhiteSpace(legtel))//法人电话 sql += $" and F_LegalTel like '%" + legtel.Trim() + "%'"; if (!string.IsNullOrWhiteSpace(subtrade))//所属行业 sql += $" and isnull(F_SubTrade,'') = '" + subtrade.Trim() + "'"; if (!string.IsNullOrWhiteSpace(layer))//客户信誉等级 sql += $" and isnull(F_Layer,'') = '" + layer.Trim() + "'"; if (!string.IsNullOrWhiteSpace(bedept))//客户所属部门 sql += $" and isnull(F_BeDept,'') = '" + bedept.Trim() + "'"; if (!string.IsNullOrWhiteSpace(berelacc))//涉税会计 sql += $" and F_BeRelatedAcc like '%" + berelacc.Trim() + "%'"; if (!string.IsNullOrWhiteSpace(beaudacc))//审核会计 sql += $" and F_BeAuditAcc like '%" + beaudacc.Trim() + "%'"; if (!string.IsNullOrWhiteSpace(bemakeacc))//做账会计 sql += $" and F_BeMakeAcc like '%" + bemakeacc.Trim() + "%'"; if (!string.IsNullOrWhiteSpace(conname) || !string.IsNullOrWhiteSpace(contel)) { var sqlcon = " where F_DeleteFlag=0 "; if (!string.IsNullOrWhiteSpace(conname)) // 联系人 sqlcon += " and F_Name like '%" + conname + "%'"; if (!string.IsNullOrWhiteSpace(contel)) //联系人电话 sqlcon += " and F_Telephone like '%" + contel + "%'"; sql += $" and F_CustomerId in (select F_CustomerId from T_Cus_ContactPersonNew " + sqlcon + " )"; } #endregion int recordCount = 0; if (!string.IsNullOrWhiteSpace(sql)) { sql += " and F_DeleteFlag=0"; dt = BLL.PagerBLL.GetListPager( "T_Cus_CustomerBaseNew", "F_CustomerId", "*,dbo.GetDeptName(F_BeDept) as F_BeDeptName", sql, "ORDER BY F_CustomerId desc", pagesize, pageindex, true, out recordCount); #region dt.Columns.Add("F_Contact"); var dept = new BLL.T_Sys_Department().GetModelList(""); foreach (DataRow dr in dt.Rows) { #region 绑定联系人信息 var contact = ""; string cusid = dr["F_CustomerId"] != null ? dr["F_CustomerId"].ToString() : ""; if (cusid != "") { var conlist = conbll.GetModelList(" F_CustomerId=" + cusid); if (conlist.Count > 0) { foreach (var item in conlist) { contact += item.F_Name + "(" + item.F_Telephone + "),"; } } dr["F_Contact"] = contact.TrimEnd(','); } #endregion } #endregion } var obj = new { state = "success", message = "成功", rows = dt, total = recordCount }; return Content(obj.ToJson()); } /// /// 根据关键字搜索公司 /// public ActionResult GetListByKey(string keywords) { string sql = ""; if (!string.IsNullOrWhiteSpace(keywords))//关键字搜索 sql += $" F_KeyWords like '%" + keywords.Trim() + "%'"; var list = new List(); if (!string.IsNullOrWhiteSpace(sql)) { sql += " and F_DeleteFlag=0"; list = cusbll.GetModelList(sql); } if (list.Count > 0) return Success("根据关键字获取公司信息", list.FirstOrDefault()); else return Error("没有获取到相关公司,请重新搜索"); } /// /// 获取客户信息 /// /// public ActionResult GetCustomer(int cusid) { if (cusid != 0) { Model.T_Cus_CustomerBaseNew userModel = cusbll.GetModel(cusid); if (userModel != null) { #region 绑定部门 var deptname = ""; var deptmodel = deptbll.GetModel(userModel.F_BeDept.Value); if (deptmodel != null) deptname = deptmodel.F_DeptName; #endregion var conlist = conbll.GetModelList(" F_DeleteFlag=0 and F_CustomerId=" + cusid); var obj = new { CustomerBase = userModel, BeDeptName = deptname, ContactList = conlist, }; return Success("获取成功", obj); } else { return Error("获取失败"); } } else { return Error("参数传输失败"); } } /// /// 通过来电号码获取来电弹屏左侧客户信息 /// /// public ActionResult GetCustomerByTel(string tel) { if (!string.IsNullOrWhiteSpace(tel)) { var sql = $" and (F_LegalTel like '%" + tel.Trim() + "%') or F_CustomerId in (select F_CustomerId from T_Cus_ContactPersonNew where F_Telephone like '%" + tel.Trim() + "%')"; var userModel = new BLL.T_Cus_CustomerBaseNew().GetModelList(" F_DeleteFlag = 0 " + sql); if (userModel.Count() > 0) { var model = userModel.Last(); int cusid = model.F_CustomerId; var conlist = conbll.GetModelList(" F_DeleteFlag=0 and F_CustomerId=" + cusid); #region 绑定部门 var deptname = ""; var deptmodel = deptbll.GetModel(model.F_BeDept.Value); if (deptmodel != null) deptname = deptmodel.F_DeptName; #endregion var obj = new { CustomerBase = model, BeDeptName = deptname, ContactList = conlist, }; return Success("获取成功", obj); } else { return Success("获取成功"); } } else { return Error("参数传输失败"); } } /// /// 添加客户信息 /// /// public ActionResult Add(CustomerBaseNewInput input) { string usercode = CurrentUser.UserData.F_UserCode; #region 添加验证判断 if (string.IsNullOrEmpty(input.F_CustomerCode)) return Error("编号不能为空!"); if (getunique(0, input.F_CustomerCode)) return Error("编号已被占用,请重新输入!"); #endregion var model = new Model.T_Cus_CustomerBaseNew(); #region 保存客户基本信息 model.F_CustomerCode = input.F_CustomerCode; model.F_CompanyName = input.F_CompanyName; #region 处理关键字 var keyword = model.F_CompanyName; var keyconfig = Configs.GetValue("keystring"); var keystring = keyconfig.Split(','); foreach (var item in keystring) { if (model.F_CompanyName.Contains(item)) keyword = keyword.Replace(item, ""); } model.F_KeyWords = keyword; #endregion //model.F_KeyWords = input.F_KeyWords; model.F_RegisteredAddress = input.F_RegisteredAddress; model.F_CompanyAddress = input.F_CompanyAddress; model.F_LegalName = input.F_LegalName; model.F_LegalIDCards = input.F_LegalIDCards; model.F_LegalTel = input.F_LegalTel; model.F_Qualification = input.F_Qualification; model.F_Layer = input.F_Layer; model.F_InvoiceRange = input.F_InvoiceRange; model.F_SubTrade = input.F_SubTrade; model.F_TaxCategory = input.F_TaxCategory; model.F_TaxPointDes = input.F_TaxPointDes; model.F_WxPassword = input.F_WxPassword; model.F_BeDept = input.F_BeDept; model.F_FinancialManager = input.F_FinancialManager; model.F_BusinessOwner = input.F_BusinessOwner; model.F_BeRelatedAcc = input.F_BeRelatedAcc; model.F_BeAuditAcc = input.F_BeAuditAcc; model.F_BeMakeAcc = input.F_BeMakeAcc; model.F_Remark1 = input.F_Remark1; model.F_Remark2 = input.F_Remark2; model.F_Remark3 = input.F_Remark3; model.F_CreateBy = usercode; model.F_CreatedOn = DateTime.Now; model.F_DeleteFlag = 0; #endregion int n = cusbll.Add(model); if (n > 0) { #region 添加联系人 if (input.ContactList != null && input.ContactList.Count > 0) { var conmodel = new Model.T_Cus_ContactPersonNew(); conmodel.F_CustomerId = n; conmodel.F_CreateBy = usercode; conmodel.F_CreateOn = DateTime.Now; conmodel.F_DeleteFlag = 0; foreach (var item in input.ContactList) { conmodel.F_IsMain = item.F_IsMain; conmodel.F_Name = item.F_Name; conmodel.F_Telephone = item.F_Telephone; conmodel.F_Duties = item.F_Duties; conmodel.F_Email = item.F_Email; conmodel.F_MSN = item.F_MSN; conmodel.F_QQ = item.F_QQ; conmodel.F_Remark = item.F_Remark; conbll.Add(conmodel); } } #endregion return Success("新增成功!"); } else return Error("新增失败!"); } /// /// 修改客户信息 /// /// public ActionResult Update(CustomerBaseNewInput input) { string usercode = CurrentUser.UserData.F_UserCode; #region 添加验证判断 if (input.F_CustomerId <= 0) return Error("参数错误!"); if (getunique(input.F_CustomerId, input.F_CustomerCode)) return Error("编号已被占用,请重新输入!"); #endregion var model = cusbll.GetModel(input.F_CustomerId); #region 保存客户基本信息 //model.F_CustomerCode = input.F_CustomerCode; model.F_CompanyName = input.F_CompanyName; #region 处理关键字 var keyword = model.F_CompanyName; var keyconfig = Configs.GetValue("keystring"); var keystring = keyconfig.Split(','); foreach (var item in keystring) { if (model.F_CompanyName.Contains(item)) keyword = keyword.Replace(item, ""); } model.F_KeyWords = keyword; #endregion model.F_RegisteredAddress = input.F_RegisteredAddress; model.F_CompanyAddress = input.F_CompanyAddress; model.F_LegalName = input.F_LegalName; model.F_LegalIDCards = input.F_LegalIDCards; model.F_LegalTel = input.F_LegalTel; model.F_Qualification = input.F_Qualification; model.F_Layer = input.F_Layer; model.F_InvoiceRange = input.F_InvoiceRange; model.F_SubTrade = input.F_SubTrade; model.F_TaxCategory = input.F_TaxCategory; model.F_TaxPointDes = input.F_TaxPointDes; model.F_WxPassword = input.F_WxPassword; model.F_BeDept = input.F_BeDept; model.F_FinancialManager = input.F_FinancialManager; model.F_BusinessOwner = input.F_BusinessOwner; model.F_BeRelatedAcc = input.F_BeRelatedAcc; model.F_BeAuditAcc = input.F_BeAuditAcc; model.F_BeMakeAcc = input.F_BeMakeAcc; model.F_Remark1 = input.F_Remark1; model.F_Remark2 = input.F_Remark2; model.F_Remark3 = input.F_Remark3; #endregion bool n = cusbll.Update(model); if (n) { return Success("保存成功!"); } else return Error("保存失败!"); } /// /// 验证客户编号是否唯一 /// private bool getunique(int id, string code) { var sql = " F_DeleteFlag=0 and F_CustomerCode='" + code + "'"; if (id > 0) sql += " and F_CustomerId<>"+id; var count=cusbll.GetModelList(sql).Count(); return count > 0; } /// /// 删除客户 /// /// /// public ActionResult DelCustomer(string[] ids) { if (ids != null && ids.Length > 0) { string idd = " "; foreach (string str in ids) { idd += str + ","; } if (!string.IsNullOrEmpty(idd.Trim())) { if (cusbll.DeleteList(idd.TrimEnd(','))) { conbll.DeleteListByCusid(idd.TrimEnd(',')); return Success("设置成功"); } else { return Error("设置失败"); } } else { return Error("请选择用户"); } } else { return Error("获取参数失败"); } } /// /// 获取客户联系人列表 /// public ActionResult GetConList(string tel, string cusid, int pageindex = 1, int pagesize = 10) { string sql = ""; DataTable dt = new DataTable(); #region 筛选条件 if (!string.IsNullOrWhiteSpace(cusid)) sql += " and F_CustomerId=" + cusid + ""; if (!string.IsNullOrWhiteSpace(tel)) //联系人电话 sql += " and F_Telephone like '%" + tel + "%'"; #endregion int recordCount = 0; if (!string.IsNullOrWhiteSpace(sql)) { sql += " and F_DeleteFlag=0"; dt = BLL.PagerBLL.GetListPager( "T_Cus_ContactPersonNew", "F_ManId", "*", sql, "ORDER BY F_ManId desc", pagesize, pageindex, true, out recordCount); } var obj = new { state = "success", message = "成功", rows = dt, total = recordCount }; return Content(obj.ToJson()); } /// /// 添加联系人 /// /// /// public ActionResult AddContact(ContactPersonNewInput coninput) { string usercode = CurrentUser.UserData.F_UserCode; #region 添加验证判断 if (coninput.F_CustomerId <= 0) return Error("客户ID获取失败!"); #endregion var conmodel = new Model.T_Cus_ContactPersonNew(); conmodel.F_CustomerId = coninput.F_CustomerId; conmodel.F_IsMain = coninput.F_IsMain; conmodel.F_Name = coninput.F_Name; conmodel.F_Telephone = coninput.F_Telephone; conmodel.F_Duties = coninput.F_Duties; conmodel.F_Email = coninput.F_Email; conmodel.F_MSN = coninput.F_MSN; conmodel.F_QQ = coninput.F_QQ; conmodel.F_Remark = coninput.F_Remark; conmodel.F_CreateBy = usercode; conmodel.F_CreateOn = DateTime.Now; conmodel.F_DeleteFlag = 0; int n = conbll.Add(conmodel); if (n > 0) return Success("联系人保存成功!"); else return Error("联系人保存失败!"); } /// /// 修改联系人 /// /// /// public ActionResult UpdateContact(ContactPersonNewInput coninput) { string usercode = CurrentUser.UserData.F_UserCode; #region 添加验证判断 if (coninput.F_ManId <= 0) return Error("参数错误!"); if (coninput.F_CustomerId <= 0) return Error("客户ID获取失败!"); #endregion var conmodel = conbll.GetModel(coninput.F_ManId); conmodel.F_CustomerId = coninput.F_CustomerId; conmodel.F_IsMain = coninput.F_IsMain; conmodel.F_Name = coninput.F_Name; conmodel.F_Telephone = coninput.F_Telephone; conmodel.F_Duties = coninput.F_Duties; conmodel.F_Email = coninput.F_Email; conmodel.F_MSN = coninput.F_MSN; conmodel.F_QQ = coninput.F_QQ; conmodel.F_Remark = coninput.F_Remark; var n = conbll.Update(conmodel); if (n) return Success("联系人保存成功!"); else return Error("联系人保存失败!"); } /// /// 删除联系人 /// /// /// public ActionResult DelContact(string[] ids) { if (ids != null && ids.Length > 0) { string idd = " "; foreach (string str in ids) { idd += str + ","; } if (!string.IsNullOrEmpty(idd.Trim())) { if (conbll.DeleteList(idd.TrimEnd(','))) { return Success("设置成功"); } else { return Error("设置失败"); } } else { return Error("请选择联系人"); } } else { return Error("获取参数失败"); } } /// /// 导入excel /// public ActionResult ImportExcel() { string usercode = CurrentUser.UserData.F_UserCode; if (!string.IsNullOrWhiteSpace(usercode)) { HttpPostedFile _upFile = RequestString.GetFile("upFile"); if (_upFile != null) { int headrow = 0; #region 上传文件 string filepath = ""; string datepath = DateTime.Now.ToString("yyyyMMddHHMMss"); string aLastName = _upFile.FileName.Substring(_upFile.FileName.LastIndexOf(".") + 1, (_upFile.FileName.Length - _upFile.FileName.LastIndexOf(".") - 1)); //扩展名 if (aLastName != "xls" && aLastName != "xlsx") { return Error("文件类型错误,请选择Excel文件"); } string newpath = datepath + "_" + _upFile.FileName; if (!Directory.Exists(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData"))) { Directory.CreateDirectory(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData")); } filepath = this.Request.ApplicationPath + "/ExcelData/" + newpath; string PhysicalPath = Server.MapPath(filepath); _upFile.SaveAs(PhysicalPath); #endregion DataTable dt = new DataTable(); #region 读取excel中内容 IWorkbook workbook = null; using (FileStream file = new FileStream(PhysicalPath, FileMode.Open, FileAccess.Read)) { if (aLastName == "xlsx") // 2007版本 { workbook = new XSSFWorkbook(file); } else if (aLastName == "xls") // 2003版本 { workbook = new HSSFWorkbook(file); } } ISheet sheet = workbook.GetSheetAt(0); System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); //获取sheet的第一行 IRow headerRow = sheet.GetRow(headrow); int cellCount = headerRow.LastCellNum; if (cellCount < 1) return Error("文件标题没有数据"); for (int j = 0; j < cellCount; j++) { ICell cell = headerRow.GetCell(j); dt.Columns.Add(cell.ToString()); } #endregion int count = 0; for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++) { #region 数据入库 IRow row = sheet.GetRow(i); DataRow dataRow = dt.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) != null) dataRow[j] = row.GetCell(j).ToString(); } dt.Rows.Add(dataRow); if (dt.Rows.Count < 1) return Error("文件内容没有数据"); var cusmodel = new Model.T_Cus_CustomerBaseNew(); var conmodel = new Model.T_Cus_ContactPersonNew(); #region 信息对应 //报税会计 做账会计 编号 客服 公司名称 负责人电话 交接日期 cusmodel.F_CustomerCode = dataRow["编号"].ToString(); cusmodel.F_CompanyName = dataRow["公司名称"].ToString(); #region 处理关键字 var keyword = cusmodel.F_CompanyName; var keyconfig = Configs.GetValue("keystring"); var keystring = keyconfig.Split(','); foreach (var item in keystring) { if (cusmodel.F_CompanyName.Contains(item)) keyword = keyword.Replace(item, ""); } cusmodel.F_KeyWords = keyword; #endregion cusmodel.F_BeRelatedAcc = dataRow["报税会计"].ToString(); cusmodel.F_BeMakeAcc = dataRow["做账会计"].ToString(); cusmodel.F_Remark1 = dataRow["交接日期"].ToString(); cusmodel.F_Remark2 = dataRow["客服"].ToString(); cusmodel.F_CreateBy = usercode; cusmodel.F_CreatedOn = DateTime.Now; cusmodel.F_DeleteFlag = 0; #endregion int n = cusbll.Add(cusmodel); if (n > 0) { #region 添加联系人 if (dataRow["负责人电话"] != null) { conmodel.F_CustomerId = n; conmodel.F_CreateBy = usercode; conmodel.F_CreateOn = DateTime.Now; conmodel.F_DeleteFlag = 0; conmodel.F_IsMain = true; conmodel.F_Telephone = dataRow["负责人电话"].ToString(); conbll.Add(conmodel); } #endregion count++; } #endregion } if (dt == null || dt.Rows.Count == 0) return Error("文件没有数据"); else { var jstr = DataTableToJson(dt); if (count > 0) { return Success("导入成功 " + count + "条信息", jstr); } else { return Success("导入成功 " + count + "条信息", jstr); } } } return Error("数据源上传失败"); } return Error("用户登录失败,请重新登录"); } public string DataTableToJson(DataTable dt) { string JsonString = string.Empty; JsonString = JsonConvert.SerializeObject(dt); return JsonString; } } }