123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- package com.imed.costaccount.service.impl;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.imed.costaccount.common.exception.CostException;
- import com.imed.costaccount.common.util.*;
- import com.imed.costaccount.constants.NumberConstant;
- import com.imed.costaccount.enums.DateStyleEnum;
- import com.imed.costaccount.enums.ErrorCodeEnum;
- import com.imed.costaccount.mapper.CostIncomeGroupMapper;
- import com.imed.costaccount.model.*;
- import com.imed.costaccount.model.vo.CostIncomeGroupAllAmountVO;
- import com.imed.costaccount.model.vo.CostIncomeGroupBeforeVO;
- import com.imed.costaccount.model.vo.IncomeErrorMessage;
- import com.imed.costaccount.service.*;
- import lombok.extern.slf4j.Slf4j;
- import org.jetbrains.annotations.NotNull;
- import org.springframework.beans.factory.annotation.Value;
- 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.io.File;
- import java.io.IOException;
- import java.math.BigDecimal;
- import java.util.*;
- import java.util.concurrent.atomic.AtomicReference;
- import java.util.stream.Collectors;
- @Slf4j
- @Service("costIncomeGroupService")
- public class CostIncomeGroupServiceImpl extends ServiceImpl<CostIncomeGroupMapper, CostIncomeGroup> implements CostIncomeGroupService {
- @Value("${file.serverPath}")
- private String fileTempPath;
- @Value("${file.serverUrl}")
- private String FILE_PATH;
- private final DepartmentService departmentService;
- private final ResponsibilityService responsibilityService;
- private final ProductService productService;
- private final AccountingService accountingService;
- private final ResponsibilityDepartmentService responsibilityDepartmentService;
- private final AccountingProductService accountingProductService;
- private final CostIncomeFileService costIncomeFileService;
- public CostIncomeGroupServiceImpl(DepartmentService departmentService, ResponsibilityService responsibilityService, ProductService productService, AccountingService accountingService, ResponsibilityDepartmentService responsibilityDepartmentService, AccountingProductService accountingProductService, CostIncomeFileService costIncomeFileService) {
- this.departmentService = departmentService;
- this.responsibilityService = responsibilityService;
- this.productService = productService;
- this.accountingService = accountingService;
- this.responsibilityDepartmentService = responsibilityDepartmentService;
- this.accountingProductService = accountingProductService;
- this.costIncomeFileService = costIncomeFileService;
- }
- /**
- * 分页查询收入归集前的数据
- *
- * @param current 当前页
- * @param pageSize 当前页大小
- * @param dateTime 年月
- * @param responsibilityCode 责任中心代码
- * @param accountCode 会计科目的Code
- * @param hospId 医院Id
- * @return
- */
- @Override
- public PageUtils queryList(Integer current, Integer pageSize, String dateTime, String responsibilityCode, String accountCode, Long hospId) {
- int year = 0;
- int month = 0;
- if (StrUtil.isNotBlank(dateTime)) {
- year = DateUtils.getYear(dateTime);
- month = DateUtils.getMonth(dateTime);
- }
- Page<CostIncomeGroup> costIncomeGroupPage = new Page<>(current, pageSize);
- Page<CostIncomeGroup> pages = this.page(costIncomeGroupPage, new QueryWrapper<CostIncomeGroup>().lambda()
- .eq(Objects.nonNull(hospId), CostIncomeGroup::getHospId, hospId)
- .eq(!NumberConstant.ZERO.equals(year), CostIncomeGroup::getDateYear, year)
- .eq(!NumberConstant.ONE.equals(month), CostIncomeGroup::getDateMonth, month)
- .and(StrUtil.isNotBlank(responsibilityCode), i -> i.like(CostIncomeGroup::getOpenResponsibilityCode, responsibilityCode)
- .or().like(CostIncomeGroup::getStartResponsibilityCode, responsibilityCode))
- .like(StrUtil.isNotBlank(accountCode), CostIncomeGroup::getAccountCode, accountCode));
- List<CostIncomeGroup> records = pages.getRecords();
- List<CostIncomeGroupBeforeVO> costIncomeGroupBeforeVOList = BeanUtil.convertList(records, CostIncomeGroupBeforeVO.class);
- // 查询所有的责任中心 科室 会计科目 成本项目的数据 处理名字
- // TODO 修改直接获取名字
- setCodeName(hospId, costIncomeGroupBeforeVOList);
- // 进行金额合并
- List<CostIncomeGroupAllAmountVO> costIncomeGroupAllAmountVoS = baseMapper.countMoney(costIncomeGroupBeforeVOList);
- // 对,的金额进行合并
- AtomicReference<BigDecimal> totalAmount = new AtomicReference<>(new BigDecimal("0.0000"));
- costIncomeGroupAllAmountVoS.forEach(i -> {
- String allMoney = i.getAllMoney();
- if (StrUtil.isNotBlank(allMoney) && allMoney.contains(StrUtil.COMMA)) {
- // 存在,在进行求和
- long sum;
- List<Long> list = Arrays.stream(allMoney.split(StrUtil.COMMA)).map(Long::valueOf).collect(Collectors.toList());
- sum = list.stream().mapToLong(m -> m).sum();
- i.setAmount(BigDecimal.valueOf(sum));
- }
- //TODO 统计总金额
- totalAmount.updateAndGet(v -> v.add(i.getAmount()));
- });
- PageUtils pageUtils = new PageUtils(pages);
- pageUtils.setList(costIncomeGroupAllAmountVoS);
- pageUtils.setTotalAmount(totalAmount.get());
- return pageUtils;
- }
- /**
- * 批量导入收入数据
- *
- * @param list 输入的文件
- * @param user 用户
- * @param file
- * @return
- */
- @Override
- @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
- public Result importIncomeGroup(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_DD);
- if (StrUtil.isNotBlank(dateTime)) {
- year = DateUtil.year(date);
- month =DateUtil.month(date)+1;
- }
- Long hospId = user.getHospId();
- List<CostIncomeGroup> groups = this.list(new QueryWrapper<CostIncomeGroup>().lambda().eq(CostIncomeGroup::getHospId, hospId)
- .eq(CostIncomeGroup::getDateYear, year).eq(CostIncomeGroup::getDateMonth, month));
- if (!CollectionUtils.isEmpty(groups)){
- throw new CostException(500,year+"年"+month+"月数据已存在");
- }
- // 移除前几行的抬头内容 list的大小对应的就是行数的大小
- 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));
- }
- }
- log.info("读取的数据为:{}", list);
- List<IncomeErrorMessage> incomeErrorMessageList = new ArrayList<>();
- //获取所有的科室 成本项目 责任中心 会计科目
- //
- Map<String, Department> departmentMap = getDepartmentByCodeNameMap(hospId);
- Map<String, Product> productMap = getProductByCodeNameMap(hospId);
- Map<Long, Responsibility> responsibilityMap = getLongResponsibilityMap(hospId);
- Map<Long, Accounting> accountingMap = getLongAccountingMap(hospId);
- List<ResponsibilityDepartment> responsibilityDepartmentList = 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 = getDepartmentIdResponsibilityIdMap(responsibilityDepartmentList);
- Map<Long, Long> accountProMap = getProductIdAccountIdMap(accountingProductList);
- List<CostIncomeGroup> costIncomeGroupArrayList = new ArrayList<>();
- // 检验数据
- checkImportData(list, incomeErrorMessageList, costIncomeGroupArrayList, departmentMap, productMap, responsibilityMap, accountingMap, responsibilityDepMap, accountProMap,hospId,year,month);
- // 文件上传
- String uploadFile = uploadFile(file, UserContext.getCurrentUser());
- // 上传记录保存
- if (StrUtil.isBlank(uploadFile)){
- throw new CostException(500,"文件上传异常");
- }
- // 记录文件上传记录
- CostIncomeFile costIncomeFile =costIncomeFileService.saveCostIncomeFile(list, user, file, hospId, incomeErrorMessageList, uploadFile,fileType);
- Long id = costIncomeFile.getId();
- costIncomeGroupArrayList.forEach(i->{
- // 设置文件Id
- i.setFileId(id);
- });
- if (CollectionUtils.isEmpty(incomeErrorMessageList)){
- this.saveBatch(costIncomeGroupArrayList);
- }
- return Result.ok();
- }
- /**
- * 返回成本会计对照数据 Key 成本项目Id Value 会计科目Id
- * @param accountingProductList
- * @return
- */
- @NotNull
- private Map<Long, Long> getProductIdAccountIdMap(List<AccountingProduct> accountingProductList) {
- return accountingProductList.stream().collect(Collectors.toMap(AccountingProduct::getProductId, AccountingProduct::getAccountingId));
- }
- /**
- *
- * @param responsibilityDepartmentList
- * @return
- */
- @NotNull
- private Map<Long, Long> getDepartmentIdResponsibilityIdMap(List<ResponsibilityDepartment> responsibilityDepartmentList) {
- return responsibilityDepartmentList.stream().collect(Collectors.toMap(ResponsibilityDepartment::getDepartmentId, ResponsibilityDepartment::getResponsibilityId));
- }
- private List<ResponsibilityDepartment> getResponsibilityDepartments(Long hospId) {
- return responsibilityDepartmentService.list(new QueryWrapper<ResponsibilityDepartment>().lambda()
- .eq(ResponsibilityDepartment::getHospId, hospId));
- }
- @NotNull
- private Map<Long, Accounting> getLongAccountingMap(Long hospId) {
- return accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId)).stream().collect(Collectors.toMap(Accounting::getId, synOe -> synOe));
- }
- @NotNull
- private Map<Long, Responsibility> getLongResponsibilityMap(Long hospId) {
- return responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId)).stream().collect(Collectors.toMap(Responsibility::getId, synOe -> synOe));
- }
- @NotNull
- private Map<String, Product> getProductByCodeNameMap(Long hospId) {
- return productService.list(new QueryWrapper<Product>().lambda().eq(Product::getHospId, hospId)).stream().collect(Collectors.toMap(k -> k.getProductCode() + k.getProductName(), synOe -> synOe));
- }
- @NotNull
- private Map<String, Department> getDepartmentByCodeNameMap(Long hospId) {
- return departmentService.list(new QueryWrapper<Department>().lambda().eq(Department::getHospId, hospId)).stream().collect(Collectors.toMap(k -> k.getDepartmentCode() + k.getDepartmentName(), synOe -> synOe));
- }
- /**
- * 导入收入数据
- *
- * @param read 读取到的初始数据
- * @param user 当前登录用户
- * @param file 导入的文件
- * @param year 年
- * @param month 月
- * @return {@link Result}
- */
- @Override
- public Result importDataByIncomeData(List<List<Object>> read, User user, MultipartFile file, Integer year, Integer month) {
- return null;
- }
- /**
- * 检验数据
- * @param list 表单数据
- * @param incomeErrorMessageList 存储错误信息的集合
- * @param costIncomeGroupArrayList
- * @param departmentMap 科室Map
- * @param productMap 成本项目map
- * @param responsibilityMap 责任中心Map
- * @param accountingMap 会计科目Map
- * @param responsibilityDepMap 责任中心科室对照Map
- * @param accountProMap 责任中心Map
- * @param hospId
- */
- private void checkImportData(List<List<Object>> list, List<IncomeErrorMessage> incomeErrorMessageList, List<CostIncomeGroup> costIncomeGroupArrayList, Map<String, Department> departmentMap, Map<String, Product> productMap, Map<Long, Responsibility> responsibilityMap, Map<Long, Accounting> accountingMap, Map<Long, Long> responsibilityDepMap, Map<Long, Long> accountProMap, Long hospId,Integer year,Integer month) {
- for (int i = 0; i < list.size(); i++) {
- CostIncomeGroup costIncomeGroup = new CostIncomeGroup();
- // 用来检验数据合理性的循环
- IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
- List<Object> data = list.get(i);
- log.info("用户输入的数据是{}", data);
- // 成本项目的代码和名称
- String productCode = data.get(0).toString();
- String productName = data.get(1).toString();
- // 开单科室 执行科室的代码和名称
- String openDepartmentName = data.get(2).toString();
- String openDepartmentCode = data.get(3).toString();
- String startDepartmentName = data.get(4).toString();
- String startDepartmentCode = data.get(5).toString();
- BigDecimal beforeMoney = BigDecimal.valueOf(Double.parseDouble(data.get(15).toString()));
- boolean checkNull = StrUtil.isBlank(data.get(15).toString());
- boolean checkOne = NumberConstant.ZERO.equals(data.get(15).toString());
- if (checkNull || checkOne) {
- // 要求这一行的数据必须全部填写
- for (int j = 0; j < NumberConstant.FIVE; j++) {
- if (Objects.isNull(data.get(j))) {
- incomeErrorMessage.setTotal(i);
- int row = i + 1;
- int column = j + 1;
- incomeErrorMessage.setErrMessage("第{" + row + "}行第{" + column + "}列数据为空");
- incomeErrorMessageList.add(incomeErrorMessage);
- }
- }
- }
- // 检验收入成本项目数据是否存在
- Product product = productMap.get(productCode + productName);
- Department department = departmentMap.get(openDepartmentCode + openDepartmentName);
- Department department1 = departmentMap.get(startDepartmentCode + startDepartmentName);
- if (!checkNull && !checkOne ){
- if (StrUtil.isNotBlank(productCode) && StrUtil.isNotBlank(productName)){
- if (Objects.isNull(product)) {
- incomeErrorMessage.setTotal(i);
- incomeErrorMessage.setErrMessage("代码:" + productCode + " 名称:" + productName + "成本项目不存在");
- incomeErrorMessageList.add(incomeErrorMessage);
- } else {
- // 检验对应的会计科目是否存在
- Long id = product.getId();
- Long accountId = accountProMap.get(id);
- if (Objects.isNull(accountingMap.get(accountId))) {
- incomeErrorMessage.setTotal(i);
- incomeErrorMessage.setErrMessage("代码:" + productCode + " 名称:" + productName + "成本项目对应的会计科目不存在");
- incomeErrorMessageList.add(incomeErrorMessage);
- } else {
- costIncomeGroup.setProductCode(productCode);
- costIncomeGroup.setProductName(productName);
- costIncomeGroup.setAccountCode(accountingMap.get(accountId).getAccountingCode());
- costIncomeGroup.setAccountName(accountingMap.get(accountId).getAccountingName());
- }
- }
- }else{
- costIncomeGroup.setProductCode(null);
- costIncomeGroup.setProductName(null);
- costIncomeGroup.setAccountCode( null);
- costIncomeGroup.setAccountName(null);
- }
- }
- // 检验开单科室
- if (!checkNull && !checkOne ){
- if (StrUtil.isNotBlank(openDepartmentCode) && StrUtil.isNotBlank(openDepartmentName)){
- // 开单科室
- if (Objects.isNull(department)) {
- incomeErrorMessage.setTotal(i);
- incomeErrorMessage.setErrMessage("代码:" + productCode + " 名称:" + productName + "开单科室不存在");
- incomeErrorMessageList.add(incomeErrorMessage);
- } else {
- Long id = department.getId();
- Long responsibilityId = responsibilityDepMap.get(id);
- if (Objects.isNull(responsibilityMap.get(responsibilityId))) {
- incomeErrorMessage.setTotal(i);
- incomeErrorMessage.setErrMessage("代码:" + openDepartmentCode + " 名称:" + openDepartmentName + "科室对应的责任中心不存在");
- incomeErrorMessageList.add(incomeErrorMessage);
- } else {
- costIncomeGroup.setOpenDepartmentCode(openDepartmentCode);
- costIncomeGroup.setOpenDepartmentName(openDepartmentName);
- costIncomeGroup.setOpenResponsibilityCode(responsibilityMap.get(responsibilityId).getResponsibilityCode());
- costIncomeGroup.setOpenResponsibilityName(responsibilityMap.get(responsibilityId).getResponsibilityName());
- }
- }
- }else {
- costIncomeGroup.setOpenDepartmentCode(null);
- costIncomeGroup.setOpenDepartmentName(null);
- costIncomeGroup.setOpenResponsibilityCode(null);
- costIncomeGroup.setOpenResponsibilityName(null);
- }
- }
- // 检验执行科室
- if ( !checkNull && !checkOne ){
- if (StrUtil.isNotBlank(startDepartmentCode) && StrUtil.isNotBlank(startDepartmentName)){
- //执行科室
- if (Objects.isNull(department1)) {
- incomeErrorMessage.setTotal(i);
- incomeErrorMessage.setErrMessage("代码:" + productCode + " 名称:" + productName + "执行科室不存在");
- incomeErrorMessageList.add(incomeErrorMessage);
- } else {
- Long id = department1.getId();
- Long responsibilityId = responsibilityDepMap.get(id);
- if (Objects.isNull(responsibilityMap.get(responsibilityId))) {
- incomeErrorMessage.setTotal(i);
- incomeErrorMessage.setErrMessage("代码:" + openDepartmentCode + " 名称:" + openDepartmentName + "科室对应的责任中心不存在");
- incomeErrorMessageList.add(incomeErrorMessage);
- } else {
- costIncomeGroup.setStartDepartmentCode(startDepartmentCode);
- costIncomeGroup.setStartDepartmentName(startDepartmentName);
- costIncomeGroup.setStartResponsibilityCode(responsibilityMap.get(responsibilityId).getResponsibilityCode());
- costIncomeGroup.setStartResponsibilityName(responsibilityMap.get(responsibilityId).getResponsibilityName());
- }
- }
- }else {
- costIncomeGroup.setStartDepartmentCode(null);
- costIncomeGroup.setStartDepartmentName(null);
- costIncomeGroup.setStartResponsibilityCode(null);
- costIncomeGroup.setStartResponsibilityName(null);
- }
- }
- costIncomeGroup.setDoctorNumber(Long.parseLong(data.get(6).toString())).setDoctorName(data.get(7).toString())
- .setPatientId(Long.parseLong(data.get(8).toString())).setOutpatientId(Long.parseLong(data.get(9).toString()))
- .setPatientName(data.get(10).toString()).setPatientFee(data.get(11).toString()).setReceiptFee(data.get(12).toString())
- .setTotalNumber(Integer.parseInt(data.get(13).toString())).setUnit(data.get(14).toString())
- .setFeeDatetime(DateUtils.StringToDate(data.get(16).toString(), DateStyleEnum.YYYY_MM_DD_HH_MM_SS_EN));
- //
- costIncomeGroup.setHospId(hospId);
- costIncomeGroup.setCreateTime(System.currentTimeMillis());
- costIncomeGroup.setDateYear(year);
- costIncomeGroup.setDateMonth(month);
- costIncomeGroup.setAmount(beforeMoney);
- costIncomeGroupArrayList.add(costIncomeGroup);
- }
- }
- /**
- * 设置相关名称
- *
- * @param hospId
- * @param costIncomeGroupBeforeVOList
- */
- private void setCodeName(Long hospId, List<CostIncomeGroupBeforeVO> costIncomeGroupBeforeVOList) {
- // List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId));
- // Map<String, String> responsibilityMap = responsibilityList.stream().collect(Collectors.toMap(Responsibility::getResponsibilityCode, Responsibility::getResponsibilityName));
- // List<Department> departmentList = departmentService.list(new QueryWrapper<Department>().lambda().eq(Department::getHospId, hospId));
- // Map<String, String> departmentMap = departmentList.stream().collect(Collectors.toMap(Department::getDepartmentCode, Department::getDepartmentName));
- // List<Product> productList = productService.list(new QueryWrapper<Product>().lambda().eq(Product::getHospId, hospId));
- // Map<String, String> productMap = productList.stream().collect(Collectors.toMap(Product::getProductCode, Product::getProductName));
- // List<Accounting> accountingList = accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId));
- // Map<String, String> accountMap = accountingList.stream().collect(Collectors.toMap(Accounting::getAccountingCode, Accounting::getAccountingName));
- costIncomeGroupBeforeVOList.forEach(i -> {
- // 以为这里的数据导入的 在导入的时候进行数据校验
- // 设置开单科室名称 执行科室名称 开单责任中心名称 执行责任中心名称 成本项目的名称 会计科目名称
- i.setOpenDepartmentCodeName("[" + i.getOpenDepartmentCode() + "]" + i.getOpenDepartmentName());
- i.setOpenResponsibilityCodeName("[" + i.getOpenResponsibilityCode() + "]" + i.getOpenResponsibilityName());
- i.setStartDepartmentCodeName("[" + i.getStartDepartmentCode() + "]" + i.getStartDepartmentName());
- i.setStartResponsibilityCodeName("[" + i.getStartResponsibilityCode() + "]" + i.getStartResponsibilityCode());
- i.setProductCodeName("[" + i.getProductCode() + "]" + i.getProductName());
- i.setAccountCodeName("[" + i.getAccountCode() + "]" + i.getAccountName());
- });
- }
- /**
- * 文件上传
- */
- public String uploadFile(MultipartFile file, User user) {
- Long hospId = user.getHospId();
- String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."))+System.currentTimeMillis()+".xsl";
- String localFilePath = fileTempPath + StrUtil.SLASH + hospId + StrUtil.SLASH;
- File tempFile = new File(localFilePath);
- if (!tempFile.exists()) {
- tempFile.mkdirs();
- }
- localFilePath = localFilePath + fileName;
- try {
- file.transferTo(new File(localFilePath));
- } catch (IOException e) {
- log.error("【文件上传至本地】失败,绝对路径:{}", e.getMessage());
- throw new CostException(ErrorCodeEnum.FILE_UPLOAD_ERROR);
- } finally {
- }
- return FILE_PATH + hospId + StrUtil.SLASH + fileName;
- }
- }
|