按照“总毛重”字段,分坎级统计运单数量。
package org.jeecg.modules.ys.nf.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.ys.grzx.entity.YsOrganizationalRelationships;
import org.jeecg.modules.ys.grzx.service.IUserRolePermissionService;
import org.jeecg.modules.ys.grzx.service.IYsOrganizationalRelationshipsService;
import org.jeecg.modules.ys.yn.entity.YsKjYnWaybillQuery;
import org.jeecg.modules.ys.yn.entity.YsWaybillQuery;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.*;
import java.util.regex.Pattern;
@Api(tags = "奶粉运单毛重坎级统计")
@RestController
@RequestMapping("/nf/ysKjNfWaybillGrossWeightLevelStatistics")
@Slf4jy
public class YsKjNfWaybillGrossWeightLevelStatisticsController {
@Autowired
RedisTemplate redisTemplate;
@Autowired
IUserRolePermissionService userRolePermissionService;
@Autowired
IYsOrganizationalRelationshipsService organizationalRelationshipsService;
@Autowired
private MongoTemplate mongoTemplate;
@ApiOperation(value = "奶粉运单毛重坎级统计-分页列表查询", notes = "奶粉运单毛重坎级统计-分页列表查询")
@GetMapping(value = "/list")
public Result<PageImpl<YsWaybillQuery>> queryPageList(YsKjYnWaybillQuery ysKjYnWaybillQuery,
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
@RequestParam(name = "dateType", defaultValue = "3") String dateType) {
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
Query query = buildQuery(ysKjYnWaybillQuery, sysUser, dateType);
PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
query.with(pageRequest);
long totalCount = mongoTemplate.count(query, YsWaybillQuery.class);
List<YsWaybillQuery> waybillList = mongoTemplate.find(query, YsWaybillQuery.class);
Map<String, YsWaybillQuery> resultMap = processWaybillList(waybillList,dateType);
List<YsWaybillQuery> resultList = new ArrayList<>(resultMap.values());
PageImpl<YsWaybillQuery> page = new PageImpl<>(resultList, pageRequest, totalCount);
return Result.OK(page);
}
@RequestMapping(value = "/exportXls")
public ModelAndView exportXls(@RequestParam(name = "adName", required = false) String adName,
@RequestParam(name = "startTime", required = false) String startTime,
@RequestParam(name = "endTime", required = false) String endTime,
@RequestParam(name = "dateType", defaultValue = "3") String dateType) {
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
Query query = buildQuery(new YsKjYnWaybillQuery().setAdministrativeOrganizationCode(adName).setFistTime(startTime).setLastTime(endTime), sysUser, dateType);
List<YsWaybillQuery> waybillList = mongoTemplate.find(query, YsWaybillQuery.class);
Map<String, YsWaybillQuery> resultMap = processWaybillList(waybillList,dateType);
List<YsWaybillQuery> resultList = new ArrayList<>(resultMap.values());
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
mv.addObject(NormalExcelConstants.FILE_NAME,"奶粉运单毛重坎级统计" );
mv.addObject(NormalExcelConstants.CLASS, YsWaybillQuery.class);
mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("奶粉运单毛重坎级统计表", "导出人:" + sysUser.getRealname(), "奶粉运单毛重坎级统计"));
mv.addObject(NormalExcelConstants.DATA_LIST, resultList);
return mv;
}
private Query buildQuery(YsKjYnWaybillQuery ysKjYnWaybillQuery, LoginUser sysUser, String dateType) {
Query query = new Query();
boolean isProjectLeader = checkIfProjectLeader(sysUser);
if (isProjectLeader) {
List<String> orgCodes = getOrganizationalCodes(sysUser);
query.addCriteria(Criteria.where("administrativeOrganizationCode").in(orgCodes));
}
if (StringUtils.isNotEmpty(ysKjYnWaybillQuery.getAdministrativeOrganizationName())) {
query.addCriteria(Criteria.where("administrativeOrganizationName")
.regex(Pattern.compile(ysKjYnWaybillQuery.getAdministrativeOrganizationName(), Pattern.CASE_INSENSITIVE)));
}
if (StringUtils.isNotEmpty(ysKjYnWaybillQuery.getFistTime()) && StringUtils.isNotEmpty(ysKjYnWaybillQuery.getLastTime())) {
addDateCriteria(query, ysKjYnWaybillQuery.getFistTime(), ysKjYnWaybillQuery.getLastTime(), dateType);
} else {
query.addCriteria(Criteria.where("type").is("5").and("status").is("50"));
}
query.with(Sort.by(Sort.Order.desc("createdDate")));
return query;
}
private boolean checkIfProjectLeader(LoginUser sysUser) {
List<String> roleName = userRolePermissionService.getRoleName(sysUser.getId());
return roleName.stream().anyMatch(s -> s.contains("项目组负责人"));
}
private List<String> getOrganizationalCodes(LoginUser sysUser) {
List<String> ids = userRolePermissionService.findIds(sysUser.getId());
List<String> orgCodes = new ArrayList<>();
for (String id : ids) {
YsOrganizationalRelationships user = organizationalRelationshipsService.getById(id);
if (user != null) {
orgCodes.add(user.getAdministrativeOrganizationCode());
}
}
if (orgCodes.isEmpty()) {
orgCodes.add("没有权限数据");
}
return orgCodes;
}
private void addDateCriteria(Query query, String startTime, String endTime, String dateType) {
if ("3".equals(dateType)) { // 按天查询
query.addCriteria(Criteria.where("createdDate")
.gte(startTime)
.lte(endTime)
.and("type").is("5")
.and("status").is("50"));
} else if ("4".equals(dateType)) { // 按月查询
query.addCriteria(Criteria.where("createdDate")
.gte(startTime.substring(0, 7) + "-01")
.lte(endTime.substring(0, 7) + "-31")
.and("type").is("5")
.and("status").is("50"));
}
}
private Map<String, YsWaybillQuery> processWaybillList(List<YsWaybillQuery> waybillList,String dateType) {
Map<String, YsWaybillQuery> resultMap = new LinkedHashMap<>();
for (YsWaybillQuery waybill : waybillList) {
String key = waybill.getAdministrativeOrganizationName() + waybill.getActualTmId() + waybill.getCreatedDate().substring(0, 10);
YsWaybillQuery result = resultMap.getOrDefault(key, new YsWaybillQuery());
result.setAdministrativeOrganizationName(waybill.getAdministrativeOrganizationName());
result.setActualTmId(waybill.getActualTmId());
if ("3".equals(dateType))
result.setCreatedDate(waybill.getCreatedDate().substring(0, 10));
else if ("4".equals(dateType))
result.setCreatedDate(waybill.getCreatedDate().substring(0, 7));
Double totalGrossWeight = waybill.getTotalGrossWeight();
if (totalGrossWeight != null) {
updateWeightLevels(result, totalGrossWeight);
}
resultMap.put(key, result);
}
return resultMap;
}
private void updateWeightLevels(YsWaybillQuery result, Double totalGrossWeight) {
if (totalGrossWeight <= 0.5) {
result.setWeightLevel1(result.getWeightLevel1() + 1);
} else if (totalGrossWeight <= 1) {
result.setWeightLevel2(result.getWeightLevel2() + 1);
} else if (totalGrossWeight <= 3) {
result.setWeightLevel3(result.getWeightLevel3() + 1);
} else if (totalGrossWeight <= 5) {
result.setWeightLevel4(result.getWeightLevel4() + 1);
} else if (totalGrossWeight <= 10) {
result.setWeightLevel5(result.getWeightLevel5() + 1);
} else {
result.setWeightLevel6(result.getWeightLevel6() + 1);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.