/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the dreamlu.net developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ package org.sxkj.gd.orderdata.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import lombok.AllArgsConstructor; import org.springblade.core.log.exception.ServiceException; import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.utils.BeanUtil; import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.StringUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.sxkj.gd.orderdata.dto.GdSupplyDemandDTO; import org.sxkj.gd.orderdata.entity.GdSupplyDemandAuditAttachmentEntity; import org.sxkj.gd.orderdata.entity.GdSupplyDemandAuditEntity; import org.sxkj.gd.orderdata.entity.GdSupplyDemandEntity; import org.sxkj.gd.orderdata.excel.GdSupplyDemandExcel; import org.sxkj.gd.orderdata.mapper.GdSupplyDemandMapper; import org.sxkj.gd.orderdata.param.GdSupplyDemandPageParam; import org.sxkj.gd.orderdata.service.IGdSupplyDemandAuditAttachmentService; import org.sxkj.gd.orderdata.service.IGdSupplyDemandAuditService; import org.sxkj.gd.orderdata.service.IGdSupplyDemandService; import org.sxkj.gd.orderdata.vo.GdSupplyDemandVO; import org.sxkj.system.cache.SysCache; import java.util.ArrayList; import java.util.List; import java.util.Objects; /** * 供需需求信息表 服务实现类 * * @author lw * @since 2026-01-16 */ @Service @AllArgsConstructor public class GdSupplyDemandServiceImpl extends BaseServiceImpl implements IGdSupplyDemandService { /** * 需求状态常量:0-草稿、1-申请中、2-审核通过、3-拒绝申请 */ private static final String STATUS_DRAFT = "0"; private static final String STATUS_APPLY = "1"; private static final String STATUS_APPROVED = "2"; private static final String STATUS_REJECTED = "3"; /** * 审核状态常量:0-待审核、1-审核通过、2-审核拒绝 */ private static final String AUDIT_PENDING = "0"; private static final String AUDIT_PASS = "1"; private static final String AUDIT_REJECT = "2"; private final IGdSupplyDemandAuditService gdSupplyDemandAuditService; private final IGdSupplyDemandAuditAttachmentService gdSupplyDemandAuditAttachmentService; @Override public IPage selectGdSupplyDemandPage(IPage page, GdSupplyDemandPageParam gdSupplyDemand) { List deptList = new ArrayList<>(); if (!AuthUtil.isAdministrator()) { deptList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId())); } gdSupplyDemand.setDeptList(deptList); return page.setRecords(baseMapper.selectGdSupplyDemandPage(page, gdSupplyDemand)); } @Override public IPage selectGdSupplyDemandList(IPage page, GdSupplyDemandDTO gdSupplyDemand) { return page.setRecords(baseMapper.selectGdSupplyDemandList(page, gdSupplyDemand)); } @Override public GdSupplyDemandEntity detailSupplyDemand(GdSupplyDemandDTO gdSupplyDemand) { if (Func.isEmpty(gdSupplyDemand)) { throw new ServiceException("需求参数不能为空"); } List deptList = new ArrayList<>(); if (!AuthUtil.isAdministrator()) { deptList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId())); } GdSupplyDemandEntity detail = baseMapper.selectGdSupplyDemandDetail(gdSupplyDemand, deptList); if (detail == null) { throw new ServiceException("未查询到数据"); } return detail; } @Override @Transactional(rollbackFor = Exception.class) public boolean submitSupplyDemand(GdSupplyDemandDTO gdSupplyDemand) { if (Func.isEmpty(gdSupplyDemand)) { throw new ServiceException("需求参数不能为空"); } GdSupplyDemandEntity supplyDemand = Objects.requireNonNull(BeanUtil.copy(gdSupplyDemand, GdSupplyDemandEntity.class)); // 获取前端传入的需求状态 String demandStatus = supplyDemand.getDemandStatus(); if (StringUtil.isBlank(demandStatus)) { throw new ServiceException("需求状态不能为空"); } // 校验状态值有效性 if (!STATUS_DRAFT.equals(demandStatus) && !STATUS_APPLY.equals(demandStatus)) { throw new ServiceException("需求状态值无效,仅支持 0(草稿) 或 1(申请中)"); } boolean isInsert = Func.isEmpty(supplyDemand.getId()); Long userId = AuthUtil.getUserId(); Long deptId = Long.valueOf(AuthUtil.getDeptId()); supplyDemand.setUpdateUser(userId); boolean saved; if (isInsert) { supplyDemand.setCreateUser(userId); supplyDemand.setCreateDept(deptId); saved = baseMapper.insertGdSupplyDemand(supplyDemand) > 0; } else { saved = baseMapper.updateGdSupplyDemand(supplyDemand) > 0; } if (!saved) { return false; } // 仅当状态为"申请中"(1)时才创建审核记录 if (STATUS_APPLY.equals(demandStatus)) { GdSupplyDemandEntity auditDemand = loadSupplyDemandForAudit(supplyDemand.getId()); GdSupplyDemandAuditEntity auditEntity = buildAuditEntity(auditDemand, AUDIT_PENDING, null); return gdSupplyDemandAuditService.saveSupplyDemandAudit(auditEntity); } // 状态为"草稿"(0)时直接返回成功 return true; } @Override @Transactional(rollbackFor = Exception.class) public boolean approveSupplyDemand(Long demandId, String auditOpinion, List attachIds) { GdSupplyDemandEntity supplyDemand = loadSupplyDemandForAudit(demandId); Long userId = AuthUtil.getUserId(); GdSupplyDemandEntity update = new GdSupplyDemandEntity(); update.setId(demandId); update.setDemandStatus(STATUS_APPROVED); update.setUpdateUser(userId); boolean updated = baseMapper.updateGdSupplyDemand(update) > 0; if (!updated) { return false; } GdSupplyDemandAuditEntity auditEntity = buildAuditEntity(supplyDemand, AUDIT_PASS, auditOpinion); boolean auditSaved = gdSupplyDemandAuditService.saveSupplyDemandAudit(auditEntity); if (!auditSaved) { return false; } if (Func.isEmpty(attachIds)) { return true; } List attachments = new ArrayList<>(); Long deptId = Long.valueOf(AuthUtil.getDeptId()); attachIds.forEach(attachId -> { if (attachId == null) { return; } GdSupplyDemandAuditAttachmentEntity attachment = new GdSupplyDemandAuditAttachmentEntity(); attachment.setDemandId(demandId); attachment.setAttachId(attachId); attachment.setAreaCode(supplyDemand.getAreaCode()); attachment.setCreateUser(userId); attachment.setCreateDept(deptId); attachments.add(attachment); }); if (attachments.isEmpty()) { return true; } return gdSupplyDemandAuditAttachmentService.saveSupplyDemandAuditAttachments(attachments); } @Override @Transactional(rollbackFor = Exception.class) public boolean rejectSupplyDemand(Long demandId, String auditOpinion) { if (StringUtil.isBlank(auditOpinion)) { throw new ServiceException("拒绝原因不能为空"); } GdSupplyDemandEntity supplyDemand = loadSupplyDemandForAudit(demandId); Long userId = AuthUtil.getUserId(); GdSupplyDemandEntity update = new GdSupplyDemandEntity(); update.setId(demandId); update.setDemandStatus(STATUS_REJECTED); update.setUpdateUser(userId); boolean updated = baseMapper.updateGdSupplyDemand(update) > 0; if (!updated) { return false; } GdSupplyDemandAuditEntity auditEntity = buildAuditEntity(supplyDemand, AUDIT_REJECT, auditOpinion); return gdSupplyDemandAuditService.saveSupplyDemandAudit(auditEntity); } private GdSupplyDemandAuditEntity buildAuditEntity(GdSupplyDemandEntity supplyDemand, String auditStatus, String auditOpinion) { if (Func.isEmpty(supplyDemand) || supplyDemand.getId() == null) { throw new ServiceException("需求数据不存在"); } Long userId = AuthUtil.getUserId(); Long deptId = Long.valueOf(AuthUtil.getDeptId()); GdSupplyDemandAuditEntity auditEntity = new GdSupplyDemandAuditEntity(); auditEntity.setDemandId(supplyDemand.getId()); auditEntity.setAuditStatus(auditStatus); auditEntity.setAuditOpinion(StringUtil.isBlank(auditOpinion) ? null : auditOpinion); auditEntity.setAreaCode(supplyDemand.getAreaCode()); auditEntity.setCreateUser(userId); auditEntity.setCreateDept(deptId); return auditEntity; } private GdSupplyDemandEntity loadSupplyDemandForAudit(Long demandId) { if (demandId == null) { throw new ServiceException("需求编号不能为空"); } GdSupplyDemandDTO query = new GdSupplyDemandDTO(); query.setId(demandId); GdSupplyDemandEntity detail = baseMapper.selectGdSupplyDemandDetail(query, SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()))); if (detail == null) { throw new ServiceException("需求数据不存在"); } return detail; } @Override public List exportGdSupplyDemand(Wrapper queryWrapper) { List gdSupplyDemandList = baseMapper.exportGdSupplyDemand(queryWrapper); //gdSupplyDemandList.forEach(gdSupplyDemand -> { // gdSupplyDemand.setTypeName(DictCache.getValue(DictEnum.YES_NO, GdSupplyDemand.getType())); //}); return gdSupplyDemandList; } }