package com.gkhy.exam.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.api.CommonResult;
import com.gkhy.exam.common.constant.UserConstant;
import com.gkhy.exam.common.utils.PageUtils;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.system.domain.DocumentBorrowCopy;
import com.gkhy.exam.system.domain.DocumentBorrowCopyRecord;
import com.gkhy.exam.system.domain.DocumentDistributionCollection;
import com.gkhy.exam.system.domain.DocumentDistributionCollectionRecord;
import com.gkhy.exam.system.mapper.DocumentDistributionCollectionMapper;
import com.gkhy.exam.system.mapper.DocumentDistributionCollectionRecordMapper;
import com.gkhy.exam.system.service.DocumentDistributionCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
/**
*
* 文件发放回收主表 服务实现类
*
*
* @author hh
* @since 2025-08-25 15:01:44
*/
@Service
public class DocumentDistributionCollectionServiceImpl extends ServiceImpl implements DocumentDistributionCollectionService {
@Autowired
private DocumentDistributionCollectionMapper documentDistributionCollectionMapper;
@Autowired
private DocumentDistributionCollectionRecordMapper documentDistributionCollectionRecordMapper;
@Override
public CommonPage selectDocumentDistributionCollectionList(DocumentDistributionCollection documentDistributionCollection) {
PageUtils.startPage();
List documentDistributionCollections = documentDistributionCollectionMapper.selectDocumentDistributionCollectionList(documentDistributionCollection);
return CommonPage.restPage(documentDistributionCollections);
}
@Override
@Transactional
public CommonResult saveDocumentDistributionCollection(DocumentDistributionCollection documentDistributionCollection) {
List documentDistributionCollectionRecordList = documentDistributionCollection.getDocumentDistributionCollectionRecordList();
if (ObjectUtils.isEmpty(documentDistributionCollectionRecordList)){
return CommonResult.failed("保存参数不能为空");
}
int i = 0;
if (documentDistributionCollection.getId() == null){
documentDistributionCollection.setCreateBy(SecurityUtils.getUsername());
documentDistributionCollection.setCreateTime(LocalDateTime.now());
i = documentDistributionCollectionMapper.insert(documentDistributionCollection);
}else {
documentDistributionCollection.setUpdateBy(SecurityUtils.getUsername());
documentDistributionCollection.setUpdateTime(LocalDateTime.now());
i = documentDistributionCollectionMapper.updateById(documentDistributionCollection);
}
if (i > 0){
batchSave(documentDistributionCollection.getId(), documentDistributionCollectionRecordList,documentDistributionCollection.getDeleteIds());
}
return i > 0 ? CommonResult.success() : CommonResult.failed();
}
private void batchSave(Long id, List documentDistributionCollectionRecordList,List delIds) {
if (ObjectUtils.isNotEmpty(delIds)) {
int update = documentDistributionCollectionRecordMapper.update(new DocumentDistributionCollectionRecord(),
new LambdaUpdateWrapper().set(DocumentDistributionCollectionRecord::getDelFlag, UserConstant.DISENABLE)
.set(DocumentDistributionCollectionRecord::getUpdateTime, LocalDateTime.now())
.set(DocumentDistributionCollectionRecord::getUpdateBy, SecurityUtils.getUsername()).in(DocumentDistributionCollectionRecord::getId, delIds)
);
}
if (ObjectUtils.isNotEmpty(documentDistributionCollectionRecordList)) {
documentDistributionCollectionRecordList.forEach(documentDistributionCollectionRecord -> {
if (documentDistributionCollectionRecord.getId() == null){
documentDistributionCollectionRecord.setDocumentId(id);
documentDistributionCollectionRecord.setCreateTime(LocalDateTime.now());
documentDistributionCollectionRecord.setCreateBy(SecurityUtils.getUsername());
documentDistributionCollectionRecordMapper.insert(documentDistributionCollectionRecord);
}else {
documentDistributionCollectionRecord.setUpdateTime(LocalDateTime.now());
documentDistributionCollectionRecord.setUpdateBy(SecurityUtils.getUsername());
documentDistributionCollectionRecordMapper.updateById(documentDistributionCollectionRecord);
}
});
}
}
@Override
public CommonResult getDocumentDistributionCollection(Long id) {
DocumentDistributionCollection documentDistributionCollection = documentDistributionCollectionMapper.selectById(id);
if (documentDistributionCollection != null){
List documentBorrowCopyRecordList = documentDistributionCollectionRecordMapper.selectList(
new LambdaQueryWrapper<>(DocumentDistributionCollectionRecord.class).eq(DocumentDistributionCollectionRecord::getDocumentId, id)
.eq(DocumentDistributionCollectionRecord::getDelFlag, UserConstant.ENABLE).orderByAsc(DocumentDistributionCollectionRecord::getSort)
);
documentDistributionCollection.setDocumentDistributionCollectionRecordList(documentBorrowCopyRecordList);
}
return CommonResult.success(documentDistributionCollection);
}
@Override
@Transactional
public CommonResult deletedDocumentDistributionCollection(Long id) {
int i = documentDistributionCollectionMapper.update(new DocumentDistributionCollection(),
new LambdaUpdateWrapper().set(DocumentDistributionCollection::getDelFlag, UserConstant.DISENABLE)
.set(DocumentDistributionCollection::getUpdateTime, LocalDateTime.now())
.set(DocumentDistributionCollection::getUpdateBy, SecurityUtils.getUsername()).eq(DocumentDistributionCollection::getId, id)
);
if (i > 0){
documentDistributionCollectionRecordMapper.update(new DocumentDistributionCollectionRecord(),
new LambdaUpdateWrapper().set(DocumentDistributionCollectionRecord::getDelFlag, UserConstant.DISENABLE)
.set(DocumentDistributionCollectionRecord::getUpdateTime, LocalDateTime.now()).set(DocumentDistributionCollectionRecord::getUpdateBy, SecurityUtils.getUsername())
.eq(DocumentDistributionCollectionRecord::getDocumentId, id));
}
return CommonResult.success();
}
}