MarketCustomerResourcesController.cs 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. using Aspose.Cells;
  2. using EyeSoft.Extensions;
  3. using OASystem.API.OAMethodLib;
  4. using OASystem.Domain.AesEncryption;
  5. using OASystem.Domain.Attributes;
  6. using OASystem.Domain.Dtos.CRM;
  7. using OASystem.Domain.Entities.Customer;
  8. using OASystem.Infrastructure.Repositories.CRM;
  9. using OASystem.RedisRepository;
  10. using System.Collections;
  11. using System.Data;
  12. namespace OASystem.API.Controllers
  13. {
  14. /// <summary>
  15. /// 市场客户资料
  16. /// </summary>
  17. [Route("api/[controller]/[action]")]
  18. public class MarketCustomerResourcesController : ControllerBase
  19. {
  20. private readonly NewClientDataRepository _clientDataRepository;
  21. private readonly SqlSugarClient _sqlSugar;
  22. private string keyName = "newClient_SearchKey";
  23. /// <summary>
  24. /// 初始化
  25. /// </summary>
  26. public MarketCustomerResourcesController(NewClientDataRepository clientDataRepository, SqlSugarClient sqlSugar)
  27. {
  28. this._clientDataRepository = clientDataRepository;
  29. _sqlSugar = sqlSugar;
  30. }
  31. /// <summary>
  32. /// 客户资料数据
  33. /// 基础数据
  34. /// </summary>
  35. /// <returns></returns>
  36. [HttpPost]
  37. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  38. public async Task<IActionResult> MarketCustomerInit(MarketCustomerInitDto dto)
  39. {
  40. JsonView jw = new JsonView();
  41. try
  42. {
  43. Result resultData = await _clientDataRepository._Init(dto);
  44. if (resultData.Code == 0)
  45. {
  46. jw = JsonView(true, "查询成功!", resultData.Data);
  47. }
  48. else
  49. {
  50. jw = JsonView(false, resultData.Msg);
  51. }
  52. }
  53. catch (Exception)
  54. {
  55. jw = JsonView(false, "程序错误!");
  56. }
  57. return Ok(jw);
  58. }
  59. /// <summary>
  60. /// 查询客户资料数据
  61. /// </summary>
  62. /// <returns></returns>
  63. [HttpPost]
  64. [ApiLog("Crm_NewClientData", OperationEnum.List)]
  65. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  66. public async Task<IActionResult> QueryNewClientData(NewClientDataQueryDto dto)
  67. {
  68. #region 参数验证
  69. if (dto.OperationUserId < 0)
  70. return Ok(JsonView(false, "请传入有效的OperationUserId参数!"));
  71. if (dto.PortType < 0)
  72. return Ok(JsonView(false, "请传入有效的PortType参数!"));
  73. #endregion
  74. JsonView jw = new JsonView();
  75. try
  76. {
  77. Result resultData = await _clientDataRepository.QueryNewClientData(dto);
  78. if (resultData.Code == 0)
  79. {
  80. #region 客户资料表操作记录
  81. //await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.NoOperation, dto.OperationUserId, 0, "");
  82. #endregion
  83. jw = JsonView(true, resultData.Msg, resultData.Data);
  84. }
  85. else
  86. {
  87. jw = JsonView(false, resultData.Msg);
  88. }
  89. }
  90. catch (Exception)
  91. {
  92. jw = JsonView(false, "程序错误!");
  93. }
  94. return Ok(jw);
  95. }
  96. /// <summary>
  97. /// 客户资料数据
  98. /// Details
  99. /// </summary>
  100. /// <returns></returns>
  101. [HttpPost]
  102. [ApiLog("Crm_NewClientData", OperationEnum.Details)]
  103. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  104. public async Task<IActionResult> PostNewClientDataDetails(NewClientDataDetailsDto dto)
  105. {
  106. #region 参数验证
  107. if (dto.Id < 0)
  108. return Ok(JsonView(false, "请传入有效的Id参数!"));
  109. if (dto.UserId < 0)
  110. return Ok(JsonView(false, "请传入有效的UserId参数!"));
  111. if (dto.PortType < 0)
  112. return Ok(JsonView(false, "请传入有效的PortType参数!"));
  113. #endregion
  114. JsonView jw = new JsonView();
  115. try
  116. {
  117. Result resultData = await _clientDataRepository._Details(dto.PortType, dto.Id);
  118. if (resultData.Code == 0)
  119. {
  120. #region 客户资料表操作记录
  121. //await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.Details, dto.UserId, dto.Id, "");
  122. #endregion
  123. jw = JsonView(true, "查询成功!", resultData.Data);
  124. }
  125. else
  126. {
  127. jw = JsonView(false, resultData.Msg);
  128. }
  129. }
  130. catch (Exception)
  131. {
  132. jw = JsonView(false, "程序错误!");
  133. }
  134. return Ok(jw);
  135. }
  136. /// <summary>
  137. /// 客户资料操作(Status:1.新增,2.修改)
  138. /// </summary>
  139. /// <param name="dto"></param>
  140. /// <returns></returns>
  141. [HttpPost]
  142. [ApiLog("Crm_NewClientData", OperationEnum.NoOperation)]
  143. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  144. public async Task<IActionResult> NewClientOp(NewClientOpDto dto)
  145. {
  146. #region 参数验证
  147. if (dto.CreateUserId < 0)
  148. {
  149. return Ok(JsonView(false, "请传入有效的CreateUserId参数!"));
  150. }
  151. if (dto.PortType < 0)
  152. {
  153. return Ok(JsonView(false, "请传入有效的PortType参数!"));
  154. }
  155. #endregion
  156. try
  157. {
  158. Domain.Result result = await _clientDataRepository.NewClientOp(dto);
  159. if (result.Code != 0)
  160. {
  161. return Ok(JsonView(false, result.Msg));
  162. }
  163. #region 客户资料操作记录
  164. //OperationEnum operationEnum = OperationEnum.NoOperation;
  165. //if (dto.Status == 1)
  166. //{
  167. // operationEnum = OperationEnum.Add;
  168. // dto.Id = Convert.ToInt32(result.Data);
  169. //}
  170. //else if (dto.Status == 2) operationEnum = OperationEnum.Edit;
  171. //await GeneralMethod.NewClientOperationRecord(dto.PortType, operationEnum, dto.CreateUserId, dto.Id, "");
  172. #endregion
  173. //this.keyName += "_" + dto.CreateUserId;
  174. //var exists = await RedisFactory.CreateRedisRepository().KeyExistsAsync(keyName);
  175. //if (exists) await RedisFactory.CreateRedisRepository().KeyDeleteAsync(keyName);
  176. return Ok(JsonView(true, result.Msg + "Id:" + dto.Id));
  177. }
  178. catch (Exception ex)
  179. {
  180. return Ok(JsonView(false, "程序错误!Msg:" + ex.Message));
  181. }
  182. }
  183. /// <summary>
  184. /// 新客户资料操作(删除)
  185. /// </summary>
  186. /// <param name="dto"></param>
  187. /// <returns></returns>
  188. [HttpPost]
  189. [ApiLog("Crm_NewClientData", OperationEnum.Del)]
  190. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  191. public async Task<IActionResult> NewClientDel(DelBaseDto dto)
  192. {
  193. #region 参数验证
  194. if (dto.Id < 0)
  195. {
  196. return Ok(JsonView(false, "请传入有效的Id参数!"));
  197. }
  198. if (dto.DeleteUserId < 0)
  199. {
  200. return Ok(JsonView(false, "请传入有效的DeleteUserId参数!"));
  201. }
  202. if (dto.PortType < 0)
  203. {
  204. return Ok(JsonView(false, "请传入有效的PortType参数!"));
  205. }
  206. #endregion
  207. var res = await _clientDataRepository.DelNewClientData(dto);
  208. if (res.Code != 0)
  209. {
  210. return Ok(JsonView(false, "删除失败"));
  211. }
  212. #region 客户资料表操作记录
  213. //await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.Del, dto.DeleteUserId, dto.Id, "");
  214. #endregion
  215. //this.keyName += "_" + dto.DeleteUserId;
  216. //var exists = await RedisFactory.CreateRedisRepository().KeyExistsAsync(keyName);
  217. //if (exists) await RedisFactory.CreateRedisRepository().KeyDeleteAsync(keyName);
  218. return Ok(JsonView(true, "删除成功!"));
  219. }
  220. /// <summary>
  221. /// 获取下拉列表数据和单条数据信息
  222. /// </summary>
  223. /// <param name="dto"></param>
  224. /// <returns></returns>
  225. [HttpPost]
  226. [ApiLog("Crm_NewClientData", OperationEnum.Details)]
  227. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  228. public async Task<IActionResult> QuerySelectAndSingleData(QuerySingleDto dto)
  229. {
  230. JsonView jw = new JsonView();
  231. var result = await _clientDataRepository.QuerySelectAndSingleData(dto);
  232. if (result.Code == 0)
  233. {
  234. #region 客户资料表操作记录
  235. //await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.Details, dto.UserId, dto.Id, "");
  236. #endregion
  237. jw = JsonView(true, result.Msg, result.Data);
  238. }
  239. else
  240. {
  241. jw = JsonView(false, result.Msg);
  242. }
  243. return Ok(jw);
  244. }
  245. /// <summary>
  246. /// 获取现有负责人
  247. /// </summary>
  248. /// <returns></returns>
  249. [HttpPost]
  250. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  251. public async Task<IActionResult> QueryUserSelect()
  252. {
  253. Result resTable = _clientDataRepository.QueryUserSelect();
  254. return Ok(JsonView(true, resTable.Msg, resTable.Data));
  255. }
  256. /// <summary>
  257. /// 获取出团数据
  258. /// </summary>
  259. /// <returns></returns>
  260. [HttpPost]
  261. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  262. public async Task<IActionResult> QueryNumberGroups()
  263. {
  264. var result = await _clientDataRepository.QueryNumberGroups();
  265. if (result.Code != 0)
  266. {
  267. return Ok(JsonView(false, result.Msg));
  268. }
  269. return Ok(JsonView(true, result.Msg, result.Data));
  270. }
  271. /// <summary>
  272. /// 新客户资料操作
  273. /// 批量分配
  274. /// </summary>
  275. /// <param name="dto"></param>
  276. /// <returns></returns>
  277. [HttpPost]
  278. [ApiLog("Crm_NewClientData", OperationEnum.BatchAssignment)]
  279. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  280. public async Task<IActionResult> PostBatchAssignment(BatchAssignmentDto dto)
  281. {
  282. #region 参数验证
  283. if (dto.UserId < 0)
  284. {
  285. return Ok(JsonView(false, "请传入有效的UserId参数!"));
  286. }
  287. if (dto.PortType < 0)
  288. {
  289. return Ok(JsonView(false, "请传入有效的PortType参数!"));
  290. }
  291. #endregion
  292. var res = await _clientDataRepository._BatchAssignment(dto);
  293. if (res.Code != 0)
  294. {
  295. return Ok(JsonView(false, res.Msg));
  296. }
  297. return Ok(JsonView(true, "操作成功!"));
  298. }
  299. private Dictionary<int, List<int>> MCRSetting()
  300. {
  301. Dictionary<int, List<int>> keyValuePairs = new Dictionary<int, List<int>>();
  302. keyValuePairs.Add(419, new List<int>() //四川
  303. {
  304. 376,377,378,381,382,387,388,389,390,753,754,1296,1303
  305. });
  306. keyValuePairs.Add(420, new List<int>() //云南
  307. {
  308. 407,408,409,410,449,451,452,453,567,754,1297,1304
  309. });
  310. keyValuePairs.Add(421, new List<int>() { 424, 425, 426, 427, 428, 429, 754, 1298, 1305, 1307, 1306 }); // 贵州
  311. keyValuePairs.Add(422, new List<int>() { 415, 416, 754, 1317, 1316, 1315, 455, 1321, 1320, 1319, 1318 }); // 西藏
  312. keyValuePairs.Add(423, new List<int>() { 417, 418, 454, 456, 754, 1313, 1314 }); // 重庆
  313. keyValuePairs.Add(578, new List<int>() { 581, 582, 583, 754, 1299, 1300 }); // 青海
  314. keyValuePairs.Add(605, new List<int>() { 588, 589, 590, 591, 592, 593, 754, 1301, 1310, 1309, 1308 }); // 陕西
  315. keyValuePairs.Add(606, new List<int>() { 597, 598, 599, 600, 601, 602, 603, 604, 754, 1324, 1323, 1322 }); // 宁夏
  316. keyValuePairs.Add(625, new List<int>() { 617, 618, 619, 620, 621, 622, 622, 623, 624, 754, 1302, 1312, 1311 }); // 甘肃
  317. keyValuePairs.Add(634, new List<int>() { 630, 631, 632, 633, 754 }); // 新疆
  318. return keyValuePairs;
  319. }
  320. private Dictionary<int, List<int>> MCRSecondSetting()
  321. {
  322. Dictionary<int, List<int>> keyValuePairs = new Dictionary<int, List<int>>();
  323. // 四川
  324. keyValuePairs.Add(349, new List<int>() { 376, 378, 381, 387, 389, 1296 }); // 四川省级
  325. keyValuePairs.Add(350, new List<int>() { 377, 382, 388, 390, 1303 }); // 成都市级
  326. keyValuePairs.Add(348, new List<int>() { 387 }); // 四川地市州 - Explicitly assigning to ensure all source2 IDs are used
  327. keyValuePairs.Add(701, new List<int>() { 388 }); // 成都区市县
  328. // 云南
  329. keyValuePairs.Add(399, new List<int>() { 407, 408, 409, 410, 451 }); // 云南省级
  330. keyValuePairs.Add(400, new List<int>() { 449, 452, 453, 1297, 1304 }); // 昆明市级
  331. keyValuePairs.Add(450, new List<int>() { 410 }); // 云南地市州
  332. keyValuePairs.Add(566, new List<int>() { 567 }); // 昆明区市县
  333. // 贵州
  334. keyValuePairs.Add(401, new List<int>() { 424, 426, 427, 428, 1298 }); // 贵州省级
  335. keyValuePairs.Add(402, new List<int>() { 425, 429, 1306, 1307, 1305 }); // 贵阳市级
  336. keyValuePairs.Add(414, new List<int>() { 428 }); // 贵州地市州
  337. //keyValuePairs.Add(1305, new List<int>() { /* Add relevant IDs if available */ }); // 贵阳区市县 - Placeholder for now
  338. // 重庆
  339. keyValuePairs.Add(403, new List<int>() { 417, 418, 454, 456, 1314 }); // 重庆市级
  340. keyValuePairs.Add(404, new List<int>() { 1313 }); // 重庆区县 - No direct match in source1
  341. // 西藏
  342. keyValuePairs.Add(405, new List<int>() { 415, 1317, 1316, 1315 }); // 西藏自治区
  343. keyValuePairs.Add(406, new List<int>() { 455, 1321, 1320, 1319, 1318 }); // 拉萨市级
  344. keyValuePairs.Add(413, new List<int>() { 416 }); // 西藏地市州
  345. // 青海
  346. keyValuePairs.Add(579, new List<int>() { 581, 582, 583, 1300 }); // 青海省级
  347. keyValuePairs.Add(580, new List<int>() { 1299 }); // 青海市州县 (Includes 地市州)
  348. // 陕西
  349. keyValuePairs.Add(585, new List<int>() { 588, 589, 590, 591, 1301 }); // 陕西省级
  350. keyValuePairs.Add(586, new List<int>() { 589 }); // 陕西地市州
  351. keyValuePairs.Add(587, new List<int>() { 592, 593, 1309, 1310, 1308 }); // 西安市级
  352. //keyValuePairs.Add(1308, new List<int>() { /* Add relevant IDs if available */ }); // 西安区市县 - Placeholder
  353. // 宁夏
  354. keyValuePairs.Add(594, new List<int>() { 597, 598, 599, 600, 601, 1322 }); // 宁夏自治区 (Includes 地市州 and 区级)
  355. keyValuePairs.Add(595, new List<int>() { 597 }); // 宁夏地市州
  356. keyValuePairs.Add(596, new List<int>() { 598, 599, 600, 603, 604, 602, 1323, 1324 }); // 银川市级 (Includes 区级)
  357. //keyValuePairs.Add(602, new List<int>()); // 银川区市县 - No direct match, but could be considered covered by "区级"
  358. // 甘肃
  359. keyValuePairs.Add(614, new List<int>() { 617, 618, 619, 620, 621, 1302 }); // 甘肃省级
  360. keyValuePairs.Add(615, new List<int>() { 618 }); // 甘肃地市州
  361. keyValuePairs.Add(616, new List<int>() { 622, 623, 624, 1312, 1311 }); // 兰州市级
  362. //keyValuePairs.Add(1311, new List<int>() { /* Add relevant IDs if available */ }); // 兰州区市县 - Placeholder
  363. // 新疆
  364. keyValuePairs.Add(627, new List<int>() { 630, 631, 632, 1327, 1326, 1325 }); // 新疆自治区级
  365. keyValuePairs.Add(628, new List<int>() { 1326 }); // 新疆地市州 - No direct match
  366. keyValuePairs.Add(629, new List<int>() { 633, 1331, 1330, 1329, 1328 }); // 乌鲁木齐市级
  367. // 中央直属企业 (Special case)
  368. keyValuePairs.Add(351, new List<int>() { 754 }); // Using "未分级" for 中央直属企业
  369. return keyValuePairs;
  370. }
  371. private Dictionary<int, List<int>> MCRLvSetting()
  372. {
  373. var keyValuePairs = new Dictionary<int, List<int>>();
  374. // 四川 (419)
  375. keyValuePairs.Add(419, new List<int>() { 348, 349, 350, 701 }); // Includes all Sichuan-related IDs from source2
  376. // 云南 (420)
  377. keyValuePairs.Add(420, new List<int>() { 399, 400, 450, 566 }); // Includes all Yunnan-related IDs
  378. // 贵州 (421)
  379. keyValuePairs.Add(421, new List<int>() { 401, 402, 414 });
  380. // 西藏 (422)
  381. keyValuePairs.Add(422, new List<int>() { 405, 406, 413 });
  382. // 重庆 (423)
  383. keyValuePairs.Add(423, new List<int>() { 403, 404 });
  384. // 青海 (578)
  385. keyValuePairs.Add(578, new List<int>() { 579, 580, 626 }); // Includes 市州县 and 西宁市级
  386. // 陕西 (605)
  387. keyValuePairs.Add(605, new List<int>() { 585, 586, 587 });
  388. // 宁夏 (606)
  389. keyValuePairs.Add(606, new List<int>() { 594, 595, 596 });
  390. // 甘肃 (625)
  391. keyValuePairs.Add(625, new List<int>() { 614, 615, 616 });
  392. // 新疆 (634)
  393. keyValuePairs.Add(634, new List<int>() { 627, 628, 629 });
  394. // 未分级 (351) - You'll likely want to remove this as it's no longer a primary key.
  395. // keyValuePairs.Add(351, new List<int>()); //Remove this line.
  396. return keyValuePairs;
  397. }
  398. [HttpPost]
  399. public IActionResult QueryClientType(QueryClientTypeDto Dto)
  400. {
  401. var jw = JsonView(true, "获取成功!");
  402. var dic_lv = MCRLvSetting();
  403. var keyValuePairs = MCRSetting();
  404. ArrayList arr = new ArrayList();
  405. ArrayList lvArr = new ArrayList();
  406. var ids = new List<int>();
  407. var lvids = new List<int>();
  408. foreach (var item in Dto.SetDataIdArr)
  409. {
  410. if (keyValuePairs.Keys.Contains(item))
  411. {
  412. ids.AddRange(keyValuePairs[item]);
  413. }
  414. if (dic_lv.Keys.Contains(item))
  415. {
  416. lvids.AddRange(dic_lv[item]);
  417. }
  418. }
  419. arr.AddRange(_sqlSugar.Queryable<Sys_SetData>()
  420. .Where(u => ids.Contains(u.Id) && u.IsDel == 0)
  421. .Select(x => new { x.Id, x.Name, x.Remark })
  422. .ToList());
  423. if (arr.Count == 0)
  424. {
  425. arr.AddRange(_sqlSugar.Queryable<Sys_SetData>()
  426. .Where(u => u.STid == 37 && u.IsDel == 0)
  427. .Select(x => new { x.Id, x.Name, x.Remark }).ToList());
  428. }
  429. // 创建比较器实例
  430. IComparer remakeComparer = new RemakeComparer();
  431. // 使用ArrayList.Sort方法和自定义比较器对arr进行排序
  432. arr.Sort(remakeComparer);
  433. var rangelv = _sqlSugar.Queryable<Sys_SetData>()
  434. .Where(u => lvids.Contains(u.Id) && u.IsDel == 0)
  435. .Select(x => new { x.Id, x.Name })
  436. .ToList();
  437. lvArr.AddRange(rangelv);
  438. if (lvArr.Count == 0)
  439. {
  440. lvArr.AddRange(_sqlSugar.Queryable<Sys_SetData>()
  441. .Where(u => u.STid == 33 && u.IsDel == 0)
  442. .Select(x => new { x.Id, x.Name }).ToList());
  443. }
  444. jw.Data = new
  445. {
  446. TypeArr = arr,
  447. LvArr = lvArr,
  448. };
  449. return Ok(jw);
  450. }
  451. [HttpPost]
  452. public IActionResult QueryClientTypeSecond(QueryClientTypeDto Dto)
  453. {
  454. var jw = JsonView(true, "获取成功!");
  455. var keyValuePairs = MCRSecondSetting();
  456. ArrayList arr = new ArrayList();
  457. var ids = new List<int>();
  458. foreach (var item in Dto.SetDataIdArr)
  459. {
  460. if (keyValuePairs.Keys.Contains(item))
  461. {
  462. ids.AddRange(keyValuePairs[item]);
  463. }
  464. }
  465. arr.AddRange(_sqlSugar.Queryable<Sys_SetData>()
  466. .Where(u => ids.Contains(u.Id) && u.IsDel == 0)
  467. .Select(x => new { x.Id, x.Name, x.Remark })
  468. .ToList());
  469. if (arr.Count == 0)
  470. {
  471. arr.AddRange(_sqlSugar.Queryable<Sys_SetData>()
  472. .Where(u => u.STid == 37 && u.IsDel == 0)
  473. .Select(x => new { x.Id, x.Name, x.Remark }).ToList());
  474. }
  475. // 创建比较器实例
  476. IComparer remakeComparer = new RemakeComparer();
  477. // 使用ArrayList.Sort方法和自定义比较器对arr进行排序
  478. arr.Sort(remakeComparer);
  479. jw.Data = arr;
  480. return Ok(jw);
  481. }
  482. /// <summary>
  483. /// 客户资料数据 全信息导出 excel 下载
  484. /// </summary>
  485. /// <param name="dto"></param>
  486. /// <returns></returns>
  487. [HttpPost]
  488. [ApiLog("Crm_NewClientData", OperationEnum.Download)]
  489. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  490. public async Task<IActionResult> NewClientDataExcelDownload(NewClientDataExcelDownloadDto dto)
  491. {
  492. #region 参数验证
  493. if (dto.OperationUserId < 0) return Ok(JsonView(false, "请传入有效的OperationUserId参数!"));
  494. if (dto.PortType < 0) return Ok(JsonView(false, "请传入有效的PortType参数!"));
  495. if (dto.PageId < 0) return Ok(JsonView(false, "请传入有效的PageId参数!"));
  496. #endregion
  497. #region 页面操作权限验证
  498. var pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(dto.OperationUserId, dto.PageId);
  499. if (pageFunAuthView.FilesDownloadAuth == 0) return Ok(JsonView(false, "您没有文件下载权!"));
  500. #endregion
  501. var dt = await _clientDataRepository.NewClientDataExcelDownload(dto);
  502. if (dt != null)
  503. {
  504. string tempPath = (AppSettingsHelper.Get("ExcelBasePath") + "Template/公司客户资料导出模板.xlsx");
  505. var designer = new WorkbookDesigner();
  506. designer.Workbook = new Workbook(tempPath);
  507. designer.SetDataSource("NCDDT", dt);
  508. designer.Process();
  509. //文件名
  510. string fileName = $"公司客户资料(全信息导出){DateTime.Now.ToString("yyyyMMddHHmmss")}.xls";
  511. designer.Workbook.Save(AppSettingsHelper.Get("ExcelBasePath") + "NewClientDataExcelDownload/" + fileName);
  512. string url = AppSettingsHelper.Get("ExcelBaseUrl") + "Office/Excel/NewClientDataExcelDownload/" + fileName;
  513. #region 客户资料表操作记录
  514. var respose = JsonView(true, "成功", url);
  515. //var paramData = new
  516. //{
  517. // APIName = @"MarketCustomerResources/NewClientDataExcelDownload",
  518. // RequestParam = dto,
  519. // ResposeParam = respose
  520. //};
  521. //await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.Download, dto.OperationUserId, 0, JsonConvert.SerializeObject(paramData));
  522. #endregion
  523. return Ok(respose);
  524. }
  525. return Ok(JsonView(false));
  526. }
  527. /// <summary>
  528. /// 客户资料数据 分权限导出 excel 下载
  529. /// </summary>
  530. /// <param name="dto"></param>
  531. /// <returns></returns>
  532. [HttpPost]
  533. [ApiLog("Crm_NewClientData", OperationEnum.Download)]
  534. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  535. public async Task<IActionResult> NewClientDataAuthorityExcelDownload(NewClientDataExcelDownloadDto dto)
  536. {
  537. #region 参数验证
  538. if (dto.OperationUserId < 0) return Ok(JsonView(false, "请传入有效的OperationUserId参数!"));
  539. if (dto.PortType < 0) return Ok(JsonView(false, "请传入有效的PortType参数!"));
  540. if (dto.PageId < 0) return Ok(JsonView(false, "请传入有效的PageId参数!"));
  541. #endregion
  542. #region 页面操作权限验证
  543. var pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(dto.OperationUserId, dto.PageId);
  544. if (pageFunAuthView.FilesDownloadAuth == 0) return Ok(JsonView(false, "您没有文件下载权!"));
  545. #endregion
  546. var dt = await _clientDataRepository.NewClientDataExcelDownload(dto);
  547. if (dt != null)
  548. {
  549. string tempPath = (AppSettingsHelper.Get("ExcelBasePath") + "Template/公司客户资料分权限导出模板.xlsx");
  550. var designer = new WorkbookDesigner();
  551. designer.Workbook = new Workbook(tempPath);
  552. designer.SetDataSource("NCDDT", dt);
  553. designer.Process();
  554. //文件名
  555. string fileName = $"公司客户资料(分权限导出){DateTime.Now.ToString("yyyyMMddHHmmss")}.xls";
  556. designer.Workbook.Save(AppSettingsHelper.Get("ExcelBasePath") + "NewClientDataExcelDownload/" + fileName);
  557. string url = AppSettingsHelper.Get("ExcelBaseUrl") + "Office/Excel/NewClientDataExcelDownload/" + fileName;
  558. var respose = JsonView(true, "成功", url);
  559. return Ok(respose);
  560. }
  561. return Ok(JsonView(false));
  562. }
  563. /// <summary>
  564. /// 客户资料数据 历史记录 Init
  565. /// </summary>
  566. /// <param name="dto"></param>
  567. /// <returns></returns>
  568. [HttpPost]
  569. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  570. public async Task<IActionResult> NewClientDataRecordInit(NewClientDataRecordInitDto dto)
  571. {
  572. #region 参数验证
  573. if (dto.UserId < 0) return Ok(JsonView(false, MsgTips.UserId));
  574. if (!SharingStaticData.PortTypes.Contains(dto.PortType)) return Ok(JsonView(false, MsgTips.Port));
  575. if (dto.PageId < 0) return Ok(JsonView(false, MsgTips.PageId));
  576. #endregion
  577. #region 页面操作权限验证
  578. var pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(dto.UserId, dto.PageId);
  579. if (pageFunAuthView.CheckAuth == 0) return Ok(JsonView(false, "您没有查看权限!"));
  580. #endregion
  581. var enumfiltrates = new string[] { "NoOperation", "Audit", "Login" };
  582. var operations = EnumHelper.GetEnumDescriptions(typeof(OperationEnum), enumfiltrates);
  583. var operation1 = new List<dynamic>();
  584. foreach (var item in operations)
  585. {
  586. operation1.Add(new { value = item.Key, text = item.Value });
  587. }
  588. object userDatas = Array.Empty<object>();
  589. if (dto.UserId == 21)
  590. {
  591. var userIds = await _sqlSugar.Queryable<Crm_ClientDataAndUser>().Where(x => x.IsDel == 0).Select(x => x.usersId).Distinct().ToListAsync();
  592. if (userIds.Any())
  593. {
  594. userDatas = await _sqlSugar.Queryable<Sys_Users>().Where(x => x.IsDel == 0 && userIds.Contains(x.Id)).Select(x => new { value = x.Id, text = x.CnName }).ToListAsync();
  595. }
  596. }
  597. var data = new
  598. {
  599. operations = operation1,
  600. userDatas = userDatas
  601. };
  602. return Ok(JsonView(data));
  603. }
  604. /// <summary>
  605. /// 客户资料数据 历史记录
  606. /// </summary>
  607. /// <param name="dto"></param>
  608. /// <returns></returns>
  609. [HttpPost]
  610. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  611. public async Task<IActionResult> NewClientDataRecord(NewClientDataRecordDto dto)
  612. {
  613. #region 参数验证
  614. if (dto.UserId < 0) return Ok(JsonView(false, MsgTips.UserId));
  615. if (!SharingStaticData.PortTypes.Contains(dto.PortType)) return Ok(JsonView(false, MsgTips.Port));
  616. if (dto.PageId < 0) return Ok(JsonView(false, MsgTips.PageId));
  617. #endregion
  618. #region 页面操作权限验证
  619. var pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(dto.UserId, dto.PageId);
  620. if (pageFunAuthView.CheckAuth == 0) return Ok(JsonView(false, "您没有查看权限!"));
  621. #endregion
  622. return Ok(await _clientDataRepository.NewClientDataRecord(dto));
  623. }
  624. #region 修改数据
  625. //[HttpPost]
  626. //public IActionResult SynchronizationData()
  627. //{
  628. // var keyValuePairs = new Dictionary<int, List<string>>
  629. // {
  630. // { 5, new List<string> { "省级部门", "省级单位" } },
  631. // { 10, new List<string> { "市州" } },
  632. // { 15, new List<string> { "省属国企" } },
  633. // { 20, new List<string> { "省属学校", "省属高校" } },
  634. // { 25, new List<string> { "省级行业协会", "省级社团", "省级联合会" , "省属行业协会", "民营企业", "地方国企" } },
  635. // { 30, new List<string> { "市级部门", "市级单位" } },
  636. // { 35, new List<string> { "区市县" } },
  637. // { 40, new List<string> { "市级国企", "市属国企" } },
  638. // { 45, new List<string> { "市属学校", "市属高校" } },
  639. // { 50, new List<string> { "市级行业协会", "市级社团", "市级联合会" } }
  640. // };
  641. // var setting = MCRSetting();
  642. // var arr = new List<List<Sys_SetData>>();
  643. // foreach (var item in setting.Keys)
  644. // {
  645. // var setDataList_DB = _sqlSugar.Queryable<Sys_SetData>()
  646. // .Where(x => x.IsDel == 0 && setting[item].Contains(x.Id))
  647. // .ToList();
  648. // foreach (var setData in setDataList_DB)
  649. // {
  650. // foreach (var lv in keyValuePairs.Keys)
  651. // {
  652. // var islv = keyValuePairs[lv].Where(x => setData.Name.Contains(x)).Count() > 0;
  653. // if (islv)
  654. // {
  655. // setData.Remark = lv.ToString();
  656. // }
  657. // }
  658. // }
  659. // arr.Add(setDataList_DB);
  660. // }
  661. // _sqlSugar.Updateable(arr.SelectMany(x => x).ToList()).UpdateColumns(x => new
  662. // {
  663. // x.Remark
  664. // }).ExecuteCommand();
  665. // return Ok(arr);
  666. //}
  667. [HttpPost]
  668. public IActionResult QueryData()
  669. {
  670. var db = _sqlSugar.Queryable<Crm_NewClientData>()
  671. .Where(x => x.IsDel == 0)
  672. .Select(x => new Crm_NewClientData
  673. {
  674. Id = x.Id,
  675. Client = x.Client,
  676. Category = x.Category,
  677. Location = x.Location,
  678. }).ToList();
  679. var count = 0;
  680. var updateid = new List<int>();
  681. foreach (var item in db)
  682. {
  683. EncryptionProcessor.DecryptProperties(item);
  684. if (!string.IsNullOrWhiteSpace(item.Location) && item.Location.Contains("重庆") && item.Category == 0)
  685. {
  686. count++;
  687. //if (!string.IsNullOrWhiteSpace(item.Client) && (item.Client.Contains("大学") || item.Client.Contains("学院")))
  688. {
  689. updateid.Add(item.Id);
  690. }
  691. }
  692. }
  693. var updateCount = _sqlSugar.Updateable<Crm_NewClientData>().Where(x => updateid.Contains(x.Id))
  694. .SetColumns(x => new Crm_NewClientData { Category = 456 }).ExecuteCommand();
  695. return Ok(new
  696. {
  697. notCount = count,
  698. updateCount = updateCount
  699. });
  700. }
  701. [HttpPost]
  702. public IActionResult InsertData()
  703. {
  704. string sql = $@"SELECT * FROM [dbo].[Crm_NewClientData] WHERE ID IN (
  705. --所有四川省域的
  706. select Id from [dbo].[Crm_NewClientData] where ISDEL = 0 AND Lvlid IN (
  707. select S.Id from Sys_SetData S
  708. where STid = 33 and isdel = 0 and
  709. (Name like '%四%川%' or Name like '%成%都%')
  710. )
  711. ) AND IsDel = 0
  712. AND ID IN (
  713. SELECT NewClientDataId FROM [dbo].[Crm_ClientDataAndUser]
  714. WHERE NewClientDataId IN (
  715. --所有单独数据
  716. SELECT NewClientDataId FROM [dbo].[Crm_ClientDataAndUser]
  717. WHERE ISDEL = 0
  718. GROUP BY NewClientDataId
  719. HAVING COUNT(NewClientDataId) = 1
  720. ) AND IsDel = 0 AND UsersId = 21
  721. )";
  722. var clientList = _sqlSugar.SqlQueryable<Crm_NewClientData>(sql).Select(x => x.Id).ToList().Select(x => new Crm_ClientDataAndUser
  723. {
  724. NewClientDataId = x,
  725. usersId = 330,
  726. CreateTime = DateTime.Now,
  727. CreateUserId = 235,
  728. IsDel = 0,
  729. }).ToList();
  730. var insertCount = _sqlSugar.Insertable(clientList).ExecuteCommand();
  731. return Ok(insertCount);
  732. }
  733. [HttpPost]
  734. public IActionResult ExportData()
  735. {
  736. var soure = _sqlSugar.Queryable<Crm_NewClientData>()
  737. .InnerJoin<Crm_ClientDataAndUser>((a, b) => b.NewClientDataId == a.Id && b.IsDel == 0)
  738. .Where((a, b) => a.IsDel == 0 && b.usersId == 95)
  739. .Select((a, b) => a)
  740. .ToList();
  741. foreach (var item in soure)
  742. {
  743. EncryptionProcessor.DecryptProperties(item);
  744. }
  745. // 创建WorkbookDesigner对象
  746. WorkbookDesigner designer = new WorkbookDesigner();
  747. // 加载模板文件(包含智能标记)
  748. designer.Workbook = new Workbook("C:\\Users\\PC\\Desktop\\111.xlsx");
  749. // 设置数据源,这里假设你的List集合名为listData
  750. designer.SetDataSource("ListData", soure);
  751. // 处理智能标记以将数据填充到工作表中
  752. designer.Process();
  753. // 保存Excel文件
  754. designer.Workbook.Save("C:\\Users\\PC\\Desktop\\111000.xlsx");
  755. return Ok(1);
  756. }
  757. [HttpPost]
  758. public IActionResult DeleteData()
  759. {
  760. var soure = _sqlSugar.Queryable<Crm_NewClientData>()
  761. .InnerJoin<Crm_ClientDataAndUser>((a, b) => b.NewClientDataId == a.Id && b.IsDel == 0)
  762. .Where((a, b) => a.IsDel == 0 && b.usersId == 330)
  763. .Select((a, b) => b.Id)
  764. .ToList();
  765. var count = _sqlSugar.Updateable<Crm_ClientDataAndUser>()
  766. .Where(u => soure.Contains(u.Id) && u.IsDel == 0)
  767. .SetColumns(x => new Crm_ClientDataAndUser
  768. {
  769. DeleteTime = DateTime.Now.ToString(),
  770. DeleteUserId = 235,
  771. IsDel = 1
  772. }).ExecuteCommand();
  773. return Ok(count);
  774. }
  775. [HttpPost]
  776. public IActionResult ReassignCustomers()
  777. {
  778. var user_Client = _sqlSugar.Queryable<Crm_ClientDataAndUser>()
  779. .Where(x => x.usersId == 302 && x.IsDel == 0)
  780. .Select(x => new
  781. {
  782. x.Id,
  783. x.NewClientDataId
  784. })
  785. .ToList();
  786. var ids = user_Client.Select(x => x.NewClientDataId).ToList();
  787. string setDataSql = $"select * from Sys_SetData where STid = 33 and isdel = 0 and (Name like '%重庆%') ";
  788. var setDataids = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
  789. var clients = _sqlSugar.Queryable<Crm_NewClientData>().Where(x => ids.Contains(x.Id) && setDataids.Contains(x.Lvlid) && x.IsDel == 0).ToList();
  790. var clients_ids = clients.Select(x => x.Id).ToList();
  791. int updateCount = 0;
  792. _sqlSugar.BeginTran();
  793. if (clients.Count == 223)
  794. {
  795. updateCount = _sqlSugar.Updateable<Crm_ClientDataAndUser>()
  796. .Where(x => x.usersId == 302 && clients_ids.Contains(x.NewClientDataId) && x.IsDel == 0)
  797. .SetColumns(x => new Crm_ClientDataAndUser
  798. {
  799. usersId = 21
  800. })
  801. .ExecuteCommand();
  802. }
  803. if (updateCount == clients.Count)
  804. {
  805. _sqlSugar.CommitTran();
  806. }
  807. else
  808. {
  809. _sqlSugar.RollbackTran();
  810. }
  811. return Ok(new
  812. {
  813. count = clients.Count,
  814. updateCount,
  815. });
  816. }
  817. [HttpPost]
  818. public IActionResult InsertDataJiang()
  819. {
  820. var list_DB = _sqlSugar.Queryable<Crm_NewClientData>().Where(x => x.IsDel == 0).ToList();
  821. foreach (var item in list_DB)
  822. {
  823. EncryptionProcessor.DecryptProperties(item);
  824. }
  825. list_DB = list_DB.Where(x => !string.IsNullOrWhiteSpace(x.Client) && (x.Client.Contains("学院") || x.Client.Contains("大学"))).ToList();
  826. var arr = list_DB.Select(x => x.Id).ToList().Select(x => new Crm_ClientDataAndUser
  827. {
  828. NewClientDataId = x,
  829. usersId = 327,
  830. CreateTime = DateTime.Now,
  831. CreateUserId = 235,
  832. IsDel = 0,
  833. }).ToList();
  834. var insertCount = _sqlSugar.Insertable(arr).ExecuteCommand();
  835. return Ok(insertCount);
  836. }
  837. [HttpPost]
  838. public IActionResult InsertDataGu()
  839. {
  840. string sql = @"SELECT * FROM Crm_NewClientData cncd WHERE IsDel = 0
  841. AND Lvlid in (
  842. select Id from Sys_SetData where STid = 33 and isdel = 0
  843. and (Name like '%云%南%' or Name like '%昆%明%')
  844. )";
  845. var list = _sqlSugar.SqlQueryable<Crm_NewClientData>(sql).Select(x => x.Id).ToList();
  846. var toUserList = new List<Crm_ClientDataAndUser>();
  847. foreach (var item in list)
  848. {
  849. toUserList.Add(new Crm_ClientDataAndUser
  850. {
  851. CreateTime = DateTime.Now,
  852. CreateUserId = 235,
  853. NewClientDataId = item,
  854. usersId = 364,
  855. IsDel = 0,
  856. });
  857. }
  858. var updateCount = 0;
  859. if (toUserList.Count == 327)
  860. {
  861. updateCount = _sqlSugar.Insertable(toUserList).ExecuteCommand();
  862. }
  863. return Ok(new
  864. {
  865. updateCount
  866. });
  867. }
  868. [HttpPost]
  869. public IActionResult CloneUserClient(CloneUserClientDto dto)
  870. {
  871. var list = _sqlSugar.Queryable<Crm_ClientDataAndUser>()
  872. .Where(x => x.IsDel == 0 && x.usersId == dto.UserId)
  873. .ToList();
  874. foreach (var item in list)
  875. {
  876. item.usersId = dto.ToUserId;
  877. }
  878. var count = _sqlSugar.Insertable<Crm_ClientDataAndUser>(list).ExecuteCommand();
  879. return Ok(new
  880. {
  881. count,
  882. });
  883. }
  884. #endregion
  885. /// <summary>
  886. /// 客户资料 操作记录记录查询
  887. /// </summary>
  888. /// <param name="userId"></param>
  889. /// <returns></returns>
  890. [HttpGet]
  891. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  892. public async Task<IActionResult> NewClientDataOperatingRrecord(int userId)
  893. {
  894. var datas = await _sqlSugar.Queryable<Crm_TableOperationRecord, Crm_NewClientData, Sys_Users>((tor, ncd, u) =>
  895. new JoinQueryInfos(
  896. JoinType.Left, tor.DataId == ncd.Id,
  897. JoinType.Left, tor.CreateUserId == u.Id
  898. ))
  899. .Where((tor, ncd, u) => tor.TableName == "Crm_NewClientData" && tor.CreateUserId == userId && (int)tor.OperationItem > 1)
  900. .Select((tor, ncd, u) => new
  901. {
  902. tor.PortType,
  903. tor.OperationItem,
  904. tor.DataId,
  905. ncd.Client,
  906. u.CnName,
  907. tor.CreateTime
  908. }).ToListAsync();
  909. var view = new List<dynamic>();
  910. if (datas.Any())
  911. {
  912. datas = datas.OrderByDescending(x => x.CreateTime).ToList();
  913. foreach (var item in datas)
  914. {
  915. var text = item.OperationItem.GetDescription();
  916. view.Add(new
  917. {
  918. Client = AesEncryptionHelper.Decrypt(item.Client),
  919. item.PortType,
  920. item.OperationItem,
  921. text = text,
  922. item.DataId,
  923. item.CnName,
  924. item.CreateTime
  925. });
  926. }
  927. }
  928. return Ok(JsonView(view));
  929. }
  930. [HttpPost]
  931. public async Task<IActionResult> SearchClientByKeyword(SearchClientByKeywordDto dto)
  932. {
  933. var jw = JsonView(true);
  934. var expression = Expressionable.Create<Crm_NewClientData>()
  935. .And(x => x.IsDel == 0);
  936. this.keyName += "_" + dto.UserId;
  937. //获取个人的客户
  938. if (dto.UserId != 21)
  939. {
  940. var userList = _clientDataRepository.GetNewExistClient(dto.UserId).Select(x => x.Id);
  941. var newClientData = _sqlSugar.Queryable<Crm_ClientDataAndUser>()
  942. .Where(x => x.IsDel == 0 && userList.Contains(x.usersId))
  943. .Select(x => x.NewClientDataId)
  944. .Distinct()
  945. .ToList();
  946. expression = expression.And(x => newClientData.Contains(x.Id));
  947. }
  948. var exists = false;
  949. //var exists = await RedisFactory.CreateRedisRepository().KeyExistsAsync(keyName);
  950. //if (exists) exists = await RedisFactory.CreateRedisRepository().HashFieldExistAsync(keyName, dto.Keyword);
  951. if (!exists)
  952. {
  953. string field = "Id,Client";
  954. var newClitnData = _sqlSugar.Queryable<Crm_NewClientData>()
  955. .Where(expression.ToExpression())
  956. .Select(field)
  957. .ToList();
  958. foreach (var item in newClitnData)
  959. {
  960. EncryptionProcessor.DecryptProperties(item);
  961. }
  962. var keywordArray = dto.Keyword.ToCharArray();
  963. var matches = newClitnData
  964. .Where(fullString => !string.IsNullOrWhiteSpace(fullString.Client) &&
  965. keywordArray.All(keyword => fullString.Client.Contains(keyword)))
  966. .Select(x => new
  967. {
  968. x.Client,
  969. x.Id
  970. })
  971. .DistinctBy(x => x.Client)
  972. .ToList();
  973. if (matches.Any())
  974. {
  975. //await RedisFactory.CreateRedisRepository().HashSetAsync(keyName, dto.Keyword, matches);
  976. //await RedisFactory.CreateRedisRepository().KeyExpireInAsync(keyName, new TimeSpan(0,1,0));
  977. jw.Data = matches;
  978. }
  979. else
  980. {
  981. jw.Code = 201;
  982. jw.Msg = "empty list";
  983. jw.Data = new List<string>(1);
  984. }
  985. }
  986. else
  987. {
  988. jw.Data = await RedisFactory.CreateRedisRepository().HashGetAsync<List<string>>(keyName, dto.Keyword);
  989. }
  990. return Ok(jw);
  991. }
  992. [HttpPost]
  993. public async Task<IActionResult> QueryUnlockDropList()
  994. {
  995. var jw = JsonView(true);
  996. var userList = await _sqlSugar.Queryable<Sys_SetData>().FirstAsync
  997. (x => x.Id == 1434 && x.IsDel == 0);
  998. try
  999. {
  1000. if (userList != null)
  1001. {
  1002. var result = JsonConvert.DeserializeObject<List<int>>(userList.Remark);
  1003. jw.Data = result;
  1004. }
  1005. }
  1006. catch (Exception)
  1007. {
  1008. jw.Code = 500;
  1009. jw.Msg = "数据源错误!";
  1010. jw.Data = new List<int>(1);
  1011. }
  1012. return Ok(jw);
  1013. }
  1014. [HttpPost]
  1015. public IActionResult InsertDataExcel(InsertDataExcelDto dto)
  1016. {
  1017. //string client = dto.Client;
  1018. int toUser = dto.ToUser;
  1019. var clietnArr = dto.Client;
  1020. var count = 0;
  1021. //var notClentUser = new List<int>() { 21, 95 , 327 };
  1022. var notClentUser = new List<int>() { 213, 327 };
  1023. var insertList = new List<Crm_ClientDataAndUser>();
  1024. if (clietnArr.Count > 0)
  1025. {
  1026. _sqlSugar.BeginTran();
  1027. //检索相关客户数据
  1028. var newClientArr = _sqlSugar.Queryable<Crm_NewClientData>()
  1029. .Where(x => x.IsDel == 0)
  1030. .Select(x => new Crm_NewClientData
  1031. {
  1032. Client = x.Client,
  1033. Id = x.Id
  1034. })
  1035. .ToList()
  1036. .Select(x => new
  1037. {
  1038. Client = AesEncryptionHelper.Decrypt(x.Client),
  1039. Id = x.Id
  1040. });
  1041. foreach (var item in clietnArr)
  1042. {
  1043. var searchClient = newClientArr.Where(x => x.Client == item).ToList();
  1044. foreach (var clientData in searchClient)
  1045. {
  1046. var clientDataAndUser = _sqlSugar.Queryable<Crm_ClientDataAndUser>()
  1047. .Where(x => x.NewClientDataId == clientData.Id && x.IsDel == 0)
  1048. .ToList();
  1049. if (clientDataAndUser.Count > 0)
  1050. {
  1051. //清除关联表数据
  1052. _sqlSugar.Updateable<Crm_ClientDataAndUser>()
  1053. .Where(x => !notClentUser.Contains(x.usersId)
  1054. && x.NewClientDataId == clientData.Id && x.IsDel == 0)
  1055. .SetColumns(x => new Crm_ClientDataAndUser
  1056. {
  1057. IsDel = 1,
  1058. DeleteTime = DateTime.Now.ToString(),
  1059. DeleteUserId = 235
  1060. })
  1061. .ExecuteCommand();
  1062. }
  1063. insertList.Add(new Crm_ClientDataAndUser
  1064. {
  1065. usersId = toUser,
  1066. NewClientDataId = clientData.Id,
  1067. CreateTime = DateTime.Now,
  1068. CreateUserId = 235,
  1069. });
  1070. }
  1071. }
  1072. count = _sqlSugar.Insertable(insertList).ExecuteCommand();
  1073. _sqlSugar.CommitTran();
  1074. }
  1075. return Ok(new
  1076. {
  1077. count,
  1078. });
  1079. }
  1080. /// <summary>
  1081. /// 客户资料分配(按照数据ID) - UserId
  1082. /// </summary>
  1083. /// <param name="file"></param>
  1084. /// <returns></returns>
  1085. [HttpPost]
  1086. public IActionResult InsertDataExcel1(IFormFile file)
  1087. {
  1088. // 检查文件是否为空
  1089. if (file == null || file.Length == 0)
  1090. {
  1091. return BadRequest("No file uploaded.");
  1092. }
  1093. // 保存文件到服务器
  1094. var uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), "File");
  1095. if (!Directory.Exists(uploadsFolder))
  1096. {
  1097. Directory.CreateDirectory(uploadsFolder);
  1098. }
  1099. var filePath = Path.Combine(uploadsFolder, file.FileName);
  1100. using (var stream = new FileStream(filePath, FileMode.Create))
  1101. {
  1102. file.CopyToAsync(stream);
  1103. }
  1104. Workbook workbook = new Workbook(filePath);
  1105. // 获取第一个工作表
  1106. Worksheet worksheet = workbook.Worksheets[0];
  1107. // 获取表头(第一行作为列名)
  1108. int headerRowIndex = 0; // 假设第一行是表头
  1109. Row headerRow = worksheet.Cells.Rows[headerRowIndex];
  1110. int colCount = worksheet.Cells.MaxDataColumn + 1;
  1111. // 动态存储列名
  1112. var clients = new List<Crm_ClientDataAndUser>();
  1113. // 遍历数据行(从第二行开始)
  1114. int rowCount = worksheet.Cells.MaxDataRow + 1;
  1115. for (int row = headerRowIndex + 1; row < rowCount; row++)
  1116. {
  1117. var cellVal1 = worksheet.Cells[row, 0].Value; //序号
  1118. var cellVal2 = worksheet.Cells[row, 1].Value; //数据Id
  1119. var cellVal3 = worksheet.Cells[row, 2].Value; //userId
  1120. int parentId = !string.IsNullOrEmpty(cellVal2?.ToString()) ? int.Parse(cellVal2?.ToString()) : 0;
  1121. int userId = !string.IsNullOrEmpty(cellVal3?.ToString()) ? int.Parse(cellVal3?.ToString()) : 0;
  1122. var rowData = new Crm_ClientDataAndUser()
  1123. {
  1124. usersId = userId,
  1125. NewClientDataId = parentId
  1126. };
  1127. clients.Add(rowData); // 将当前行数据添加到集合中
  1128. }
  1129. var count = 0;
  1130. //var notClentUser = new List<int>() { 21, 95 , 327 };
  1131. var notClentUser = new List<int>() { 21, 95, 327, 213 };
  1132. var insertList = new List<Crm_ClientDataAndUser>();
  1133. if (clients.Count > 0)
  1134. {
  1135. _sqlSugar.BeginTran();
  1136. //检索相关客户数据
  1137. var newClientArr = _sqlSugar.Queryable<Crm_NewClientData>()
  1138. .Where(x => x.IsDel == 0)
  1139. .Select(x => new Crm_NewClientData
  1140. {
  1141. Client = x.Client,
  1142. Id = x.Id
  1143. })
  1144. .ToList();
  1145. foreach (var item in clients)
  1146. {
  1147. var searchClient = newClientArr.Where(x => x.Id == item.NewClientDataId).ToList();
  1148. foreach (var clientData in searchClient)
  1149. {
  1150. var clientDataAndUser = _sqlSugar.Queryable<Crm_ClientDataAndUser>()
  1151. .Where(x => x.NewClientDataId == clientData.Id && x.IsDel == 0)
  1152. .ToList();
  1153. if (clientDataAndUser.Count > 0)
  1154. {
  1155. //清除关联表数据
  1156. _sqlSugar.Updateable<Crm_ClientDataAndUser>()
  1157. .Where(x => !notClentUser.Contains(x.usersId)
  1158. && x.NewClientDataId == clientData.Id && x.IsDel == 0)
  1159. .SetColumns(x => new Crm_ClientDataAndUser
  1160. {
  1161. IsDel = 1,
  1162. DeleteTime = DateTime.Now.ToString(),
  1163. DeleteUserId = 235
  1164. })
  1165. .ExecuteCommand();
  1166. }
  1167. insertList.Add(new Crm_ClientDataAndUser
  1168. {
  1169. usersId = item.usersId,
  1170. NewClientDataId = clientData.Id,
  1171. CreateTime = DateTime.Now,
  1172. CreateUserId = 235,
  1173. });
  1174. }
  1175. }
  1176. count = _sqlSugar.Insertable(insertList).ExecuteCommand();
  1177. _sqlSugar.CommitTran();
  1178. }
  1179. return Ok(new
  1180. {
  1181. count,
  1182. });
  1183. }
  1184. [HttpPost]
  1185. public IActionResult SchoolAllocation()
  1186. {
  1187. var clients = _sqlSugar.Queryable<Crm_NewClientData>()
  1188. .Where(x => x.IsDel == 0)
  1189. .Select(x => new Crm_NewClientData { Id = x.Id, Client = x.Client })
  1190. .ToList();
  1191. var keys = new List<string>()
  1192. {
  1193. "大学", "学院", "中学", "教育", "学校", "一中", "二中", "七中", "十五中", "二十五中","高校"
  1194. };
  1195. var count = 0;
  1196. var insertArr = new List<Crm_ClientDataAndBusiness>();
  1197. // --368 教育
  1198. foreach (var item in clients)
  1199. {
  1200. EncryptionProcessor.DecryptProperties(item);
  1201. foreach (var key in keys)
  1202. {
  1203. if (!string.IsNullOrWhiteSpace(item.Client) && item.Client.Contains(key))
  1204. {
  1205. insertArr.Add(new Crm_ClientDataAndBusiness
  1206. {
  1207. NewClientDataId = item.Id,
  1208. SetDataId = 368, // 教育
  1209. CreateTime = DateTime.Now,
  1210. CreateUserId = 235,
  1211. IsDel = 0
  1212. });
  1213. }
  1214. }
  1215. }
  1216. count = _sqlSugar.Insertable(insertArr)
  1217. .ExecuteCommand();
  1218. return count > 0 ? Ok(JsonView(true, "分配成功!", count)) : Ok(JsonView(false, "分配失败!"));
  1219. }
  1220. #region 回滚数据记录
  1221. //[HttpPost]
  1222. //public async Task<IActionResult> actionResult()
  1223. //{
  1224. // var jw = JsonView(true);
  1225. // var sql = @" SELECT * FROM OA2023DB.dbo.Crm_TableOperationRecord
  1226. // WHERE TableName ='Crm_NewClientData'
  1227. // AND OperationItem IN(4) AND CreateTime > '2025-02-03' AND CreateTime < '2025-03-06'
  1228. // AND ReturnResult LIKE '%修改成功%' ";
  1229. // var list = _sqlSugar.SqlQueryable<Crm_TableOperationRecord>(sql).Select(x=>new Crm_TableOperationRecord
  1230. // {
  1231. // RequestParam = x.RequestParam
  1232. // }).ToList();
  1233. // foreach (var item in list)
  1234. // {
  1235. // var string1 = item.RequestParam.Trim('"').Replace("\\", "");
  1236. // //if (!string.IsNullOrEmpty(item.RequestParam))
  1237. // //{
  1238. // // var req = item.RequestParam;
  1239. // // if (CommonFun.IsValidJson(item.RequestParam))
  1240. // // {
  1241. // // var res1 = JToken.Parse(item.RequestParam);
  1242. // // }
  1243. // // NewClientOpDto dto = JsonConvert.DeserializeObject<NewClientOpDto>(JsonConvert.DeserializeObject<string>(item.RequestParam));
  1244. // NewClientOpDto dto1 = JsonConvert.DeserializeObject<NewClientOpDto>(string1);
  1245. // var result = await this.NewClientOp(dto1);
  1246. // //}
  1247. // }
  1248. // return Ok(jw);
  1249. //}
  1250. #endregion
  1251. }
  1252. }