三元财务API

CustomerNewController.cs 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. using CallCenter.Utility;
  2. using CallCenterApi.Interface.Controllers.Base;
  3. using CallCenterApi.Interface.Models.Input;
  4. using Newtonsoft.Json;
  5. using NPOI.HSSF.UserModel;
  6. using NPOI.SS.UserModel;
  7. using NPOI.XSSF.UserModel;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Web;
  14. using System.Web.Mvc;
  15. namespace CallCenterApi.Interface.Controllers.customer
  16. {
  17. public class CustomerNewController : BaseController
  18. {
  19. BLL.T_Cus_CustomerBaseNew cusbll = new BLL.T_Cus_CustomerBaseNew();
  20. BLL.T_Cus_ContactPersonNew conbll = new BLL.T_Cus_ContactPersonNew();
  21. BLL.T_Sys_Department deptbll = new BLL.T_Sys_Department();
  22. /// <summary>
  23. /// 获取客户列表
  24. /// </summary>
  25. /// <param name="code">客户编号</param>
  26. /// <param name="cmpname">公司名称</param>
  27. /// <param name="cmpaddress">公司地址</param>
  28. /// <param name="legname">法人姓名</param>
  29. /// <param name="legtel">法人电话</param>
  30. /// <param name="subtrade">所属行业</param>
  31. /// <param name="layer">客户信誉等级</param>
  32. /// <param name="bedept">客户归属部门</param>
  33. /// <param name="bereladd">客户归属涉税会计</param>
  34. /// <param name="beaudacc">客户归属审核会计</param>
  35. /// <param name="bemakeacc">客户归属做账会计</param>
  36. /// <param name="conname">联系人姓名</param>
  37. /// <param name="contel">联系人电话</param>
  38. /// <returns></returns>
  39. public ActionResult GetList(string keywords, string code, string cmpname, string cmpaddress,
  40. string legname, string legtel, string subtrade, string layer, string bedept,
  41. string berelacc, string beaudacc, string bemakeacc, string conname, string contel,
  42. int pageindex = 1, int pagesize = 10)
  43. {
  44. string sql = "";
  45. DataTable dt = new DataTable();
  46. #region 筛选条件
  47. if (!string.IsNullOrWhiteSpace(keywords))//关键字搜索
  48. sql += $" and F_KeyWords like '%" + keywords.Trim() + "%'";
  49. if (!string.IsNullOrWhiteSpace(code))//客户编号
  50. sql += $" and F_CustomerCode like '%" + code.Trim() + "%'";
  51. if (!string.IsNullOrWhiteSpace(cmpname))//公司名称
  52. sql += $" and F_KeyWords like '%" + cmpname.Trim() + "%'";
  53. if (!string.IsNullOrWhiteSpace(cmpaddress))//公司地址
  54. sql += $" and F_CompanyAddress like '%" + cmpaddress.Trim() + "%'";
  55. if (!string.IsNullOrWhiteSpace(legname))//法人姓名
  56. sql += $" and F_LegalName like '%" + legname.Trim() + "%'";
  57. if (!string.IsNullOrWhiteSpace(legtel))//法人电话
  58. sql += $" and F_LegalTel like '%" + legtel.Trim() + "%'";
  59. if (!string.IsNullOrWhiteSpace(subtrade))//所属行业
  60. sql += $" and isnull(F_SubTrade,'') = '" + subtrade.Trim() + "'";
  61. if (!string.IsNullOrWhiteSpace(layer))//客户信誉等级
  62. sql += $" and isnull(F_Layer,'') = '" + layer.Trim() + "'";
  63. if (!string.IsNullOrWhiteSpace(bedept))//客户所属部门
  64. sql += $" and isnull(F_BeDept,'') = '" + bedept.Trim() + "'";
  65. if (!string.IsNullOrWhiteSpace(berelacc))//涉税会计
  66. sql += $" and F_BeRelatedAcc like '%" + berelacc.Trim() + "%'";
  67. if (!string.IsNullOrWhiteSpace(beaudacc))//审核会计
  68. sql += $" and F_BeAuditAcc like '%" + beaudacc.Trim() + "%'";
  69. if (!string.IsNullOrWhiteSpace(bemakeacc))//做账会计
  70. sql += $" and F_BeMakeAcc like '%" + bemakeacc.Trim() + "%'";
  71. if (!string.IsNullOrWhiteSpace(conname) || !string.IsNullOrWhiteSpace(contel))
  72. {
  73. var sqlcon = " where F_DeleteFlag=0 ";
  74. if (!string.IsNullOrWhiteSpace(conname)) // 联系人
  75. sqlcon += " and F_Name like '%" + conname + "%'";
  76. if (!string.IsNullOrWhiteSpace(contel)) //联系人电话
  77. sqlcon += " and F_Telephone like '%" + contel + "%'";
  78. sql += $" and F_CustomerId in (select F_CustomerId from T_Cus_ContactPersonNew " + sqlcon + " )";
  79. }
  80. #endregion
  81. int recordCount = 0;
  82. if (!string.IsNullOrWhiteSpace(sql))
  83. {
  84. sql += " and F_DeleteFlag=0";
  85. dt = BLL.PagerBLL.GetListPager(
  86. "T_Cus_CustomerBaseNew",
  87. "F_CustomerId",
  88. "*,dbo.GetDeptName(F_BeDept) as F_BeDeptName",
  89. sql,
  90. "ORDER BY F_CustomerId desc",
  91. pagesize,
  92. pageindex,
  93. true,
  94. out recordCount);
  95. #region
  96. dt.Columns.Add("F_Contact");
  97. var dept = new BLL.T_Sys_Department().GetModelList("");
  98. foreach (DataRow dr in dt.Rows)
  99. {
  100. #region 绑定联系人信息
  101. var contact = "";
  102. string cusid = dr["F_CustomerId"] != null ? dr["F_CustomerId"].ToString() : "";
  103. if (cusid != "")
  104. {
  105. var conlist = conbll.GetModelList(" F_CustomerId=" + cusid);
  106. if (conlist.Count > 0)
  107. {
  108. foreach (var item in conlist)
  109. {
  110. contact += item.F_Name + "(" + item.F_Telephone + "),";
  111. }
  112. }
  113. dr["F_Contact"] = contact.TrimEnd(',');
  114. }
  115. #endregion
  116. }
  117. #endregion
  118. }
  119. var obj = new
  120. {
  121. state = "success",
  122. message = "成功",
  123. rows = dt,
  124. total = recordCount
  125. };
  126. return Content(obj.ToJson());
  127. }
  128. /// <summary>
  129. /// 根据关键字搜索公司
  130. /// </summary>
  131. public ActionResult GetListByKey(string keywords)
  132. {
  133. string sql = "";
  134. if (!string.IsNullOrWhiteSpace(keywords))//关键字搜索
  135. sql += $" F_KeyWords like '%" + keywords.Trim() + "%'";
  136. var list = new List<Model.T_Cus_CustomerBaseNew>();
  137. if (!string.IsNullOrWhiteSpace(sql))
  138. {
  139. sql += " and F_DeleteFlag=0";
  140. list = cusbll.GetModelList(sql);
  141. }
  142. if (list.Count > 0)
  143. return Success("根据关键字获取公司信息", list.FirstOrDefault());
  144. else
  145. return Error("没有获取到相关公司,请重新搜索");
  146. }
  147. /// <summary>
  148. /// 获取客户信息
  149. /// </summary>
  150. /// <returns></returns>
  151. public ActionResult GetCustomer(int cusid)
  152. {
  153. if (cusid != 0)
  154. {
  155. Model.T_Cus_CustomerBaseNew userModel = cusbll.GetModel(cusid);
  156. if (userModel != null)
  157. {
  158. #region 绑定部门
  159. var deptname = "";
  160. var deptmodel = deptbll.GetModel(userModel.F_BeDept.Value);
  161. if (deptmodel != null)
  162. deptname = deptmodel.F_DeptName;
  163. #endregion
  164. var conlist = conbll.GetModelList(" F_DeleteFlag=0 and F_CustomerId=" + cusid);
  165. var obj = new
  166. {
  167. CustomerBase = userModel,
  168. BeDeptName = deptname,
  169. ContactList = conlist,
  170. };
  171. return Success("获取成功", obj);
  172. }
  173. else
  174. {
  175. return Error("获取失败");
  176. }
  177. }
  178. else
  179. {
  180. return Error("参数传输失败");
  181. }
  182. }
  183. /// <summary>
  184. /// 通过来电号码获取来电弹屏左侧客户信息
  185. /// </summary>
  186. /// <returns></returns>
  187. public ActionResult GetCustomerByTel(string tel)
  188. {
  189. if (!string.IsNullOrWhiteSpace(tel))
  190. {
  191. 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() + "%')";
  192. var userModel = new BLL.T_Cus_CustomerBaseNew().GetModelList(" F_DeleteFlag = 0 " + sql);
  193. if (userModel.Count() > 0)
  194. {
  195. var model = userModel.Last();
  196. int cusid = model.F_CustomerId;
  197. var conlist = conbll.GetModelList(" F_DeleteFlag=0 and F_CustomerId=" + cusid);
  198. #region 绑定部门
  199. var deptname = "";
  200. var deptmodel = deptbll.GetModel(model.F_BeDept.Value);
  201. if (deptmodel != null)
  202. deptname = deptmodel.F_DeptName;
  203. #endregion
  204. var obj = new
  205. {
  206. CustomerBase = model,
  207. BeDeptName = deptname,
  208. ContactList = conlist,
  209. };
  210. return Success("获取成功", obj);
  211. }
  212. else
  213. {
  214. return Success("获取成功");
  215. }
  216. }
  217. else
  218. {
  219. return Error("参数传输失败");
  220. }
  221. }
  222. /// <summary>
  223. /// 添加客户信息
  224. /// </summary>
  225. /// <returns></returns>
  226. public ActionResult Add(CustomerBaseNewInput input)
  227. {
  228. string usercode = CurrentUser.UserData.F_UserCode;
  229. #region 添加验证判断
  230. if (string.IsNullOrEmpty(input.F_CustomerCode))
  231. return Error("编号不能为空!");
  232. if (getunique(0, input.F_CustomerCode))
  233. return Error("编号已被占用,请重新输入!");
  234. #endregion
  235. var model = new Model.T_Cus_CustomerBaseNew();
  236. #region 保存客户基本信息
  237. model.F_CustomerCode = input.F_CustomerCode;
  238. model.F_CompanyName = input.F_CompanyName;
  239. #region 处理关键字
  240. var keyword = model.F_CompanyName;
  241. var keyconfig = Configs.GetValue("keystring");
  242. var keystring = keyconfig.Split(',');
  243. foreach (var item in keystring)
  244. {
  245. if (model.F_CompanyName.Contains(item))
  246. keyword = keyword.Replace(item, "");
  247. }
  248. model.F_KeyWords = keyword;
  249. #endregion
  250. //model.F_KeyWords = input.F_KeyWords;
  251. model.F_RegisteredAddress = input.F_RegisteredAddress;
  252. model.F_CompanyAddress = input.F_CompanyAddress;
  253. model.F_LegalName = input.F_LegalName;
  254. model.F_LegalIDCards = input.F_LegalIDCards;
  255. model.F_LegalTel = input.F_LegalTel;
  256. model.F_Qualification = input.F_Qualification;
  257. model.F_Layer = input.F_Layer;
  258. model.F_InvoiceRange = input.F_InvoiceRange;
  259. model.F_SubTrade = input.F_SubTrade;
  260. model.F_TaxCategory = input.F_TaxCategory;
  261. model.F_TaxPointDes = input.F_TaxPointDes;
  262. model.F_WxPassword = input.F_WxPassword;
  263. model.F_BeDept = input.F_BeDept;
  264. model.F_FinancialManager = input.F_FinancialManager;
  265. model.F_BusinessOwner = input.F_BusinessOwner;
  266. model.F_BeRelatedAcc = input.F_BeRelatedAcc;
  267. model.F_BeAuditAcc = input.F_BeAuditAcc;
  268. model.F_BeMakeAcc = input.F_BeMakeAcc;
  269. model.F_Remark1 = input.F_Remark1;
  270. model.F_Remark2 = input.F_Remark2;
  271. model.F_Remark3 = input.F_Remark3;
  272. model.F_CreateBy = usercode;
  273. model.F_CreatedOn = DateTime.Now;
  274. model.F_DeleteFlag = 0;
  275. #endregion
  276. int n = cusbll.Add(model);
  277. if (n > 0)
  278. {
  279. #region 添加联系人
  280. if (input.ContactList != null && input.ContactList.Count > 0)
  281. {
  282. var conmodel = new Model.T_Cus_ContactPersonNew();
  283. conmodel.F_CustomerId = n;
  284. conmodel.F_CreateBy = usercode;
  285. conmodel.F_CreateOn = DateTime.Now;
  286. conmodel.F_DeleteFlag = 0;
  287. foreach (var item in input.ContactList)
  288. {
  289. conmodel.F_IsMain = item.F_IsMain;
  290. conmodel.F_Name = item.F_Name;
  291. conmodel.F_Telephone = item.F_Telephone;
  292. conmodel.F_Duties = item.F_Duties;
  293. conmodel.F_Email = item.F_Email;
  294. conmodel.F_MSN = item.F_MSN;
  295. conmodel.F_QQ = item.F_QQ;
  296. conmodel.F_Remark = item.F_Remark;
  297. conbll.Add(conmodel);
  298. }
  299. }
  300. #endregion
  301. return Success("新增成功!");
  302. }
  303. else
  304. return Error("新增失败!");
  305. }
  306. /// <summary>
  307. /// 修改客户信息
  308. /// </summary>
  309. /// <returns></returns>
  310. public ActionResult Update(CustomerBaseNewInput input)
  311. {
  312. string usercode = CurrentUser.UserData.F_UserCode;
  313. #region 添加验证判断
  314. if (input.F_CustomerId <= 0)
  315. return Error("参数错误!");
  316. if (getunique(input.F_CustomerId, input.F_CustomerCode))
  317. return Error("编号已被占用,请重新输入!");
  318. #endregion
  319. var model = cusbll.GetModel(input.F_CustomerId);
  320. #region 保存客户基本信息
  321. //model.F_CustomerCode = input.F_CustomerCode;
  322. model.F_CompanyName = input.F_CompanyName;
  323. #region 处理关键字
  324. var keyword = model.F_CompanyName;
  325. var keyconfig = Configs.GetValue("keystring");
  326. var keystring = keyconfig.Split(',');
  327. foreach (var item in keystring)
  328. {
  329. if (model.F_CompanyName.Contains(item))
  330. keyword = keyword.Replace(item, "");
  331. }
  332. model.F_KeyWords = keyword;
  333. #endregion
  334. model.F_RegisteredAddress = input.F_RegisteredAddress;
  335. model.F_CompanyAddress = input.F_CompanyAddress;
  336. model.F_LegalName = input.F_LegalName;
  337. model.F_LegalIDCards = input.F_LegalIDCards;
  338. model.F_LegalTel = input.F_LegalTel;
  339. model.F_Qualification = input.F_Qualification;
  340. model.F_Layer = input.F_Layer;
  341. model.F_InvoiceRange = input.F_InvoiceRange;
  342. model.F_SubTrade = input.F_SubTrade;
  343. model.F_TaxCategory = input.F_TaxCategory;
  344. model.F_TaxPointDes = input.F_TaxPointDes;
  345. model.F_WxPassword = input.F_WxPassword;
  346. model.F_BeDept = input.F_BeDept;
  347. model.F_FinancialManager = input.F_FinancialManager;
  348. model.F_BusinessOwner = input.F_BusinessOwner;
  349. model.F_BeRelatedAcc = input.F_BeRelatedAcc;
  350. model.F_BeAuditAcc = input.F_BeAuditAcc;
  351. model.F_BeMakeAcc = input.F_BeMakeAcc;
  352. model.F_Remark1 = input.F_Remark1;
  353. model.F_Remark2 = input.F_Remark2;
  354. model.F_Remark3 = input.F_Remark3;
  355. #endregion
  356. bool n = cusbll.Update(model);
  357. if (n)
  358. {
  359. return Success("保存成功!");
  360. }
  361. else
  362. return Error("保存失败!");
  363. }
  364. /// <summary>
  365. /// 验证客户编号是否唯一
  366. /// </summary>
  367. private bool getunique(int id, string code)
  368. {
  369. var sql = " F_DeleteFlag=0 and F_CustomerCode='" + code + "'";
  370. if (id > 0)
  371. sql += " and F_CustomerId<>"+id;
  372. var count=cusbll.GetModelList(sql).Count();
  373. return count > 0;
  374. }
  375. /// <summary>
  376. /// 删除客户
  377. /// </summary>
  378. /// <param name="ids"></param>
  379. /// <returns></returns>
  380. public ActionResult DelCustomer(string[] ids)
  381. {
  382. if (ids != null && ids.Length > 0)
  383. {
  384. string idd = " ";
  385. foreach (string str in ids)
  386. {
  387. idd += str + ",";
  388. }
  389. if (!string.IsNullOrEmpty(idd.Trim()))
  390. {
  391. if (cusbll.DeleteList(idd.TrimEnd(',')))
  392. {
  393. conbll.DeleteListByCusid(idd.TrimEnd(','));
  394. return Success("设置成功");
  395. }
  396. else
  397. {
  398. return Error("设置失败");
  399. }
  400. }
  401. else
  402. {
  403. return Error("请选择用户");
  404. }
  405. }
  406. else
  407. {
  408. return Error("获取参数失败");
  409. }
  410. }
  411. /// <summary>
  412. /// 获取客户联系人列表
  413. /// </summary>
  414. public ActionResult GetConList(string tel, string cusid, int pageindex = 1, int pagesize = 10)
  415. {
  416. string sql = "";
  417. DataTable dt = new DataTable();
  418. #region 筛选条件
  419. if (!string.IsNullOrWhiteSpace(cusid))
  420. sql += " and F_CustomerId=" + cusid + "";
  421. if (!string.IsNullOrWhiteSpace(tel)) //联系人电话
  422. sql += " and F_Telephone like '%" + tel + "%'";
  423. #endregion
  424. int recordCount = 0;
  425. if (!string.IsNullOrWhiteSpace(sql))
  426. {
  427. sql += " and F_DeleteFlag=0";
  428. dt = BLL.PagerBLL.GetListPager(
  429. "T_Cus_ContactPersonNew",
  430. "F_ManId",
  431. "*",
  432. sql,
  433. "ORDER BY F_ManId desc",
  434. pagesize,
  435. pageindex,
  436. true,
  437. out recordCount);
  438. }
  439. var obj = new
  440. {
  441. state = "success",
  442. message = "成功",
  443. rows = dt,
  444. total = recordCount
  445. };
  446. return Content(obj.ToJson());
  447. }
  448. /// <summary>
  449. /// 添加联系人
  450. /// </summary>
  451. /// <param name="coninput"></param>
  452. /// <returns></returns>
  453. public ActionResult AddContact(ContactPersonNewInput coninput)
  454. {
  455. string usercode = CurrentUser.UserData.F_UserCode;
  456. #region 添加验证判断
  457. if (coninput.F_CustomerId <= 0)
  458. return Error("客户ID获取失败!");
  459. #endregion
  460. var conmodel = new Model.T_Cus_ContactPersonNew();
  461. conmodel.F_CustomerId = coninput.F_CustomerId;
  462. conmodel.F_IsMain = coninput.F_IsMain;
  463. conmodel.F_Name = coninput.F_Name;
  464. conmodel.F_Telephone = coninput.F_Telephone;
  465. conmodel.F_Duties = coninput.F_Duties;
  466. conmodel.F_Email = coninput.F_Email;
  467. conmodel.F_MSN = coninput.F_MSN;
  468. conmodel.F_QQ = coninput.F_QQ;
  469. conmodel.F_Remark = coninput.F_Remark;
  470. conmodel.F_CreateBy = usercode;
  471. conmodel.F_CreateOn = DateTime.Now;
  472. conmodel.F_DeleteFlag = 0;
  473. int n = conbll.Add(conmodel);
  474. if (n > 0)
  475. return Success("联系人保存成功!");
  476. else
  477. return Error("联系人保存失败!");
  478. }
  479. /// <summary>
  480. /// 修改联系人
  481. /// </summary>
  482. /// <param name="coninput"></param>
  483. /// <returns></returns>
  484. public ActionResult UpdateContact(ContactPersonNewInput coninput)
  485. {
  486. string usercode = CurrentUser.UserData.F_UserCode;
  487. #region 添加验证判断
  488. if (coninput.F_ManId <= 0)
  489. return Error("参数错误!");
  490. if (coninput.F_CustomerId <= 0)
  491. return Error("客户ID获取失败!");
  492. #endregion
  493. var conmodel = conbll.GetModel(coninput.F_ManId);
  494. conmodel.F_CustomerId = coninput.F_CustomerId;
  495. conmodel.F_IsMain = coninput.F_IsMain;
  496. conmodel.F_Name = coninput.F_Name;
  497. conmodel.F_Telephone = coninput.F_Telephone;
  498. conmodel.F_Duties = coninput.F_Duties;
  499. conmodel.F_Email = coninput.F_Email;
  500. conmodel.F_MSN = coninput.F_MSN;
  501. conmodel.F_QQ = coninput.F_QQ;
  502. conmodel.F_Remark = coninput.F_Remark;
  503. var n = conbll.Update(conmodel);
  504. if (n)
  505. return Success("联系人保存成功!");
  506. else
  507. return Error("联系人保存失败!");
  508. }
  509. /// <summary>
  510. /// 删除联系人
  511. /// </summary>
  512. /// <param name="ids"></param>
  513. /// <returns></returns>
  514. public ActionResult DelContact(string[] ids)
  515. {
  516. if (ids != null && ids.Length > 0)
  517. {
  518. string idd = " ";
  519. foreach (string str in ids)
  520. {
  521. idd += str + ",";
  522. }
  523. if (!string.IsNullOrEmpty(idd.Trim()))
  524. {
  525. if (conbll.DeleteList(idd.TrimEnd(',')))
  526. {
  527. return Success("设置成功");
  528. }
  529. else
  530. {
  531. return Error("设置失败");
  532. }
  533. }
  534. else
  535. {
  536. return Error("请选择联系人");
  537. }
  538. }
  539. else
  540. {
  541. return Error("获取参数失败");
  542. }
  543. }
  544. /// <summary>
  545. /// 导入excel
  546. /// </summary>
  547. public ActionResult ImportExcel()
  548. {
  549. string usercode = CurrentUser.UserData.F_UserCode;
  550. if (!string.IsNullOrWhiteSpace(usercode))
  551. {
  552. HttpPostedFile _upFile = RequestString.GetFile("upFile");
  553. if (_upFile != null)
  554. {
  555. int headrow = 0;
  556. #region 上传文件
  557. string filepath = "";
  558. string datepath = DateTime.Now.ToString("yyyyMMddHHMMss");
  559. string aLastName = _upFile.FileName.Substring(_upFile.FileName.LastIndexOf(".") + 1, (_upFile.FileName.Length - _upFile.FileName.LastIndexOf(".") - 1)); //扩展名
  560. if (aLastName != "xls" && aLastName != "xlsx")
  561. {
  562. return Error("文件类型错误,请选择Excel文件");
  563. }
  564. string newpath = datepath + "_" + _upFile.FileName;
  565. if (!Directory.Exists(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData")))
  566. {
  567. Directory.CreateDirectory(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData"));
  568. }
  569. filepath = this.Request.ApplicationPath + "/ExcelData/" + newpath;
  570. string PhysicalPath = Server.MapPath(filepath);
  571. _upFile.SaveAs(PhysicalPath);
  572. #endregion
  573. DataTable dt = new DataTable();
  574. #region 读取excel中内容
  575. IWorkbook workbook = null;
  576. using (FileStream file = new FileStream(PhysicalPath, FileMode.Open, FileAccess.Read))
  577. {
  578. if (aLastName == "xlsx") // 2007版本
  579. {
  580. workbook = new XSSFWorkbook(file);
  581. }
  582. else if (aLastName == "xls") // 2003版本
  583. {
  584. workbook = new HSSFWorkbook(file);
  585. }
  586. }
  587. ISheet sheet = workbook.GetSheetAt(0);
  588. System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  589. //获取sheet的第一行
  590. IRow headerRow = sheet.GetRow(headrow);
  591. int cellCount = headerRow.LastCellNum;
  592. if (cellCount < 1)
  593. return Error("文件标题没有数据");
  594. for (int j = 0; j < cellCount; j++)
  595. {
  596. ICell cell = headerRow.GetCell(j);
  597. dt.Columns.Add(cell.ToString());
  598. }
  599. #endregion
  600. int count = 0;
  601. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  602. {
  603. #region 数据入库
  604. IRow row = sheet.GetRow(i);
  605. DataRow dataRow = dt.NewRow();
  606. for (int j = row.FirstCellNum; j < cellCount; j++)
  607. {
  608. if (row.GetCell(j) != null)
  609. dataRow[j] = row.GetCell(j).ToString();
  610. }
  611. dt.Rows.Add(dataRow);
  612. if (dt.Rows.Count < 1)
  613. return Error("文件内容没有数据");
  614. var cusmodel = new Model.T_Cus_CustomerBaseNew();
  615. var conmodel = new Model.T_Cus_ContactPersonNew();
  616. #region 信息对应
  617. //报税会计 做账会计 编号 客服 公司名称 负责人电话 交接日期
  618. cusmodel.F_CustomerCode = dataRow["编号"].ToString();
  619. cusmodel.F_CompanyName = dataRow["公司名称"].ToString();
  620. #region 处理关键字
  621. var keyword = cusmodel.F_CompanyName;
  622. var keyconfig = Configs.GetValue("keystring");
  623. var keystring = keyconfig.Split(',');
  624. foreach (var item in keystring)
  625. {
  626. if (cusmodel.F_CompanyName.Contains(item))
  627. keyword = keyword.Replace(item, "");
  628. }
  629. cusmodel.F_KeyWords = keyword;
  630. #endregion
  631. cusmodel.F_BeRelatedAcc = dataRow["报税会计"].ToString();
  632. cusmodel.F_BeMakeAcc = dataRow["做账会计"].ToString();
  633. cusmodel.F_Remark1 = dataRow["交接日期"].ToString();
  634. cusmodel.F_Remark2 = dataRow["客服"].ToString();
  635. cusmodel.F_CreateBy = usercode;
  636. cusmodel.F_CreatedOn = DateTime.Now;
  637. cusmodel.F_DeleteFlag = 0;
  638. #endregion
  639. int n = cusbll.Add(cusmodel);
  640. if (n > 0)
  641. {
  642. #region 添加联系人
  643. if (dataRow["负责人电话"] != null)
  644. {
  645. conmodel.F_CustomerId = n;
  646. conmodel.F_CreateBy = usercode;
  647. conmodel.F_CreateOn = DateTime.Now;
  648. conmodel.F_DeleteFlag = 0;
  649. conmodel.F_IsMain = true;
  650. conmodel.F_Telephone = dataRow["负责人电话"].ToString();
  651. conbll.Add(conmodel);
  652. }
  653. #endregion
  654. count++;
  655. }
  656. #endregion
  657. }
  658. if (dt == null || dt.Rows.Count == 0)
  659. return Error("文件没有数据");
  660. else
  661. {
  662. var jstr = DataTableToJson(dt);
  663. if (count > 0)
  664. {
  665. return Success("导入成功 " + count + "条信息", jstr);
  666. }
  667. else
  668. {
  669. return Success("导入成功 " + count + "条信息", jstr);
  670. }
  671. }
  672. }
  673. return Error("数据源上传失败");
  674. }
  675. return Error("用户登录失败,请重新登录");
  676. }
  677. public string DataTableToJson(DataTable dt)
  678. {
  679. string JsonString = string.Empty;
  680. JsonString = JsonConvert.SerializeObject(dt);
  681. return JsonString;
  682. }
  683. }
  684. }