package com.gz.service.archive.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.core.annotation.TraceLog;
import com.gz.dto.archive.SecondaryArchiveDTO;
import com.gz.mapper.archive.SecondaryArchiveMapper;
import com.gz.rvo.archive.ArchiveRVO;
import com.gz.service.archive.ArchiveFileService;
import com.gz.service.archive.SecondaryArchiveService;
import com.gz.utils.JwtUtils;
import com.gz.vo.PageVO;
import com.gz.vo.archive.SecondaryArchiveInsertVO;
import com.gz.vo.archive.SecondaryArchiveVO;
import org.apache.catalina.webresources.JarWarResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LiuchangLan
 * @date 2021/3/2 18:30
 */
@Service
public class SecondaryArchiveServiceImpl implements SecondaryArchiveService {

    @Resource
    private SecondaryArchiveMapper secondaryArchiveMapper;

    @Resource
    private ArchiveFileService archiveFileService;

    @Override
    @Transactional
    public Integer insert(SecondaryArchiveDTO dto,MultipartFile uploadFile) throws Exception {
        dto.setCreated(JwtUtils.getCurrentUserJwtPayload().getId());
        // 插入
        Integer i = secondaryArchiveMapper.insertSelective(dto);
        // 上传附件
        if (uploadFile != null){
            archiveFileService.upload(uploadFile,dto.getArchiveId(),0,dto.getArchiveId());
        }
        return i;
    }

    @Override
    public PageInfo<SecondaryArchiveDTO> selectByPage(SecondaryArchiveVO vo) {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        SecondaryArchiveDTO secondaryArchiveDTO = BeanUtil.copyProperties(vo, SecondaryArchiveDTO.class);
        return new PageInfo<>(secondaryArchiveMapper.select(secondaryArchiveDTO));
    }

    @Override
    public Integer delete(Integer id) {
        return secondaryArchiveMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer update(SecondaryArchiveDTO dto) {
        return secondaryArchiveMapper.updateByPrimaryKeySelective(dto);
    }

    @Override
    public SecondaryArchiveDTO selectByPrimaryKey(Integer id) {
        return secondaryArchiveMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SecondaryArchiveDTO> selectByArchiveId(Integer archiveId) {
        SecondaryArchiveDTO param = new SecondaryArchiveDTO();
        param.setArchiveId(archiveId);
        return secondaryArchiveMapper.select(param);
    }
}
