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.AnnualVerificationPlan;
import com.gkhy.exam.system.domain.DocumentBorrowCopy;
import com.gkhy.exam.system.domain.DocumentBorrowCopyRecord;
import com.gkhy.exam.system.mapper.DocumentBorrowCopyMapper;
import com.gkhy.exam.system.mapper.DocumentBorrowCopyRecordMapper;
import com.gkhy.exam.system.service.DocumentBorrowCopyService;
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 DocumentBorrowCopyServiceImpl extends ServiceImpl implements DocumentBorrowCopyService {
@Autowired
private DocumentBorrowCopyMapper documentBorrowCopyMapper;
@Autowired
private DocumentBorrowCopyRecordMapper documentBorrowCopyRecordMapper;
@Override
public CommonPage selectDocumentBorrowCopyList(DocumentBorrowCopy documentBorrowCopy) {
PageUtils.startPage();
List documentBorrowCopies = documentBorrowCopyMapper.selectDocumentBorrowCopyList(documentBorrowCopy);
return CommonPage.restPage(documentBorrowCopies);
}
@Override
@Transactional
public CommonResult saveDocumentBorrowCopy(DocumentBorrowCopy documentBorrowCopy) {
List documentBorrowCopyRecordList = documentBorrowCopy.getDocumentBorrowCopyRecordList();
if (ObjectUtils.isEmpty(documentBorrowCopyRecordList)){
return CommonResult.failed("保存参数不能为空");
}
int i = 0;
if (documentBorrowCopy.getId() == null){
documentBorrowCopy.setCreateBy(SecurityUtils.getUsername());
documentBorrowCopy.setCreateTime(LocalDateTime.now());
i = documentBorrowCopyMapper.insert(documentBorrowCopy);
}else {
documentBorrowCopy.setUpdateBy(SecurityUtils.getUsername());
documentBorrowCopy.setUpdateTime(LocalDateTime.now());
i = documentBorrowCopyMapper.updateById(documentBorrowCopy);
}
if (i > 0){
batchSave(documentBorrowCopy.getId(), documentBorrowCopyRecordList,documentBorrowCopy.getDeleteIds());
}
return i > 0 ? CommonResult.success() : CommonResult.failed();
}
private void batchSave(Long id, List documentBorrowCopyRecordList,List delIds) {
if (ObjectUtils.isNotEmpty(delIds)) {
int update = documentBorrowCopyRecordMapper.update(new DocumentBorrowCopyRecord(),
new LambdaUpdateWrapper().set(DocumentBorrowCopyRecord::getDelFlag, UserConstant.DEPT_DISABLE)
.set(DocumentBorrowCopyRecord::getUpdateTime, LocalDateTime.now())
.set(DocumentBorrowCopyRecord::getUpdateBy, SecurityUtils.getUsername()).in(DocumentBorrowCopyRecord::getId, delIds)
);
}
if (ObjectUtils.isNotEmpty(documentBorrowCopyRecordList)) {
documentBorrowCopyRecordList.forEach(documentBorrowCopyRecord -> {
if (documentBorrowCopyRecord.getId() == null){
documentBorrowCopyRecord.setDocumentBorrowId(id);
documentBorrowCopyRecord.setCreateTime(LocalDateTime.now());
documentBorrowCopyRecord.setCreateBy(SecurityUtils.getUsername());
documentBorrowCopyRecordMapper.insert(documentBorrowCopyRecord);
}else {
documentBorrowCopyRecord.setUpdateTime(LocalDateTime.now());
documentBorrowCopyRecord.setUpdateBy(SecurityUtils.getUsername());
documentBorrowCopyRecordMapper.updateById(documentBorrowCopyRecord);
}
});
}
}
@Override
public CommonResult getDocumentBorrowCopy(Long id) {
DocumentBorrowCopy documentBorrowCopy = documentBorrowCopyMapper.selectById(id);
if (documentBorrowCopy != null){
List documentBorrowCopyRecordList = documentBorrowCopyRecordMapper.selectList(
new LambdaQueryWrapper<>(DocumentBorrowCopyRecord.class).eq(DocumentBorrowCopyRecord::getDocumentBorrowId, id)
.eq(DocumentBorrowCopyRecord::getDelFlag, UserConstant.ENABLE).orderByAsc(DocumentBorrowCopyRecord::getSort)
);
documentBorrowCopy.setDocumentBorrowCopyRecordList(documentBorrowCopyRecordList);
}
return CommonResult.success(documentBorrowCopy);
}
@Override
@Transactional
public CommonResult deletedDocumentBorrowCopy(Long id) {
int i = documentBorrowCopyMapper.update(new DocumentBorrowCopy(),
new LambdaUpdateWrapper().set(DocumentBorrowCopy::getDelFlag, UserConstant.DEPT_DISABLE)
.set(DocumentBorrowCopy::getUpdateTime, LocalDateTime.now())
.set(DocumentBorrowCopy::getUpdateBy, SecurityUtils.getUsername()).eq(DocumentBorrowCopy::getId, id)
);
if (i > 0){
documentBorrowCopyRecordMapper.update(new DocumentBorrowCopyRecord(),
new LambdaUpdateWrapper().set(DocumentBorrowCopyRecord::getDelFlag, UserConstant.DEPT_DISABLE)
.set(DocumentBorrowCopyRecord::getUpdateTime, LocalDateTime.now()).set(DocumentBorrowCopyRecord::getUpdateBy, SecurityUtils.getUsername())
.eq(DocumentBorrowCopyRecord::getDocumentBorrowId, id));
}
return CommonResult.success();
}
}