Нет описания

CustomerController.cs 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using CallCenterApi.Interface.Controllers.Base;
  8. using System.Data;
  9. using CallCenter.Utility;
  10. using CallCenterApi.Common;
  11. using CallCenterApi.DB;
  12. using System.IO;
  13. using NPOI.XSSF.UserModel;
  14. using NPOI.HSSF.UserModel;
  15. using NPOI.SS.UserModel;
  16. using Newtonsoft.Json;
  17. namespace CallCenterApi.Interface.Controllers.customer
  18. {
  19. //[Authority]
  20. public class CustomerController : BaseController
  21. {
  22. /// <summary>
  23. /// 获取客户列表
  24. /// </summary>
  25. /// <returns></returns>
  26. public ActionResult GetList()
  27. {
  28. ActionResult res = NoToken("未知错误,请重新登录");
  29. if (Request.IsAuthenticated)
  30. {
  31. string sql = " and F_DeleteFlag=0";
  32. DataTable dt = new DataTable();
  33. //联系人
  34. string strname = RequestString.GetQueryString("name");
  35. //电话
  36. string strtel = RequestString.GetQueryString("tel");
  37. //客户名称
  38. string strCus = RequestString.GetQueryString("Cusname");
  39. //省份
  40. string strprov = RequestString.GetQueryString("province");
  41. string strstarttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
  42. string strendtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
  43. string strpageindex = RequestString.GetQueryString("page");
  44. int pageindex = 1;
  45. string strpagesize = RequestString.GetQueryString("pagesize");
  46. int pagesize = 10;
  47. if (strname.Trim() != "" && strname != "undefined")
  48. {
  49. sql += " and F_CustomerName like '%" + strname.Trim() + "%' ";
  50. }
  51. if (strtel.Trim() != "" && strtel != "undefined")
  52. {
  53. sql += " and F_Telephone like '%" + strtel + "%' ";
  54. }
  55. if (strCus.Trim() != "" && strCus != "undefined")
  56. {
  57. sql += " and F_CustomerIndustry like '%" + strCus.Trim() + "%' ";
  58. }
  59. if (strprov.Trim() != "" && strprov.Trim() != "undefined")
  60. {
  61. sql += " and F_Province like '%" + strprov.Trim() + "%' ";
  62. }
  63. if (strstarttime.Trim() != "" && strstarttime != "undefined")
  64. {
  65. sql += " and datediff(day,F_CreatedOn,'" + strstarttime + "')<=0 ";
  66. }
  67. if (strendtime.Trim() != "" && strendtime != "undefined")
  68. {
  69. sql += " and datediff(day,F_CreatedOn,'" + strendtime + "')>=0 ";
  70. }
  71. if (strpageindex.Trim() != "")
  72. {
  73. pageindex = Convert.ToInt32(strpageindex);
  74. }
  75. if (strpagesize.Trim() != "")
  76. {
  77. pagesize = Convert.ToInt32(strpagesize);
  78. }
  79. int recordCount = 0;
  80. dt = BLL.PagerBLL.GetListPager(
  81. "T_Cus_CustomerBase",
  82. "F_CustomerId",
  83. "*",
  84. sql,
  85. "ORDER BY F_CustomerId desc",
  86. pagesize,
  87. pageindex,
  88. true,
  89. out recordCount);
  90. var obj = new
  91. {
  92. state = "success",
  93. message = "成功",
  94. rows = dt,
  95. total = recordCount
  96. };
  97. res = Content(obj.ToJson());
  98. }
  99. return res;
  100. }
  101. /// <summary>
  102. /// 获取客户信息
  103. /// </summary>
  104. /// <returns></returns>
  105. public ActionResult GetCustomer()
  106. {
  107. ActionResult res = NoToken("未知错误,请重新登录");
  108. if (Request.IsAuthenticated)
  109. {
  110. int cid = Utils.StrToInt(RequestString.GetQueryString("cid"), 0);
  111. if (cid != 0)
  112. {
  113. Model.T_Cus_CustomerBase userModel = new BLL.T_Cus_CustomerBase().GetModel(cid);
  114. if (userModel != null)
  115. {
  116. res = Success("获取成功", userModel);
  117. }
  118. else
  119. {
  120. res = Error("获取失败");
  121. }
  122. }
  123. else
  124. {
  125. res = Error("参数传输失败");
  126. }
  127. }
  128. return res;
  129. }
  130. /// <summary>
  131. /// 通过来电号码获取来电弹屏左侧客户信息
  132. /// </summary>
  133. /// <returns></returns>
  134. public ActionResult GetCustomerByTel()
  135. {
  136. ActionResult res = NoToken("未知错误,请重新登录");
  137. if (Request.IsAuthenticated)
  138. {
  139. string tel = HttpUtility.UrlDecode(RequestString.GetQueryString("tel"));
  140. if (!string.IsNullOrEmpty(tel))
  141. {
  142. var userModel = new BLL.T_Cus_CustomerBase().GetModelList(" F_Telephone like '%" + tel + "%' or F_Mobile like '%" + tel + "%' ");
  143. if (userModel.Count() > 0)
  144. {
  145. res = Success("获取成功", userModel.Last());
  146. }
  147. else
  148. {
  149. res = Success("获取成功");
  150. }
  151. }
  152. else
  153. {
  154. res = Error("参数传输失败");
  155. }
  156. }
  157. return res;
  158. }
  159. /// <summary>
  160. /// 添加/修改客户信息
  161. /// </summary>
  162. /// <returns></returns>
  163. public ActionResult AddCustomer()
  164. {
  165. ActionResult res = NoToken("未知错误,请重新登录");
  166. if (Request.IsAuthenticated)
  167. {
  168. int userId = CurrentUser.UserData.F_UserId;
  169. if (userId != 0)
  170. {
  171. //联系人
  172. string name = RequestString.GetFormString("name");
  173. //省份
  174. string province = RequestString.GetFormString("province");
  175. //来电单位
  176. string customerindustry = RequestString.GetFormString("customerindustry");
  177. //电话
  178. string mobile = RequestString.GetFormString("mobile");
  179. //获取当前表格选择的客户
  180. int cid = Utils.StrToInt(RequestString.GetFormString("cid"), 0);
  181. Model.T_Cus_CustomerBase model = new Model.T_Cus_CustomerBase();
  182. BLL.T_Cus_CustomerBase bll = new BLL.T_Cus_CustomerBase();
  183. //添加或修改时要根据电话判断是否已有记录
  184. if (cid == 0)
  185. {
  186. model.F_CustomerName = name;
  187. model.F_Province = province;
  188. model.F_CustomerIndustry = customerindustry;
  189. //统一为F_Telephone
  190. model.F_Telephone = mobile;
  191. model.F_CreateBy = userId;
  192. model.F_CreatedOn = DateTime.Now;
  193. model.F_DeleteFlag = 0;
  194. int n = bll.Add(model);
  195. if (n > 0)
  196. {
  197. res = Success("新增成功!", model);
  198. }
  199. else
  200. {
  201. res = Error("新增失败!");
  202. }
  203. }
  204. else
  205. {
  206. model = bll.GetModel(cid);
  207. if (model != null)
  208. {
  209. model.F_CustomerName = name;
  210. model.F_Province = province;
  211. model.F_CustomerIndustry = customerindustry;
  212. model.F_Telephone = mobile;
  213. if (bll.Update(model))
  214. {
  215. res = Success("修改成功!", model);
  216. }
  217. else
  218. {
  219. res = Error("修改失败!");
  220. }
  221. }
  222. }
  223. List<string> lddep = new BLL.T_Cus_CustomerBase().GetLDdep();
  224. CacheHelper.Insert("LDDep", lddep);
  225. }
  226. }
  227. return res;
  228. }
  229. /// <summary>
  230. /// 删除客户
  231. /// </summary>
  232. /// <param name="ids"></param>
  233. /// <returns></returns>
  234. public ActionResult DelCustomer(string[] ids)
  235. {
  236. ActionResult res = NoToken("未知错误,请重新登录");
  237. if (Request.IsAuthenticated)
  238. {
  239. if (ids != null && ids.Length > 0)
  240. {
  241. string idd = " ";
  242. foreach (string str in ids)
  243. {
  244. idd += str + ",";
  245. }
  246. //string state = RequestString.GetQueryString("state");
  247. string sql = "update T_Cus_CustomerBase set F_DeleteFlag=1 where F_CustomerId in (" + idd.TrimEnd(',') + ")";
  248. if (!string.IsNullOrEmpty(idd.Trim()))
  249. {
  250. if (DbHelperSQL.ExecuteSql(sql) > 0)
  251. {
  252. res = Success("设置成功");
  253. List<string> lddep = new BLL.T_Cus_CustomerBase().GetLDdep();
  254. CacheHelper.Insert("LDDep", lddep);
  255. }
  256. else
  257. {
  258. res = Error("设置失败");
  259. }
  260. }
  261. else
  262. {
  263. res = Error("请选择用户");
  264. }
  265. }
  266. else
  267. {
  268. res = Error("获取参数失败");
  269. }
  270. }
  271. return res;
  272. }
  273. /// <summary>
  274. /// 绑定来电单位
  275. /// </summary>
  276. /// <returns></returns>
  277. public ActionResult BindLDdep()
  278. {
  279. ActionResult res = NoToken("未知错误,请重新登录");
  280. string keypara = RequestString.GetQueryString("keypara");
  281. if (Request.IsAuthenticated)
  282. {
  283. //List<string> lddep = new BLL.T_Cus_CustomerBase().GetLDdep();
  284. List<string> lddep = null;
  285. lddep = CacheHelper.Get("LDDep") as List<string>;
  286. List<string> resdep = new List<string>();
  287. if (lddep != null && lddep.Count > 0)
  288. {
  289. foreach (string dep in lddep)
  290. {
  291. if (dep.Contains(keypara))
  292. {
  293. resdep.Add(dep);
  294. }
  295. }
  296. var obj = new
  297. {
  298. dep = resdep
  299. };
  300. res = Success("获取成功", obj);
  301. }
  302. else
  303. {
  304. lddep = new BLL.T_Cus_CustomerBase().GetLDdep();
  305. if (lddep != null && lddep.Count > 0)
  306. {
  307. foreach (string dep in lddep)
  308. {
  309. if (dep.Contains(keypara))
  310. {
  311. resdep.Add(dep);
  312. }
  313. }
  314. var obj = new
  315. {
  316. dep = resdep
  317. };
  318. res = Success("获取成功", obj);
  319. }
  320. else
  321. res = Error("获取失败,没有对应数据");
  322. }
  323. }
  324. return res;
  325. }
  326. /// <summary>
  327. /// 判断来电单位是否存在。不存在才可以
  328. /// </summary>
  329. /// <returns></returns>
  330. public ActionResult LDdepExist()
  331. {
  332. ActionResult res = NoToken("未知错误,请重新登录");
  333. if (Request.IsAuthenticated)
  334. {
  335. string lddep = RequestString.GetQueryString("lddep");
  336. if (!string.IsNullOrEmpty(lddep))
  337. {
  338. bool lddepexists = new BLL.T_Cus_CustomerBase().LDdepExist(lddep);
  339. if (lddepexists)
  340. {
  341. res = Success("获取成功");
  342. }
  343. else
  344. {
  345. res = Error("获取失败");
  346. }
  347. }
  348. else
  349. {
  350. res = Error("参数传输失败");
  351. }
  352. }
  353. return res;
  354. }
  355. /// <summary>
  356. /// 导入excel
  357. /// </summary>
  358. /// <returns></returns>
  359. public ActionResult ExportExcel()
  360. {
  361. ActionResult res = NoToken("未知错误,请重新登录");
  362. if (Request.IsAuthenticated)
  363. {
  364. //string filestr = RequestString.GetFormString("file");
  365. //string filestr = RequestString.GetQueryString("file");
  366. int userId = CurrentUser.UserData.F_UserId;
  367. if (userId != 0)
  368. {
  369. ////需要先将文件保存到服务器项目下,再读取
  370. ////传入的应该是整个文件,file和上传按钮
  371. //string file = Request.Files[0].FileName;
  372. //string fileextension = Request.Files[0].ContentType;
  373. //if (!string.IsNullOrEmpty(file) && file != "undefined")
  374. //{
  375. // //string extensionstr = Path.GetExtension(filestr);
  376. // //if (extensionstr == ".xls" || extensionstr == ".xlsx")
  377. // if (fileextension == "application/vnd.ms-excel" || fileextension == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  378. // {
  379. // //将上传的文件保存在服务器下
  380. // Request.SaveAs(Server.MapPath("~/App_Data/" + file), false);
  381. // if (System.IO.File.Exists(Server.MapPath("~/App_Data/" + file)))
  382. // {
  383. // HSSFWorkbook hssfworkbook;
  384. // XSSFWorkbook xssfworkbook;
  385. // ISheet sheet;
  386. // DataTable dt = new DataTable();
  387. // using (FileStream filestream = new FileStream(Path.GetFullPath(file), FileMode.Open, FileAccess.Read))
  388. // {
  389. // if (fileextension == "application/vnd.ms-excel")
  390. // {
  391. // hssfworkbook = new HSSFWorkbook(filestream);
  392. // sheet = hssfworkbook.GetSheetAt(0);
  393. // }
  394. // else
  395. // {
  396. // xssfworkbook = new XSSFWorkbook(filestream);
  397. // sheet = xssfworkbook.GetSheetAt(0);
  398. // }
  399. // }
  400. // System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  401. // IRow headerRow = sheet.GetRow(0);
  402. // int cellCount = headerRow.LastCellNum;
  403. // for (int j = 0; j < cellCount; j++)
  404. // {
  405. // ICell cell = headerRow.GetCell(j);
  406. // dt.Columns.Add(cell.ToString());
  407. // }
  408. // Model.T_Cus_CustomerBase model = new Model.T_Cus_CustomerBase();
  409. // BLL.T_Cus_CustomerBase bll = new BLL.T_Cus_CustomerBase();
  410. // int count = 0;
  411. // for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
  412. // {
  413. // IRow row = sheet.GetRow(i);
  414. // DataRow dataRow = dt.NewRow();
  415. // for (int j = row.FirstCellNum; j < cellCount; j++)
  416. // {
  417. // if (row.GetCell(j) != null)
  418. // dataRow[j] = row.GetCell(j).ToString();
  419. // }
  420. // dt.Rows.Add(dataRow);
  421. // //导入后要插入数据库
  422. // model.F_CustomerName = dataRow[2].ToString();
  423. // model.F_Province = dataRow[0].ToString();
  424. // model.F_CustomerIndustry = dataRow[1].ToString();
  425. // model.F_Mobile = dataRow[3].ToString();
  426. // model.F_CreateBy = userId;
  427. // model.F_CreatedOn = DateTime.Now;
  428. // model.F_DeleteFlag = 0;
  429. // int n = bll.Add(model);
  430. // if (n > 0)
  431. // { count++; }
  432. // }
  433. // res = Success("共导入" + dt.Rows.Count + "条,成功" + count + "条", dt);
  434. // }
  435. // else
  436. // {
  437. // res = Error("文件未上传成功");
  438. // }
  439. // }
  440. // else
  441. // {
  442. // res = Error("文件类型错误");
  443. // }
  444. //}
  445. //else
  446. //{
  447. // res = Error("参数传输失败");
  448. //}
  449. HttpPostedFile _upFile = RequestString.GetFile("upFile");
  450. if (_upFile != null)
  451. {
  452. string filepath = "";
  453. string datepath = DateTime.Now.ToString("yyyyMMddHHMMss");
  454. string aLastName = _upFile.FileName.Substring(_upFile.FileName.LastIndexOf(".") + 1, (_upFile.FileName.Length - _upFile.FileName.LastIndexOf(".") - 1)); //扩展名
  455. if (aLastName != "xls" && aLastName != "xlsx")
  456. {
  457. res = Error("文件类型错误,请选择Excel文件");
  458. }
  459. string newpath = datepath + "_" + _upFile.FileName;
  460. if (!Directory.Exists(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData")))
  461. {
  462. Directory.CreateDirectory(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData"));
  463. }
  464. filepath = this.Request.ApplicationPath + "/ExcelData/" + newpath;
  465. string PhysicalPath = Server.MapPath(filepath);
  466. _upFile.SaveAs(PhysicalPath);
  467. DataTable dt = new DataTable();
  468. IWorkbook workbook = null;
  469. using (FileStream file = new FileStream(PhysicalPath, FileMode.Open, FileAccess.Read))
  470. {
  471. if (aLastName == "xlsx") // 2007版本
  472. {
  473. workbook = new XSSFWorkbook(file);
  474. }
  475. else if (aLastName == "xls") // 2003版本
  476. {
  477. workbook = new HSSFWorkbook(file);
  478. }
  479. //hssfworkbook = new HSSFWorkbook(file);
  480. }
  481. ISheet sheet = workbook.GetSheetAt(0);
  482. System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  483. IRow headerRow = sheet.GetRow(0);
  484. int cellCount = headerRow.LastCellNum;
  485. if (cellCount < 1)
  486. res = Error("文件标题没有数据");
  487. for (int j = 0; j < cellCount; j++)
  488. {
  489. ICell cell = headerRow.GetCell(j);
  490. dt.Columns.Add(cell.ToString());
  491. }
  492. Model.T_Cus_CustomerBase model = new Model.T_Cus_CustomerBase();
  493. BLL.T_Cus_CustomerBase bll = new BLL.T_Cus_CustomerBase();
  494. int count = 0;
  495. //客户名称加入来电单位
  496. List<string> lddep = null;
  497. lddep = CacheHelper.Get("LDDep") as List<string>;
  498. for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
  499. {
  500. IRow row = sheet.GetRow(i);
  501. DataRow dataRow = dt.NewRow();
  502. for (int j = row.FirstCellNum; j < cellCount; j++)
  503. {
  504. if (row.GetCell(j) != null)
  505. dataRow[j] = row.GetCell(j).ToString();
  506. }
  507. //dt.Rows.Add(dataRow);
  508. dt.Rows.Add(dataRow);
  509. if (dt.Rows.Count < 1)
  510. res = Error("文件内容没有数据");
  511. //导入后要插入数据库
  512. model.F_CustomerName = dataRow[2].ToString();
  513. model.F_Province = dataRow[0].ToString();
  514. model.F_CustomerIndustry = dataRow[1].ToString();
  515. //model.F_Mobile = dataRow[3].ToString();
  516. model.F_Telephone = dataRow[3].ToString();
  517. model.F_CreateBy = userId;
  518. model.F_CreatedOn = DateTime.Now;
  519. model.F_DeleteFlag = 0;
  520. int n = bll.Add(model);
  521. if (n > 0)
  522. {
  523. count++;
  524. var cusindustry = dataRow[1].ToString();
  525. if (!lddep.Contains(cusindustry))
  526. lddep.Add(cusindustry);
  527. }
  528. }
  529. //if (dt == null || dt.Rows.Count == 0)
  530. if (dt == null || dt.Rows.Count == 0)
  531. res = Error("文件没有数据");
  532. //var jstr = DataTableToJson(dt);
  533. else
  534. {
  535. var jstr = DataTableToJson(dt);
  536. res = Success("导入成功", jstr);
  537. }
  538. }
  539. }
  540. }
  541. return res;
  542. }
  543. public string DataTableToJson(DataTable dt)
  544. {
  545. string JsonString = string.Empty;
  546. JsonString = JsonConvert.SerializeObject(dt);
  547. return JsonString;
  548. }
  549. }
  550. }