|
@@ -0,0 +1,499 @@
|
|
|
+package com.qunzhixinxi.hnqz.admin.recharge.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.constant.CacheConstants;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.SysDept;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.SysDeptRelation;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.SysUser;
|
|
|
+import com.qunzhixinxi.hnqz.admin.entity.WmScorePackage;
|
|
|
+import com.qunzhixinxi.hnqz.admin.entity.WmScorePackageStatus;
|
|
|
+import com.qunzhixinxi.hnqz.admin.enums.DelEnum;
|
|
|
+import com.qunzhixinxi.hnqz.admin.enums.PackageStatusEnum;
|
|
|
+import com.qunzhixinxi.hnqz.admin.enums.ScorePackageStatusEnum;
|
|
|
+import com.qunzhixinxi.hnqz.admin.mapper.WmScorePackageMapper;
|
|
|
+import com.qunzhixinxi.hnqz.admin.mapper.WmScorePackageStatusMapper;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.dto.SysDeptInvoiceRequest;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.entity.SysDeptInvoice;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.entity.SysDeptInvoiceRelation;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.entity.SysDeptRecharge;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.enums.InvoiceApprovalStatus;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.enums.InvoiceStatus;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.mapper.SysDeptInvoiceMapper;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.mapper.SysDeptInvoiceRelationMapper;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.mapper.SysDeptRechargeMapper;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.service.SysDeptInvoiceRelationService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.service.SysDeptInvoiceService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.vo.SysDeptInvoiceRelationVO;
|
|
|
+import com.qunzhixinxi.hnqz.admin.recharge.vo.SysDeptInvoiceVO;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.SysDeptRelationService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.SysDeptService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.SysUserService;
|
|
|
+import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+/**
|
|
|
+* @author snows
|
|
|
+* @description 针对表【sys_dept_invoice(企业开票表)】的数据库操作Service实现
|
|
|
+* @createDate 2023-06-16 14:08:32
|
|
|
+*/
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class SysDeptInvoiceServiceImpl extends ServiceImpl<SysDeptInvoiceMapper, SysDeptInvoice>
|
|
|
+ implements SysDeptInvoiceService {
|
|
|
+
|
|
|
+ private final SysDeptRelationService deptRelationService;
|
|
|
+ private final WmScorePackageMapper scorePackageMapper;
|
|
|
+ private final WmScorePackageStatusMapper scorePackageStatusMapper;
|
|
|
+ private final SysUserService userService;
|
|
|
+ private final SysDeptService deptService;
|
|
|
+ private final SysDeptInvoiceRelationService deptInvoiceRelationService;
|
|
|
+ private final SysDeptInvoiceRelationMapper deptInvoiceRelationMapper;
|
|
|
+ private final SysDeptRechargeMapper deptRechargeMapper;
|
|
|
+ private final RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 园区-开票管理列表
|
|
|
+// *
|
|
|
+// * @param toScorePackagePage 分页参数
|
|
|
+// * @return 分页结果
|
|
|
+// */
|
|
|
+// @Override
|
|
|
+// public IPage<SysDeptInvoiceRelationVO> pageInvoiceForPark(Page<SysDeptInvoiceRelationVO> page, SysDeptInvoiceRequest.ToScorePackagePage toScorePackagePage) {
|
|
|
+//
|
|
|
+// Integer deptId = SecurityUtils.getUser().getDeptId();
|
|
|
+// List<SysJyEntRelation> jyEnts = jyEntRelationService.list(Wrappers.<SysJyEntRelation>lambdaQuery()
|
|
|
+// .eq(SysJyEntRelation::getJyDept, deptId));
|
|
|
+// if (CollUtil.isEmpty(jyEnts)) {
|
|
|
+// return page;
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 查询园区关联的企业
|
|
|
+// Set<Integer> queryDeptIds = new HashSet<>();
|
|
|
+// Set<Integer> jyEntRelationDeptIds = jyEnts.stream().map(SysJyEntRelation::getEntDept).collect(Collectors.toSet());
|
|
|
+// List<SysDeptRelation> deptRelations = deptRelationService.list(Wrappers.<SysDeptRelation>lambdaQuery()
|
|
|
+// .in(SysDeptRelation::getAncestor, jyEntRelationDeptIds)
|
|
|
+// .notIn(SysDeptRelation::getDescendant, jyEntRelationDeptIds));
|
|
|
+// if (CollUtil.isEmpty(deptRelations)) {
|
|
|
+// return page;
|
|
|
+// }
|
|
|
+// queryDeptIds.addAll(jyEntRelationDeptIds);
|
|
|
+//
|
|
|
+// Set<Integer> subDeptIds = deptRelations.stream().map(SysDeptRelation::getDescendant).collect(Collectors.toSet());
|
|
|
+// queryDeptIds.addAll(subDeptIds);
|
|
|
+// // 查询下级企业的信息
|
|
|
+// List<SysDeptRelation> subRelations = deptRelationService.list(Wrappers.<SysDeptRelation>lambdaQuery()
|
|
|
+// .in(SysDeptRelation::getAncestor, subDeptIds));
|
|
|
+// if (CollUtil.isNotEmpty(subRelations)) {
|
|
|
+// Set<Integer> sub1DeptIds = subRelations.stream().map(SysDeptRelation::getDescendant).collect(Collectors.toSet());
|
|
|
+// queryDeptIds.addAll(sub1DeptIds);
|
|
|
+// // 查询下下级企业的信息
|
|
|
+// List<SysDeptRelation> sub2Relations = deptRelationService.list(Wrappers.<SysDeptRelation>lambdaQuery()
|
|
|
+// .in(SysDeptRelation::getAncestor, sub1DeptIds));
|
|
|
+// if (CollUtil.isNotEmpty(sub2Relations)) {
|
|
|
+// Set<Integer> sub2DeptIds = sub2Relations.stream().map(SysDeptRelation::getDescendant).collect(Collectors.toSet());
|
|
|
+// queryDeptIds.addAll(sub2DeptIds);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 企业名称查询过滤
|
|
|
+// if (StrUtil.isNotEmpty(toScorePackagePage.getDeptName())) {
|
|
|
+// List<SysDept> queryLikeDepts = deptService.list(Wrappers.<SysDept>lambdaQuery()
|
|
|
+// .like(SysDept::getName, toScorePackagePage.getDeptName()));
|
|
|
+// if (CollUtil.isEmpty(queryLikeDepts)) {
|
|
|
+// return page;
|
|
|
+// }
|
|
|
+// Set<Integer> likeDeptIds = queryLikeDepts.stream().map(SysDept::getDeptId).collect(Collectors.toSet());
|
|
|
+// queryDeptIds.retainAll(likeDeptIds);
|
|
|
+// }
|
|
|
+//
|
|
|
+// IPage<SysDeptInvoiceRelationVO> relationVOIPage =
|
|
|
+// deptInvoiceRelationMapper.pageInvoiceScorePacakgeRelation(new Page<>(page.getCurrent(), page.getSize()), toScorePackagePage, queryDeptIds);
|
|
|
+//
|
|
|
+// List<SysDeptInvoiceRelationVO> records = relationVOIPage.getRecords();
|
|
|
+// if (CollUtil.isEmpty(records)) {
|
|
|
+// return page;
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 积分包接单用户map
|
|
|
+// Map<String, String> packageUsersMap = new HashMap<>();
|
|
|
+// // 查领包记录
|
|
|
+// Set<String> packageIds = records.stream().map(SysDeptInvoiceRelationVO::getScorePackageId).collect(Collectors.toSet());
|
|
|
+// List<WmScorePackageStatus> scorePackageStatusList = scorePackageStatusMapper.selectList(Wrappers.<WmScorePackageStatus>lambdaQuery()
|
|
|
+// .in(WmScorePackageStatus::getPackageId, packageIds)
|
|
|
+// .eq(WmScorePackageStatus::getStatus, PackageStatusEnum.APPROVED.val()));
|
|
|
+// if (CollUtil.isNotEmpty(scorePackageStatusList)) {
|
|
|
+// // 查询领包记录的所有用户
|
|
|
+// Set<String> statusUserIds = scorePackageStatusList.stream().map(WmScorePackageStatus::getUserId).collect(Collectors.toSet());
|
|
|
+// List<SysUser> statusUsers = userService.listByIds(statusUserIds);
|
|
|
+// // 转为map(key-积分包id,value-领包用户逗号分隔)
|
|
|
+// Map<Integer, String> statusUserMap = statusUsers.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getRealname));
|
|
|
+// packageUsersMap = scorePackageStatusList.stream()
|
|
|
+// .collect(Collectors.groupingBy(WmScorePackageStatus::getPackageId,
|
|
|
+// Collectors.mapping(wmScorePackageStatus ->
|
|
|
+// statusUserMap.getOrDefault(Integer.parseInt(wmScorePackageStatus.getUserId()), ""), Collectors.joining(","))));
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 查询积分包涉及的企业
|
|
|
+// Set<Integer> deptIds = records.stream().map(SysDeptInvoiceRelationVO::getDeptId).collect(Collectors.toSet());
|
|
|
+// List<SysDept> depts = deptService.listByIds(deptIds);
|
|
|
+// Map<Integer, String> deptMap = depts.stream().collect(Collectors.toMap(SysDept::getDeptId, SysDept::getName));
|
|
|
+//
|
|
|
+// Map<String, String> finalPackageUsersMap = packageUsersMap;
|
|
|
+// records.forEach(vo -> {
|
|
|
+// vo.setDeptName(deptMap.getOrDefault(vo.getDeptId(), ""));
|
|
|
+// vo.setTaskUsers(finalPackageUsersMap.getOrDefault(vo.getScorePackageId(), ""));
|
|
|
+// vo.setInvoiceStatusStr(vo.getInvoiceStatus().getDescription());
|
|
|
+// });
|
|
|
+//
|
|
|
+// return relationVOIPage;
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 平台财务管理-开票列表
|
|
|
+ *
|
|
|
+ * @param page 分页参数
|
|
|
+ * @param toInvoicePage 请求参数
|
|
|
+ * @return 响应结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public IPage<SysDeptInvoiceVO> pageInvoiceForAdmin(Page<SysDeptInvoiceVO> page, SysDeptInvoiceRequest.ToInvoicePage toInvoicePage) {
|
|
|
+ LambdaQueryWrapper<SysDeptInvoice> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.eq(SysDeptInvoice::getDelFlag, DelEnum.NOT_DEL.val());
|
|
|
+ if (toInvoicePage.getApprovalStatus() != null) {
|
|
|
+ queryWrapper.eq(SysDeptInvoice::getApprovalStatus, toInvoicePage.getApprovalStatus());
|
|
|
+ }
|
|
|
+ if (toInvoicePage.getCreateTime() != null && toInvoicePage.getCreateTime().length == 2) {
|
|
|
+ queryWrapper.between(SysDeptInvoice::getCreateTime, toInvoicePage.getCreateTime()[0], toInvoicePage.getCreateTime()[1]);
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(toInvoicePage.getDeptName())) {
|
|
|
+ List<SysDept> queryLikeDepts = deptService.list(Wrappers.<SysDept>lambdaQuery()
|
|
|
+ .like(SysDept::getName, toInvoicePage.getDeptName()));
|
|
|
+ if (CollUtil.isEmpty(queryLikeDepts)) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+ Set<Integer> likeDeptIds = queryLikeDepts.stream().map(SysDept::getDeptId).collect(Collectors.toSet());
|
|
|
+ queryWrapper.in(SysDeptInvoice::getDeptId, likeDeptIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ Page<SysDeptInvoice> invoicePage = this.page(new Page<>(page.getCurrent(), page.getSize()), queryWrapper);
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(invoicePage.getRecords())) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询发票列表涉及的企业
|
|
|
+ Set<Integer> deptIds = invoicePage.getRecords().stream().map(SysDeptInvoice::getDeptId).collect(Collectors.toSet());
|
|
|
+ List<SysDept> depts = deptService.listByIds(deptIds);
|
|
|
+ Map<Integer, String> deptMap = depts.stream().collect(Collectors.toMap(SysDept::getDeptId, SysDept::getName));
|
|
|
+
|
|
|
+ List<SysDeptInvoiceVO> voList = new ArrayList<>();
|
|
|
+ invoicePage.getRecords().forEach(sysDeptInvoice -> {
|
|
|
+ SysDeptInvoiceVO deptInvoiceVO = BeanUtil.copyProperties(sysDeptInvoice, SysDeptInvoiceVO.class);
|
|
|
+ deptInvoiceVO.setDeptName(deptMap.getOrDefault(sysDeptInvoice.getDeptId(), ""));
|
|
|
+ voList.add(deptInvoiceVO);
|
|
|
+ });
|
|
|
+
|
|
|
+ page.setPages(invoicePage.getPages());
|
|
|
+ page.setTotal(invoicePage.getTotal());
|
|
|
+ page.setRecords(voList);
|
|
|
+
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 园区-开票列表
|
|
|
+// *
|
|
|
+// * @param page 分页参数
|
|
|
+// * @param toInvoicePage 请求参数
|
|
|
+// * @return 响应结果
|
|
|
+// */
|
|
|
+// @Override
|
|
|
+// public IPage<SysDeptInvoiceVO> pageInvoiceRecordForPark(Page<SysDeptInvoiceVO> page, SysDeptInvoiceRequest.ToInvoicePage toInvoicePage) {
|
|
|
+// Integer deptId = SecurityUtils.getUser().getDeptId();
|
|
|
+//
|
|
|
+// LambdaQueryWrapper<SysDeptInvoice> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+// queryWrapper.eq(SysDeptInvoice::getDelFlag, DelEnum.NOT_DEL.val());
|
|
|
+// queryWrapper.eq(SysDeptInvoice::getDeptId, deptId);
|
|
|
+// if (toInvoicePage.getApprovalStatus() != null) {
|
|
|
+// queryWrapper.eq(SysDeptInvoice::getApprovalStatus, toInvoicePage.getApprovalStatus());
|
|
|
+// }
|
|
|
+// if (toInvoicePage.getCreateTime() != null && toInvoicePage.getCreateTime().length == 2) {
|
|
|
+// queryWrapper.between(SysDeptInvoice::getCreateTime, toInvoicePage.getCreateTime()[0], toInvoicePage.getCreateTime()[1]);
|
|
|
+// }
|
|
|
+// if (StrUtil.isNotBlank(toInvoicePage.getDeptName())) {
|
|
|
+// List<SysDept> queryLikeDepts = deptService.list(Wrappers.<SysDept>lambdaQuery()
|
|
|
+// .like(SysDept::getName, toInvoicePage.getDeptName()));
|
|
|
+// if (CollUtil.isEmpty(queryLikeDepts)) {
|
|
|
+// return page;
|
|
|
+// }
|
|
|
+// Set<Integer> likeDeptIds = queryLikeDepts.stream().map(SysDept::getDeptId).collect(Collectors.toSet());
|
|
|
+// queryWrapper.in(SysDeptInvoice::getDeptId, likeDeptIds);
|
|
|
+// }
|
|
|
+//
|
|
|
+// Page<SysDeptInvoice> invoicePage = this.page(new Page<>(page.getCurrent(), page.getSize()), queryWrapper);
|
|
|
+//
|
|
|
+// if (CollUtil.isEmpty(invoicePage.getRecords())) {
|
|
|
+// return page;
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 查询发票列表涉及的企业
|
|
|
+// SysDept dept = deptService.getById(deptId);
|
|
|
+//
|
|
|
+// List<SysDeptInvoiceVO> voList = new ArrayList<>();
|
|
|
+// invoicePage.getRecords().forEach(sysDeptInvoice -> {
|
|
|
+// SysDeptInvoiceVO deptInvoiceVO = BeanUtil.copyProperties(sysDeptInvoice, SysDeptInvoiceVO.class);
|
|
|
+// deptInvoiceVO.setDeptName(dept.getName());
|
|
|
+// voList.add(deptInvoiceVO);
|
|
|
+// });
|
|
|
+//
|
|
|
+// page.setPages(invoicePage.getPages());
|
|
|
+// page.setTotal(invoicePage.getTotal());
|
|
|
+// page.setRecords(voList);
|
|
|
+//
|
|
|
+// return page;
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 平台财务-关联积分包列表
|
|
|
+ *
|
|
|
+ * @param page 分页参数
|
|
|
+ * @param invoiceId 企业开票表ID
|
|
|
+ * @return 响应结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public IPage<SysDeptInvoiceRelationVO> pageInvoice(Page<SysDeptInvoiceRelationVO> page, Integer invoiceId) {
|
|
|
+
|
|
|
+ IPage<SysDeptInvoiceRelationVO> relationVOIPage =
|
|
|
+ deptInvoiceRelationMapper.pageInvoiceScorePacakgeRelation2(new Page<>(page.getCurrent(), page.getSize()), invoiceId);
|
|
|
+
|
|
|
+ List<SysDeptInvoiceRelationVO> records = relationVOIPage.getRecords();
|
|
|
+ if (CollUtil.isEmpty(records)) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 积分包接单用户map
|
|
|
+ Map<String, String> packageUsersMap = new HashMap<>();
|
|
|
+ // 查领包记录
|
|
|
+ Set<String> packageIds = records.stream().map(SysDeptInvoiceRelationVO::getScorePackageId).collect(Collectors.toSet());
|
|
|
+ List<WmScorePackageStatus> scorePackageStatusList = scorePackageStatusMapper.selectList(Wrappers.<WmScorePackageStatus>lambdaQuery()
|
|
|
+ .in(WmScorePackageStatus::getPackageId, packageIds)
|
|
|
+ .eq(WmScorePackageStatus::getStatus, PackageStatusEnum.APPROVED.val()));
|
|
|
+ if (CollUtil.isNotEmpty(scorePackageStatusList)) {
|
|
|
+ // 查询领包记录的所有用户
|
|
|
+ Set<String> statusUserIds = scorePackageStatusList.stream().map(WmScorePackageStatus::getUserId).collect(Collectors.toSet());
|
|
|
+ List<SysUser> statusUsers = userService.listByIds(statusUserIds);
|
|
|
+ // 转为map(key-积分包id,value-领包用户逗号分隔)
|
|
|
+ Map<Integer, String> statusUserMap = statusUsers.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getRealname));
|
|
|
+ packageUsersMap = scorePackageStatusList.stream()
|
|
|
+ .collect(Collectors.groupingBy(WmScorePackageStatus::getPackageId,
|
|
|
+ Collectors.mapping(wmScorePackageStatus ->
|
|
|
+ statusUserMap.getOrDefault(Integer.parseInt(wmScorePackageStatus.getUserId()), ""), Collectors.joining(","))));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询积分包涉及的企业
|
|
|
+ Set<Integer> deptIds = records.stream().map(SysDeptInvoiceRelationVO::getDeptId).collect(Collectors.toSet());
|
|
|
+ List<SysDept> depts = deptService.listByIds(deptIds);
|
|
|
+ Map<Integer, String> deptMap = depts.stream().collect(Collectors.toMap(SysDept::getDeptId, SysDept::getName));
|
|
|
+
|
|
|
+ Map<String, String> finalPackageUsersMap = packageUsersMap;
|
|
|
+ records.forEach(vo -> {
|
|
|
+ vo.setDeptName(deptMap.getOrDefault(vo.getDeptId(), ""));
|
|
|
+ vo.setTaskUsers(finalPackageUsersMap.getOrDefault(vo.getScorePackageId(), ""));
|
|
|
+ vo.setInvoiceStatusStr(vo.getInvoiceStatus().getDescription());
|
|
|
+ vo.setScorePackageStatusStr(Objects.requireNonNull(ScorePackageStatusEnum.getByVal(vo.getScorePackageStatus())).getDesc());
|
|
|
+ });
|
|
|
+
|
|
|
+ return relationVOIPage;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 可开票积分
|
|
|
+ *
|
|
|
+ * @param deptId 企业id
|
|
|
+ * @return 可开票积分记录
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<SysDeptInvoiceRelationVO> listToInvoiceScore(Integer deptId) {
|
|
|
+ Set<Integer> queryDeptIds = new HashSet<>();
|
|
|
+ // 查询关联的企业
|
|
|
+ List<SysDeptRelation> deptRelations = deptRelationService.list(Wrappers.<SysDeptRelation>lambdaQuery()
|
|
|
+ .eq(SysDeptRelation::getAncestor, deptId)
|
|
|
+ .ne(SysDeptRelation::getDescendant, deptId));
|
|
|
+ if (CollUtil.isNotEmpty(deptRelations)) {
|
|
|
+ Set<Integer> subDeptIds = deptRelations.stream().map(SysDeptRelation::getDescendant).collect(Collectors.toSet());
|
|
|
+ queryDeptIds.addAll(subDeptIds);
|
|
|
+ // 查询下级企业的信息
|
|
|
+ List<SysDeptRelation> subRelations = deptRelationService.list(Wrappers.<SysDeptRelation>lambdaQuery()
|
|
|
+ .in(SysDeptRelation::getAncestor, subDeptIds));
|
|
|
+ if (CollUtil.isNotEmpty(subRelations)) {
|
|
|
+ Set<Integer> sub1DeptIds = subRelations.stream().map(SysDeptRelation::getDescendant).collect(Collectors.toSet());
|
|
|
+ queryDeptIds.addAll(sub1DeptIds);
|
|
|
+ // 查询下下级企业的信息
|
|
|
+ List<SysDeptRelation> sub2Relations = deptRelationService.list(Wrappers.<SysDeptRelation>lambdaQuery()
|
|
|
+ .in(SysDeptRelation::getAncestor, sub1DeptIds));
|
|
|
+ if (CollUtil.isNotEmpty(sub2Relations)) {
|
|
|
+ Set<Integer> sub2DeptIds = sub2Relations.stream().map(SysDeptRelation::getDescendant).collect(Collectors.toSet());
|
|
|
+ queryDeptIds.addAll(sub2DeptIds);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ SysDeptInvoiceRequest.ToScorePackagePage toScorePackagePage = new SysDeptInvoiceRequest.ToScorePackagePage();
|
|
|
+ toScorePackagePage.setInvoiceStatus(InvoiceStatus.NOT_APPLY);
|
|
|
+
|
|
|
+ return deptInvoiceRelationMapper.listInvoiceScorePacakgeRelation(toScorePackagePage, queryDeptIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开发票
|
|
|
+ *
|
|
|
+ * @param toInvoice 开票参数
|
|
|
+ * @return 响应结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean toInvoice(SysDeptInvoiceRequest.ToInvoice toInvoice) {
|
|
|
+ Integer deptId = SecurityUtils.getUser().getDeptId();
|
|
|
+ Integer userId = SecurityUtils.getUser().getId();
|
|
|
+
|
|
|
+ String[] packageIds = toInvoice.getPackageIds();
|
|
|
+ Set<String> packageIdSet = Stream.of(packageIds).collect(Collectors.toSet());
|
|
|
+ int relationCount = deptInvoiceRelationService.count(Wrappers.<SysDeptInvoiceRelation>lambdaQuery()
|
|
|
+ .in(SysDeptInvoiceRelation::getPackageId, packageIdSet)
|
|
|
+ .eq(SysDeptInvoiceRelation::getDelFlag, DelEnum.NOT_DEL.val()));
|
|
|
+ if (relationCount > 0) {
|
|
|
+ throw new RuntimeException("请选择开票状态为未申请的数据");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<WmScorePackage> scorePackages = scorePackageMapper.selectBatchIds(packageIdSet);
|
|
|
+
|
|
|
+ // 选择开票的总积分
|
|
|
+ int totalScore = scorePackages.stream().mapToInt(WmScorePackage::getScore).sum();
|
|
|
+ // 计算开票金额
|
|
|
+ SysDeptRecharge deptRecharge = deptRechargeMapper.selectOne(Wrappers.<SysDeptRecharge>lambdaQuery()
|
|
|
+ .eq(SysDeptRecharge::getDeptId, deptId));
|
|
|
+ if (deptRecharge == null || deptRecharge.getServiceCharge() == null) {
|
|
|
+ throw new RuntimeException("没有配置服务费率");
|
|
|
+ }
|
|
|
+ // 开票金额 = 开票积分 * (服务费率/100)
|
|
|
+ BigDecimal invoiceAmount = deptRecharge.getServiceCharge().divide(new BigDecimal("100.00"))
|
|
|
+ .multiply(BigDecimal.valueOf(totalScore)).setScale(2, RoundingMode.HALF_EVEN);
|
|
|
+ // 保存开票信息
|
|
|
+ SysDeptInvoice deptInvoice = new SysDeptInvoice();
|
|
|
+ deptInvoice.setDeptId(deptId);
|
|
|
+ deptInvoice.setInvoiceType(toInvoice.getInvoiceType());
|
|
|
+ deptInvoice.setAddress(toInvoice.getAddress());
|
|
|
+ deptInvoice.setAddresseeName(toInvoice.getAddresseeName());
|
|
|
+ deptInvoice.setAddresseePhone(toInvoice.getAddresseePhone());
|
|
|
+ deptInvoice.setApprovalStatus(InvoiceApprovalStatus.PENDING);
|
|
|
+ deptInvoice.setRelationPackageNumber(scorePackages.size());
|
|
|
+ deptInvoice.setRelationPackageScore(totalScore);
|
|
|
+ deptInvoice.setInvoiceAmount(invoiceAmount);
|
|
|
+ this.save(deptInvoice);
|
|
|
+
|
|
|
+ // 保存开票积分包关联信息
|
|
|
+ Set<SysDeptInvoiceRelation> invoiceRelations = scorePackages.stream().map(scorePackage -> {
|
|
|
+ SysDeptInvoiceRelation invoiceRelation = new SysDeptInvoiceRelation();
|
|
|
+ invoiceRelation.setDeptId(Integer.valueOf(scorePackage.getDeptId()));
|
|
|
+ invoiceRelation.setInvoiceId(deptInvoice.getId());
|
|
|
+ invoiceRelation.setInvoiceStatus(InvoiceStatus.PENDING);
|
|
|
+ invoiceRelation.setCreateUser(userId);
|
|
|
+ invoiceRelation.setPackageId(scorePackage.getId());
|
|
|
+ return invoiceRelation;
|
|
|
+ }).collect(Collectors.toSet());
|
|
|
+ boolean b = deptInvoiceRelationService.saveBatch(invoiceRelations);
|
|
|
+ if (!b) {
|
|
|
+ throw new RuntimeException("保存失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 缓存信息
|
|
|
+ String cacheKey = CacheConstants.INVOICE_DEPT_KEY + deptId;
|
|
|
+ redisTemplate.opsForValue().set(cacheKey, JSONUtil.toJsonStr(deptInvoice));
|
|
|
+
|
|
|
+ return Boolean.TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开票审核
|
|
|
+ *
|
|
|
+ * @param toApprove 开票参数
|
|
|
+ * @return 响应结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean toApprove(SysDeptInvoiceRequest.ToApprove toApprove) {
|
|
|
+ Integer userId = SecurityUtils.getUser().getId();
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+
|
|
|
+ // 查询开票表
|
|
|
+ SysDeptInvoice deptInvoice = this.getById(toApprove.getInvoiceId());
|
|
|
+ if (!InvoiceApprovalStatus.PENDING.equals(deptInvoice.getApprovalStatus())) {
|
|
|
+ throw new RuntimeException("开票状态不正确");
|
|
|
+ }
|
|
|
+ // 查询开票积分包关联表
|
|
|
+ List<SysDeptInvoiceRelation> deptInvoiceRelations = deptInvoiceRelationService.list(Wrappers.<SysDeptInvoiceRelation>lambdaQuery()
|
|
|
+ .eq(SysDeptInvoiceRelation::getInvoiceId, toApprove.getInvoiceId())
|
|
|
+ .eq(SysDeptInvoiceRelation::getDelFlag, DelEnum.NOT_DEL.val()));
|
|
|
+
|
|
|
+ // 更新开票积分包关联表
|
|
|
+ List<SysDeptInvoiceRelation> updateRelationList = new ArrayList<>();
|
|
|
+ deptInvoiceRelations.forEach(relation -> {
|
|
|
+ // 校验
|
|
|
+ if (!InvoiceStatus.PENDING.equals(relation.getInvoiceStatus())) {
|
|
|
+ throw new RuntimeException("关联积分包的审核状态不正确");
|
|
|
+ }
|
|
|
+ SysDeptInvoiceRelation updateRelation = new SysDeptInvoiceRelation();
|
|
|
+ updateRelation.setId(relation.getId());
|
|
|
+ updateRelation.setInvoiceStatus(InvoiceStatus.getByApproveStatus(toApprove.getApprovalStatus()));
|
|
|
+ updateRelation.setInvoiceTime(now);
|
|
|
+ updateRelation.setUpdateTime(now);
|
|
|
+ updateRelation.setUpdateUser(userId);
|
|
|
+ updateRelationList.add(updateRelation);
|
|
|
+ });
|
|
|
+ boolean b = deptInvoiceRelationService.updateBatchById(updateRelationList);
|
|
|
+ if (!b) {
|
|
|
+ throw new RuntimeException("更新失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新开票信息
|
|
|
+ SysDeptInvoice updateDeptInvoice = new SysDeptInvoice();
|
|
|
+ updateDeptInvoice.setId(toApprove.getInvoiceId());
|
|
|
+ updateDeptInvoice.setApprovalStatus(toApprove.getApprovalStatus());
|
|
|
+ updateDeptInvoice.setApprovalTime(now);
|
|
|
+ updateDeptInvoice.setUpdateTime(now);
|
|
|
+ updateDeptInvoice.setUpdateUser(userId);
|
|
|
+ this.updateById(updateDeptInvoice);
|
|
|
+
|
|
|
+ return Boolean.TRUE;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|