package com.doumee.service.business.impl; import com.doumee.biz.system.SystemDictDataBiz; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; import com.doumee.core.model.LoginUserInfo; import com.doumee.core.model.PageData; import com.doumee.core.model.PageWrap; import com.doumee.core.utils.Constants; import com.doumee.core.utils.DateUtil; import com.doumee.core.utils.Utils; import com.doumee.dao.business.*; import com.doumee.dao.business.dao.CompanyMapper; import com.doumee.dao.business.model.*; import com.doumee.dao.business.vo.EditRecordDataVO; import com.doumee.dao.system.MultifileMapper; import com.doumee.dao.system.SystemUserMapper; import com.doumee.dao.system.model.Multifile; import com.doumee.dao.system.model.SystemUser; import com.doumee.service.business.YwContractRevenueService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.yulichang.wrapper.MPJLambdaWrapper; import org.apache.commons.lang3.StringUtils; import org.checkerframework.checker.units.qual.C; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * 运维合同收支流水Service实现 * @author 江蹄蹄 * @date 2024/11/25 10:29 */ @Service public class YwContractRevenueServiceImpl implements YwContractRevenueService { @Autowired private YwContractRevenueMapper ywContractRevenueMapper; @Autowired private YwContractBillMapper ywContractBillMapper; @Autowired private YwContractMapper ywContractMapper; @Autowired private CompanyMapper companyMapper; @Autowired private YwAccountMapper ywAccountMapper; @Autowired private MultifileMapper multifileMapper; @Autowired private YwContractRoomMapper ywContractRoomMapper; @Autowired private SystemUserMapper systemUserMapper; @Autowired private SystemDictDataBiz systemDictDataBiz; @Override @Transactional(rollbackFor = {Exception.class,BusinessException.class}) public Integer create(YwContractRevenue ywContractRevenue) { if(Objects.isNull(ywContractRevenue) || Objects.isNull(ywContractRevenue.getActReceivableFee()) || Objects.isNull(ywContractRevenue.getActPayDate()) || Objects.isNull(ywContractRevenue.getPayType()) || Objects.isNull(ywContractRevenue.getCompanyId()) || Objects.isNull(ywContractRevenue.getAccountId()) || Objects.isNull(ywContractRevenue.getBillId()) || ywContractRevenue.getActReceivableFee().compareTo(BigDecimal.ZERO) <= Constants.ZERO ){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } YwContractBill ywContractBill = ywContractBillMapper.selectJoinOne(YwContractBill.class, new MPJLambdaWrapper() .selectAll(YwContractBill.class) .selectAs(YwContract::getStatus, YwContractBill::getContractStatus) .leftJoin(YwContract.class,YwContract::getId,YwContractBill::getContractId) .eq(YwContractBill::getId,ywContractRevenue.getBillId())); if(Objects.isNull(ywContractBill)){ throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到账单信息"); } if(!Constants.equalsInteger(ywContractBill.getStatus(),Constants.ZERO)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"账单状态已流转,无法进行该操作"); } if(!(Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.ZERO) || Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.TWO) || Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.THREE) || Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.FOUR) )){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"账单结清状态已流转,无法进行该操作"); } Company company =companyMapper.selectById(ywContractRevenue.getCompanyId()); if(Objects.isNull(company)){ throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到所属公司信息"); } YwAccount ywAccount =ywAccountMapper.selectById(ywContractRevenue.getAccountId()); if(Objects.isNull(ywAccount)){ throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到收支账户信息"); } LoginUserInfo loginUserInfo = ywContractRevenue.getLoginUserInfo(); ywContractRevenue.setCreateDate(new Date()); ywContractRevenue.setCreator(loginUserInfo.getId()); ywContractRevenue.setIsdeleted(Constants.ZERO); ywContractRevenue.setStatus(Constants.ZERO); ywContractRevenue.setContractId(ywContractBill.getContractId()); //根据收支情况 更新账单数据 //查询当前账单下 已支付的费用 List ywContractRevenueList = ywContractRevenueMapper.selectList(new QueryWrapper() .lambda().eq(YwContractRevenue::getIsdeleted,Constants.ZERO) .eq(YwContractRevenue::getStatus,Constants.ZERO) .eq(YwContractRevenue::getBillId,ywContractRevenue.getBillId()) ); //已支付金额 BigDecimal payTotal = BigDecimal.ZERO; //待支付金额 BigDecimal waitPayTotal = BigDecimal.ZERO; //无付款记录 则为初次支付 根据账单类型 判断是支出 / 收入 if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(ywContractRevenueList)){ ywContractRevenue.setRevenueType(ywContractBill.getBillType()); if(ywContractRevenue.getActReceivableFee().compareTo(ywContractBill.getReceivableFee())>Constants.ZERO){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"请输入正确的金额!"); }else if(ywContractRevenue.getActReceivableFee().compareTo(ywContractBill.getReceivableFee())==Constants.ZERO){ ywContractBill.setPayStatus(Constants.ONE); }else{ ywContractBill.setPayStatus(Constants.TWO); } }else{ //获取已支付的总金额 (可能有收入 有支出) if(Constants.equalsInteger(ywContractBill.getBillType(),Constants.ZERO)){ payTotal = ywContractRevenueList.stream().map(i-> i.getActReceivableFee().multiply( BigDecimal.valueOf(Constants.equalsInteger(i.getRevenueType(),Constants.ZERO)?Constants.ONE:-Constants.ONE)) ).reduce(BigDecimal.ZERO,BigDecimal::add); //获取待支付款金额 如果账单类型为支出 或 (账单类型为收入 且 待支付金额 小于 0) 则为支付 waitPayTotal = ywContractBill.getReceivableFee().subtract(payTotal); //如果待支付金额 大于 0 则是 收入 否则是支出 其他状态 为异常! if(waitPayTotal.compareTo(BigDecimal.ZERO)>Constants.ZERO){ ywContractRevenue.setRevenueType(Constants.ZERO); }else if(waitPayTotal.compareTo(BigDecimal.ZERO) i.getActReceivableFee().multiply( BigDecimal.valueOf(Constants.equalsInteger(i.getRevenueType(),Constants.ZERO)?-Constants.ONE:Constants.ONE)) ).reduce(BigDecimal.ZERO,BigDecimal::add); //获取待支付款金额 如果账单类型为支出 或 (账单类型为收入 且 待支付金额 小于 0) 则为支付 waitPayTotal = ywContractBill.getReceivableFee().subtract(payTotal); //如果待支付金额 大于 0 则是 收入 否则是支出 其他状态 为异常! if(waitPayTotal.compareTo(BigDecimal.ZERO)>Constants.ZERO){ ywContractRevenue.setRevenueType(Constants.ONE); }else if(waitPayTotal.compareTo(BigDecimal.ZERO)().lambda().eq(YwContractBill::getContractId,ywContractBill.getContractId()) .ne(YwContractBill::getId,ywContractBill.getId()) .eq(YwContractBill::getStatus,Constants.ZERO) .in(YwContractBill::getPayStatus,Constants.ZERO,Constants.FOUR, Constants.TWO,Constants.THREE) ) == Constants.ZERO){ ywContractMapper.update(new UpdateWrapper().lambda() .set(YwContract::getStatus,Constants.FOUR) .set(YwContract::getEditDate,DateUtil.getCurrDateTime()) .eq(YwContract::getId,ywContractBill.getContractId()) ); } } //存储附件信息 if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRevenue.getMultifileList())){ for (Multifile multifile:ywContractRevenue.getMultifileList()) { if(Objects.isNull(multifile) || StringUtils.isBlank(multifile.getFileurl()) || StringUtils.isBlank(multifile.getName())){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"附件信息错误"); } multifile.setCreator(loginUserInfo.getId()); multifile.setCreateDate(new Date()); multifile.setIsdeleted(Constants.ZERO); multifile.setObjType(Constants.MultiFile.FN_CONTRACT_REVENUE_FILE.getKey()); multifile.setObjId(ywContractRevenue.getId()); } multifileMapper.insert(ywContractRevenue.getMultifileList()); } return ywContractRevenue.getId(); } @Override public void deleteById(Integer id) { ywContractRevenueMapper.deleteById(id); } @Override public void delete(YwContractRevenue ywContractRevenue) { UpdateWrapper deleteWrapper = new UpdateWrapper<>(ywContractRevenue); ywContractRevenueMapper.delete(deleteWrapper); } @Override public void deleteByIdInBatch(List ids) { if (CollectionUtils.isEmpty(ids)) { return; } ywContractRevenueMapper.deleteBatchIds(ids); } @Override public void updateById(YwContractRevenue ywContractRevenue) { ywContractRevenueMapper.updateById(ywContractRevenue); } @Override public void updateByIdInBatch(List ywContractRevenues) { if (CollectionUtils.isEmpty(ywContractRevenues)) { return; } for (YwContractRevenue ywContractRevenue: ywContractRevenues) { this.updateById(ywContractRevenue); } } @Override public YwContractRevenue findById(Integer id) { return ywContractRevenueMapper.selectById(id); } @Override public YwContractRevenue getDetail(Integer id) { YwContractRevenue ywContractRevenue = ywContractRevenueMapper.selectJoinOne( YwContractRevenue.class, new MPJLambdaWrapper().selectAll(YwContractRevenue.class) .selectAs(YwContractBill::getCode,YwContractRevenue::getBillCode) .selectAs(YwContract::getCode,YwContractRevenue::getContractCode) .selectAs(YwContract::getStatus,YwContractRevenue::getContractStatus) .selectAs(YwCustomer::getName,YwContractRevenue::getCustomerName) .selectAs(SystemUser::getRealname,YwContractRevenue::getRealname) .selectAs(YwAccount::getTitle,YwContractRevenue::getAccountTitle) .leftJoin(YwContractBill.class,YwContractBill::getId,YwContractRevenue::getBillId) .leftJoin(YwContract.class,YwContract::getId,YwContractBill::getContractId) .leftJoin(YwCustomer.class,YwCustomer::getId,YwContract::getRenterId) .leftJoin(SystemUser.class,SystemUser::getId,YwContractRevenue::getCreator) .leftJoin(YwAccount.class,YwAccount::getId,YwContractRevenue::getAccountId) .eq(YwContractRevenue::getId,id) ); this.getRecordData(ywContractRevenue); //附件数据 List multifileList = multifileMapper.selectJoinList(Multifile.class,new MPJLambdaWrapper() .selectAll(Multifile.class) .selectAs(SystemUser::getRealname,Multifile::getUserName) .leftJoin(SystemUser.class,SystemUser::getId,Multifile::getCreator) .eq(Multifile::getObjId,id) .eq(Multifile::getIsdeleted,Constants.ZERO) .eq(Multifile::getObjType,Constants.MultiFile.FN_CONTRACT_REVENUE_FILE.getKey())); if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(multifileList)){ String path = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode() +systemDictDataBiz.queryByCode(Constants.FTP,Constants.YW_CONTRACT_BILL).getCode(); for (Multifile multifile:multifileList) { if(StringUtils.isNotBlank(multifile.getFileurl())){ multifile.setFileurlFull(path + multifile.getFileurl()); } } ywContractRevenue.setMultifileList(multifileList); } return ywContractRevenue; } /** * 操作记录 * @param ywContractRevenue */ public void getRecordData(YwContractRevenue ywContractRevenue){ List editRecordDataVOList = new ArrayList(); //模拟流水记录 if(Objects.nonNull(ywContractRevenue.getCreateDate())){ EditRecordDataVO editRecordDataVO = new EditRecordDataVO(); editRecordDataVO.setEditRemark("创建流水"); editRecordDataVO.setEditTime(ywContractRevenue.getCreateDate()); SystemUser createUser = systemUserMapper.selectById(ywContractRevenue.getCreator()); if(Objects.nonNull(createUser)){ editRecordDataVO.setEditUserName(createUser.getRealname()); } editRecordDataVOList.add(editRecordDataVO); } if(Objects.nonNull(ywContractRevenue.getEditor())){ EditRecordDataVO editRecordDataVO = new EditRecordDataVO(); editRecordDataVO.setEditRemark("关闭流水"); editRecordDataVO.setEditTime(ywContractRevenue.getEditDate()); SystemUser closeUser = systemUserMapper.selectById(ywContractRevenue.getEditor()); if(Objects.nonNull(closeUser)){ editRecordDataVO.setEditUserName(closeUser.getRealname()); } editRecordDataVOList.add(editRecordDataVO); } if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(editRecordDataVOList)){ ywContractRevenue.setEditRecordDataVOList(editRecordDataVOList); } } @Override public void closeRevenue(Integer id,LoginUserInfo loginUserInfo) { YwContractRevenue ywContractRevenue = ywContractRevenueMapper.selectById(id); if(Objects.isNull(ywContractRevenue)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } YwContractBill ywContractBill = ywContractBillMapper.selectById(ywContractRevenue.getBillId()); if(Objects.isNull(ywContractBill)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } YwContract ywContract = ywContractMapper.selectById(ywContractBill.getContractId()); if(Objects.isNull(ywContract)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } if(Constants.equalsInteger(ywContract.getStatus(),Constants.FOUR)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"合同状态已流转,无法操作"); } if(Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.ONE) || Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.FIVE)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"账单状态已流转,无法操作"); } //查询账单下 所有的账单 List ywContractRevenueList = ywContractRevenueMapper.selectList(new QueryWrapper().lambda().eq(YwContractRevenue::getStatus,Constants.ZERO) .eq(YwContractRevenue::getIsdeleted,Constants.ZERO) .eq(YwContractRevenue::getBillId,ywContractRevenue.getBillId()) .ne(YwContractRevenue::getId,id) ); if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(ywContractRevenueList)){ //无其他流水记录 根据账单创建类型 变更状态 //自建账单 根据账单收支类型进行 变更状态 if(Constants.equalsInteger(ywContractBill.getType(),Constants.ONE)){ if(Constants.equalsInteger(ywContractBill.getBillType(),Constants.ZERO)){ ywContractBill.setPayStatus(Constants.ZERO); }else{ ywContractBill.setPayStatus(Constants.THREE); } }else{ //合同账单 变为 待收款 ywContractBill.setPayStatus(Constants.ZERO); } }else{ //存在其他流水 则统一变更为 部分结算 ywContractBill.setPayStatus(Constants.TWO); } ywContractBillMapper.update(null,new UpdateWrapper().lambda() .set(YwContractBill::getPayStatus,ywContractBill.getPayStatus()) .set(YwContractBill::getEditor,loginUserInfo.getId()) .set(YwContractBill::getEditDate, DateUtil.getCurrDateTime()) .eq(YwContractBill::getId,ywContractBill.getId()) ); ywContractRevenueMapper.update(null,new UpdateWrapper().lambda() .set(YwContractRevenue::getStatus,Constants.ONE) .set(YwContractRevenue::getEditor,loginUserInfo.getId()) .set(YwContractRevenue::getEditDate,DateUtil.getCurrDateTime()) .eq(YwContractRevenue::getId,id)); } @Override public YwContractRevenue findOne(YwContractRevenue ywContractRevenue) { QueryWrapper wrapper = new QueryWrapper<>(ywContractRevenue); return ywContractRevenueMapper.selectOne(wrapper); } @Override public List findList(YwContractRevenue ywContractRevenue) { List ywContractRevenueList = ywContractRevenueMapper.selectJoinList(YwContractRevenue.class,new MPJLambdaWrapper() .selectAll(YwContractRevenue.class) .selectAs(YwCustomer::getName,YwContractRevenue::getCustomerName) .leftJoin(YwContract.class,YwContract::getId,YwContractRevenue::getContractId) .leftJoin(YwCustomer.class,YwCustomer::getId,YwContract::getRenterId) .eq(YwContractRevenue::getStatus,Constants.ZERO) .eq(Objects.nonNull(ywContractRevenue) && Objects.nonNull(ywContractRevenue.getBillId()),YwContractRevenue::getBillId,ywContractRevenue.getBillId()) ); return ywContractRevenueList; } @Override public PageData findPage(PageWrap pageWrap) { IPage page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); MPJLambdaWrapper queryWrapper = new MPJLambdaWrapper<>(); Utils.MP.blankToNull(pageWrap.getModel()); YwContractRevenue model = pageWrap.getModel(); IPage iPage = ywContractRevenueMapper.selectJoinPage(page,YwContractRevenue.class, queryWrapper.selectAll(YwContractRevenue.class) .selectAs(YwContractBill::getCode,YwContractRevenue::getBillCode) .selectAs(YwContractBill::getType,YwContractRevenue::getBillType) .selectAs(YwContract::getCode,YwContractRevenue::getContractCode) .selectAs(YwContract::getStatus,YwContractRevenue::getContractStatus) .selectAs(YwCustomer::getName,YwContractRevenue::getCustomerName) .selectAs(SystemUser::getRealname,YwContractRevenue::getRealname) .leftJoin(YwContractBill.class,YwContractBill::getId,YwContractRevenue::getBillId) .leftJoin(YwContract.class,YwContract::getId,YwContractBill::getContractId) .leftJoin(YwCustomer.class,YwCustomer::getId,YwContract::getRenterId) .leftJoin(SystemUser.class,SystemUser::getId,YwContractRevenue::getCreator) .eq(YwContractRevenue::getIsdeleted,Constants.ZERO) .like(Objects.nonNull(model)&&StringUtils.isNotBlank(model.getCustomerName()), YwCustomer::getName,model.getCustomerName()) .eq(Objects.nonNull(model)&&Objects.nonNull(model.getPayType()), YwContractRevenue::getPayType,model.getPayType()) .eq(Objects.nonNull(model)&&Objects.nonNull(model.getRevenueType()), YwContractRevenue::getRevenueType,model.getRevenueType()) .ge(Objects.nonNull(model)&&Objects.nonNull(model.getPayDateStart()),YwContractRevenue::getActPayDate, Utils.Date.getStart(model.getPayDateStart())) .le(Objects.nonNull(model)&&Objects.nonNull(model.getPayDateEnd()),YwContractRevenue::getActPayDate, Utils.Date.getEnd(model.getPayDateEnd())) .orderByDesc(YwContractRevenue::getId)) ; this.dealRoomDetail(iPage.getRecords()); for (YwContractRevenue ywContractRevenue:iPage.getRecords()) { //楼宇名称 List ywContractRoomList = ywContractRevenue.getYwContractRoomList(); if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRoomList)){ StringBuilder roomPathName = new StringBuilder(); for (YwContractRoom ywContractRoom:ywContractRoomList) { if(Constants.equalsInteger(ywContractRevenue.getBillType(),Constants.ZERO)){ if(!(Constants.equalsInteger(ywContractRevenue.getContractId(),ywContractRoom.getContractId()) && Constants.equalsInteger(ywContractRoom.getType(),Constants.ZERO))){ continue; } }else{ if(!(Constants.equalsInteger(ywContractRevenue.getBillId(),ywContractRoom.getContractId()) && Constants.equalsInteger(ywContractRoom.getType(),Constants.ONE))){ continue; } } if(StringUtils.isNotBlank(ywContractRoom.getProjectName())){ roomPathName.append(ywContractRoom.getProjectName()+"/"); } if(StringUtils.isNotBlank(ywContractRoom.getBuildingName())){ roomPathName.append(ywContractRoom.getBuildingName()+"/"); } if(StringUtils.isNotBlank(ywContractRoom.getFloorName())){ roomPathName.append(ywContractRoom.getFloorName()+"/"); } if(StringUtils.isNotBlank(ywContractRoom.getRoomName())){ roomPathName.append(ywContractRoom.getRoomName()); } if(StringUtils.isNotBlank(roomPathName)){ roomPathName.append(";"); } } ywContractRevenue.setRoomPathName(roomPathName.toString()); } } return PageData.from(iPage); } public void dealRoomDetail(List ywContractRevenueList){ //查询账单下的楼宇数据 if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRevenueList)){ //获取所有数据 List billIdList = ywContractRevenueList.stream().map(i->i.getBillId()).collect(Collectors.toList()); List contractIdList = ywContractRevenueList.stream().map(i->i.getContractId()).collect(Collectors.toList()); if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(billIdList)){ List ywContractRoomList = ywContractRoomMapper.selectJoinList(YwContractRoom.class,new MPJLambdaWrapper() .selectAll(YwContractRoom.class) .selectAs(YwProject::getName,YwRoom::getProjectName) .selectAs(YwFloor::getName,YwRoom::getFloorName) .selectAs(YwBuilding::getName,YwRoom::getBuildingName) .selectAs(YwRoom::getCode,YwContractRoom::getRoomName) .leftJoin(YwRoom.class,YwRoom::getId,YwContractRoom::getRoomId) .leftJoin(YwFloor.class,YwFloor::getId,YwRoom::getFloor) .leftJoin(YwProject.class,YwProject::getId,YwRoom::getProjectId) .leftJoin(YwBuilding.class,YwBuilding::getId,YwRoom::getBuildingId) .and(i->i.in(YwContractRoom::getContractId,billIdList).eq(YwContractRoom::getType,Constants.ONE) .or().in(YwContractRoom::getContractId,contractIdList).eq(YwContractRoom::getType,Constants.ZERO) ) // .in(YwContractRoom::getContractId,billIdList) // .eq(YwContractRoom::getType,Constants.ONE) ); if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRoomList)){ for (YwContractRevenue ywContractRevenue:ywContractRevenueList) { ywContractRevenue.setYwContractRoomList( ywContractRoomList.stream().filter(i-> Constants.equalsInteger(i.getContractId(),ywContractRevenue.getBillId())||Constants.equalsInteger(i.getContractId(),ywContractRevenue.getContractId())) .collect(Collectors.toList()) ); } } } } } @Override public long count(YwContractRevenue ywContractRevenue) { QueryWrapper wrapper = new QueryWrapper<>(ywContractRevenue); return ywContractRevenueMapper.selectCount(wrapper); } }