package com.sinata.system.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinata.common.core.domain.entity.SysDictData;
import com.sinata.common.entity.PageDTO;
import com.sinata.common.exception.ServiceException;
import com.sinata.common.utils.BeanUtils;
import com.sinata.common.utils.CollUtils;
import com.sinata.common.utils.StringUtils;
import com.sinata.system.domain.MwAttachment;
import com.sinata.system.domain.MwProtectionRegulation;
import com.sinata.system.domain.dto.MwProtectionRegulationDTO;
import com.sinata.system.domain.query.MwProtectionRegulationQuery;
import com.sinata.system.domain.vo.MwAttachmentVO;
import com.sinata.system.domain.vo.MwProtectionRegulationVO;
import com.sinata.system.enums.AttachmentTypeEnum;
import com.sinata.system.mapper.MwProtectionRegulationMapper;
import com.sinata.system.service.ISysDictDataService;
import com.sinata.system.service.MwAttachmentService;
import com.sinata.system.service.MwProtectionRegulationService;
import com.sinata.system.service.SysDepartmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;
/**
*
* 规章制度 服务实现类
*
*
* @author mitao
* @since 2024-12-02
*/
@Service
@RequiredArgsConstructor
public class MwProtectionRegulationServiceImpl extends ServiceImpl implements MwProtectionRegulationService {
private final SysDepartmentService sysDepartmentService;
private final MwAttachmentService mwAttachmentService;
private final ISysDictDataService sysDictDataService;
/**
* 规章制度分页列表
*
* @param query
* @return
*/
@Override
public PageDTO pageList(MwProtectionRegulationQuery query) {
String treeCode = sysDepartmentService.getTreeCodeByDepartmentId(query.getDepartmentId());
if (StringUtils.isBlank(treeCode)) {
return PageDTO.empty(0L, 0L);
}
Page page = baseMapper.pageList(new Page<>(query.getPageCurr(), query.getPageSize()), query, treeCode);
return PageDTO.of(page);
}
/**
* 详情
*
* @param id
* @return
*/
@Override
public MwProtectionRegulationVO detail(Long id) {
MwProtectionRegulation protectionRegulation = getById(id);
MwProtectionRegulationVO mwProtectionRegulationVO = null;
if (Objects.nonNull(protectionRegulation)) {
mwProtectionRegulationVO = BeanUtils.copyBean(protectionRegulation, MwProtectionRegulationVO.class);
List list = mwAttachmentService.lambdaQuery().eq(MwAttachment::getType, AttachmentTypeEnum.PROTECTION_REGULATION.getCode()).eq(MwAttachment::getTargetId, id).list();
mwProtectionRegulationVO.setAttachmentList(BeanUtils.copyToList(list, MwAttachmentVO.class));
return mwProtectionRegulationVO;
}
return mwProtectionRegulationVO;
}
/**
* 新增规章制度
*
* @param dto
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void add(MwProtectionRegulationDTO dto) {
MwProtectionRegulation mwProtectionRegulation = BeanUtils.copyBean(dto, MwProtectionRegulation.class);
SysDictData dictData = sysDictDataService.lambdaQuery().eq(SysDictData::getDictCode, dto.getProtectionRegulationType()).one();
if (Objects.nonNull(dictData)) {
mwProtectionRegulation.setProtectionRegulationTypeStr(dictData.getDictLabel());
}
save(mwProtectionRegulation);
if (CollUtils.isNotEmpty(dto.getAttachmentList())) {
List mwAttachments = BeanUtils.copyToList(dto.getAttachmentList(), MwAttachment.class);
mwAttachments.forEach(attachment -> {
attachment.setTargetId(mwProtectionRegulation.getId());
attachment.setType(AttachmentTypeEnum.PROTECTION_REGULATION.getCode());
});
mwAttachmentService.saveBatch(mwAttachments);
}
}
/**
* 编辑规章制度
*
* @param dto
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void edit(MwProtectionRegulationDTO dto) {
if (Objects.isNull(dto.getId())) {
throw new ServiceException("规章制度id不能为空");
}
MwProtectionRegulation mwProtectionRegulation = BeanUtils.copyBean(dto, MwProtectionRegulation.class);
SysDictData dictData = sysDictDataService.lambdaQuery().eq(SysDictData::getDictCode, dto.getProtectionRegulationType()).one();
if (Objects.nonNull(dictData)) {
mwProtectionRegulation.setProtectionRegulationTypeStr(dictData.getDictLabel());
}
updateById(mwProtectionRegulation);
//保存附件
if (CollUtils.isNotEmpty(dto.getAttachmentList())) {
//删除原有的附件
mwAttachmentService.lambdaUpdate().eq(MwAttachment::getType, AttachmentTypeEnum.PROTECTION_REGULATION.getCode()).eq(MwAttachment::getTargetId, dto.getId()).remove();
List mwAttachments = BeanUtils.copyToList(dto.getAttachmentList(), MwAttachment.class);
mwAttachments.forEach(attachment -> {
attachment.setTargetId(mwProtectionRegulation.getId());
attachment.setType(AttachmentTypeEnum.PROTECTION_REGULATION.getCode());
});
mwAttachmentService.saveBatch(mwAttachments);
}
}
/**
* 删除
*
* @param id
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(Long id) {
removeById(id);
mwAttachmentService.lambdaUpdate().eq(MwAttachment::getType, AttachmentTypeEnum.PROTECTION_REGULATION.getCode()).eq(MwAttachment::getTargetId, id).remove();
}
}