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.dto.archive.ArchiveDTO;
import com.gz.mapper.archive.ArchiveMapper;
import com.gz.service.archive.ArchiveService;
import com.gz.utils.JwtUtils;
import com.gz.vo.archive.SearchArchiveVO;
import org.springframework.stereotype.Service;

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

/**
 * @author LiuchangLan
 * @date 2021/2/23 18:12
 */
@Service
public class ArchiveServiceImpl implements ArchiveService {

    @Resource
    private ArchiveMapper mapper;

    @Override
    public Integer insert(ArchiveDTO dto) {
        dto.setCreated(JwtUtils.getCurrentUserJwtPayload().getId());
        return mapper.insertSelective(dto);
    }

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

    @Override
    public Integer update(ArchiveDTO dto) {
        dto.setUpdated(JwtUtils.getCurrentUserJwtPayload().getId());
        return mapper.updateByPrimaryKeySelective(dto);
    }

    @Override
    public List<ArchiveDTO> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public PageInfo<ArchiveDTO> selectByPage(SearchArchiveVO vo) {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        ArchiveDTO param = BeanUtil.copyProperties(vo, ArchiveDTO.class);
        return new PageInfo<>(mapper.select(param));
    }

    @Override
    public ArchiveDTO selectByPrimaryKey(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }
}
