颐和api

Call_RecordsRepository.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using MadRunFabric.Common;
  2. using MadRunFabric.Common.Options;
  3. using MadRunFabric.Model;
  4. using MadRunFabric.Model.CallCenterApi;
  5. using Microsoft.Extensions.Logging;
  6. using Microsoft.Extensions.Options;
  7. using MongoDB.Bson;
  8. using MongoDB.Driver;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using WechatApi.IRepositories;
  15. namespace WechatApi.Repositories
  16. {
  17. public class Call_RecordsRepository : BaseRepository<mw_call_records, string>, ICall_RecordsRepository
  18. {
  19. protected readonly ILogger<BaseRepository<mw_call_records, string>> _logger;
  20. protected readonly IMongoCollection<mw_call_records> _collection_mw_call_records;
  21. protected readonly IMongoCollection<Sys_User_Account> _collection_sys_user_account;
  22. protected readonly IMongoCollection<MW_Agent_State_Record> _collection_mw_agent_state_record;
  23. public Call_RecordsRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<mw_call_records, string>> logger) : base(settings, logger)
  24. {
  25. _logger = logger;
  26. _collection_mw_call_records = _context.GetCollection<mw_call_records>();
  27. _collection_sys_user_account = _context.GetCollection<Sys_User_Account>();
  28. }
  29. /// <summary>
  30. /// 获取坐席总数
  31. /// </summary>
  32. /// <returns></returns>
  33. public int GetUserCount()
  34. {
  35. var users = from sysuser in _collection_sys_user_account.AsQueryable()
  36. where sysuser.delete_flag == false
  37. select sysuser;
  38. return users.Count();
  39. }
  40. /// <summary>
  41. /// 获取通话量
  42. /// </summary>
  43. /// <param name="agentcode">坐席工号</param>
  44. /// <param name="start">开始时间</param>
  45. /// <param name="end">结束时间</param>
  46. /// <param name="tag">呼叫类型</param>
  47. /// <param name="state">呼叫状态</param>
  48. public object GetHighChartData(string agentcode, string start, string end, string tag, string state)
  49. {
  50. #region 条件搜索
  51. string bxwhere = "";
  52. if (!string.IsNullOrEmpty(tag))
  53. {
  54. if (bxwhere != "")
  55. {
  56. bxwhere = bxwhere + ",{calltype:" + tag + "}";
  57. }
  58. else
  59. {
  60. bxwhere = "{calltype:" + tag + "}";
  61. }
  62. }
  63. if (!string.IsNullOrEmpty(state))
  64. {
  65. if (bxwhere != "")
  66. {
  67. bxwhere = bxwhere + ",{callstate:" + state + "}";
  68. }
  69. else
  70. {
  71. bxwhere = "{callstate:" + state + "}";
  72. }
  73. }
  74. if (!string.IsNullOrEmpty(agentcode))
  75. {
  76. if (bxwhere != "")
  77. {
  78. bxwhere = bxwhere + ",{agent_code:'" + agentcode + "'}";
  79. }
  80. else
  81. {
  82. bxwhere = "{agent_code:'" + agentcode + "'}";
  83. }
  84. }
  85. if (!string.IsNullOrEmpty(start))
  86. {
  87. if (bxwhere != "")
  88. {
  89. bxwhere = bxwhere + ",{begintime:{$gte:new Date('" + start + "')}}";
  90. }
  91. else
  92. {
  93. bxwhere = "{begintime:{$gte:new Date('" + start + "')}}";
  94. }
  95. }
  96. if (!string.IsNullOrEmpty(end))
  97. {
  98. if (bxwhere != "")
  99. {
  100. bxwhere = bxwhere + ",{begintime:{$lte:new Date('" + end + "')}}";
  101. }
  102. else
  103. {
  104. bxwhere = "{begintime:{$lte:new Date('" + end + "')}}";
  105. }
  106. }
  107. #endregion
  108. var bxstages = new List<IPipelineStageDefinition>();
  109. bxstages.Add(new JsonPipelineStageDefinition<mw_call_records, BsonDocument>("{$match:{$and:[" + bxwhere + "]}}"));
  110. bxstages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>("{$group:{_id:\"$id\", count: {$sum: 1}}}"));
  111. var bxpipeline = new PipelineStagePipelineDefinition<mw_call_records, BsonDocument>(bxstages);
  112. var bxresult = _collection_mw_call_records.Aggregate(bxpipeline).ToList();
  113. if (bxresult.Count > 0)
  114. {
  115. return bxresult[0]["count"];
  116. }
  117. else
  118. { return "0"; }
  119. }
  120. /// <summary>
  121. /// 获取通话时长
  122. /// </summary>
  123. /// <param name="agentcode">坐席工号</param>
  124. /// <param name="start">开始时间</param>
  125. /// <param name="end">结束时间</param>
  126. /// <param name="tag">呼叫类型</param>
  127. /// <param name="state">呼叫状态</param>
  128. public object GetCallRecordTotalTime(string agentcode, string tag, string start, string end, string state)
  129. {
  130. #region 条件搜索
  131. string bxwhere = "";
  132. if (!string.IsNullOrEmpty(tag))
  133. {
  134. if (bxwhere != "")
  135. {
  136. bxwhere = bxwhere + ",{calltype:" + tag + "}";
  137. }
  138. else
  139. {
  140. bxwhere = "{calltype:" + tag + "}";
  141. }
  142. }
  143. if (!string.IsNullOrEmpty(state))
  144. {
  145. if (bxwhere != "")
  146. {
  147. bxwhere = bxwhere + ",{callstate:" + state + "}";
  148. }
  149. else
  150. {
  151. bxwhere = "{callstate:" + state + "}";
  152. }
  153. }
  154. if (!string.IsNullOrEmpty(agentcode))
  155. {
  156. if (bxwhere != "")
  157. {
  158. bxwhere = bxwhere + ",{agent_code:'" + agentcode + "'}";
  159. }
  160. else
  161. {
  162. bxwhere = "{agent_code:'" + agentcode + "'}";
  163. }
  164. }
  165. if (!string.IsNullOrEmpty(start))
  166. {
  167. if (bxwhere != "")
  168. {
  169. bxwhere = bxwhere + ",{begintime:{$gte:new Date('" + start + "')}}";
  170. }
  171. else
  172. {
  173. bxwhere = "{begintime:{$gte:new Date('" + start + "')}}";
  174. }
  175. }
  176. if (!string.IsNullOrEmpty(end))
  177. {
  178. if (bxwhere != "")
  179. {
  180. bxwhere = bxwhere + ",{begintime:{$lte:new Date('" + end + "')}}";
  181. }
  182. else
  183. {
  184. bxwhere = "{begintime:{$lte:new Date('" + end + "')}}";
  185. }
  186. }
  187. #endregion
  188. var bxstages = new List<IPipelineStageDefinition>();
  189. bxstages.Add(new JsonPipelineStageDefinition<mw_call_records, BsonDocument>("{$match:{$and:[" + bxwhere + "]}}"));//agent_code
  190. bxstages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>("{$group:{_id:\"$id\", count: {$sum: 1},totalcount:{$sum:\"$longs_talk\"}}}"));
  191. var bxpipeline = new PipelineStagePipelineDefinition<mw_call_records, BsonDocument>(bxstages);
  192. var bxresult = _collection_mw_call_records.Aggregate(bxpipeline).ToList();
  193. if (bxresult.Count > 0)
  194. {
  195. return bxresult[0]["count"] + "," + bxresult[0]["totalcount"];
  196. }
  197. else
  198. { return "0,0"; }
  199. }
  200. /// <summary>
  201. /// 获取日周月年话务数据24小时统计
  202. /// </summary>
  203. /// <param name="stime"></param>
  204. /// <param name="etime"></param>
  205. /// <returns></returns>
  206. public object GetHW24CountReport(string agentcode, string stime, string etime)
  207. {
  208. int[] hours = Enumerable.Range(0, 24).ToArray<int>();
  209. int[] counts = new int[24];
  210. int[] yjrcounts = new int[24];
  211. int[] wjrcounts = new int[24];
  212. var query = from p in _collection.AsQueryable() where 1 == 1 select p;
  213. if (!string.IsNullOrEmpty(agentcode))
  214. query = query.Where(it => it.agent_code.Equals(agentcode));
  215. if (!string.IsNullOrEmpty(stime))
  216. query = query.Where(it => it.begintime >= Convert.ToDateTime(stime));
  217. if (!string.IsNullOrEmpty(etime))
  218. query = query.Where(it => it.begintime <= Convert.ToDateTime(etime));
  219. var list = query.ToList();
  220. for (int i = 0; i < hours.Length; i++)
  221. {
  222. counts[i] = list.Where(it => it.begintime.Hour == i).Count();
  223. }
  224. #region 已接入
  225. var query1 = from p in _collection.AsQueryable() where p.callstate == 1 select p;
  226. if (!string.IsNullOrEmpty(agentcode))
  227. query1 = query1.Where(it => it.agent_code.Equals(agentcode));
  228. if (!string.IsNullOrEmpty(stime))
  229. query1 = query1.Where(it => it.begintime >= Convert.ToDateTime(stime));
  230. if (!string.IsNullOrEmpty(etime))
  231. query1 = query1.Where(it => it.begintime <= Convert.ToDateTime(etime));
  232. var list1 = query1.ToList();
  233. for (int i = 0; i < hours.Length; i++)
  234. {
  235. yjrcounts[i] = list1.Where(it => it.begintime.Hour == i).Count();
  236. }
  237. #endregion
  238. #region 未接入
  239. var query2 = from p in _collection.AsQueryable() where p.callstate == 0 select p;
  240. if (!string.IsNullOrEmpty(agentcode))
  241. query2 = query2.Where(it => it.agent_code.Equals(agentcode));
  242. if (!string.IsNullOrEmpty(stime))
  243. query2 = query2.Where(it => it.begintime >= Convert.ToDateTime(stime));
  244. if (!string.IsNullOrEmpty(etime))
  245. query2 = query2.Where(it => it.begintime <= Convert.ToDateTime(etime));
  246. var list2 = query2.ToList();
  247. for (int i = 0; i < hours.Length; i++)
  248. {
  249. wjrcounts[i] = list2.Where(it => it.begintime.Hour == i).Count();
  250. }
  251. #endregion
  252. var result = new
  253. {
  254. hours,
  255. counts,
  256. yjrcounts,
  257. wjrcounts
  258. };
  259. return result;
  260. }
  261. /// <summary>
  262. /// 坐席状态统计
  263. /// </summary>
  264. /// <param name="agentcode"></param>
  265. /// <param name="start"></param>
  266. /// <param name="end"></param>
  267. /// <returns></returns>
  268. public object GetAgentState(string agentcode, string start, string end)
  269. {
  270. #region 条件搜索
  271. string bxwhere = "";
  272. if (!string.IsNullOrEmpty(agentcode))
  273. {
  274. if (bxwhere != "")
  275. {
  276. bxwhere = bxwhere + ",{agent_id:" + agentcode + "}";
  277. }
  278. else
  279. {
  280. bxwhere = "{agent_id:" + agentcode + "}";
  281. }
  282. }
  283. if (!string.IsNullOrEmpty(start))
  284. {
  285. if (bxwhere != "")
  286. {
  287. bxwhere = bxwhere + ",{time_login:{$gte:new Date('" + start + "')}}";
  288. }
  289. else
  290. {
  291. bxwhere = "{time_login:{$gte:new Date('" + start + "')}}";
  292. }
  293. }
  294. if (!string.IsNullOrEmpty(end))
  295. {
  296. if (bxwhere != "")
  297. {
  298. bxwhere = bxwhere + ",{time_login:{$lte:new Date('" + end + "')}}";
  299. }
  300. else
  301. {
  302. bxwhere = "{time_login:{$lte:new Date('" + end + "')}}";
  303. }
  304. }
  305. #endregion
  306. var bxstages = new List<IPipelineStageDefinition>();
  307. bxstages.Add(new JsonPipelineStageDefinition<MW_Agent_State_Record, BsonDocument>("{$match:{$and:[" + bxwhere + "]}}"));//agent_code
  308. bxstages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>("{$group:{_id:\"$id\", logintimes:{$sum:\"$login_times\"}, freetimes:{$sum:\"free_times\"}, reposetimes:{$sum:\"repose_times\"}, talktimes:{$sum:\"talk_times\"}, reposenum:{$sum:\"repose_num\"}, answernum:{$sum:\"answer_num\"}}}"));
  309. var bxpipeline = new PipelineStagePipelineDefinition<MW_Agent_State_Record, BsonDocument>(bxstages);
  310. var bxresult = _collection_mw_agent_state_record.Aggregate(bxpipeline).ToList();
  311. if (bxresult.Count > 0)
  312. {
  313. var obj = new {
  314. logintimes = bxresult[0]["logintimes"],
  315. freetimes = bxresult[0]["freetimes"],
  316. reposetimes = bxresult[0]["reposetimes"],
  317. talktimes = bxresult[0]["talktimes"],
  318. reposenum = bxresult[0]["reposenum"],
  319. answernum = bxresult[0]["answernum"],
  320. };
  321. return obj;
  322. }
  323. else
  324. { return null; }
  325. }
  326. #region
  327. ///// <summary>
  328. ///// 获取24小时通话情况
  329. ///// </summary>
  330. ///// <param name="agentcode">坐席工号</param>
  331. ///// <param name="start">开始时间</param>
  332. ///// <param name="end">结束时间</param>
  333. ///// <param name="tag">呼叫类型</param>
  334. ///// <param name="state">呼叫状态</param>
  335. //public object Get24HourCall(string agentcode, string tag, string start, string end, string state)
  336. //{
  337. // #region 条件搜索
  338. // string bxwhere = "";
  339. // if (!string.IsNullOrEmpty(tag))
  340. // {
  341. // if (bxwhere != "")
  342. // {
  343. // bxwhere = bxwhere + ",{calltype:" + tag + "}";
  344. // }
  345. // else
  346. // {
  347. // bxwhere = "{calltype:" + tag + "}";
  348. // }
  349. // }
  350. // if (!string.IsNullOrEmpty(state))
  351. // {
  352. // if (bxwhere != "")
  353. // {
  354. // bxwhere = bxwhere + ",{callstate:" + state + "}";
  355. // }
  356. // else
  357. // {
  358. // bxwhere = "{callstate:" + state + "}";
  359. // }
  360. // }
  361. // if (!string.IsNullOrEmpty(agentcode))
  362. // {
  363. // if (bxwhere != "")
  364. // {
  365. // bxwhere = bxwhere + ",{agent_code:'" + agentcode + "'}";
  366. // }
  367. // else
  368. // {
  369. // bxwhere = "{agent_code:'" + agentcode + "'}";
  370. // }
  371. // }
  372. // if (!string.IsNullOrEmpty(start))
  373. // {
  374. // if (bxwhere != "")
  375. // {
  376. // bxwhere = bxwhere + ",{begintime:{$gte:new Date('" + start + "')}}";
  377. // }
  378. // else
  379. // {
  380. // bxwhere = "{begintime:{$gte:new Date('" + start + "')}}";
  381. // }
  382. // }
  383. // if (!string.IsNullOrEmpty(end))
  384. // {
  385. // if (bxwhere != "")
  386. // {
  387. // bxwhere = bxwhere + ",{begintime:{$lte:new Date('" + end + "')}}";
  388. // }
  389. // else
  390. // {
  391. // bxwhere = "{begintime:{$lte:new Date('" + end + "')}}";
  392. // }
  393. // }
  394. // #endregion
  395. // var bxstages = new List<IPipelineStageDefinition>();
  396. // bxstages.Add(new JsonPipelineStageDefinition<mw_call_records, BsonDocument>("{$match:{$and:[" + bxwhere + "]}}"));//agent_code
  397. // bxstages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>("{$group:{_id:{$hour: {$add:[\"begintime\",28800000]} }, count: {$sum: 1},longs:{$sum:\"$longs_talk\"}}}"));
  398. // var bxpipeline = new PipelineStagePipelineDefinition<mw_call_records, BsonDocument>(bxstages);
  399. // var bxresult = _collection_mw_call_records.Aggregate(bxpipeline).ToList();
  400. // int[] hours = Enumerable.Range(0, 24).ToArray<int>();
  401. // string[] counts = new string[24];//数量
  402. // string[] longs = new string[24];//时长
  403. // for (int i = 0; i < hours.Length; i++)
  404. // {
  405. // counts[i] = "0";
  406. // longs[i] = "0";
  407. // var bx = bxresult.Where(p => p.Values.ToArray()[0].ToString() == i.ToString()).FirstOrDefault();
  408. // if (bx != null)
  409. // {
  410. // counts[i] = bx.Values.ToArray()[1].ToString();
  411. // longs[i] = bx.Values.ToArray()[2].ToString();
  412. // }
  413. // }
  414. // var obj = new
  415. // {
  416. // hours,
  417. // counts,
  418. // longs,
  419. // };
  420. // return obj;
  421. //}
  422. #endregion
  423. }
  424. }