颐和api

CusCustomerController.cs 63KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Authorization;
  7. using MadRunFabric.Common;
  8. using WorkOrderApi.IRepositories;
  9. using Microsoft.Extensions.Logging;
  10. using System.Security.Claims;
  11. using MadRunFabric.Model.WorkOrderApi;
  12. using WorkOrderApi.Areas.Hy_Order.Input;
  13. using MongoDB.Bson;
  14. using Microsoft.Extensions.Configuration;
  15. using System.Reflection;
  16. using ConfigurationApi.IRepositories;
  17. using System.Data;
  18. using WorkOrderApi.Model.Dto;
  19. using MongoDB.Driver;
  20. // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  21. namespace WorkOrderApi.Areas.Hy_Order.Controllers
  22. {
  23. /// <summary>
  24. /// 客户档案 - 售后管理系统
  25. /// </summary>
  26. [Area("hy")]
  27. //[Authorize]
  28. [ApiVersion("6.0")]
  29. [Route("api/[controller]")]
  30. [Produces("application/json")]
  31. public class CusCustomerController : BaseController
  32. {
  33. private readonly ILogger<CusCustomerController> _logger;
  34. private readonly ICus_Customer_BaseRepository _cus_customer_baseRepository;
  35. private readonly ICus_Customer_ProjectRepository _cus_customer_projectepository;
  36. private readonly IConfiguration _configuration;
  37. private readonly ISys_DictionaryValueRepository _sys_dictionaryValuerepository;
  38. private readonly ICus_FollowRecordRepository _cus_followrecordrepository;
  39. private readonly ISys_ProvincesRepository _sys_provincesrepository;
  40. private readonly ISys_CityRepository _sys_cityrepository;
  41. public CusCustomerController(
  42. ILogger<CusCustomerController> logger,
  43. ICus_Customer_BaseRepository cus_customer_baseRepository,
  44. IConfiguration configuration,
  45. ICus_Customer_ProjectRepository cus_customer_projectepository,
  46. ISys_DictionaryValueRepository sys_dictionaryValuerepository,
  47. ICus_FollowRecordRepository cus_followrecordrepository,
  48. ISys_ProvincesRepository sys_provincesrepository,
  49. ISys_CityRepository sys_cityrepository
  50. )
  51. {
  52. _logger = logger;
  53. _cus_customer_baseRepository = cus_customer_baseRepository;
  54. _configuration = configuration;
  55. _cus_customer_projectepository = cus_customer_projectepository;
  56. _sys_dictionaryValuerepository = sys_dictionaryValuerepository;
  57. _cus_followrecordrepository = cus_followrecordrepository;
  58. _sys_provincesrepository = sys_provincesrepository;
  59. _sys_cityrepository = sys_cityrepository;
  60. }
  61. #region 客户档案
  62. /// <summary>
  63. /// 获取客户分页
  64. /// </summary>
  65. [HttpGet("getlistbypage")]
  66. public IActionResult GetListsByPageAsync(string keyword, string province, string city, string stime, string etime, int pageindex = 1, int pagesize = 10)
  67. {
  68. string rolecode = User.Claims.FirstOrDefault(c => c.Type == "RoleCode").Value;
  69. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  70. int recordCount = 0;
  71. var result = _cus_customer_baseRepository.GetListsByPage(keyword, province, city, stime, etime, rolecode, usercode, pageindex, pagesize, out recordCount);
  72. var obj = new
  73. {
  74. rows = result.ToList(),
  75. total = recordCount
  76. };
  77. return Success("获取成功", obj);
  78. }
  79. /// <summary>
  80. /// 获取客户详情 by 手机,单位名称
  81. /// </summary>
  82. /// <param name="tel"></param>
  83. /// <param name="companyame"></param>
  84. /// <returns></returns>
  85. [HttpGet("getsinglebytelcompanyame")]
  86. public async Task<IActionResult> GetSingleByTelCompanyameAsync(string tel, string companyame)
  87. {
  88. if (string.IsNullOrEmpty(tel))
  89. return Success("参数错误", null);
  90. var model = (await _cus_customer_baseRepository.GetSingle(x => x.mobilephone == tel && x.companyame == companyame && x.isdelete == 0));
  91. return Success("获取成功", model);
  92. }
  93. /// <summary>
  94. /// 获取客户详情
  95. /// </summary>
  96. [HttpGet("getsingle")]
  97. public async Task<IActionResult> GetSingleAsync(string id)
  98. {
  99. var model = await _cus_customer_baseRepository.GetSingle(x => x.id == id && x.isdelete == 0);
  100. return Success("获取成功", model);
  101. }
  102. /// <summary>
  103. /// 添加客户
  104. /// </summary>
  105. [HttpPost("add")]
  106. public async Task<IActionResult> AddAsync(CusCustomerInput input)
  107. {
  108. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  109. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  110. try
  111. {
  112. #region 判断验证
  113. if (string.IsNullOrWhiteSpace(input.name))
  114. return Error("请输入联系人");
  115. if (string.IsNullOrEmpty(input.mobilephone))
  116. return Error("请输入手机号码");
  117. var listCus = await _cus_customer_baseRepository.Get(x => x.mobilephone == input.mobilephone && x.isdelete == 0);
  118. var model = listCus.FirstOrDefault();
  119. if (model != null)
  120. return Error("手机号码已存在");
  121. #endregion
  122. model = new Cus_Customer_Base();
  123. #region model 添加客户对象
  124. //model.id = cusid;
  125. model.name = input.name;
  126. model.mobilephone = input.mobilephone;
  127. model.telephone = input.telephone;
  128. model.manager = input.manager;
  129. model.customername = input.customername;
  130. model.companyame = input.companyame;
  131. model.province = input.province; //省
  132. model.city = input.city; //市
  133. model.address = input.address;
  134. model.iscustomer = input.iscustomer;
  135. model.customfields = input.customfields;
  136. model.rolecode = input.rolecodes;
  137. model.usercode = input.usercodes;
  138. model.createuser = usercode;
  139. model.createusername = username;
  140. #endregion
  141. string customerid = await _cus_customer_baseRepository.AddRetID(model);
  142. if (!string.IsNullOrEmpty(customerid))
  143. {
  144. return Success("保存成功");
  145. }
  146. else
  147. return Error("保存失败");
  148. }
  149. catch (Exception ex)
  150. {
  151. return Error(ex.Message);
  152. }
  153. }
  154. /// <summary>
  155. /// 修改客户
  156. /// </summary>
  157. [HttpPost("update")]
  158. public async Task<IActionResult> UpdateAsync(CusCustomerInput input)
  159. {
  160. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  161. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  162. var model = (await _cus_customer_baseRepository.Get(p => p.id == input.id)).FirstOrDefault();
  163. if (model != null)
  164. {
  165. #region 判断验证
  166. if (model.isdelete == 1)
  167. return Error("用户已删除");
  168. if (string.IsNullOrWhiteSpace(input.name))
  169. return Error("请输入联系人");
  170. if (string.IsNullOrEmpty(input.mobilephone))
  171. return Error("请输入联系电话");
  172. #endregion
  173. #region model 客户对象
  174. model.name = input.name;
  175. model.mobilephone = input.mobilephone;
  176. model.telephone = input.telephone;
  177. model.manager = input.manager;
  178. model.customername = input.customername;
  179. model.companyame = input.companyame;
  180. model.province = input.province; //省
  181. model.city = input.city; //市
  182. model.address = input.address;
  183. model.iscustomer = input.iscustomer;
  184. model.customfields = input.customfields;
  185. model.rolecode = input.rolecodes;
  186. model.usercode = input.usercodes;
  187. #endregion
  188. bool b = await _cus_customer_baseRepository.UpdateOne(model);
  189. if (b)
  190. {
  191. return Success("保存成功");
  192. }
  193. else
  194. return Error("保存失败");
  195. }
  196. return Error("参数错误");
  197. }
  198. /// <summary>
  199. /// 删除客户
  200. /// </summary>
  201. /// <param name="ids"></param>
  202. /// <returns></returns>
  203. [HttpPost("delete")]
  204. public async Task<IActionResult> DeleteAsync(string[] ids)
  205. {
  206. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  207. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  208. //使用逻辑删除
  209. var res = 0;
  210. if (ids != null && ids.Length > 0)
  211. {
  212. foreach (var id in ids)
  213. {
  214. var model = await _cus_customer_baseRepository.GetSingle(id);
  215. model.isdelete = 1;
  216. model.deleteuser = usercode;
  217. model.deletetime = DateTime.Now;
  218. if (await _cus_customer_baseRepository.UpdateOne(model))
  219. {
  220. #region 删除相关表数据
  221. var listCus_Pro = await _cus_customer_projectepository.Get(p => p.customerid == model.id && p.isdelete == 0);
  222. foreach (var modelPro in listCus_Pro)
  223. {
  224. modelPro.isdelete = 1;
  225. modelPro.deleteuser = usercode;
  226. modelPro.deletetime = DateTime.Now;
  227. await _cus_customer_projectepository.UpdateOne(modelPro);
  228. }
  229. #endregion
  230. res += 1;
  231. }
  232. }
  233. if (res == ids.Length)
  234. return Success("删除成功");
  235. else if (res > 0 && res < ids.Length)
  236. return Error("部分删除失败");
  237. else
  238. return Error("删除失败");
  239. }
  240. else
  241. return Error("请选择要删除的记录");
  242. }
  243. /// <summary>
  244. /// 上传文件并导入数据库 - 客户档案
  245. /// </summary>
  246. /// <param name="headrow">默认为-1:1、为-1时,获取excel标题;2、为0时,不获取excel标题</param>
  247. /// <returns></returns>
  248. [HttpPost("importexcelcus")]
  249. public async Task<IActionResult> ImportExcelCus(int headrow = -1)
  250. {
  251. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  252. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  253. Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
  254. 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"))
  255. return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
  256. NPOIHelper npoi = new NPOIHelper();
  257. var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
  258. int num = dtExcel.Rows.Count;
  259. var cols = dtExcel.Columns;
  260. int colnum = cols.Count;
  261. string dbkeys = _configuration["upload:acotdbkeys"].ToString();
  262. string[] dbcols = dbkeys.Split(",");
  263. string errmsg = string.Empty;
  264. if (num > 0)
  265. {
  266. var list_pro = (await _sys_provincesrepository.Get()).ToList();
  267. var list_city = (await _sys_cityrepository.GetAll()).ToList();
  268. int index = 1;
  269. // 初始化新增列(字段名称)
  270. string fields_names = string.Empty;
  271. foreach (System.Data.DataRow dr in dtExcel.Rows)
  272. {
  273. var dbcolslist = dbcols.ToList();
  274. if (index == 1)
  275. {
  276. // 获取第一行标题 dbcolslist.Count
  277. for (int i = 0; i < colnum; i++)
  278. {
  279. fields_names = dr[i].ToString() + ",";
  280. }
  281. fields_names = fields_names.TrimEnd(',');
  282. }
  283. else
  284. {
  285. // 获取数据
  286. var model = new Cus_Customer_Base();
  287. model.createuser = usercode;
  288. model.createusername = username;
  289. //model.cusname = dr.cusname;
  290. //model.phone = dr.phone;
  291. //model.cusaddr = dr.cusaddr;
  292. model.createtime = DateTime.Now.ToLocalTime();
  293. Type t = model.GetType();
  294. PropertyInfo[] PropertyList = t.GetProperties();
  295. var province_code = "";
  296. foreach (PropertyInfo item in PropertyList)
  297. {
  298. if (dbcolslist.Contains(item.Name))
  299. {
  300. object v = Convert.ChangeType(dr[dbcolslist.IndexOf(item.Name)].ToString(), item.PropertyType);
  301. #region 省市获取code by name
  302. if (item.Name == "province")
  303. {
  304. var model_pro = list_pro.FirstOrDefault(x => x.provincename.Equals(v.ToString()));
  305. province_code = model_pro != null ? model_pro.provincecode : "";
  306. item.SetValue(model, province_code, null);
  307. }
  308. if (item.Name == "city")
  309. {
  310. var model_city = list_city.FirstOrDefault(x => x.provincecode.Equals(province_code) && x.cityname.Equals(v.ToString()));
  311. var city_code = model_city != null ? model_city.citycode : "";
  312. item.SetValue(model, city_code, null);
  313. }
  314. #endregion
  315. item.SetValue(model, v, null);
  316. }
  317. }
  318. #region 自定义新增列 - 处理
  319. var fields_names_list = fields_names.Split(",").ToList();
  320. var customfields = new List<CustomfieldsList>();
  321. for (int i = dbcolslist.Count; i < colnum; i++)
  322. {
  323. var model_fields = new CustomfieldsList();
  324. string name = fields_names_list[i];
  325. string fields = ChineseToPinYin.Convert(name);
  326. string value = dr[i].ToString();
  327. model_fields.name = name;
  328. model_fields.fields = fields;
  329. model_fields.value = value;
  330. customfields.Add(model_fields);
  331. }
  332. #endregion
  333. model.customfields = customfields;
  334. bool b = await _cus_customer_baseRepository.Add(model);
  335. if (!b)
  336. {
  337. if (!string.IsNullOrEmpty(errmsg))
  338. {
  339. errmsg = errmsg + "\r\n第" + index + "行导入失败!";
  340. }
  341. else
  342. {
  343. errmsg = "第" + index + "行导入失败!";
  344. }
  345. }
  346. }
  347. index++;
  348. }
  349. }
  350. else
  351. {
  352. return Error("文件中无数据");
  353. }
  354. if (!string.IsNullOrEmpty(errmsg))
  355. {
  356. //删除已导入的部分
  357. return Error(errmsg);
  358. }
  359. return Success("导入成功");
  360. }
  361. /// <summary>
  362. /// 导出excel - 客户档案
  363. /// </summary>
  364. /// <param name="taskid"></param>
  365. /// <param name="calloutok"></param>
  366. /// <returns></returns>
  367. [Authorize]
  368. [HttpGet("exportexcelcus")]
  369. [AllowAnonymous]
  370. public IActionResult ExportExcelCusAsync(string keyword, string province, string city, string stime, string etime)
  371. {
  372. string rolecode = User.Claims.FirstOrDefault(c => c.Type == "RoleCode").Value;
  373. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  374. try
  375. {
  376. var result = _cus_customer_baseRepository.GetListsExport(keyword, province, city, stime, etime, rolecode, usercode);
  377. //导出
  378. NPOIHelper npoi = new NPOIHelper();
  379. var tlist = result.ToList();
  380. var tb = ModelConvertHelper<Cus_Customer_BaseDto>.ConvertListToDataTable(tlist);
  381. string[] cols = { "姓名", "手机号码", "固定号码", "所在省", "所在市", "具体地址" };
  382. string[] colname = { "name", "mobilephone", "telephone", "provincename", "cityname", "address" };
  383. byte[] sm = npoi.ExportToExcel1(tb, cols, colname);
  384. if (sm != null)
  385. {
  386. return File(sm, "application/vnd.ms-excel", "客户档案信息.xlsx");
  387. }
  388. else
  389. {
  390. return Error("导出失败");
  391. }
  392. }
  393. catch (Exception ex)
  394. {
  395. _logger.LogError(ex, "导出异常");
  396. return Error("导出失败");
  397. }
  398. }
  399. /// <summary>
  400. /// 转移客户
  401. /// </summary>
  402. [HttpPost("move")]
  403. public async Task<IActionResult> MoveAsync(string id,string rolecode, string usercode)
  404. {
  405. string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  406. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  407. var model = (await _cus_customer_baseRepository.Get(p => p.id == id)).FirstOrDefault();
  408. if (model != null)
  409. {
  410. #region 判断验证
  411. if (model.isdelete == 1)
  412. return Error("用户已删除");
  413. //if (string.IsNullOrWhiteSpace(model.rolecode))
  414. // return Error("无需转移客户信息");
  415. if (string.IsNullOrEmpty(model.usercode))
  416. return Error("此用户不是个人用户,无转移权限");
  417. #endregion
  418. #region model 客户对象
  419. model.rolecode = rolecode;
  420. model.usercode = usercode;
  421. #endregion
  422. bool b = await _cus_customer_baseRepository.UpdateOne(model);
  423. if (b)
  424. {
  425. return Success("保存成功");
  426. }
  427. else
  428. return Error("保存失败");
  429. }
  430. return Error("参数错误");
  431. }
  432. #endregion
  433. #region 来电弹屏
  434. /// <summary>
  435. /// 获取客户详情 by tel - 来电弹屏
  436. /// </summary>
  437. [HttpGet("getsinglebytelold")]
  438. public async Task<IActionResult> GetSingleByTelAsyncOLD(string tel)
  439. {
  440. if (string.IsNullOrEmpty(tel))
  441. return Success("参数错误", null);
  442. var list = (await _cus_customer_baseRepository.Get(x => x.mobilephone == tel && x.isdelete == 0)).ToList();
  443. var model = list.FirstOrDefault();
  444. return Success("获取成功", model);
  445. }
  446. //2019-2-20去掉客户信息部分做修改
  447. /// <summary>
  448. /// 获取客户项目详情 by tel - 来电弹屏
  449. /// </summary>
  450. [HttpGet("getsinglebytel")]
  451. public async Task<IActionResult> GetSingleByTelAsync(string tel)
  452. {
  453. if (string.IsNullOrEmpty(tel))
  454. return Success("参数错误", null);
  455. var list = (await _cus_customer_projectepository.Get(x => (x.project_mobilephone == tel || x.mobilephone == tel) && x.isdelete == 0)).ToList();
  456. var model = list.FirstOrDefault();
  457. return Success("获取成功", model);
  458. }
  459. /// <summary>
  460. /// 获取客户项目列表 by tel - 来电弹屏
  461. /// </summary>
  462. [HttpGet("getinfobytel")]
  463. public async Task<IActionResult> GetInfoByTelAsync(string tel)
  464. {
  465. if (string.IsNullOrEmpty(tel))
  466. return Success("参数错误", null);
  467. var list = (await _cus_customer_projectepository.Get(x => (x.project_mobilephone == tel || x.mobilephone == tel) && x.isdelete == 0)).ToList();
  468. //var model = list.FirstOrDefault();
  469. return Success("获取成功", list);
  470. }
  471. /// <summary>
  472. /// 添加修改客户档案 - 来电弹屏
  473. /// </summary>
  474. [HttpPost("save")]
  475. public async Task<IActionResult> SaveAsync(CusCustomerInput input)
  476. {
  477. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  478. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  479. #region 判断验证
  480. if (string.IsNullOrWhiteSpace(input.name))
  481. return Error("请输入联系人 asdfasdf");
  482. if (string.IsNullOrEmpty(input.mobilephone))
  483. return Error("请输入联系电话");
  484. #endregion
  485. var list = await _cus_customer_baseRepository.Get(x => x.mobilephone == input.mobilephone && x.isdelete == 0);
  486. var model = list.FirstOrDefault();
  487. if (model == null)
  488. {
  489. model = new Cus_Customer_Base();
  490. //添加客户信息
  491. #region model 添加客户对象
  492. model.name = input.name;
  493. model.mobilephone = input.mobilephone;
  494. model.telephone = input.telephone;
  495. model.manager = input.manager;
  496. model.customername = input.customername;
  497. model.companyame = input.companyame;
  498. model.province = input.province; //省
  499. model.city = input.city; //市
  500. model.address = input.address;
  501. model.createuser = usercode;
  502. model.createusername = username;
  503. #endregion
  504. string customerid = await _cus_customer_baseRepository.AddRetID(model);
  505. if (!string.IsNullOrEmpty(customerid))
  506. {
  507. return Success("保存成功");
  508. }
  509. else
  510. return Error("保存失败");
  511. }
  512. else
  513. {
  514. //model = (await _cus_customer_baseRepository.Get(p => p.id == input.id)).FirstOrDefault();
  515. #region model 客户对象
  516. model.name = input.name;
  517. model.mobilephone = input.mobilephone;
  518. model.telephone = input.telephone;
  519. model.manager = input.manager;
  520. model.customername = input.customername;
  521. model.companyame = input.companyame;
  522. model.province = input.province; //省
  523. model.city = input.city; //市
  524. model.address = input.address;
  525. #endregion
  526. bool b = await _cus_customer_baseRepository.UpdateOne(model);
  527. if (b)
  528. {
  529. return Success("保存成功");
  530. }
  531. else
  532. return Error("保存失败");
  533. }
  534. }
  535. /// <summary>
  536. /// 添加修改客户项目 - 来电弹屏
  537. /// </summary>
  538. [HttpPost("savepro")]
  539. public async Task<IActionResult> SaveProAsync(CusCustomerProInput input)
  540. {
  541. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  542. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  543. #region 判断验证
  544. //if (string.IsNullOrWhiteSpace(input.pro_customerid))
  545. // return Error("用户id不能为空");
  546. if (string.IsNullOrEmpty(input.pro_mobilephone))
  547. return Error("请输入联系电话");
  548. //if (string.IsNullOrEmpty(input.pro_project_name))
  549. // return Error("请输入项目名称");
  550. #endregion
  551. //var list = (await _cus_customer_projectepository.Get(x => x.id == input.pro_id && x.customerid == input.pro_customerid && x.isdelete == 0)).ToList();
  552. var list = (await _cus_customer_projectepository.Get(x => x.name == input.pro_name && x.project_name == input.pro_project_name && x.isdelete == 0)).ToList();
  553. var model = list.FirstOrDefault();
  554. if (model == null)
  555. {
  556. model = new Cus_Customer_Project();
  557. //添加客户信息
  558. #region model 添加客户对象
  559. model.customerid = input.pro_customerid;
  560. model.name = input.pro_name;
  561. model.mobilephone = input.pro_mobilephone;
  562. model.telephone = input.pro_telephone;
  563. model.customername = input.pro_customername;
  564. model.companyame = input.pro_companyame;
  565. model.address = input.pro_address;
  566. model.project_name = input.pro_project_name;
  567. model.ordertime = input.pro_ordertime;
  568. model.acceptancetime = input.pro_acceptancetime;
  569. model.orderendtime = input.pro_orderendtime;
  570. model.guarantee_period = input.pro_guarantee_period;
  571. model.isend = input.pro_isend;
  572. model.project_amount = input.pro_project_amount;
  573. model.project_typeid = input.pro_project_typeid;
  574. model.oem_name = input.pro_oem_name;
  575. model.content = input.pro_content;
  576. model.servicecontent = input.pro_servicecontent;
  577. model.createuser = usercode;
  578. model.createusername = username;
  579. //2019-2-20 添加项目负责人信息
  580. model.project_cusname = input.project_cusname;
  581. model.project_mobilephone = input.project_mobilephone;
  582. model.project_telephone = input.project_telephone;
  583. model.project_address = input.project_address;
  584. //2019-4-12添加客户类型
  585. model.customerdept = input.customerdept;
  586. model.customeremail = input.customeremail;
  587. model.customertype = input.customertype;
  588. #endregion
  589. bool bl = await _cus_customer_projectepository.Add(model);
  590. if (bl)
  591. {
  592. return Success("保存成功");
  593. }
  594. else
  595. return Error("保存失败");
  596. }
  597. else
  598. {
  599. #region model 客户对象
  600. model.name = input.pro_name;
  601. model.mobilephone = input.pro_mobilephone;
  602. model.telephone = input.pro_telephone;
  603. model.customername = input.pro_customername;
  604. model.companyame = input.pro_companyame;
  605. model.address = input.pro_address;
  606. model.project_name = input.pro_project_name;
  607. model.ordertime = input.pro_ordertime;
  608. model.acceptancetime = input.pro_acceptancetime;
  609. model.orderendtime = input.pro_orderendtime;
  610. model.guarantee_period = input.pro_guarantee_period;
  611. model.isend = input.pro_isend;
  612. model.project_amount = input.pro_project_amount;
  613. model.project_typeid = input.pro_project_typeid;
  614. model.oem_name = input.pro_oem_name;
  615. model.content = input.pro_content;
  616. model.servicecontent = input.pro_servicecontent;
  617. //2019-2-20 添加项目负责人信息
  618. model.project_cusname = input.project_cusname;
  619. model.project_mobilephone = input.project_mobilephone;
  620. model.project_telephone = input.project_telephone;
  621. model.project_address = input.project_address;
  622. //2019-4-12添加客户类型
  623. model.customerdept = input.customerdept;
  624. model.customeremail = input.customeremail;
  625. model.customertype = input.customertype;
  626. #endregion
  627. bool b = await _cus_customer_projectepository.UpdateOne(model);
  628. if (b)
  629. {
  630. return Success("保存成功");
  631. }
  632. else
  633. return Error("保存失败");
  634. }
  635. }
  636. #endregion
  637. #region 客户项目
  638. /// <summary>
  639. /// 获取客户分页
  640. /// </summary>
  641. [HttpGet("getprolistbypage")]
  642. public IActionResult GetProListsByPageAsync(string keyword, string customerid, string stime, string etime, string projecttypeid, string qdstime, string qdetime, string dqstime, string dqetime, string customertype, string gjstime, string gjetime, int? isend, int pageindex = 1, int pagesize = 10)
  643. {
  644. int recordCount = 0;
  645. var result = _cus_customer_projectepository.GetProListsByPage(keyword, customerid, stime, etime, projecttypeid, qdstime, qdetime, dqstime, dqetime, customertype, gjstime, gjetime, isend, pageindex, pagesize, out recordCount);
  646. var obj = new
  647. {
  648. rows = result.ToList(),
  649. total = recordCount
  650. };
  651. return Success("获取成功", obj);
  652. }
  653. /// <summary>
  654. /// 获取客户项目下拉
  655. /// </summary>
  656. [HttpGet("getprolist")]
  657. public IActionResult GetProListsAsync()
  658. {
  659. int recordCount = 0;
  660. var result = _cus_customer_projectepository.GetProLists();
  661. var obj = new
  662. {
  663. rows = result.ToList(),
  664. total = recordCount
  665. };
  666. return Success("获取成功", obj);
  667. }
  668. /// <summary>
  669. /// 获取客户项目下拉
  670. /// </summary>
  671. [HttpGet("getpro")]
  672. public IActionResult GetProAsync()
  673. {
  674. int recordCount = 0;
  675. var result = _cus_customer_projectepository.GetPro();
  676. var resu = result.Distinct().ToList();//去重
  677. var obj = new
  678. {
  679. rows = resu,
  680. total = resu.Count
  681. };
  682. return Success("获取成功", obj);
  683. }
  684. /// <summary>
  685. /// 获取客户详情
  686. /// </summary>
  687. [HttpGet("getprosingle")]
  688. public IActionResult GetProSingleAsync(string id)
  689. {
  690. var model = _cus_customer_projectepository.GetProDetails(id);
  691. return Success("获取成功", model);
  692. }
  693. /// <summary>
  694. /// 添加客户
  695. /// </summary>
  696. [HttpPost("addpro")]
  697. public async Task<IActionResult> AddProAsync(CusCustomerProInput input)
  698. {
  699. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  700. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  701. #region 判断验证
  702. //if (string.IsNullOrWhiteSpace(input.pro_customerid))
  703. // return Error("用户id不能为空");
  704. if (string.IsNullOrWhiteSpace(input.pro_name))
  705. return Error("请输入联系人");
  706. if (string.IsNullOrEmpty(input.pro_mobilephone))
  707. return Error("请输入联系电话");
  708. //if (string.IsNullOrEmpty(input.pro_project_name))
  709. // return Error("请输入项目名称");
  710. if (!string.IsNullOrEmpty(input.pro_companyame))
  711. {
  712. var listCus = (await _cus_customer_projectepository.Get(x => x.companyame == input.pro_companyame && x.isdelete == 0)).ToList();
  713. var models = listCus.FirstOrDefault();
  714. if (models != null)
  715. return Error("单位名称已存在");
  716. }
  717. #endregion
  718. var model = new Cus_Customer_Project();
  719. #region model 添加客户对象
  720. model.customerid = input.pro_customerid;
  721. model.name = input.pro_name;
  722. model.mobilephone = input.pro_mobilephone;
  723. model.telephone = input.pro_telephone;
  724. model.customername = input.pro_customername;
  725. model.companyame = input.pro_companyame;
  726. //model.province = input.province; //省
  727. //model.city = input.city; //市
  728. model.address = input.pro_address;
  729. model.project_name = input.pro_project_name;
  730. model.ordertime = input.pro_ordertime;
  731. model.acceptancetime = input.pro_acceptancetime;
  732. model.orderendtime = input.pro_orderendtime;
  733. model.guarantee_period = input.pro_guarantee_period;
  734. model.isend = input.pro_isend;
  735. model.project_amount = input.pro_project_amount;
  736. model.project_typeid = input.pro_project_typeid;
  737. model.oem_name = input.pro_oem_name;
  738. model.content = input.pro_content;
  739. model.servicecontent = input.pro_servicecontent;
  740. model.createuser = usercode;
  741. model.createusername = username;
  742. //2019-2-20 添加项目负责人信息
  743. model.project_cusname = input.project_cusname;
  744. model.project_mobilephone = input.project_mobilephone;
  745. model.project_telephone = input.project_telephone;
  746. model.project_address = input.project_address;
  747. //2019-4-12添加客户类型
  748. model.customerdept = input.customerdept;
  749. model.customeremail = input.customeremail;
  750. model.customertype = input.customertype;
  751. //2019-5-6添加附件
  752. if (input.attachment != null)
  753. {
  754. model.attachment = input.attachment;
  755. }
  756. else
  757. {
  758. model.attachment = new List<FileBaseModel>();
  759. }
  760. #endregion
  761. bool bl = await _cus_customer_projectepository.Add(model);
  762. if (bl)
  763. {
  764. return Success("保存成功");
  765. }
  766. else
  767. return Error("保存失败");
  768. }
  769. /// <summary>
  770. /// 上传文件并导入数据库
  771. /// </summary>
  772. /// <returns></returns>
  773. [HttpPost("importexcelpro")]
  774. public async Task<IActionResult> ImportExcelPro(string customerid, int headrow = 0)//string customerid,
  775. {
  776. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  777. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  778. if (Request.Form.Files.Count <= 0)
  779. {
  780. return Error("获取不到要导入的文件");
  781. }
  782. Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
  783. 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"))
  784. return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
  785. NPOIHelper npoi = new NPOIHelper();
  786. var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
  787. int num = dtExcel.Rows.Count;
  788. var cols = dtExcel.Columns;
  789. int colnum = cols.Count;
  790. string dbkeys = _configuration["upload:prodbkeys"].ToString();
  791. string[] dbcols = dbkeys.Split(",");
  792. string errmsg = string.Empty;
  793. if (num > 0)
  794. {
  795. int index = 1;
  796. foreach (System.Data.DataRow dr in dtExcel.Rows)
  797. {
  798. var model = new Cus_Customer_Project();
  799. model.createuser = usercode;
  800. model.createusername = username;
  801. //model.cusname = dr.cusname;
  802. //model.phone = dr.phone;
  803. //model.cusaddr = dr.cusaddr;
  804. model.createtime = DateTime.Now.ToLocalTime();
  805. //model.customerid = customerid;
  806. model.customertype = "签约客户";
  807. var dbcolslist = dbcols.ToList();
  808. Type t = model.GetType();
  809. PropertyInfo[] PropertyList = t.GetProperties();
  810. foreach (PropertyInfo item in PropertyList)
  811. {
  812. if (dbcolslist.Contains(item.Name))
  813. {
  814. if (item.Name == "project_typeid")
  815. {
  816. var models = await _sys_dictionaryValuerepository.GetSingle(x => x.name == dr[dbcolslist.IndexOf(item.Name)].ToString());
  817. if (models != null)
  818. {
  819. model.project_typeid = models.id;
  820. }
  821. }
  822. else
  823. {
  824. if (dr[dbcolslist.IndexOf(item.Name)].ToString() != null)
  825. {
  826. if (item.PropertyType == typeof(DateTime))
  827. {
  828. if (dr[dbcolslist.IndexOf(item.Name)].ToString() != "")
  829. {
  830. object v = Convert.ChangeType(dr[dbcolslist.IndexOf(item.Name)].ToString(), item.PropertyType);
  831. item.SetValue(model, v, null);
  832. }
  833. }
  834. else
  835. {
  836. if (dr[dbcolslist.IndexOf(item.Name)].ToString() != "")
  837. {
  838. object v = Convert.ChangeType(dr[dbcolslist.IndexOf(item.Name)].ToString(), item.PropertyType);
  839. item.SetValue(model, v, null);
  840. }
  841. }
  842. }
  843. }
  844. }
  845. }
  846. bool b = await _cus_customer_projectepository.Add(model);
  847. if (!b)
  848. {
  849. if (!string.IsNullOrEmpty(errmsg))
  850. {
  851. errmsg = errmsg + "\r\n第" + index + "行导入失败!";
  852. }
  853. else
  854. {
  855. errmsg = "第" + index + "行导入失败!";
  856. }
  857. }
  858. index++;
  859. }
  860. }
  861. else
  862. {
  863. return Error("文件中无数据");
  864. }
  865. if (!string.IsNullOrEmpty(errmsg))
  866. {
  867. //删除已导入的部分
  868. return Error(errmsg);
  869. }
  870. return Success("导入成功");
  871. }
  872. /// <summary>
  873. /// 修改客户
  874. /// </summary>
  875. [HttpPost("updatepro")]
  876. public async Task<IActionResult> UpdateProAsync(CusCustomerProInput input)
  877. {
  878. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  879. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  880. var model = (await _cus_customer_projectepository.Get(p => p.id == input.pro_id)).FirstOrDefault();
  881. if (model != null)
  882. {
  883. #region 判断验证
  884. if (model.isdelete == 1)
  885. return Error("用户已删除");
  886. //if (string.IsNullOrWhiteSpace(input.pro_customerid))
  887. // return Error("用户id不能为空");
  888. if (string.IsNullOrWhiteSpace(input.pro_name))
  889. return Error("请输入联系人");
  890. if (string.IsNullOrEmpty(input.pro_mobilephone))
  891. return Error("请输入联系电话");
  892. //if (string.IsNullOrEmpty(input.pro_project_name))
  893. // return Error("请输入项目名称");
  894. #endregion
  895. #region model 客户对象
  896. model.name = input.pro_name;
  897. model.mobilephone = input.pro_mobilephone;
  898. model.telephone = input.pro_telephone;
  899. model.customername = input.pro_customername;
  900. model.companyame = input.pro_companyame;
  901. //model.province = input.province; //省
  902. //model.city = input.city; //市
  903. model.address = input.pro_address;
  904. model.project_name = input.pro_project_name;
  905. model.ordertime = input.pro_ordertime;
  906. model.acceptancetime = input.pro_acceptancetime;
  907. model.orderendtime = input.pro_orderendtime;
  908. model.guarantee_period = input.pro_guarantee_period;
  909. model.isend = input.pro_isend;
  910. model.project_amount = input.pro_project_amount;
  911. model.project_typeid = input.pro_project_typeid;
  912. model.oem_name = input.pro_oem_name;
  913. model.content = input.pro_content;
  914. model.servicecontent = input.pro_servicecontent;
  915. //2019-2-20 添加项目负责人信息
  916. model.project_cusname = input.project_cusname;
  917. model.project_mobilephone = input.project_mobilephone;
  918. model.project_telephone = input.project_telephone;
  919. model.project_address = input.project_address;
  920. //2019-4-12添加客户类型
  921. model.customerdept = input.customerdept;
  922. model.customeremail = input.customeremail;
  923. model.customertype = input.customertype;
  924. model.followcontent = input.followcontent;
  925. model.followtime = input.followtime;
  926. //2019-5-6添加附件
  927. if (input.attachment != null)
  928. {
  929. model.attachment = input.attachment;
  930. }
  931. else
  932. {
  933. model.attachment = new List<FileBaseModel>();
  934. }
  935. #endregion
  936. bool b = await _cus_customer_projectepository.UpdateOne(model);
  937. if (b)
  938. {
  939. #region 修改最新一条跟进记录
  940. if (string.IsNullOrEmpty(input.followcontent))
  941. {
  942. var sort = Builders<Cus_FollowRecord>.Sort.Descending("createTime");
  943. var dModel = await _cus_followrecordrepository.GetSingle(s => s.proid.Equals(input.pro_id), sort);
  944. if (dModel != null)
  945. {
  946. dModel.content = input.followcontent;
  947. dModel.createTime = (DateTime)input.followtime;
  948. await _cus_followrecordrepository.UpdateOne(dModel);
  949. }
  950. }
  951. #endregion
  952. return Success("保存成功");
  953. }
  954. else
  955. return Error("保存失败");
  956. }
  957. return Error("参数错误");
  958. }
  959. /// <summary>
  960. /// 删除客户
  961. /// </summary>
  962. /// <param name="ids"></param>
  963. /// <returns></returns>
  964. [HttpPost("deletepro")]
  965. public async Task<IActionResult> DeleteProAsync(string[] ids)
  966. {
  967. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  968. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  969. //使用逻辑删除
  970. var res = 0;
  971. if (ids != null && ids.Length > 0)
  972. {
  973. foreach (var id in ids)
  974. {
  975. var model = await _cus_customer_projectepository.GetSingle(id);
  976. model.isdelete = 1;
  977. model.deleteuser = usercode;
  978. model.deletetime = DateTime.Now;
  979. if (await _cus_customer_projectepository.UpdateOne(model))
  980. {
  981. res += 1;
  982. }
  983. }
  984. if (res == ids.Length)
  985. return Success("删除成功");
  986. else if (res > 0 && res < ids.Length)
  987. return Error("部分删除失败");
  988. else
  989. return Error("删除失败");
  990. }
  991. else
  992. return Error("请选择要删除的记录");
  993. }
  994. #endregion
  995. #region 潜在客户信息
  996. /// <summary>
  997. /// 添加跟进信息
  998. /// </summary>
  999. [HttpPost("addfollowinfo")]
  1000. public async Task<IActionResult> AddFollowAsync(string id, string content)
  1001. {
  1002. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  1003. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  1004. var model = new Cus_FollowRecord();
  1005. #region model 添加跟进信息
  1006. model.content = content;
  1007. model.createName = usercode;
  1008. model.createTime = DateTime.Now.ToLocalTime();
  1009. model.proid = id;
  1010. #endregion
  1011. bool bl = await _cus_followrecordrepository.Add(model);
  1012. if (bl)
  1013. {
  1014. //更新客户项目表跟进时间
  1015. var modelc = await _cus_customer_projectepository.GetSingle(id);
  1016. modelc.followtime = DateTime.Now;
  1017. modelc.followcontent = content;
  1018. if (await _cus_customer_projectepository.UpdateOne(modelc))
  1019. {
  1020. return Success("跟进信息保存成功");
  1021. }
  1022. else
  1023. return Error("信息更新失败");
  1024. }
  1025. else
  1026. return Error("信息保存失败");
  1027. }
  1028. /// <summary>
  1029. /// 获取跟进信息
  1030. /// </summary>
  1031. [HttpGet("getfollowlist")]
  1032. public async Task<IActionResult> GetFollowListsAsync(string id)
  1033. {
  1034. var lists = (await _cus_followrecordrepository.Get(p => p.proid == id)).ToList();
  1035. return Success("获取成功", lists);
  1036. }
  1037. /// <summary>
  1038. /// 用于项目负责人、客户类型相互转移
  1039. /// </summary>
  1040. /// <param name="ids"></param>
  1041. /// <returns></returns>
  1042. [HttpPost("changeinfo")]
  1043. public async Task<IActionResult> ChangeInfoAsync(string id, string cusname, string customertype, string cusphone)
  1044. {
  1045. var model = await _cus_customer_projectepository.GetSingle(id);
  1046. if (!string.IsNullOrEmpty(cusname))
  1047. {
  1048. model.project_cusname = cusname;
  1049. }
  1050. if (!string.IsNullOrEmpty(customertype))
  1051. {
  1052. model.customertype = customertype;
  1053. }
  1054. if (!string.IsNullOrEmpty(cusphone))
  1055. {
  1056. model.project_mobilephone = cusphone;
  1057. }
  1058. if (await _cus_customer_projectepository.UpdateOne(model))
  1059. {
  1060. return Success("转移成功"); ;
  1061. }
  1062. else
  1063. return Error("转移失败");
  1064. }
  1065. /// <summary>
  1066. /// 导出excel
  1067. /// </summary>
  1068. /// <param name="taskid"></param>
  1069. /// <param name="calloutok"></param>
  1070. /// <returns></returns>
  1071. [Authorize]
  1072. [HttpGet("exportexcel")]
  1073. [AllowAnonymous]
  1074. public async Task<IActionResult> ExportExcelAsync(string keyword, string customerid, string stime, string etime, string projecttypeid, string qdstime, string qdetime, string dqstime, string dqetime, string customertype, string gjstime, string gjetime, int? isend)
  1075. {
  1076. try
  1077. {
  1078. int recordCount = 0;
  1079. var result = _cus_customer_projectepository.GetProLists(keyword, customerid, stime, etime, projecttypeid, qdstime, qdetime, dqstime, dqetime, customertype, gjstime, gjetime, isend, out recordCount);
  1080. //导出
  1081. NPOIHelper npoi = new NPOIHelper();
  1082. var tlist = result.ToList();
  1083. var tb = ModelConvertHelper<Cus_Customer_ProjectDto>.ConvertListToDataTable(tlist);
  1084. if (customertype == "签约客户")
  1085. {
  1086. string[] cols = { "姓名", "手机号码", "固定号码", "客户名称", "单位名称", "地址", "项目名称", "项目负责人", "负责人手机号", "负责人固话", "负责人地址", "项目类型", "项目金额", "合同签订时间", "验收日期", "到期日期", "质保期", "是否到期", "设备厂家 / 中间件", "授权内容", "售后服务内容" };
  1087. string[] colname = { "name", "mobilephone", "telephone", "customername", "companyame", "address", "project_name", "project_cusname", "project_mobilephone", "project_telephone", "project_address", "project_typeid", "project_amount", "ordertime", "acceptancetime", "orderendtime", "guarantee_period", "isend", "oem_name", "content", "servicecontent" };
  1088. byte[] sm = npoi.ExportToExcel1(tb, cols, colname);
  1089. if (sm != null)
  1090. {
  1091. return File(sm, "application/vnd.ms-excel", "客户信息.xlsx");
  1092. }
  1093. else
  1094. {
  1095. return Error("导出失败");
  1096. }
  1097. }
  1098. else if (customertype == "潜在客户")
  1099. {
  1100. //获取历史跟进信息
  1101. DataTable dtnew = tb;
  1102. dtnew.Columns.Add("followRecord");
  1103. for (int i = 0; i < tb.Rows.Count; i++)
  1104. {
  1105. string id = tb.Rows[i]["id"].ToString();
  1106. var lists = (await _cus_followrecordrepository.Get(p => p.proid == id)).ToList();
  1107. string fr = "";
  1108. int n = 1;
  1109. foreach (var l in lists)
  1110. {
  1111. fr += n.ToString() + "、" + l.createTime.ToString("yyyy-MM-dd") + " " + l.content + "\n";
  1112. n++;
  1113. }
  1114. dtnew.Rows[i]["followRecord"] = fr;
  1115. }
  1116. string[] cols = { "单位名称", "客户部门", "手机号码", "联系人", "邮箱", "项目负责人", "跟进日期", "跟进内容", "跟进记录" };
  1117. string[] colname = { "companyame", "customerdept", "mobilephone", "name", "customeremail", "project_cusname", "followtime", "followcontent", "followRecord" };
  1118. byte[] sm = npoi.ExportToExcel1(dtnew, cols, colname);
  1119. if (sm != null)
  1120. {
  1121. return File(sm, "application/vnd.ms-excel", "潜在客户信息.xlsx");
  1122. }
  1123. else
  1124. {
  1125. return Error("导出失败");
  1126. }
  1127. }
  1128. return Error("导出失败");
  1129. }
  1130. catch (Exception ex)
  1131. {
  1132. _logger.LogError(ex, "导出异常");
  1133. return Error("导出失败");
  1134. }
  1135. //return Success("导出成功");
  1136. }
  1137. /// <summary>
  1138. /// 上传文件并导入数据库--导入潜在客户
  1139. /// </summary>
  1140. /// <returns></returns>
  1141. [HttpPost("importexcelqz")]
  1142. public async Task<IActionResult> ImportQExcel(int headrow = 0)
  1143. {
  1144. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  1145. string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
  1146. //if (string.IsNullOrEmpty(taskid))
  1147. // return Error("任务id不能为空");
  1148. //string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  1149. Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
  1150. 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"))
  1151. return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
  1152. NPOIHelper npoi = new NPOIHelper();
  1153. var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
  1154. int num = dtExcel.Rows.Count;
  1155. var cols = dtExcel.Columns;
  1156. int colnum = cols.Count;
  1157. string dbkeys = _configuration["upload:qrodbkeys"].ToString();
  1158. string[] dbcols = dbkeys.Split(",");
  1159. string errmsg = string.Empty;
  1160. if (num > 0)
  1161. {
  1162. int index = 1;
  1163. foreach (System.Data.DataRow dr in dtExcel.Rows)
  1164. {
  1165. DateTime ftime = DateTime.Now;//跟进时间
  1166. string fcontent = "";//跟进内容
  1167. var model = new Cus_Customer_Project();
  1168. model.createuser = usercode;
  1169. model.createusername = username;
  1170. //model.cusname = dr.cusname;
  1171. //model.phone = dr.phone;
  1172. //model.cusaddr = dr.cusaddr;
  1173. model.createtime = DateTime.Now.ToLocalTime();
  1174. model.customertype = "潜在客户";
  1175. var dbcolslist = dbcols.ToList();
  1176. Type t = model.GetType();
  1177. PropertyInfo[] PropertyList = t.GetProperties();
  1178. foreach (PropertyInfo item in PropertyList)
  1179. {
  1180. _logger.LogError(item.Name.ToString(), "导出异常");
  1181. _logger.LogError(dbcolslist.ToString(), "导出异常");
  1182. if (dbcolslist != null && item != null && item.Name != null && dbcolslist.Contains(item.Name))
  1183. {
  1184. //object v = Convert.ChangeType(dr[dbcolslist.IndexOf(item.Name)].ToString(), item.PropertyType);
  1185. //item.SetValue(model, v, null);
  1186. if (dr[dbcolslist.IndexOf(item.Name)].ToString() != null)
  1187. {
  1188. if (item.PropertyType == typeof(DateTime?) || item.PropertyType == typeof(DateTime))
  1189. {
  1190. if (dr[dbcolslist.IndexOf(item.Name)] != null && dr[dbcolslist.IndexOf(item.Name)].ToString() != "")
  1191. {
  1192. #region 可空类型
  1193. /*Type ty = Type.GetType("System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]");
  1194. DateTime? date = DateTime.Now;
  1195. string s = dr[dbcolslist.IndexOf(item.Name)].ToString();
  1196. if (ty.IsGenericType && ty.GetGenericTypeDefinition() == typeof(Nullable<>))
  1197. {
  1198. if (String.IsNullOrEmpty(s))
  1199. item.SetValue(model, null, null);
  1200. else
  1201. {
  1202. //date = Convert.ChangeType(s, t.GetGenericArguments()[0]);
  1203. //object y = Convert.ChangeType(dr[dbcolslist.IndexOf(item.Name)].ToString(), item.PropertyType);
  1204. item.SetValue(model, Convert.ChangeType(s, t.GetGenericArguments()[0]), null);
  1205. }
  1206. }
  1207. else
  1208. {
  1209. item.SetValue(model, null, null);
  1210. }*/
  1211. #endregion
  1212. object v = Convert.ChangeType(dr[dbcolslist.IndexOf(item.Name)].ToString(), item.PropertyType);
  1213. item.SetValue(model, v, null);
  1214. if (item.Name == "followtime")
  1215. {
  1216. ftime = DateTime.Parse(dr[dbcolslist.IndexOf(item.Name)].ToString());
  1217. }
  1218. else if (item.Name == "followcontent")
  1219. {
  1220. fcontent = dr[dbcolslist.IndexOf(item.Name)].ToString();
  1221. }
  1222. }
  1223. }
  1224. else
  1225. {
  1226. if (dr[dbcolslist.IndexOf(item.Name)].ToString() != "")
  1227. {
  1228. object v = Convert.ChangeType(dr[dbcolslist.IndexOf(item.Name)].ToString(), item.PropertyType);
  1229. item.SetValue(model, v, null);
  1230. if (item.Name == "followcontent")
  1231. {
  1232. fcontent = dr[dbcolslist.IndexOf(item.Name)].ToString();
  1233. }
  1234. }
  1235. }
  1236. }
  1237. }
  1238. }
  1239. string addid = await _cus_customer_projectepository.AddRetID(model);//bool b = await _cus_customer_projectepository.Add(model)
  1240. //保存跟进记录
  1241. if (fcontent != "")
  1242. {
  1243. var modelr = new Cus_FollowRecord();
  1244. modelr.content = fcontent;
  1245. modelr.createName = usercode;
  1246. modelr.createTime = ftime;
  1247. modelr.proid = addid;
  1248. bool bl = await _cus_followrecordrepository.Add(modelr);
  1249. }
  1250. if (string.IsNullOrEmpty(addid))
  1251. {
  1252. if (!string.IsNullOrEmpty(errmsg))
  1253. {
  1254. errmsg = errmsg + "\r\n第" + index + "行导入失败!";
  1255. }
  1256. else
  1257. {
  1258. errmsg = "第" + index + "行导入失败!";
  1259. }
  1260. }
  1261. index++;
  1262. }
  1263. }
  1264. else
  1265. {
  1266. return Error("文件中无数据");
  1267. }
  1268. if (!string.IsNullOrEmpty(errmsg))
  1269. {
  1270. //删除已导入的部分
  1271. return Error(errmsg);
  1272. }
  1273. return Success("导入成功");
  1274. }
  1275. #endregion
  1276. //2019-5-6 获取合同到期提醒
  1277. [Authorize]
  1278. [HttpGet("gettxlist")]
  1279. public async Task<IActionResult> GetListAsync(int daykind = 0)//到期前一月或前几天提醒
  1280. {
  1281. #region 条件信息
  1282. var listfilter = new List<FilterDefinition<Cus_Customer_Project>>();
  1283. listfilter.Add(Builders<Cus_Customer_Project>.Filter.Eq("isdelete", 0));
  1284. var filter = Builders<Cus_Customer_Project>.Filter.And(listfilter);
  1285. DateTime dtstart = DateTime.Now;
  1286. var sort = Builders<Cus_Customer_Project>.Sort.Descending("orderendtime");
  1287. var list = await _cus_customer_projectepository.Get(filter, null, sort); ;
  1288. switch (daykind)
  1289. {
  1290. case 0://到期前一月orderendtime到期时间减去现在时间大于0小于等于30
  1291. DateTime dtend = dtstart.AddDays(30);
  1292. DateTime dt = DateTime.Now.ToLocalTime();
  1293. if (DateTime.TryParse(dtstart.ToString("yyyy-MM-dd") + " 00:00:00", out dt))
  1294. listfilter.Add(Builders<Cus_Customer_Project>.Filter.Gt("orderendtime", dt));
  1295. if (DateTime.TryParse(dtend.ToString("yyyy-MM-dd") + " 23:59:59", out dt))
  1296. listfilter.Add(Builders<Cus_Customer_Project>.Filter.Lt("orderendtime", dt));
  1297. var filter0 = Builders<Cus_Customer_Project>.Filter.And(listfilter);
  1298. list = await _cus_customer_projectepository.Get(filter0, null, sort);
  1299. break;
  1300. case 1://到期当天
  1301. DateTime dtend1 = dtstart;//.AddDays(30);
  1302. DateTime dt1 = DateTime.Now.ToLocalTime();
  1303. if (DateTime.TryParse(dtstart.ToString("yyyy-MM-dd") + " 00:00:00", out dt1))
  1304. listfilter.Add(Builders<Cus_Customer_Project>.Filter.Gt("orderendtime", dt1));
  1305. if (DateTime.TryParse(dtend1.ToString("yyyy-MM-dd") + " 23:59:59", out dt1))
  1306. listfilter.Add(Builders<Cus_Customer_Project>.Filter.Lt("orderendtime", dt1));
  1307. var filter1 = Builders<Cus_Customer_Project>.Filter.And(listfilter);
  1308. list = await _cus_customer_projectepository.Get(filter1, null, sort);
  1309. break;
  1310. default:
  1311. break;
  1312. }
  1313. #endregion
  1314. var obj = new
  1315. {
  1316. data = list
  1317. };
  1318. return Success("成功", obj);
  1319. }
  1320. }
  1321. }