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.BeanUtil; import com.imed.costaccount.common.util.DateUtils; import com.imed.costaccount.common.util.PageUtils; import com.imed.costaccount.enums.DateStyleEnum; import com.imed.costaccount.mapper.CostOtherPaymentsDataMapper; import com.imed.costaccount.model.CostOtherPaymentsData; import com.imed.costaccount.model.dto.CostOtherPaymentsDataEditDto; import com.imed.costaccount.model.dto.CostOtherPaymentsDataSaveDto; import com.imed.costaccount.model.vo.CostOtherPaymentsDataVO; import com.imed.costaccount.service.CostOtherPaymentsDataService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List; import java.util.Objects; @Service("costOtherPaymentsDataService") public class CostOtherPaymentsDataServiceImpl extends ServiceImpl implements CostOtherPaymentsDataService { /** * 分页查询 * * @param current 第几页 * @param pageSize 每页大小 * @param dateTime 时间 * @param hospId 医院Id * @return */ @Override public PageUtils queryList(Integer current, Integer pageSize, String dateTime, Long hospId) { // 先检验当前年月是否存在数据 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; } Page paymentsDataPage = new Page<>(current, pageSize); Page pages = this.page(paymentsDataPage, new QueryWrapper().lambda() .eq(CostOtherPaymentsData::getHospId, hospId) .eq(StrUtil.isNotBlank(dateTime),CostOtherPaymentsData::getDateYear,year) .eq(StrUtil.isNotBlank(dateTime),CostOtherPaymentsData::getDateMonth,month) .orderByDesc(CostOtherPaymentsData::getCreateTime)); List records = pages.getRecords(); List costOtherPaymentsDataVOList = BeanUtil.convertList(records, CostOtherPaymentsDataVO.class); PageUtils pageUtils = new PageUtils(pages); pageUtils.setList(costOtherPaymentsDataVOList); return pageUtils; } /** * 保存全院其他收支数据 * * @param costOtherPaymentsDataSaveDto 全院其他收支 * @param hospId 医院的Id */ @Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public void addOtherPaymentData(CostOtherPaymentsDataSaveDto costOtherPaymentsDataSaveDto, Long hospId) { String dateTime = costOtherPaymentsDataSaveDto.getDateTime(); // 先检验当前年月是否存在数据 checkOtherPaymentData(costOtherPaymentsDataSaveDto, hospId, dateTime); CostOtherPaymentsData costOtherPaymentsData = BeanUtil.convertObj(costOtherPaymentsDataSaveDto, CostOtherPaymentsData.class); 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; } costOtherPaymentsData.setHospId(hospId); costOtherPaymentsData.setCreateTime(System.currentTimeMillis()); costOtherPaymentsData.setDateYear(year); costOtherPaymentsData.setDateMonth(month); this.save(costOtherPaymentsData); } // 先检验当前年月是否存在数据 private void checkOtherPaymentData(CostOtherPaymentsDataSaveDto costOtherPaymentsDataSaveDto, Long hospId, String dateTime) { 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; } Integer paymentsType = costOtherPaymentsDataSaveDto.getPaymentsType(); String paymentsName = costOtherPaymentsDataSaveDto.getPaymentsName(); CostOtherPaymentsData paymentsData = this.getOne(new QueryWrapper().lambda() .eq(CostOtherPaymentsData::getHospId, hospId) .eq(CostOtherPaymentsData::getDateYear, year).eq(CostOtherPaymentsData::getDateMonth, month) .eq(CostOtherPaymentsData::getPaymentsType, paymentsType).eq(CostOtherPaymentsData::getPaymentsName, paymentsName)); if (Objects.nonNull(paymentsData)){ throw new CostException(500,"当前年月添加的数据已存在"); } } /** * 修改全院其他收支数据 * * @param costOtherPaymentsDataEditDto * @param hospId */ @Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public void updateOtherPaymentData(CostOtherPaymentsDataEditDto costOtherPaymentsDataEditDto, Long hospId) { String dateTime = costOtherPaymentsDataEditDto.getDateTime(); int year = 0; int month = 0; if (StrUtil.isNotBlank(dateTime)) { Date date = DateUtils.StringToDate(dateTime, DateStyleEnum.YYYY_MM); year = DateUtil.year(date); month = DateUtil.month(date) + 1; } Long id = costOtherPaymentsDataEditDto.getId(); CostOtherPaymentsData otherPaymentsData = this.getById(id); if (Objects.isNull(otherPaymentsData)){ throw new CostException(500,"全院其他收支数据不存在"); } this.removeById(id); CostOtherPaymentsDataSaveDto costOtherPaymentsDataSaveDto = BeanUtil.convertObj(costOtherPaymentsDataEditDto, CostOtherPaymentsDataSaveDto.class); // 检验数据 checkOtherPaymentData(costOtherPaymentsDataSaveDto,hospId,dateTime); // 实现数据添加 CostOtherPaymentsData costOtherPaymentsData = BeanUtil.convertObj(costOtherPaymentsDataSaveDto, CostOtherPaymentsData.class); costOtherPaymentsData.setCreateTime(System.currentTimeMillis()); costOtherPaymentsData.setHospId(hospId); costOtherPaymentsData.setDateYear(year); costOtherPaymentsData.setDateMonth(month); this.save(costOtherPaymentsData); } }