@ -5,6 +5,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil ;
import cn.hutool.core.date.DateUtil ;
import cn.hutool.core.util.ObjectUtil ;
import cn.hutool.core.util.StrUtil ;
import cn.hutool.json.JSONUtil ;
import cn.hutool.poi.excel.ExcelReader ;
import cn.hutool.poi.excel.ExcelUtil ;
@ -22,6 +23,7 @@ import com.ruoyi.common.utils.sql.HeadExcelUtils;
import com.ruoyi.contract.domain.* ;
import com.ruoyi.contract.domain.bo.* ;
import com.ruoyi.contract.domain.bo.conpurchase.ConPurchaseBoExtend ;
import com.ruoyi.contract.domain.dto.ConSaleImportDto ;
import com.ruoyi.contract.domain.vo.* ;
import com.ruoyi.contract.domain.vo.conpurchasevo.ConPurchaseTotalVo ;
import com.ruoyi.contract.mapper.* ;
@ -125,125 +127,166 @@ public class ConPurchaseServiceImpl implements IConPurchaseService {
@Resource
private Validator validator ;
/ * *
* 导入采购合同信息
* @param file 导入文件
* @return
* @throws Exception
* /
@Override
@Transactional ( rollbackFor = Exception . class )
public int importPurchase ( MultipartFile file ) {
List < ConPurchaseImportVO > list ;
try ( InputStream inputStream = file . getInputStream ( ) ) {
ExcelReader reader = ExcelUtil . getReader ( inputStream ) ;
list = reader . readAll ( ConPurchaseImportVO . class ) ;
} catch ( IOException e ) {
throw new ServiceException ( " 导入采购合同失败 " ) ;
}
public int importPurchase ( MultipartFile file ) throws Exception {
ExcelReader reader = ExcelUtil . getReader ( file . getInputStream ( ) ) ;
List < ConPurchaseImportVO > list = reader . readAll ( ConPurchaseImportVO . class ) ;
reader . close ( ) ;
if ( CollectionUtil . isEmpty ( list ) ) {
throw new ServiceException ( " 导入数据不能为空 " ) ;
}
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
ConPurchaseImportVO vo = list . get ( i ) ;
try {
/ / 校验必填项
if ( StringUtils . isBlank ( vo . getContractNumber ( ) ) ) {
throw new ServiceException ( " 合同编号不能为空 " ) ;
/ / 按合同编号对导入数据进行分组
Map < String , List < ConPurchaseImportVO > > mapByContractNumber = list . stream ( )
. filter ( vo - > StringUtils . isNotBlank ( vo . getContractNumber ( ) ) )
. collect ( Collectors . groupingBy ( ConPurchaseImportVO : : getContractNumber ) ) ;
/ / 遍历每个合同分组
for ( Map . Entry < String , List < ConPurchaseImportVO > > entry : mapByContractNumber . entrySet ( ) ) {
String contractNumber = entry . getKey ( ) ;
List < ConPurchaseImportVO > rows = entry . getValue ( ) ;
ConPurchaseImportVO firstRow = rows . get ( 0 ) ; / / 使用第一行数据作为主信息
/ / 判断合同是否存在
ConPurchase existingContract = baseMapper . selectOne ( new LambdaQueryWrapper < ConPurchase > ( )
. eq ( ConPurchase : : getContractNumber , contractNumber ) ) ;
if ( existingContract = = null ) {
/ / 合同不存在 - > 调用 insertByBo 新增
ConPurchaseBo purchaseBo = new ConPurchaseBo ( ) ;
/ / 手动映射主表字段
purchaseBo . setContractNumber ( firstRow . getContractNumber ( ) ) ;
purchaseBo . setProjNumber ( firstRow . getProjNumber ( ) ) ;
purchaseBo . setProjName ( firstRow . getProjName ( ) ) ;
purchaseBo . setStartTime ( firstRow . getStartTime ( ) ) ;
purchaseBo . setEndTime ( firstRow . getEndTime ( ) ) ;
purchaseBo . setContractMoney ( firstRow . getContractMoney ( ) ) ;
purchaseBo . setInvoiceContent ( firstRow . getInvoiceContent ( ) ) ;
purchaseBo . setSignTime ( firstRow . getSignTime ( ) ) ;
purchaseBo . setUpPrint ( firstRow . getUpPrint ( ) ) ;
purchaseBo . setExchangePrint ( firstRow . getExchangePrint ( ) ) ;
purchaseBo . setNextPrint ( firstRow . getNextPrint ( ) ) ;
purchaseBo . setMediaLink ( firstRow . getMediaLink ( ) ) ;
purchaseBo . setFirstName ( firstRow . getFirstName ( ) ) ;
purchaseBo . setMediaDeptName ( firstRow . getMediaDeptName ( ) ) ;
purchaseBo . setSecondName ( firstRow . getSecondName ( ) ) ; / / 供应商名称 ( 乙方名称 )
purchaseBo . setTaxPoints ( firstRow . getTaxPoints ( ) ) ; / / 税率 ( 税点 )
purchaseBo . setMediaLink ( firstRow . getMediaLink ( ) ) ;
/ / 状态转换
String state = " 生效 " . equals ( firstRow . getState ( ) ) ? " 1 " : " 0 " ;
purchaseBo . setState ( state ) ;
/ / 关联客户ID
if ( StringUtils . isNotBlank ( firstRow . getClientName ( ) ) ) {
ConClient conClient = conClientMapper . selectOne ( new LambdaQueryWrapper < ConClient > ( ) . eq ( ConClient : : getClientName , firstRow . getClientName ( ) ) ) ;
if ( conClient ! = null ) {
purchaseBo . setClientId ( conClient . getId ( ) ) ;
}
}
ConPurchase conPurchase = baseMapper . selectOne ( new LambdaQueryWrapper < ConPurchase > ( )
. eq ( ConPurchase : : getContractNumber , vo . getContractNumber ( ) ) ) ;
if ( conPurchase = = null ) {
/ / 不存在 , 新增合同
conPurchase = new ConPurchase ( ) ;
/ / 手动进行字段映射
conPurchase . setContractNumber ( vo . getContractNumber ( ) ) ;
conPurchase . setProjNumber ( vo . getProjNumber ( ) ) ;
conPurchase . setProjName ( vo . getProjName ( ) ) ;
conPurchase . setStartTime ( vo . getStartTime ( ) ) ;
conPurchase . setEndTime ( vo . getEndTime ( ) ) ;
conPurchase . setContractMoney ( vo . getContractMoney ( ) ) ;
conPurchase . setInvoiceContent ( vo . getInvoiceContent ( ) ) ;
conPurchase . setSignTime ( vo . getSignTime ( ) ) ;
conPurchase . setUpPrint ( vo . getUpPrint ( ) ) ;
conPurchase . setExchangePrint ( vo . getExchangePrint ( ) ) ;
conPurchase . setNextPrint ( vo . getNextPrint ( ) ) ;
conPurchase . setMediaLink ( vo . getMediaLink ( ) ) ;
String state = " 生效 " . equals ( vo . getState ( ) ) ? " 1 " : " 0 " ;
conPurchase . setState ( state ) ;
conPurchase . setFirstName ( vo . getFirstName ( ) ) ;
conPurchase . setMediaDeptName ( vo . getMediaDeptName ( ) ) ;
/ / 供应商名称 ( 乙方名称 )
conPurchase . setSecondName ( vo . getSupplierName ( ) ) ;
/ / 税率 ( 税点 )
conPurchase . setTaxPoints ( vo . getTaxRate ( ) ) ;
/ / 甲方
if ( StringUtils . isNotBlank ( vo . getFirstName ( ) ) ) {
ConFirst conFirst = conFirstMapper . selectOne ( new LambdaQueryWrapper < ConFirst > ( ) . eq ( ConFirst : : getFirstName , vo . getFirstName ( ) ) ) ;
/ / 关联甲方ID
if ( StringUtils . isNotBlank ( firstRow . getFirstName ( ) ) ) {
ConFirst conFirst = conFirstMapper . selectOne ( new LambdaQueryWrapper < ConFirst > ( ) . eq ( ConFirst : : getFirstName , firstRow . getFirstName ( ) ) ) ;
if ( conFirst ! = null ) {
conPurchase . setFirstId ( conFirst . getId ( ) ) ;
purchaseBo . setFirstId ( conFirst . getId ( ) ) ;
}
}
/ / 媒介部门
if ( StringUtils . isNotBlank ( vo . getMediaDeptName ( ) ) ) {
ConMediaDept conMediaDept = conMediaDeptMapper . selectOne ( new LambdaQueryWrapper < ConMediaDept > ( ) . eq ( ConMediaDept : : getMediaDeptName , vo . getMediaDeptName ( ) ) ) ;
/ / 关联媒介部门ID
if ( StringUtils . isNotBlank ( firstRow . getMediaDeptName ( ) ) ) {
ConMediaDept conMediaDept = conMediaDeptMapper . selectOne ( new LambdaQueryWrapper < ConMediaDept > ( ) . eq ( ConMediaDept : : getMediaDeptName , firstRow . getMediaDeptName ( ) ) ) ;
if ( conMediaDept ! = null ) {
conPurchase . setMediaDeptId ( conMediaDept . getId ( ) ) ;
purchaseBo . setMediaDeptId ( conMediaDept . getId ( ) ) ;
}
}
/ / 发票类型
if ( StringUtils . isNotBlank ( vo. getInvoiceTyp e( ) ) ) {
ConInvoice conInvoice = conInvoiceMapper . selectOne ( new LambdaQueryWrapper < ConInvoice > ( ) . eq ( ConInvoice : : getInvoiceName , vo. getInvoiceTyp e( ) ) ) ;
/ / 关联发票类型ID
if ( StringUtils . isNotBlank ( firstRow . getInvoiceName ( ) ) ) {
ConInvoice conInvoice = conInvoiceMapper . selectOne ( new LambdaQueryWrapper < ConInvoice > ( ) . eq ( ConInvoice : : getInvoiceName , firstRow . getInvoiceName ( ) ) ) ;
if ( conInvoice ! = null ) {
conPurchase . setInvoiceId ( conInvoice . getId ( ) ) ;
purchaseBo . setInvoiceId ( conInvoice . getId ( ) ) ;
}
}
validEntityBeforeSave ( conPurchase ) ;
baseMapper . insert ( conPurchase ) ;
/ / 映射媒体子表信息
List < ConPurchaseMediaBo > mediaBoList = rows . stream ( ) . map ( row - > {
ConPurchaseMediaBo mediaBo = new ConPurchaseMediaBo ( ) ;
this . mapRowToMediaBo ( row , mediaBo ) ;
return mediaBo ;
} ) . collect ( Collectors . toList ( ) ) ;
purchaseBo . setPurchaseMediaBoList ( mediaBoList ) ;
this . insertByBo ( purchaseBo ) ;
} else {
/ / 合同已存在 - > 调用 updateByBo 追加媒体信息
ConPurchaseVo existingVo = this . queryById ( existingContract . getId ( ) ) ;
ConPurchaseBo purchaseBo = BeanUtil . toBean ( existingVo , ConPurchaseBo . class ) ;
/ / 获取已有的媒体信息
List < ConPurchaseMediaBo > existingMedia = purchaseBo . getPurchaseMediaBoList ( ) ;
if ( existingMedia = = null ) {
existingMedia = new ArrayList < > ( ) ;
}
/ / 新增媒体数据
ConPurchaseMedia conPurchaseMedia = new ConPurchaseMedia ( ) ;
conPurchaseMedia . setPurchaseId ( conPurchase . getId ( ) ) ;
/ / 创建并添加本次导入的新媒体信息
List < ConPurchaseMediaBo > newMediaList = rows . stream ( ) . map ( row - > {
ConPurchaseMediaBo mediaBo = new ConPurchaseMediaBo ( ) ;
this . mapRowToMediaBo ( row , mediaBo ) ;
return mediaBo ;
} ) . collect ( Collectors . toList ( ) ) ;
existingMedia . addAll ( newMediaList ) ;
purchaseBo . setPurchaseMediaBoList ( existingMedia ) ;
/ / 媒体类型
if ( StringUtils . isNotBlank ( vo . getMediaType ( ) ) ) {
ConMediaType conMediaType = conMediaTypeMapper . selectOne ( new LambdaQueryWrapper < ConMediaType > ( ) . eq ( ConMediaType : : getMediaType , vo . getMediaType ( ) ) ) ;
if ( conMediaType ! = null ) {
conPurchaseMedia . setMediaId ( conMediaType . getId ( ) ) ;
}
}
/ / 城市
if ( StringUtils . isNotBlank ( vo . getCityName ( ) ) ) {
ConCity conCity = conCityMapper . selectList ( new LambdaQueryWrapper < ConCity > ( ) . likeRight ( ConCity : : getCityName , vo . getCityName ( ) ) ) . stream ( ) . findFirst ( ) . orElse ( null ) ;
if ( conCity ! = null ) {
conPurchaseMedia . setCityId ( conCity . getId ( ) ) ;
String cityIds = iConCityService . selectTreeIds ( conCity . getId ( ) ) ;
conPurchaseMedia . setCityIds ( cityIds ) ;
}
}
conPurchaseMedia . setMediaPosition ( vo . getMediaLocation ( ) ) ;
conPurchaseMedia . setAccountNumber ( safeParseDouble ( vo . getPurchaseQuantity ( ) ) ) ;
conPurchaseMedia . setReleaseFrequency ( vo . getFrequency ( ) ) ;
conPurchaseMedia . setUpTime ( vo . getStartTime ( ) ) ;
conPurchaseMedia . setDownTime ( vo . getEndTime ( ) ) ;
conPurchaseMedia . setPeriod ( safeParseDouble ( vo . getPublishDays ( ) ) ) ;
conPurchaseMedia . setPrintPrice ( safeParseDouble ( vo . getListPrice ( ) ) ) ;
conPurchaseMedia . setPrintPriceUnit ( vo . getListPriceUnit ( ) ) ;
conPurchaseMedia . setDiscount ( vo . getDiscount ( ) ) ;
conPurchaseMedia . setMediaFee ( safeParseDouble ( vo . getNetPrice ( ) ) ) ;
conPurchaseMedia . setProductFee ( safeParseDouble ( vo . getProductionFee ( ) ) ) ;
conPurchaseMediaMapper . insert ( conPurchaseMedia ) ;
} catch ( Exception e ) {
log . error ( " 导入采购合同失败 " , e ) ;
this . updateByBo ( purchaseBo ) ;
}
}
return list . size ( ) ;
}
/ * *
* 辅助方法 : 将导入行数据映射到媒体BO
* /
private void mapRowToMediaBo ( ConPurchaseImportVO row , ConPurchaseMediaBo mediaBo ) {
/ / 媒体类型
if ( StringUtils . isNotBlank ( row . getMediaType ( ) ) ) {
ConMediaType conMediaType = conMediaTypeMapper . selectOne ( new LambdaQueryWrapper < ConMediaType > ( ) . eq ( ConMediaType : : getMediaType , row . getMediaType ( ) ) ) ;
if ( conMediaType ! = null ) {
mediaBo . setMediaId ( conMediaType . getId ( ) ) ;
mediaBo . setMediaName ( conMediaType . getMediaType ( ) ) ;
}
}
/ / 城市
if ( StringUtils . isNotBlank ( row . getCityName ( ) ) ) {
ConCity conCity = conCityMapper . selectList ( new LambdaQueryWrapper < ConCity > ( ) . likeRight ( ConCity : : getCityName , row . getCityName ( ) ) ) . stream ( ) . findFirst ( ) . orElse ( null ) ;
if ( conCity ! = null ) {
mediaBo . setCityId ( conCity . getId ( ) ) ;
mediaBo . setCityName ( conCity . getCityName ( ) ) ;
String cityIds = iConCityService . selectTreeIds ( conCity . getId ( ) ) ;
mediaBo . setCityIds ( cityIds ) ;
}
}
mediaBo . setMediaPosition ( row . getMediaLocation ( ) ) ;
mediaBo . setAccountNumber ( safeParseDouble ( row . getPurchaseQuantity ( ) ) ) ;
mediaBo . setReleaseFrequency ( row . getFrequency ( ) ) ;
mediaBo . setUpTime ( row . getStartTime ( ) ) ;
mediaBo . setDownTime ( row . getEndTime ( ) ) ;
mediaBo . setPeriod ( safeParseDouble ( row . getPublishDays ( ) ) ) ;
mediaBo . setPrintPrice ( safeParseDouble ( row . getListPrice ( ) ) ) ;
mediaBo . setPrintPriceUnit ( row . getListPriceUnit ( ) ) ;
mediaBo . setDiscount ( StrUtil . isNotBlank ( row . getDiscount ( ) ) ? row . getDiscount ( ) : " 0 " ) ;
mediaBo . setMediaFee ( safeParseDouble ( row . getNetPrice ( ) ) ) ;
mediaBo . setMediaFeeUnit ( row . getNetPriceUnit ( ) ) ;
mediaBo . setProductFee ( safeParseDouble ( row . getProductionFee ( ) ) ) ;
}
private Double safeParseDouble ( String str ) {
if ( StringUtils . isBlank ( str ) | | " - " . equals ( str ) ) {
return null ;