足力健后端,使用.netcore版本,合并1个项目使用

VIPInfoController.cs 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Common;
  4. using System.IRepositories;
  5. using System.Linq;
  6. using System.Model;
  7. using System.Security.Claims;
  8. using System.Threading.Tasks;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Mvc;
  11. using TVShoppingCallCenter_ZLJ.Models.Inputs;
  12. using SqlSugar;
  13. using System.Common.Helpers;
  14. using Microsoft.Extensions.Configuration;
  15. using System.Data;
  16. using System.Reflection;
  17. namespace TVShoppingCallCenter_ZLJ.Controllers.Customer
  18. {
  19. //[Authorize]
  20. [Produces("application/json")]
  21. [Route("api/[controller]")]
  22. public class VipInfoController : BaseController
  23. {
  24. private readonly ICus_VipInfoRepository _cus_vip_infoRepository;
  25. private readonly ISys_UserAccountRepository _sys_user_accountRepository;
  26. private readonly ICus_MsgRepository _cus_msgRepository;
  27. private readonly IConfiguration _configuration;
  28. public VipInfoController(ICus_VipInfoRepository cus_vip_infoRepository, ISys_UserAccountRepository sys_user_accountRepository, ICus_MsgRepository cus_msgRepository,IConfiguration configuration)
  29. {
  30. _cus_vip_infoRepository = cus_vip_infoRepository;
  31. _sys_user_accountRepository = sys_user_accountRepository;
  32. _cus_msgRepository = cus_msgRepository;
  33. _configuration = configuration;
  34. }
  35. [HttpGet]
  36. public IActionResult Index()
  37. {
  38. return Success("成功");
  39. }
  40. /// <summary>
  41. /// 获取会员列表分页
  42. /// </summary>
  43. /// <param name="keyword"></param>
  44. /// <param name="pageindex"></param>
  45. /// <param name="pagesize"></param>
  46. /// <returns></returns>
  47. [HttpGet("getlistbypage")]
  48. public async Task<IActionResult> GetListsByPageAsync(string keyword,string name,string phone,string type, string province,string city,string area,string town,string address,string birthday,string recommender,string saleperson,string contact, int pageindex = 1, int pagesize = 20)
  49. {
  50. List<IConditionalModel> conModels = new List<IConditionalModel>();
  51. #region 条件筛选
  52. conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
  53. if (!string.IsNullOrEmpty(phone))
  54. conModels.Add(new ConditionalCollections()
  55. {
  56. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
  57. {
  58. new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Phone", ConditionalType = ConditionalType.Like, FieldValue = phone }),
  59. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Mobile", ConditionalType = ConditionalType.Like, FieldValue = phone }),
  60. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or, new ConditionalModel() { FieldName = "F_Mobile1", ConditionalType = ConditionalType.Like, FieldValue = phone })
  61. }
  62. });
  63. if (!string.IsNullOrEmpty(name))
  64. {
  65. conModels.Add(new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = name });
  66. }
  67. if (!string.IsNullOrEmpty(type))
  68. {
  69. conModels.Add(new ConditionalModel() { FieldName = "F_Type", ConditionalType = ConditionalType.Like, FieldValue = type });
  70. }
  71. if (!string.IsNullOrEmpty(province))
  72. {
  73. conModels.Add(new ConditionalModel() { FieldName = "F_Province", ConditionalType = ConditionalType.Like, FieldValue = province });
  74. }
  75. if (!string.IsNullOrEmpty(city))
  76. {
  77. conModels.Add(new ConditionalModel() { FieldName = "F_City", ConditionalType = ConditionalType.Like, FieldValue = city });
  78. }
  79. if (!string.IsNullOrEmpty(area))
  80. {
  81. conModels.Add(new ConditionalModel() { FieldName = "F_Area", ConditionalType = ConditionalType.Like, FieldValue = area });
  82. }
  83. if (!string.IsNullOrEmpty(town))
  84. {
  85. conModels.Add(new ConditionalModel() { FieldName = "F_Town", ConditionalType = ConditionalType.Like, FieldValue = town });
  86. }
  87. if (!string.IsNullOrEmpty(address))
  88. {
  89. conModels.Add(new ConditionalModel() { FieldName = "F_Address", ConditionalType = ConditionalType.Like, FieldValue = address });
  90. new KeyValuePair<WhereType, ConditionalModel>(WhereType.Or, new ConditionalModel() { FieldName = "F_Address1", ConditionalType = ConditionalType.Like, FieldValue = address });
  91. }
  92. if (!string.IsNullOrEmpty(birthday))
  93. {
  94. conModels.Add(new ConditionalModel() { FieldName = "F_Birthday", ConditionalType = ConditionalType.Like, FieldValue = birthday });
  95. }
  96. if (!string.IsNullOrEmpty(recommender))
  97. {
  98. conModels.Add(new ConditionalModel() { FieldName = "F_Recommender", ConditionalType = ConditionalType.Like, FieldValue = recommender });
  99. }
  100. if (!string.IsNullOrEmpty(saleperson))
  101. {
  102. conModels.Add(new ConditionalModel() { FieldName = "F_Saleperson", ConditionalType = ConditionalType.Like, FieldValue = saleperson });
  103. }
  104. if (!string.IsNullOrEmpty(contact))
  105. {
  106. conModels.Add(new ConditionalModel() { FieldName = "F_Contact", ConditionalType = ConditionalType.Like, FieldValue = contact });
  107. }
  108. #endregion
  109. int recordCount = 0;
  110. var list = await _cus_vip_infoRepository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount });
  111. var obj = new
  112. {
  113. state = "success",
  114. message = "成功",
  115. rows = list,
  116. total = recordCount,
  117. };
  118. return Content(obj.ToJson());
  119. }
  120. /// <summary>
  121. /// 获取会员信息详情
  122. /// </summary>
  123. /// <param name="id">id</param>
  124. /// <returns></returns>
  125. [HttpGet("getdetails")]
  126. public async Task<IActionResult> GetDetailsAsync(int id)
  127. {
  128. if (id <= 0)
  129. return Error("参数错误");
  130. var model = await _cus_vip_infoRepository.GetSingle(x => x.F_ID == id);
  131. if (model == null)
  132. {
  133. return Error("获取失败");
  134. }
  135. return Success("获取成功!", model);
  136. }
  137. /// <summary>
  138. /// 添加会员
  139. /// </summary>
  140. /// <param name="input"></param>
  141. /// <returns></returns>
  142. [HttpPost("add")]
  143. public async Task<IActionResult> AddAsync(VipInfoInput input)
  144. {
  145. if (string.IsNullOrEmpty(input.name ))
  146. return Error("请输入名字");
  147. if (string.IsNullOrEmpty(input.phone ))
  148. return Error("请输入手机号");
  149. var model = new T_Cus_VipInfo();
  150. model.F_VIPCode = input .vipcode ;//会员卡号
  151. model.F_Name = input.name;
  152. model.F_Birthday = input.birthday ;
  153. model.F_Phone = input.phone;
  154. model.F_Recommender = input.recommender;
  155. model.F_Sex = input.sex;
  156. model.F_Label = input.label;
  157. model.F_State = 1;
  158. model.F_Note = input.note;
  159. model.F_City = input.city;
  160. model.F_Nickname = input.nickname;
  161. model.F_Address = input.address;
  162. model.F_Address1 = input.address1;
  163. model.F_RegTime = DateTime.Now;
  164. model.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  165. model.F_CreateOn = DateTime.Now;
  166. model.F_Age = input.age;
  167. model.F_Mobile = input.mobile;
  168. model.F_Mobile1 = input.mobile1;
  169. model.F_Province = input.province;
  170. model.F_Area = input.area;
  171. model.F_Town = input.town;
  172. model.F_Postcode = input.postcode;
  173. if (input.shoesize != null && input.shoesize.ToString() != "")
  174. {
  175. model.F_ShoeSize = input.shoesize;
  176. }
  177. if (input.score != null && input.score.ToString() != "")
  178. {
  179. model.F_Score = input.score;
  180. }
  181. if (input.money != null && input.money.ToString() != "")
  182. {
  183. model.F_Money = input.money;
  184. }
  185. if (input.totalscore != null && input.totalscore.ToString() != "")
  186. {
  187. model.F_TotalScore = input.totalscore;
  188. }
  189. model.F_Saleperson = input.saleperson;
  190. model.F_Contact = input.contact;
  191. model.F_Type = input.type;
  192. if (await _cus_vip_infoRepository.GetCount(x => x.F_VIPCode == model.F_VIPCode && x.F_Name == input.name) > 0)
  193. {
  194. return Error("添加失败,存在相同会员信息");
  195. }
  196. var res = await _cus_vip_infoRepository.Add(model);
  197. if (res > 0)
  198. {
  199. return Success("添加成功");
  200. }
  201. else
  202. {
  203. return Error("添加失败");
  204. }
  205. }
  206. /// <summary>
  207. /// 修改会员信息
  208. /// </summary>
  209. [HttpPost("update")]
  210. public async Task<IActionResult> UpdateAsync(VipInfoInput input)
  211. {
  212. if (string.IsNullOrEmpty(input.name))
  213. return Error("请输入名字");
  214. if (string.IsNullOrEmpty(input.phone))
  215. return Error("请输入手机号");
  216. var model = await _cus_vip_infoRepository.GetSingle(x => x.F_ID == input.id);
  217. if (model == null)
  218. return Error("操作失败");
  219. //model.F_VIPCode = input.vipcode;//会员卡号
  220. model.F_Name = input.name;
  221. model.F_Birthday = input.birthday;
  222. model.F_Phone = input.phone;
  223. model.F_Recommender = input.recommender;
  224. model.F_Sex = input.sex;
  225. model.F_Label = input.label;
  226. model.F_State = input.state;
  227. model.F_Note = input.note;
  228. model.F_City = input.city;
  229. model.F_Nickname = input.nickname;
  230. model.F_Address = input.address;
  231. model.F_Address1 = input.address1;
  232. model.F_RegTime = DateTime.Now;
  233. model.F_LastModifyOn = DateTime.Now.ToLocalTime();
  234. model.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  235. model.F_Age = input.age;
  236. model.F_Mobile = input.mobile;
  237. model.F_Mobile1 = input.mobile1;
  238. model.F_Province = input.province;
  239. model.F_Area = input.area;
  240. model.F_Town = input.town;
  241. model.F_Postcode = input.postcode;
  242. if (input.shoesize != null && input.shoesize.ToString() != "")
  243. {
  244. model.F_ShoeSize = input.shoesize;
  245. }
  246. if (input.score != null && input.score.ToString() != "")
  247. {
  248. model.F_Score = input.score;
  249. }
  250. if (input.money != null && input.money.ToString() != "")
  251. {
  252. model.F_Money = input.money;
  253. }
  254. if (input.totalscore != null && input.totalscore.ToString() != "")
  255. {
  256. model.F_TotalScore = input.totalscore;
  257. }
  258. model.F_Saleperson = input.saleperson;
  259. model.F_Contact = input.contact;
  260. model.F_Type = input.type;
  261. bool b = await _cus_vip_infoRepository.Update(model);
  262. if (b)
  263. return Success("修改成功");
  264. return Error("修改失败");
  265. }
  266. /// <summary>
  267. /// 删除会员信息 by ids
  268. /// </summary>
  269. /// <param name="ids"></param>
  270. /// <returns></returns>
  271. [HttpPost("delete")]
  272. public async Task<IActionResult> Remove(int[] ids)
  273. {
  274. var res = 0;
  275. if (ids != null && ids.Length > 0)
  276. {
  277. foreach (var item in ids)
  278. {
  279. var ml = await _cus_vip_infoRepository.GetSingle(x => x.F_ID == item);
  280. ml.F_State = (int)EnumDelState.Delete;
  281. ml.F_DeleteOn = DateTime.Now.ToLocalTime();
  282. ml.F_DeleteBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  283. if (_cus_vip_infoRepository.Update(ml).Result)
  284. res += 1;
  285. }
  286. if (res == ids.Length)
  287. return Success("删除成功");
  288. else if (res > 0 && res < ids.Length)
  289. return Error("部分删除失败,请查看后重新操作");
  290. else
  291. return Error("删除失败,请查看后重新操作");
  292. }
  293. else
  294. return Error("请选择要删除的记录");
  295. }
  296. /// <summary>
  297. /// 转移会员信息
  298. /// </summary>
  299. /// <param name="ids"></param>
  300. /// <returns></returns>
  301. [HttpPost("transfer")]
  302. public async Task<IActionResult> Transfer(int[] ids,string toperson)
  303. {
  304. var res = 0;
  305. if (ids != null && ids.Length > 0)
  306. {
  307. foreach (var item in ids)
  308. {
  309. var ml = await _cus_vip_infoRepository.GetSingle(x => x.F_ID == item);
  310. if (ml != null)
  311. {
  312. #region 创建消息提醒
  313. var model = new T_Cus_Msg();
  314. model.F_Type = 1;
  315. model.F_VipInfoID = item;
  316. model.F_ToPerson = toperson;
  317. model.F_State = 0;
  318. model.F_Notice = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  319. model.F_NotifyPerson = ml.F_Saleperson;
  320. model.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  321. model.F_CreateOn = DateTime.Now;
  322. var su = await _cus_msgRepository.Add(model);
  323. if (su > 0)
  324. {
  325. res += 1;
  326. }
  327. #endregion
  328. }
  329. //ml.F_Saleperson = toperson;
  330. //ml.F_LastModifyOn = DateTime.Now.ToLocalTime();
  331. //ml.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  332. //if (_cus_vip_infoRepository.Update(ml).Result)
  333. // res += 1;
  334. }
  335. if (res == ids.Length)
  336. return Success("转移消息创建成功");
  337. else if (res > 0 && res < ids.Length)
  338. return Error("部分转移消息创建失败,请查看后重新操作");
  339. else
  340. return Error("转移消息创建失败,请查看后重新操作");
  341. }
  342. else
  343. return Error("请选择要转移的记录");
  344. }
  345. /// <summary>
  346. /// 转移消息处理
  347. /// </summary>
  348. /// <param name="ids"></param>
  349. /// <returns></returns>
  350. [HttpPost("deal")]
  351. public async Task<IActionResult> Deal(int[] ids, int notagree)//同意传0,不同意传1
  352. {
  353. var res = 0;
  354. if (ids != null && ids.Length > 0)
  355. {
  356. foreach (var item in ids)
  357. {
  358. var ml = await _cus_msgRepository.GetSingle(x => x.F_ID == item);
  359. if (ml != null)
  360. {
  361. #region 创建消息提醒
  362. //var model = new T_Cus_Msg();
  363. ml.F_Type = 1;
  364. ml.F_State = notagree +2;
  365. ml.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  366. ml.F_LastModifyOn = DateTime.Now;
  367. if (_cus_msgRepository.Update(ml).Result)
  368. {
  369. if (notagree == 0)//同意转移时更新会员信息
  370. {
  371. var m = await _cus_vip_infoRepository.GetSingle(x => x.F_ID == ml.F_VipInfoID );
  372. m.F_Saleperson = ml.F_ToPerson ;
  373. m.F_LastModifyOn = DateTime.Now.ToLocalTime();
  374. m.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  375. if (_cus_vip_infoRepository.Update(m).Result)
  376. { }
  377. }
  378. res += 1;
  379. }
  380. #endregion
  381. }
  382. }
  383. if (res == ids.Length)
  384. return Success("处理成功");
  385. else if (res > 0 && res < ids.Length)
  386. return Error("部分消息处理失败,请查看后重新操作");
  387. else
  388. return Error("处理消息失败,请查看后重新操作");
  389. }
  390. else
  391. return Error("请选择要处理的记录");
  392. }
  393. #region 导入、导出
  394. //导出
  395. [Authorize]
  396. [HttpGet("exportexcel")]
  397. [AllowAnonymous]
  398. public async Task<IActionResult> ExportExcelAsync(string keyword, string name, string phone, string type, string province, string city, string area, string town, string address, string birthday, string recommender, string saleperson, string contact)
  399. {
  400. try
  401. {
  402. List<IConditionalModel> conModels = new List<IConditionalModel>();
  403. #region 条件筛选
  404. conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
  405. if (!string.IsNullOrEmpty(phone))
  406. conModels.Add(new ConditionalCollections()
  407. {
  408. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
  409. {
  410. new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Phone", ConditionalType = ConditionalType.Like, FieldValue = phone }),
  411. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Mobile", ConditionalType = ConditionalType.Like, FieldValue = phone }),
  412. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or, new ConditionalModel() { FieldName = "F_Mobile1", ConditionalType = ConditionalType.Like, FieldValue = phone })
  413. }
  414. });
  415. if (!string.IsNullOrEmpty(name))
  416. {
  417. conModels.Add(new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = name });
  418. }
  419. if (!string.IsNullOrEmpty(type))
  420. {
  421. conModels.Add(new ConditionalModel() { FieldName = "F_Type", ConditionalType = ConditionalType.Like, FieldValue = type });
  422. }
  423. if (!string.IsNullOrEmpty(province))
  424. {
  425. conModels.Add(new ConditionalModel() { FieldName = "F_Province", ConditionalType = ConditionalType.Like, FieldValue = province });
  426. }
  427. if (!string.IsNullOrEmpty(city))
  428. {
  429. conModels.Add(new ConditionalModel() { FieldName = "F_City", ConditionalType = ConditionalType.Like, FieldValue = city });
  430. }
  431. if (!string.IsNullOrEmpty(area))
  432. {
  433. conModels.Add(new ConditionalModel() { FieldName = "F_Area", ConditionalType = ConditionalType.Like, FieldValue = area });
  434. }
  435. if (!string.IsNullOrEmpty(town))
  436. {
  437. conModels.Add(new ConditionalModel() { FieldName = "F_Town", ConditionalType = ConditionalType.Like, FieldValue = town });
  438. }
  439. if (!string.IsNullOrEmpty(address))
  440. {
  441. conModels.Add(new ConditionalModel() { FieldName = "F_Address", ConditionalType = ConditionalType.Like, FieldValue = address });
  442. new KeyValuePair<WhereType, ConditionalModel>(WhereType.Or, new ConditionalModel() { FieldName = "F_Address1", ConditionalType = ConditionalType.Like, FieldValue = address });
  443. }
  444. if (!string.IsNullOrEmpty(birthday))
  445. {
  446. conModels.Add(new ConditionalModel() { FieldName = "F_Birthday", ConditionalType = ConditionalType.Like, FieldValue = birthday });
  447. }
  448. if (!string.IsNullOrEmpty(recommender))
  449. {
  450. conModels.Add(new ConditionalModel() { FieldName = "F_Recommender", ConditionalType = ConditionalType.Like, FieldValue = recommender });
  451. }
  452. if (!string.IsNullOrEmpty(saleperson))
  453. {
  454. conModels.Add(new ConditionalModel() { FieldName = "F_Saleperson", ConditionalType = ConditionalType.Like, FieldValue = saleperson });
  455. }
  456. if (!string.IsNullOrEmpty(contact))
  457. {
  458. conModels.Add(new ConditionalModel() { FieldName = "F_Contact", ConditionalType = ConditionalType.Like, FieldValue = contact });
  459. }
  460. #endregion
  461. int recordCount = 0;
  462. var list = await _cus_vip_infoRepository.GetListALL(conModels, "F_CreateOn desc");
  463. var dtNew = list.ToList();
  464. if (dtNew != null)
  465. {
  466. var tb = ModelConvertHelper<T_Cus_VipInfo>.ConvertListToDataTable(dtNew);
  467. //导出dtnew
  468. NPOIHelper npoi = new NPOIHelper();
  469. string[] cols = { "姓名", "电话", "出生日期", "性别", "标签", "推荐人", "地址", "鞋码", "创建人", "创建时间" };
  470. string[] colname = { "F_Name", "F_Phone", "F_Birthday", "F_Sex", "F_Label", "F_Recommender", "F_Address", "F_ShoeSize", "F_CreateBy", "F_CreateOn" };
  471. byte[] sm = npoi.ExportToExcel1(tb, cols, colname);
  472. if (sm != null)
  473. {
  474. return File(sm, "application/vnd.ms-excel", "会员信息.xlsx");
  475. }
  476. else
  477. {
  478. return Error("导出失败");
  479. }
  480. }
  481. return Error("导出失败");
  482. //return Success("根据条件获取数据成功", tasklist);
  483. }
  484. catch (Exception ex)
  485. {
  486. //_logger.LogError(ex, "导出异常");
  487. return Error("导出失败");
  488. }
  489. //return Success("导出成功");
  490. }
  491. /// <summary>
  492. /// 上传文件并导入数据库
  493. /// </summary>
  494. /// <returns></returns>
  495. [HttpPost("importexcel")]
  496. public async Task<IActionResult> ImportExcel( int headrow = 0)
  497. {
  498. //string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  499. Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
  500. if (!_upfile.ContentType.Equals("application/vnd.ms-excel") && !_upfile.ContentType.Equals("application/x-xls") && !_upfile.ContentType.Equals("application/x-xlsx") && !_upfile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && !_upfile.ContentType.Equals("application/octet-stream"))
  501. return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
  502. NPOIHelper npoi = new NPOIHelper();
  503. var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
  504. int num = dtExcel.Rows.Count;
  505. var cols = dtExcel.Columns;
  506. int colnum = cols.Count;
  507. string dbkeys = _configuration["upload:acotdbkeys"].ToString();
  508. string[] dbcols = dbkeys.Split(",");
  509. string errmsg = string.Empty;
  510. if (num > 0)
  511. {
  512. int index = 1;
  513. foreach (DataRow dr in dtExcel.Rows)
  514. {
  515. var model = new T_Cus_VipInfo();
  516. model.F_State = 1;
  517. model.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  518. model.F_CreateOn = DateTime.Now;
  519. var dbcolslist = dbcols.ToList();
  520. Type t = model.GetType();
  521. PropertyInfo[] PropertyList = t.GetProperties();
  522. foreach (PropertyInfo item in PropertyList)
  523. {
  524. if (dbcolslist.Contains(item.Name))
  525. {
  526. object v = Convert.ChangeType(dr[dbcolslist.IndexOf(item.Name)].ToString(), item.PropertyType);
  527. item.SetValue(model, v, null);
  528. }
  529. }
  530. int b = await _cus_vip_infoRepository.Add(model);
  531. if (b>0)
  532. {
  533. if (!string.IsNullOrEmpty(errmsg))
  534. {
  535. errmsg = errmsg + "\r\n第" + index + "行导入失败!";
  536. }
  537. else
  538. {
  539. errmsg = "第" + index + "行导入失败!";
  540. }
  541. }
  542. index++;
  543. }
  544. }
  545. else
  546. {
  547. return Error("文件中无数据");
  548. }
  549. if (!string.IsNullOrEmpty(errmsg))
  550. {
  551. //删除已导入的部分
  552. return Error(errmsg);
  553. }
  554. return Success("导入成功");
  555. }
  556. #endregion
  557. }
  558. }