123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- package com.imed.costaccount.service.impl;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.StrUtil;
- import com.alibaba.druid.util.StringUtils;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.imed.costaccount.common.exception.CostException;
- import com.imed.costaccount.common.util.BeanUtil;
- import com.imed.costaccount.common.util.DateUtils;
- import com.imed.costaccount.common.util.Result;
- import com.imed.costaccount.common.util.UserContext;
- import com.imed.costaccount.constants.NumberConstant;
- import com.imed.costaccount.enums.DateStyleEnum;
- import com.imed.costaccount.mapper.CostCostingGroupMapper;
- import com.imed.costaccount.model.*;
- import com.imed.costaccount.model.vo.IncomeErrorMessage;
- import com.imed.costaccount.service.AccountingProductService;
- import com.imed.costaccount.service.CostCostingGroupService;
- import com.imed.costaccount.service.CostIncomeFileService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.CollectionUtils;
- import org.springframework.web.multipart.MultipartFile;
- import java.math.BigDecimal;
- import java.util.*;
- @Service("costCostingGroupService")
- @Slf4j
- public class CostCostingGroupServiceImpl extends ServiceImpl<CostCostingGroupMapper, CostCostingGroup> implements CostCostingGroupService {
- private final CostIncomeGroupServiceImpl costIncomeGroupService;
- private final AccountingProductService accountingProductService;
- private final CostIncomeFileService costIncomeFileService;
- public CostCostingGroupServiceImpl(CostIncomeGroupServiceImpl costIncomeGroupService, AccountingProductService accountingProductService, CostIncomeFileService costIncomeFileService) {
- this.costIncomeGroupService = costIncomeGroupService;
- this.accountingProductService = accountingProductService;
- this.costIncomeFileService = costIncomeFileService;
- }
- /**
- * 批量导入成本数据
- *
- * @param list
- * @param user
- * @param file
- * @param dateTime
- * @param fileType
- * @return
- */
- @Override
- @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
- public Result importCostingGroup(List<List<Object>> list, User user, MultipartFile file, String dateTime, Integer fileType) {
- // 先检验当前年月是否存在数据
- int year = 0;
- int month = 0;
- Date date = DateUtils.StringToDate(dateTime, DateStyleEnum.YYYY_MM);
- if (StrUtil.isNotBlank(dateTime)) {
- year = DateUtil.year(date);
- month = DateUtil.month(date) + 1;
- }
- Long hospId = user.getHospId();
- List<CostCostingGroup> groups = this.list(new QueryWrapper<CostCostingGroup>().lambda().eq(CostCostingGroup::getHospId, hospId)
- .eq(CostCostingGroup::getDateYear, year).eq(CostCostingGroup::getDateMonth, month));
- if (!CollectionUtils.isEmpty(groups)) {
- throw new CostException(500, year + "年" + month + "月数据已存在");
- }
- for (int i = list.size() - 1; i >= 0; i--) {
- if (i == NumberConstant.ZERO || i == NumberConstant.ONE || i == NumberConstant.TWO || i == NumberConstant.THREE) {
- list.remove(list.get(i));
- }
- }
- List<IncomeErrorMessage> incomeErrorMessageList = new ArrayList<>();
- //获取所有的科室 成本项目 责任中心 会计科目
- Map<String, Department> departmentMap = costIncomeGroupService.getDepartmentByCodeNameMap(hospId);
- Map<String, Product> productMap = costIncomeGroupService.getProductByCodeNameMap(hospId);
- Map<Long, Responsibility> responsibilityMap = costIncomeGroupService.getResponsibilityIdResponsibilityMap(hospId);
- Map<Long, Accounting> accountingMap = costIncomeGroupService.getAccountIdAccountingMap(hospId);
- List<ResponsibilityDepartment> responsibilityDepartmentList = costIncomeGroupService.getResponsibilityDepartments(hospId);
- if (CollectionUtils.isEmpty(responsibilityDepartmentList)) {
- throw new CostException(500, "没有科室责任中心对照数据");
- }
- List<AccountingProduct> accountingProductList = accountingProductService.list(new QueryWrapper<AccountingProduct>().lambda().eq(AccountingProduct::getHospId, hospId));
- if (CollectionUtils.isEmpty(accountingProductList)) {
- throw new CostException(500, "没有成本会计对照数据");
- }
- Map<Long, Long> responsibilityDepMap = costIncomeGroupService.getDepartmentIdResponsibilityIdMap(responsibilityDepartmentList);
- Map<Long, Long> accountProMap = costIncomeGroupService.getProductIdAccountIdMap(accountingProductList);
- String endStatus = list.get(0).get(list.get(0).size() - 1).toString();
- // 要保存的数据
- List<CostCostingGroup> costCostingGroupArrayList = new LinkedList<>();
- // 判断最后科室代码的最后一个字符是否是-1
- if (!"-1".equals(endStatus)) {
- // 如果没有-1说明终结符
- IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
- incomeErrorMessage.setTotal(5);
- incomeErrorMessage.setErrMessage("科室代码末尾没有-1终结符");
- incomeErrorMessageList.add(incomeErrorMessage);
- // 文件上传
- String uploadFile = costIncomeGroupService.uploadFile(file, UserContext.getCurrentUser());
- // 上传记录保存
- if (StrUtil.isBlank(uploadFile)) {
- throw new CostException(500, "文件上传异常");
- }
- // 记录文件上传记录
- CostIncomeFile costIncomeFile = costIncomeFileService.saveCostIncomeFile(list, user, file, hospId, incomeErrorMessageList, uploadFile, fileType,year,month);
- return Result.build(500, "数据未成功导入", null);
- }
- // 检验数据的合理性
- // 检验成本数据
- checkCostData(list, incomeErrorMessageList, departmentMap, productMap, responsibilityMap, accountingMap, responsibilityDepMap, accountProMap, costCostingGroupArrayList, year, month);
- // 文件上传
- String uploadFile = costIncomeGroupService.uploadFile(file, UserContext.getCurrentUser());
- // 上传记录保存
- if (StrUtil.isBlank(uploadFile)) {
- throw new CostException(500, "文件上传异常");
- }
- // 记录文件上传记录
- CostIncomeFile costIncomeFile = costIncomeFileService.saveCostIncomeFile(list, user, file, hospId, incomeErrorMessageList, uploadFile, fileType,year,month);
- Long id = costIncomeFile.getId();
- costCostingGroupArrayList.forEach(i -> {
- // 设置文件Id
- i.setFileId(id);
- });
- if (CollectionUtils.isEmpty(incomeErrorMessageList)) {
- this.saveBatch(costCostingGroupArrayList);
- return Result.build(200, "数据导入成功", null);
- } else {
- return Result.build(200, "数据未成功导入", null);
- }
- }
- /**
- * 检验成本数据
- *
- * @param list
- * @param incomeErrorMessageList
- * @param departmentMap
- * @param productMap
- * @param responsibilityMap
- * @param accountingMap
- * @param responsibilityDepMap
- * @param accountProMap
- * @param costCostingGroupArrayList
- */
- private void checkCostData(List<List<Object>> list, List<IncomeErrorMessage> incomeErrorMessageList, Map<String, Department> departmentMap, Map<String, Product> productMap, Map<Long, Responsibility> responsibilityMap, Map<Long, Accounting> accountingMap, Map<Long, Long> responsibilityDepMap, Map<Long, Long> accountProMap, List<CostCostingGroup> costCostingGroupArrayList, Integer year, Integer month) {
- List<Object> departmentCodes = list.get(0);
- List<Object> departmentNames = list.get(1);
- //检验数据是否准确
- for (int i = 2; i < list.size(); i++) {
- int row=i+5;
- List<Object> data = list.get(i);
- for (int j = 2; j < departmentCodes.size(); j++) {
- int column=j+1;
- if (data.size() > j ) {
- if (Objects.isNull(data.get(j))){
- data.set(j, NumberConstant.ZERO);
- }else if ( data.get(j).toString().contains("-") || !StringUtils.isNumber(data.get(j).toString())){
- IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
- incomeErrorMessage.setTotal(row);
- incomeErrorMessage.setErrMessage("第"+row+"行 第"+column+"列数据不符合规范");
- incomeErrorMessageList.add(incomeErrorMessage);
- }else {
- data.set(j, Double.parseDouble(data.get(j).toString()));
- }
- }else {
- data.add(NumberConstant.ZERO);
- }
- }
- if (CollectionUtils.isEmpty(incomeErrorMessageList)){
- Integer emptyStatus = 0;
- for (int j = 2; j < data.size()-1 ; j++) {
- BigDecimal parseInt = new BigDecimal (data.get(j).toString());
- if (!BigDecimal.ZERO.equals(parseInt)) {
- emptyStatus = 1;
- break;
- }
- }
- // 检验成本项目是否正确
- CostCostingGroup costCostingGroupRequest = new CostCostingGroup();
- String productCode = data.get(0).toString();
- String productName = data.get(1).toString();
- Product product = productMap.get(productCode + productName);
- if (Objects.nonNull(product)) {
- Long id = product.getId();
- Long accountTingId = accountProMap.get(id);
- if (Objects.nonNull(accountTingId)) {
- Accounting accounting = accountingMap.get(accountTingId);
- if (Objects.nonNull(accounting)) {
- costCostingGroupRequest.setProductCode(productCode);
- costCostingGroupRequest.setProductName(productName);
- costCostingGroupRequest.setAccountCode(accounting.getAccountingCode());
- costCostingGroupRequest.setAccountName(accounting.getAccountingName());
- } else {
- IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
- incomeErrorMessage.setTotal(1);
- incomeErrorMessage.setErrMessage("成本代码" + productCode + " 成本名称" + productName + " 不存在对应的会计科目不存在");
- incomeErrorMessageList.add(incomeErrorMessage);
- }
- } else {
- IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
- incomeErrorMessage.setTotal(1);
- incomeErrorMessage.setErrMessage("成本代码" + productCode + " 成本名称" + productName + " 不存在对应的会计科目不存在");
- incomeErrorMessageList.add(incomeErrorMessage);
- }
- } else {
- IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
- incomeErrorMessage.setTotal(1);
- incomeErrorMessage.setErrMessage("成本代码" + productCode + " 成本名称" + productName + "不存在");
- incomeErrorMessageList.add(incomeErrorMessage);
- }
- // 0表示全为0 1表示不全为0
- BigDecimal combined = new BigDecimal (data.get(data.size() - 1).toString());
- if (NumberConstant.ZERO.equals(emptyStatus) &&BigDecimal.ZERO.equals(combined)) {
- // 全为0
- } else if (!BigDecimal.ZERO.equals(combined) && NumberConstant.ZERO.equals(emptyStatus)) {
- // 这条数据是保存到其他责任中心的
- CostCostingGroup costCostingGroup = costCostingGroupRequest;
- // TODO 设置其他责任中心
- costCostingGroup.setResponsibilityCode("其他责任中心的Code");
- costCostingGroup.setResponsibilityName("其他责任中心的Name");
- costCostingGroup.setAmount(combined);
- costCostingGroup.setHospId(UserContext.getHospId());
- costCostingGroup.setCreateTime(System.currentTimeMillis());
- costCostingGroup.setDateYear(year);
- costCostingGroup.setDateMonth(month);
- costCostingGroupArrayList.add(costCostingGroup);
- } else {
- for (int j = 2; j < data.size() - 1; j++) {
- CostCostingGroup costCostingGroup = BeanUtil.convertObj(costCostingGroupRequest,CostCostingGroup.class);
- // 检验科室信息是否准确
- String departmentCode = departmentCodes.get(j).toString();
- String departmentName = departmentNames.get(j).toString();
- Department department = departmentMap.get(departmentCode + departmentName);
- if (Objects.nonNull(department)) {
- // 检测责任中心是否存在
- Long id = department.getId();
- Long responsibilityId = responsibilityDepMap.get(id);
- if (Objects.nonNull(responsibilityId)) {
- Responsibility responsibility = responsibilityMap.get(responsibilityId);
- if (Objects.nonNull(responsibility)) {
- costCostingGroup.setDepartmentCode(departmentCode);
- costCostingGroup.setDepartmentName(departmentName);
- costCostingGroup.setResponsibilityCode(responsibility.getResponsibilityCode());
- costCostingGroup.setResponsibilityName(responsibility.getResponsibilityName());
- } else {
- IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
- incomeErrorMessage.setTotal(j);
- incomeErrorMessage.setErrMessage("第" + j + "列科室信息对应的责任中心不存在");
- incomeErrorMessageList.add(incomeErrorMessage);
- }
- } else {
- IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
- incomeErrorMessage.setTotal(j);
- incomeErrorMessage.setErrMessage("第" + j + "列科室信息不存在对应的责任中心");
- incomeErrorMessageList.add(incomeErrorMessage);
- }
- } else {
- IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
- incomeErrorMessage.setTotal(j);
- incomeErrorMessage.setErrMessage("第" + j + "列科室信息不存在");
- incomeErrorMessageList.add(incomeErrorMessage);
- }
- costCostingGroup.setAmount(new BigDecimal((Objects.isNull(data.get(j)) || "0".equals(data.get(j).toString())) ? "0.00" : data.get(j).toString()));
- costCostingGroup.setHospId(UserContext.getHospId());
- costCostingGroup.setCreateTime(System.currentTimeMillis());
- costCostingGroup.setDateYear(year);
- costCostingGroup.setDateMonth(month);
- costCostingGroupArrayList.add(costCostingGroup);
- }
- }
- }
- }
- }
- }
|