package com.sf.dangbao.core.controller;import com.alibaba.fastjson.JSONObject;import com.sf.dangbao.core.constant.CommonConstant;import com.sf.dangbao.core.entity.PaperUser;import com.sf.dangbao.core.service.DistributionTaskService;import com.sf.dangbao.core.utils.RoleUtils;import io.swagger.annotations.Api;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import org.water.base.entity.R;import org.water.common.bind.annotation.CurrentUser;import org.water.common.util.StringUtil;import java.util.HashMap;import java.util.List;import java.util.Map;/** * @ClassName SendReportController * @Description 首页数据展示接口 * @Author pxz * @Date 2019/10/11 * @Version 1.0 **/@Controller@RequestMapping("/first/report")@Api(description = "首页报表")public class FirstReportController { private static Logger logger = LogManager.getLogger(FirstReportController.class); @Autowired private DistributionTaskService distributionTaskService; @RequestMapping(value = "/delieverInfo", method = RequestMethod.GET) @ApiOperation(value = "统计任务量、完成率", notes = "统计任务量、完成率", httpMethod = CommonConstant.HTTP_METHOD_GET) @ApiImplicitParams({ @ApiImplicitParam(name = "customerCode", value = "客户编码", paramType = CommonConstant.PARAM_TYPE_QUERY), @ApiImplicitParam(name = "date", value = "查询日期", paramType = CommonConstant.PARAM_TYPE_QUERY), @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = CommonConstant.PARAM_TYPE_QUERY) }) @ResponseBody public R delieverInfo(@RequestParam(required = false) String customerCode, @CurrentUser PaperUser sysUser, @RequestParam(required = false) String date, String endTime) { if(sysUser == null){ return R.error("请登录").setCode(401); } String roleType = RoleUtils.getRoleCustomerCode(sysUser); if(!StringUtil.isEmpty(roleType)){ customerCode = roleType; } // 按客户等级统计 List<JSONObject> result = distributionTaskService.statisticByCusLevel(customerCode,date); // 首页任务量,任务完成率 List<JSONObject> resultList = distributionTaskService.delieverInfo(customerCode,date, endTime); logger.info("任务量、完成率统计" + resultList); Map<String,Object> data = new HashMap<>(6); data.put("one",result); data.put("two", resultList); return R.ok(data); }}——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
package com.sf.dangbao.core.service;import cn.hutool.core.date.DateUtil;import com.alibaba.fastjson.JSONObject;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.sf.dangbao.core.dto.CusLevelAndLoactionDto;import com.sf.dangbao.core.dto.KeyValueDto;import com.sf.dangbao.core.dto.StatusQueryDto;import com.sf.dangbao.core.entity.*;import com.sf.dangbao.core.mapper.CustomerMapper;import com.sf.dangbao.core.mapper.DistributionTaskMapper;import com.sf.dangbao.core.mapper.EmpDistributeMapper;import org.apache.commons.collections.CollectionUtils;import org.apache.commons.lang.StringUtils;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.text.DecimalFormat;import java.util.*;import java.util.stream.Collectors;@Servicepublic class DistributionTaskService extends ServiceImpl<DistributionTaskMapper, DistributionTask> { private static Logger LOGGER = LogManager.getLogger(DistributionTaskService.class); private static final String ALL_CUSLEVEL = "全部等级"; private static final String TODAY_DATE = DateUtil.today(); @Autowired private DistributionTaskMapper distributionTaskMapper; @Autowired private EmpDistributeMapper empDistributeMapper; @Autowired private CustomerMapper customerMapper; public void getPage(Page page, QueryWrapper queryWrapper){ page.setRecords(getBaseMapper().getPage(page,queryWrapper)); } /** * 对外接口任务状态查询 * @param orderNo * @param customerCode * @param startTime * @param endTime * @return */ public StatusQueryDto taskNumQuery(String orderNo,String customerCode, String startTime,String endTime){ StatusQueryDto statusQueryDto = distributionTaskMapper.taskNumQuery(orderNo,customerCode,startTime,endTime); return statusQueryDto; } /** * 按客户等级统计 * @param customerCode * @param date * @return */ public List<JSONObject> statisticByCusLevel(String customerCode, String date) { List<JSONObject> resultList = new ArrayList<>(); // 当日总的任务数量 List<KeyValueDto> countPlans = distributionTaskMapper.countPlansTaskByCusLevel(customerCode,date); // 已执行的任务数量 List<KeyValueDto> countSended = distributionTaskMapper.countSendedTaskByCusLevel(customerCode,date); // 获取客户等级 List<String> cusLevelList = empDistributeMapper.queryCusLevel(customerCode); // 遍历总的任务数量 for (String cusLevel : cusLevelList){ String planCount = "0"; String actualCount = "0"; for (KeyValueDto plansTask : countPlans) { if (cusLevel.equals(plansTask.getK())) { planCount = plansTask.getV(); break; } } // 遍历已执行的任务数量 for (KeyValueDto sendedTask : countSended) { if (cusLevel.equals(sendedTask.getK())) { actualCount = sendedTask.getV(); break; } } JSONObject jsonObject = new JSONObject(); jsonObject.put("type", cusLevel); jsonObject.put("plan",planCount); jsonObject.put("actual", actualCount); resultList.add(jsonObject); } return resultList; } /** * 首页完成量、完成率 * @param customerCode * @param date * @param endTime * @return */ public List<JSONObject> delieverInfo(String customerCode,String date,String endTime) { List<JSONObject> resultList = new ArrayList<>(); JSONObject jsonObject = new JSONObject(); // 获取客户等级 List<String> cusLevelList = empDistributeMapper.queryCusLevel(customerCode); // 获取客户地址名称的列表 List<String> LocationList = distributionTaskMapper.getLocationList(customerCode); // 客户业务类型(0揽件 1揽件&派件 2派件) QueryWrapper<Customer> query = new QueryWrapper<>(); query.eq("customer_code", customerCode); Customer customer = customerMapper.selectOne(query); Integer businessType = customer.getBusinessType(); // 当日时间开始时间 String startTime = date + "00:00:00"; // 构建所有网点信息 List<JSONObject> allPointList = buildAllPointInfo(LocationList,customerCode,businessType, date,startTime, endTime); // type为全部 时为所有网点任务量信息 jsonObject.put("type", "全部"); jsonObject.put("data", allPointList); resultList.add(jsonObject); /** * 实际任务完成量 */ List<CusLevelAndLoactionDto> actualResult = distributionTaskMapper.countByCustomerAndCity(customerCode,businessType, date,startTime, endTime); Map<String, List<CusLevelAndLoactionDto>> actualMap = actualResult.stream().collect(Collectors.groupingBy(CusLevelAndLoactionDto::getCusLevel)); /** * 计划任务完成量 */ List<CusLevelAndLoactionDto> planResult = distributionTaskMapper.countPlanByCustomerAndCity(customerCode,date); Map<String, List<CusLevelAndLoactionDto>> planMap = planResult.stream().collect(Collectors.groupingBy(CusLevelAndLoactionDto::getCusLevel)); /** * 不同的客户等级所对应的任务量量及完成率 */ for (int i = 0;i < cusLevelList.size(); i++) { String cusLevel = cusLevelList.get(i); List<JSONObject> list = new ArrayList<>(); JSONObject jsonObj = new JSONObject(); jsonObj.put("type", cusLevel); List<CusLevelAndLoactionDto> typeList = actualMap.get(cusLevel); List<CusLevelAndLoactionDto> planList = planMap.get(cusLevel); if (CollectionUtils.isEmpty(planList)) { planList = new ArrayList<>(); } int all = 0; int planAll = 0; for (CusLevelAndLoactionDto planDto : planList) { String cusLocation = planDto.getLocation(); if (StringUtils.isBlank(cusLocation)) { continue; } planAll += planDto.getNum(); int num = 0; String rate = "0"; if (CollectionUtils.isNotEmpty(typeList)) { for (CusLevelAndLoactionDto dto : typeList) { if (cusLocation.equals(dto.getLocation())) { num = dto.getNum(); rate = calcPercent(num, planDto.getNum()); all += num; } } } JSONObject json = buildData(cusLocation, num, rate); list.add(json); } if (planList.size() == 0) { for (String cusLevel2 : LocationList) { JSONObject json = buildData(cusLevel2, 0, "0"); list.add(json); } } //处理"全部"任务信息 String allRate = calcPercent(all, planAll); JSONObject json = buildData(ALL_CUSLEVEL, all, allRate); list.add(json); jsonObj.put("data", list); resultList.add(jsonObj); } return resultList; } /** * 构建所有网点任务信息 * @param LocationList * @param customerCode * @param businessType * @param date * @param endTime * @return */ private List<JSONObject> buildAllPointInfo(List<String> LocationList,String customerCode,Integer businessType, String date,String startTime, String endTime) { // 按客户所在城市统计完成任务量 List<Map<String, Object>> result = distributionTaskMapper.countByCusLocation(customerCode,businessType, date,startTime, endTime); Map<String, Integer> cityNumMap = getCityNumMap(date); List<JSONObject> jsonObjects = new ArrayList<>(); int all = 0; for (String location : LocationList ) { if (StringUtils.isBlank(location)) { continue; } int num = 0; String rate = "0"; for (Map<String, Object> map : result) { if (StringUtils.isBlank(location)) { continue; } if (location.equals(map.get("location").toString())) { num = Integer.valueOf(map.get("num").toString()); rate = calcPercent(num, cityNumMap.get(location)); all += num; } } // 所在地数据(返回每个地区的数据) JSONObject jsonObject = buildData(location, num, rate); jsonObjects.add(jsonObject); } //处理"全部等级"任务信息(计算完成率) String rate = calcPercent(all, cityNumMap.get(ALL_CUSLEVEL)); JSONObject jsonObject = buildData(ALL_CUSLEVEL, all, rate); jsonObjects.add(jsonObject); return jsonObjects; } /** * 该城市当日任务量 key 为location value 为num计划任务数量 * @param date * @return */ public Map<String, Integer> getCityNumMap(String date) { Map<String, Integer> cityNumMap = new HashMap<>(); List<Map<String, Object>> list = distributionTaskMapper.countByDate(date); int allCusLevel = 0; for (Map<String, Object> map : list) { int num = Integer.valueOf(map.get("num").toString()); cityNumMap.put(String.valueOf(map.get("location")), num); allCusLevel += num; } cityNumMap.put(ALL_CUSLEVEL, allCusLevel); return cityNumMap; } /** * 任务完成率 * @param dividend * @param divisor * @return */ private String calcPercent(int dividend, int divisor) { DecimalFormat df = new DecimalFormat(); if (dividend == 0 || divisor == 0) { return "0"; } String s = df.format(dividend * 100 / divisor); return s; } /** * 所在地数据(返回每个地区的数据) * @param city * @param all * @param rate * @return */ private JSONObject buildData(String city, int all, String rate) { JSONObject jsonObject = new JSONObject(); jsonObject.put("city", city); jsonObject.put("num", all); jsonObject.put("rate", rate); return jsonObject; }}