CompanyStatisticsController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package com.fs.company.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fs.common.core.controller.BaseController;
  4. import com.fs.common.core.domain.AjaxResult;
  5. import com.fs.common.core.domain.R;
  6. import com.fs.common.utils.ServletUtils;
  7. import com.fs.common.utils.StringUtils;
  8. import com.fs.common.utils.TimeUtils;
  9. import com.fs.common.utils.poi.ExcelUtil;
  10. import com.fs.company.domain.CompanyUser;
  11. import com.fs.company.param.CompanyStatisticsParam;
  12. import com.fs.company.service.ICompanySmsLogsService;
  13. import com.fs.company.service.ICompanyUserService;
  14. import com.fs.company.service.ICompanyVoiceLogsService;
  15. import com.fs.company.vo.CompanyPackageOrderStatisticsVO;
  16. import com.fs.company.vo.CompanySmsLogsStatisticsVO;
  17. import com.fs.company.vo.CompanyVoiceLogsStatisticsVO;
  18. import com.fs.core.security.LoginUser;
  19. import com.fs.core.web.service.TokenService;
  20. import com.fs.crm.service.ICrmCustomerUserService;
  21. import com.fs.crm.service.ICrmCustomerVisitService;
  22. import com.fs.crm.vo.*;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.security.access.prepost.PreAuthorize;
  25. import org.springframework.web.bind.annotation.GetMapping;
  26. import org.springframework.web.bind.annotation.RequestMapping;
  27. import org.springframework.web.bind.annotation.RestController;
  28. import java.math.BigDecimal;
  29. import java.text.DecimalFormat;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import java.util.stream.Collectors;
  33. /**
  34. * 统计
  35. *
  36. * @author fs
  37. * @date 2021-03-22
  38. */
  39. @RestController
  40. @RequestMapping("/company/statistics")
  41. public class CompanyStatisticsController extends BaseController
  42. {
  43. @Autowired
  44. private ICompanyUserService userService;
  45. @Autowired
  46. private ICompanyVoiceLogsService voiceLogsService;
  47. @Autowired
  48. private ICompanySmsLogsService smsLogsService;
  49. @Autowired
  50. private TokenService tokenService;
  51. @PreAuthorize("@ss.hasPermi('company:statistics:voiceLogs')")
  52. @GetMapping("/voiceLogs")
  53. public R voiceLogs(CompanyStatisticsParam param)
  54. {
  55. if(StringUtils.isNotEmpty(param.getUserIds())){
  56. String[] userIds=param.getUserIds().split(",");
  57. Long[] ids=new Long[userIds.length];
  58. for(int i=0;i<ids.length; i++){
  59. ids[i]=Long.parseLong(userIds[i]);
  60. }
  61. param.setUsers(ids);
  62. }
  63. else{
  64. //获取部门下的所有用户
  65. CompanyUser usermap=new CompanyUser();
  66. usermap.setDeptId(param.getDeptId());
  67. List<CompanyUser> users = userService.getUserListByDeptId(usermap);
  68. List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
  69. param.setUsers(userIds.toArray(new Long[userIds.size()]));
  70. }
  71. if(param.getUsers()!=null&&param.getUsers().length>0){
  72. List<CompanyVoiceLogsStatisticsVO> list= voiceLogsService.selectVoiceLogsStatisticsList(param);
  73. if(list!=null){
  74. for(CompanyVoiceLogsStatisticsVO vo:list){
  75. double f1 = new BigDecimal((float)vo.getCallSuccessCount()/vo.getCallCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()*100;
  76. vo.setCallRate(f1);
  77. }
  78. }
  79. TimeUtils.TimeEntity timeEntity=TimeUtils.parseTime(param.getType().toString(),param.getStartTime(),param.getEndTime());
  80. timeEntity.setUserIds(param.getUsers());
  81. Integer cycleNum = timeEntity.getCycleNum();
  82. Integer beginTime = timeEntity.getBeginTime();
  83. List<Integer> timeList = new ArrayList<>();
  84. for (int i = 1; i <= cycleNum; i++) {
  85. timeList.add(beginTime);
  86. beginTime = TimeUtils.formatTime(beginTime);
  87. }
  88. List<JSONObject> jsonObjectList = voiceLogsService.selectVoiceLogsTotalCount(timeEntity.toMap());
  89. List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
  90. List<Integer> callCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("callCount")).collect(Collectors.toList());
  91. List<Integer> callSuccessCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("callSuccessCount")).collect(Collectors.toList());
  92. List<Integer> times = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("times")).collect(Collectors.toList());
  93. List<Integer> billingTime = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("billingTime")).collect(Collectors.toList());
  94. return R.ok().put("list",list).put("dates",dates).put("callCount",callCount).put("callSuccessCount",callSuccessCount).put("times",times).put("billingTime",billingTime);
  95. }
  96. else {
  97. return R.ok("未查找到数据");
  98. }
  99. }
  100. @PreAuthorize("@ss.hasPermi('company:statistics:exportVoiceLogs')")
  101. @GetMapping("/exportVoiceLogs")
  102. public AjaxResult exportVoiceLogs(CompanyStatisticsParam param)
  103. {
  104. if(StringUtils.isNotEmpty(param.getUserIds())){
  105. String[] userIds=param.getUserIds().split(",");
  106. Long[] ids=new Long[userIds.length];
  107. for(int i=0;i<ids.length; i++){
  108. ids[i]=Long.parseLong(userIds[i]);
  109. }
  110. param.setUsers(ids);
  111. }
  112. else{
  113. //获取所有员工
  114. CompanyUser usermap=new CompanyUser();
  115. usermap.setDeptId(param.getDeptId());
  116. List<CompanyUser> users = userService.getUserListByDeptId(usermap);
  117. List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
  118. param.setUsers(userIds.toArray(new Long[userIds.size()]));
  119. }
  120. List<CompanyVoiceLogsStatisticsVO> list= voiceLogsService.selectVoiceLogsStatisticsList(param);
  121. if(list!=null){
  122. for(CompanyVoiceLogsStatisticsVO vo:list){
  123. double f1 = new BigDecimal((float)vo.getCallSuccessCount()/vo.getCallCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()*100;
  124. vo.setCallRate(f1);
  125. }
  126. }
  127. ExcelUtil<CompanyVoiceLogsStatisticsVO> util = new ExcelUtil<CompanyVoiceLogsStatisticsVO>(CompanyVoiceLogsStatisticsVO.class);
  128. return util.exportExcel(list, "voiceLogs");
  129. }
  130. @PreAuthorize("@ss.hasPermi('company:statistics:myVoiceLogs')")
  131. @GetMapping("/myVoiceLogs")
  132. public R myVoiceLogs(CompanyStatisticsParam param)
  133. {
  134. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  135. param.setUsers(new Long[]{loginUser.getUser().getUserId()});
  136. if(param.getUsers()!=null&&param.getUsers().length>0){
  137. List<CompanyVoiceLogsStatisticsVO> list= voiceLogsService.selectVoiceLogsStatisticsList(param);
  138. if(list!=null){
  139. for(CompanyVoiceLogsStatisticsVO vo:list){
  140. double f1 = new BigDecimal((float)vo.getCallSuccessCount()/vo.getCallCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()*100;
  141. vo.setCallRate(f1);
  142. }
  143. }
  144. TimeUtils.TimeEntity timeEntity=TimeUtils.parseTime(param.getType().toString(),param.getStartTime(),param.getEndTime());
  145. timeEntity.setUserIds(param.getUsers());
  146. Integer cycleNum = timeEntity.getCycleNum();
  147. Integer beginTime = timeEntity.getBeginTime();
  148. List<Integer> timeList = new ArrayList<>();
  149. for (int i = 1; i <= cycleNum; i++) {
  150. timeList.add(beginTime);
  151. beginTime = TimeUtils.formatTime(beginTime);
  152. }
  153. List<JSONObject> jsonObjectList = voiceLogsService.selectVoiceLogsTotalCount(timeEntity.toMap());
  154. List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
  155. List<Integer> callCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("callCount")).collect(Collectors.toList());
  156. List<Integer> callSuccessCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("callSuccessCount")).collect(Collectors.toList());
  157. List<Integer> times = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("times")).collect(Collectors.toList());
  158. List<Integer> billingTime = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("billingTime")).collect(Collectors.toList());
  159. return R.ok().put("list",list).put("dates",dates).put("callCount",callCount).put("callSuccessCount",callSuccessCount).put("times",times).put("billingTime",billingTime);
  160. }
  161. else {
  162. return R.ok("未查找到数据");
  163. }
  164. }
  165. @PreAuthorize("@ss.hasPermi('company:statistics:exportMyVoiceLogs')")
  166. @GetMapping("/exportMyVoiceLogs")
  167. public AjaxResult exportMyVoiceLogs(CompanyStatisticsParam param)
  168. {
  169. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  170. param.setUsers(new Long[]{loginUser.getUser().getUserId()});
  171. List<CompanyVoiceLogsStatisticsVO> list= voiceLogsService.selectVoiceLogsStatisticsList(param);
  172. if(list!=null){
  173. for(CompanyVoiceLogsStatisticsVO vo:list){
  174. double f1 = new BigDecimal((float)vo.getCallSuccessCount()/vo.getCallCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()*100;
  175. vo.setCallRate(f1);
  176. }
  177. }
  178. ExcelUtil<CompanyVoiceLogsStatisticsVO> util = new ExcelUtil<CompanyVoiceLogsStatisticsVO>(CompanyVoiceLogsStatisticsVO.class);
  179. return util.exportExcel(list, "voiceLogs");
  180. }
  181. @PreAuthorize("@ss.hasPermi('company:statistics:smsLogs')")
  182. @GetMapping("/smsLogs")
  183. public R smsLogs(CompanyStatisticsParam param)
  184. {
  185. if(StringUtils.isNotEmpty(param.getUserIds())){
  186. String[] userIds=param.getUserIds().split(",");
  187. Long[] ids=new Long[userIds.length];
  188. for(int i=0;i<ids.length; i++){
  189. ids[i]=Long.parseLong(userIds[i]);
  190. }
  191. param.setUsers(ids);
  192. }
  193. else{
  194. //获取部门下的所有用户
  195. CompanyUser usermap=new CompanyUser();
  196. usermap.setDeptId(param.getDeptId());
  197. List<CompanyUser> users = userService.getUserListByDeptId(usermap);
  198. List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
  199. param.setUsers(userIds.toArray(new Long[userIds.size()]));
  200. }
  201. if(param.getUsers()!=null&&param.getUsers().length>0){
  202. List<CompanySmsLogsStatisticsVO> list= smsLogsService.selectSmsLogsStatisticsList(param);
  203. TimeUtils.TimeEntity timeEntity=TimeUtils.parseTime(param.getType().toString(),param.getStartTime(),param.getEndTime());
  204. timeEntity.setUserIds(param.getUsers());
  205. Integer cycleNum = timeEntity.getCycleNum();
  206. Integer beginTime = timeEntity.getBeginTime();
  207. List<Integer> timeList = new ArrayList<>();
  208. for (int i = 1; i <= cycleNum; i++) {
  209. timeList.add(beginTime);
  210. beginTime = TimeUtils.formatTime(beginTime);
  211. }
  212. List<JSONObject> jsonObjectList = smsLogsService.selectSmsLogsCounts(timeEntity.toMap());
  213. List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
  214. List<Integer> smsCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("smsCount")).collect(Collectors.toList());
  215. List<Integer> successCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("successCount")).collect(Collectors.toList());
  216. return R.ok().put("list",list).put("dates",dates).put("smsCount",smsCount).put("successCount",successCount);
  217. }
  218. else {
  219. return R.ok("未查找到数据");
  220. }
  221. }
  222. @PreAuthorize("@ss.hasPermi('company:statistics:exportSmsLogs')")
  223. @GetMapping("/exportSmsLogs")
  224. public AjaxResult exportSmsLogs(CompanyStatisticsParam param)
  225. {
  226. if(StringUtils.isNotEmpty(param.getUserIds())){
  227. String[] userIds=param.getUserIds().split(",");
  228. Long[] ids=new Long[userIds.length];
  229. for(int i=0;i<ids.length; i++){
  230. ids[i]=Long.parseLong(userIds[i]);
  231. }
  232. param.setUsers(ids);
  233. }
  234. else{
  235. //获取所有员工
  236. CompanyUser usermap=new CompanyUser();
  237. usermap.setDeptId(param.getDeptId());
  238. List<CompanyUser> users = userService.getUserListByDeptId(usermap);
  239. List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
  240. param.setUsers(userIds.toArray(new Long[userIds.size()]));
  241. }
  242. List<CompanySmsLogsStatisticsVO> list= smsLogsService.selectSmsLogsStatisticsList(param);
  243. ExcelUtil<CompanySmsLogsStatisticsVO> util = new ExcelUtil<CompanySmsLogsStatisticsVO>(CompanySmsLogsStatisticsVO.class);
  244. return util.exportExcel(list, "voiceLogs");
  245. }
  246. }