|
@@ -0,0 +1,147 @@
|
|
|
|
+package com.imed.costaccount.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
+import cn.hutool.extra.template.TemplateUtil;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
+import com.imed.costaccount.common.constants.Constant;
|
|
|
|
+import com.imed.costaccount.common.exception.CostException;
|
|
|
|
+import com.imed.costaccount.common.util.PageUtils;
|
|
|
|
+import com.imed.costaccount.mapper.CostIncomeGroupMapper;
|
|
|
|
+import com.imed.costaccount.model.CostIncomeGroup;
|
|
|
|
+import com.imed.costaccount.model.vo.CollectionVO;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+
|
|
|
|
+import com.imed.costaccount.mapper.IncomeCollectionMapper;
|
|
|
|
+import com.imed.costaccount.model.IncomeCollection;
|
|
|
|
+import com.imed.costaccount.service.IncomeCollectionService;
|
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+import static com.imed.costaccount.common.constants.Constant.LIMIT;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@Slf4j
|
|
|
|
+@Service("incomeCollectionService")
|
|
|
|
+public class IncomeCollectionServiceImpl
|
|
|
|
+ extends ServiceImpl<IncomeCollectionMapper, IncomeCollection>
|
|
|
|
+ implements IncomeCollectionService {
|
|
|
|
+
|
|
|
|
+ private final CostIncomeGroupMapper incomeGroupMapper;
|
|
|
|
+
|
|
|
|
+ public IncomeCollectionServiceImpl(CostIncomeGroupMapper incomeGroupMapper) {
|
|
|
|
+ this.incomeGroupMapper = incomeGroupMapper;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取收入归集分页列表
|
|
|
|
+ *
|
|
|
|
+ * @param current 当前页
|
|
|
|
+ * @param pageSize 页码数据大小
|
|
|
|
+ * @param date 日期 yyyy-MM-dd
|
|
|
|
+ * @param hospId 医院id
|
|
|
|
+ * @return {@link PageUtils} 分页对象
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public PageUtils getCollections(Integer current, Integer pageSize, String date, Long hospId) {
|
|
|
|
+ current = current - 1;
|
|
|
|
+ Integer startIndex = current * pageSize;
|
|
|
|
+ List<CollectionVO> list = incomeGroupMapper.getCollections(startIndex, pageSize, date, hospId);
|
|
|
|
+ int count = incomeGroupMapper.getCollectionCount(date, hospId);
|
|
|
|
+
|
|
|
|
+ // 设置是否归集字段
|
|
|
|
+ list.forEach(i -> {
|
|
|
|
+ i.setIsCollection(true);
|
|
|
|
+ IncomeCollection one = this.getOne(
|
|
|
|
+ new LambdaQueryWrapper<IncomeCollection>().select(IncomeCollection::getId)
|
|
|
|
+ .eq(IncomeCollection::getYear, i.getYear())
|
|
|
|
+ .eq(IncomeCollection::getMonth,i.getMonth())
|
|
|
|
+ .last(LIMIT)
|
|
|
|
+ );
|
|
|
|
+ if (Objects.isNull(one)) {
|
|
|
|
+ i.setIsCollection(false);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return new PageUtils(list, count, pageSize, current);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 按年月归集数据
|
|
|
|
+ *
|
|
|
|
+ * @param year 年 数字类型
|
|
|
|
+ * @param month 月 数字
|
|
|
|
+ * @param hospId 医院id
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
|
|
|
|
+ public void collect(Integer year, Integer month, Long hospId) {
|
|
|
|
+ // 可能几十万次数据 需要分段异步多线程处理,分布式锁处理,
|
|
|
|
+ List<CostIncomeGroup> costIncomeGroups = incomeGroupMapper.selectList(
|
|
|
|
+ new LambdaQueryWrapper<CostIncomeGroup>()
|
|
|
|
+ .eq(CostIncomeGroup::getDateYear, year)
|
|
|
|
+ .eq(CostIncomeGroup::getDateMonth, month)
|
|
|
|
+ .eq(CostIncomeGroup::getHospId, hospId)
|
|
|
|
+ );
|
|
|
|
+ if (costIncomeGroups.isEmpty()) {
|
|
|
|
+ String format = StrUtil.format("{}年{}月数据不存在,请先导入数据",year,month);
|
|
|
|
+ throw new CostException(format);
|
|
|
|
+ }
|
|
|
|
+ // 根据会计科目和成本项目归纳所有数据
|
|
|
|
+ Map<String, List<CostIncomeGroup>> collectMap = costIncomeGroups.stream()
|
|
|
|
+ .collect(Collectors.groupingBy(i -> i.getAccountCode() + "cost" + i.getProductCode()));
|
|
|
|
+
|
|
|
|
+ // 遍历map 组装成List保存
|
|
|
|
+ List<IncomeCollection> list = new LinkedList<>();
|
|
|
|
+ Set<Map.Entry<String, List<CostIncomeGroup>>> entries = collectMap.entrySet();
|
|
|
|
+ entries.stream().map(Map.Entry::getValue).flatMap(Collection::stream).forEach(costIncomeGroup -> {
|
|
|
|
+ IncomeCollection incomeCollection = new IncomeCollection();
|
|
|
|
+ incomeCollection.setYear(year);
|
|
|
|
+ incomeCollection.setMonth(month);
|
|
|
|
+ incomeCollection.setAccountingCode(costIncomeGroup.getAccountCode());
|
|
|
|
+ incomeCollection.setAccountingName(costIncomeGroup.getAccountName());
|
|
|
|
+ incomeCollection.setProductName(costIncomeGroup.getProductName());
|
|
|
|
+ incomeCollection.setProductCode(costIncomeGroup.getProductCode());
|
|
|
|
+ incomeCollection.setHospId(hospId);
|
|
|
|
+ // todo 来源id暂时不确定
|
|
|
|
+ incomeCollection.setFileId(costIncomeGroup.getFileId());
|
|
|
|
+ incomeCollection.setCreateTime(System.currentTimeMillis());
|
|
|
|
+ // 开单中心的数据
|
|
|
|
+ incomeCollection.setDepartmentCode(costIncomeGroup.getOpenDepartmentCode());
|
|
|
|
+ incomeCollection.setResponsibilityCode(costIncomeGroup.getOpenResponsibilityCode());
|
|
|
|
+ incomeCollection.setAmount(costIncomeGroup.getOpenDepartmentAmount());
|
|
|
|
+ list.add(incomeCollection);
|
|
|
|
+ // 执行科室数据
|
|
|
|
+ incomeCollection.setDepartmentCode(costIncomeGroup.getStartDepartmentCode());
|
|
|
|
+ incomeCollection.setResponsibilityCode(costIncomeGroup.getStartResponsibilityCode());
|
|
|
|
+ incomeCollection.setAmount(costIncomeGroup.getStartDepartmentAmount());
|
|
|
|
+ list.add(incomeCollection);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ // TODO: 2021/8/10 几十万条数据如何处理
|
|
|
|
+ this.saveBatch(list);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 按年月撤销归集
|
|
|
|
+ *
|
|
|
|
+ * @param year 年 数字类型
|
|
|
|
+ * @param month 月 数字
|
|
|
|
+ * @param hospId 医院id
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
|
|
|
|
+ public void cancelCollect(Integer year, Integer month, Long hospId) {
|
|
|
|
+ LambdaQueryWrapper<IncomeCollection> wrapper = new LambdaQueryWrapper<IncomeCollection>()
|
|
|
|
+ .eq(IncomeCollection::getYear, year)
|
|
|
|
+ .eq(IncomeCollection::getMonth, month)
|
|
|
|
+ .eq(IncomeCollection::getHospId, hospId);
|
|
|
|
+ this.remove(wrapper);
|
|
|
|
+ }
|
|
|
|
+}
|