|
@@ -0,0 +1,160 @@
|
|
|
|
|
+package com.sckw.system.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
|
+import com.sckw.core.model.page.PageResult;
|
|
|
|
|
+import com.sckw.core.web.context.LoginUserHolder;
|
|
|
|
|
+import com.sckw.system.model.KwsEnterprise;
|
|
|
|
|
+import com.sckw.system.model.KwsWeighbridge;
|
|
|
|
|
+import com.sckw.system.model.KwsWeighbridgeRecord;
|
|
|
|
|
+import com.sckw.system.model.report.WeighbridgeRecordExcel;
|
|
|
|
|
+import com.sckw.system.model.vo.req.WeighbridgeRecordPageReqVo;
|
|
|
|
|
+import com.sckw.system.model.vo.res.WeighbridgeRecordResVo;
|
|
|
|
|
+import com.sckw.system.repository.KwsEnterpriseRepository;
|
|
|
|
|
+import com.sckw.system.repository.KwsWeighbridgeRecordRepository;
|
|
|
|
|
+import com.sckw.system.repository.KwsWeighbridgeRepository;
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.LinkedHashSet;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 地磅称重记录业务服务。
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
|
+public class KwsWeighbridgeRecordManageService {
|
|
|
|
|
+
|
|
|
|
|
+ private final KwsWeighbridgeRecordRepository kwsWeighbridgeRecordRepository;
|
|
|
|
|
+ private final KwsWeighbridgeRepository kwsWeighbridgeRepository;
|
|
|
|
|
+ private final KwsEnterpriseRepository kwsEnterpriseRepository;
|
|
|
|
|
+
|
|
|
|
|
+ public PageResult page(WeighbridgeRecordPageReqVo reqVo) {
|
|
|
|
|
+ Set<Long> weighbridgeIds = resolveWeighbridgeIds(reqVo);
|
|
|
|
|
+ if (weighbridgeIds.isEmpty()) {
|
|
|
|
|
+ return PageResult.build(reqVo.getPage(), reqVo.getPageSize(), 0L, Collections.emptyList());
|
|
|
|
|
+ }
|
|
|
|
|
+ IPage<KwsWeighbridgeRecord> page = kwsWeighbridgeRecordRepository.pageQuery(reqVo.getPage(), reqVo.getPageSize(),
|
|
|
|
|
+ weighbridgeIds, reqVo.getTruckNo(), reqVo.getMinWeight(), reqVo.getMaxWeight(),
|
|
|
|
|
+ reqVo.getCreateStartTime(), reqVo.getCreateEndTime(), reqVo.getReceiveStartTime(), reqVo.getReceiveEndTime());
|
|
|
|
|
+ return PageResult.of(page, buildRes(page.getRecords()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<WeighbridgeRecordExcel> exportList(WeighbridgeRecordPageReqVo reqVo) {
|
|
|
|
|
+ Set<Long> weighbridgeIds = resolveWeighbridgeIds(reqVo);
|
|
|
|
|
+ if (weighbridgeIds.isEmpty()) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ List<KwsWeighbridgeRecord> records = kwsWeighbridgeRecordRepository.listQuery(weighbridgeIds, reqVo.getTruckNo(),
|
|
|
|
|
+ reqVo.getMinWeight(), reqVo.getMaxWeight(), reqVo.getCreateStartTime(), reqVo.getCreateEndTime(),
|
|
|
|
|
+ reqVo.getReceiveStartTime(), reqVo.getReceiveEndTime());
|
|
|
|
|
+ return buildExportRows(buildRes(records));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<WeighbridgeRecordResVo> buildRes(List<KwsWeighbridgeRecord> records) {
|
|
|
|
|
+ if (records == null || records.isEmpty()) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<Long, KwsWeighbridge> weighbridgeMap = weighbridgeMap(records.stream()
|
|
|
|
|
+ .map(KwsWeighbridgeRecord::getWeighbridgeId).collect(Collectors.toSet()));
|
|
|
|
|
+ Map<Long, String> entNameMap = enterpriseNameMap(weighbridgeMap.values().stream()
|
|
|
|
|
+ .map(KwsWeighbridge::getEntId).collect(Collectors.toSet()));
|
|
|
|
|
+ List<WeighbridgeRecordResVo> result = new ArrayList<>(records.size());
|
|
|
|
|
+ for (KwsWeighbridgeRecord record : records) {
|
|
|
|
|
+ KwsWeighbridge weighbridge = weighbridgeMap.get(record.getWeighbridgeId());
|
|
|
|
|
+ WeighbridgeRecordResVo resVo = new WeighbridgeRecordResVo();
|
|
|
|
|
+ resVo.setId(record.getId());
|
|
|
|
|
+ resVo.setWeighbridgeId(record.getWeighbridgeId());
|
|
|
|
|
+ resVo.setWeighbridgeName(weighbridge == null ? "" : weighbridge.getWeighbridgeName());
|
|
|
|
|
+ resVo.setUniqueCode(weighbridge == null ? "" : weighbridge.getUniqueCode());
|
|
|
|
|
+ resVo.setEntId(weighbridge == null ? null : weighbridge.getEntId());
|
|
|
|
|
+ resVo.setEnterpriseName(weighbridge == null ? "" : entNameMap.getOrDefault(weighbridge.getEntId(), ""));
|
|
|
|
|
+ resVo.setTruckNo(record.getTruckNo());
|
|
|
|
|
+ resVo.setWeight(record.getWeight());
|
|
|
|
|
+ resVo.setCreateTime(record.getCreateTime());
|
|
|
|
|
+ resVo.setReceiveTime(record.getReceiveTime());
|
|
|
|
|
+ result.add(resVo);
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<WeighbridgeRecordExcel> buildExportRows(List<WeighbridgeRecordResVo> rows) {
|
|
|
|
|
+ if (rows == null || rows.isEmpty()) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ List<WeighbridgeRecordExcel> result = new ArrayList<>(rows.size());
|
|
|
|
|
+ for (WeighbridgeRecordResVo row : rows) {
|
|
|
|
|
+ WeighbridgeRecordExcel excel = new WeighbridgeRecordExcel();
|
|
|
|
|
+ excel.setWeighbridgeName(row.getWeighbridgeName());
|
|
|
|
|
+ excel.setUniqueCode(row.getUniqueCode());
|
|
|
|
|
+ excel.setEnterpriseName(row.getEnterpriseName());
|
|
|
|
|
+ excel.setTruckNo(row.getTruckNo());
|
|
|
|
|
+ excel.setWeight(row.getWeight());
|
|
|
|
|
+ excel.setCreateTime(row.getCreateTime());
|
|
|
|
|
+ excel.setReceiveTime(row.getReceiveTime());
|
|
|
|
|
+ result.add(excel);
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Set<Long> resolveWeighbridgeIds(WeighbridgeRecordPageReqVo reqVo) {
|
|
|
|
|
+ if (reqVo.getWeighbridgeId() != null) {
|
|
|
|
|
+ KwsWeighbridge weighbridge = kwsWeighbridgeRepository.findAvailableById(reqVo.getWeighbridgeId());
|
|
|
|
|
+ if (weighbridge == null || !checkEntPermission(weighbridge.getEntId())) {
|
|
|
|
|
+ return Collections.emptySet();
|
|
|
|
|
+ }
|
|
|
|
|
+ return Collections.singleton(weighbridge.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ Set<Long> entIds = resolveAuthorizedEntIds();
|
|
|
|
|
+ List<KwsWeighbridge> weighbridges;
|
|
|
|
|
+ if (LoginUserHolder.isManager()) {
|
|
|
|
|
+ weighbridges = kwsWeighbridgeRepository.listByNameAndCode(reqVo.getWeighbridgeName(), reqVo.getUniqueCode(), null);
|
|
|
|
|
+ } else if (entIds.isEmpty()) {
|
|
|
|
|
+ return Collections.emptySet();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ weighbridges = kwsWeighbridgeRepository.listByNameAndCode(reqVo.getWeighbridgeName(), reqVo.getUniqueCode(), entIds);
|
|
|
|
|
+ }
|
|
|
|
|
+ return weighbridges.stream().map(KwsWeighbridge::getId).collect(Collectors.toCollection(LinkedHashSet::new));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean checkEntPermission(Long entId) {
|
|
|
|
|
+ return LoginUserHolder.isManager() || resolveAuthorizedEntIds().contains(entId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Set<Long> resolveAuthorizedEntIds() {
|
|
|
|
|
+ if (LoginUserHolder.isManager()) {
|
|
|
|
|
+ return Collections.emptySet();
|
|
|
|
|
+ }
|
|
|
|
|
+ Set<Long> result = new LinkedHashSet<>();
|
|
|
|
|
+ if (LoginUserHolder.getEntId() != null) {
|
|
|
|
|
+ result.add(LoginUserHolder.getEntId());
|
|
|
|
|
+ }
|
|
|
|
|
+ result.addAll(LoginUserHolder.getAuthEntIdList());
|
|
|
|
|
+ result.addAll(LoginUserHolder.getChildEntList());
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<Long, KwsWeighbridge> weighbridgeMap(Collection<Long> weighbridgeIds) {
|
|
|
|
|
+ if (weighbridgeIds == null || weighbridgeIds.isEmpty()) {
|
|
|
|
|
+ return Collections.emptyMap();
|
|
|
|
|
+ }
|
|
|
|
|
+ return kwsWeighbridgeRepository.listByIds(weighbridgeIds).stream()
|
|
|
|
|
+ .filter(item -> Objects.equals(item.getDelFlag(), 0))
|
|
|
|
|
+ .collect(Collectors.toMap(KwsWeighbridge::getId, item -> item, (a, b) -> a));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<Long, String> enterpriseNameMap(Collection<Long> entIds) {
|
|
|
|
|
+ if (entIds == null || entIds.isEmpty()) {
|
|
|
|
|
+ return Collections.emptyMap();
|
|
|
|
|
+ }
|
|
|
|
|
+ return kwsEnterpriseRepository.listByIds(entIds).stream()
|
|
|
|
|
+ .collect(Collectors.toMap(KwsEnterprise::getId, KwsEnterprise::getFirmName, (a, b) -> a));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|