package com.xkrs.service.impl;

import com.xkrs.dao.FileDocumentDao;
import com.xkrs.model.entity.FileDocumentEntity;
import com.xkrs.model.qo.FileDocumentQo;
import com.xkrs.service.FileDocumentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

import static com.xkrs.utils.DateTimeUtil.getNowTime;

/**
 * 文档管理接口
 * @author tajochen
 */
@Service
@CacheConfig(cacheNames = "FileDocumentServiceCache")
public class FileDocumentServiceImpl implements FileDocumentService {

    Logger logger = LoggerFactory.getLogger(FileDocumentServiceImpl.class);

    @Resource
    private FileDocumentDao fileDocumentDao;

    /**
     * 文档列表
     * @return
     */
    @Cacheable(keyGenerator = "keyGenerator",unless="#result == null")
    @Override
    public Iterable<FileDocumentEntity> getAllFile() {
        Iterable<FileDocumentEntity> list = fileDocumentDao.findAll();
        return list;
    }

    /**
     * 获取指定id记录
     * @param id
     * @return
     */
    @Cacheable(keyGenerator = "keyGenerator",unless="#result == null")
    @Override
    public Optional getById(Integer id) {
        Optional <FileDocumentEntity> obj = fileDocumentDao.findById(id);
        return obj;
    }

    /**
     * 新增文档记录
     * @param fileDocumentQo
     * @param userName
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    @CacheEvict(value = "FileDocumentServiceCache",allEntries = true)
    public void add(FileDocumentQo fileDocumentQo, String userName) {
        FileDocumentEntity fileDocumentEntity = new FileDocumentEntity();
        fileDocumentEntity.setName(fileDocumentQo.getName());
        fileDocumentEntity.setFilePath(fileDocumentQo.getFilePath());
        fileDocumentEntity.setAddUserName(userName);
        fileDocumentEntity.setCategory(fileDocumentQo.getCategory());
        fileDocumentEntity.setMemo(fileDocumentQo.getMemo());
        fileDocumentEntity.setMd5(fileDocumentQo.getMd5());
        fileDocumentEntity.setSize(fileDocumentQo.getSize());
        // 设置系统时间
        fileDocumentEntity.setAddTime(getNowTime());
        fileDocumentDao.save(fileDocumentEntity);
    }

    /**
     * 删除文档记录
     * @param id
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    @CacheEvict(value = "FileDocumentServiceCache",allEntries = true)
    public void delete(Integer id) {
        fileDocumentDao.deleteById(id);
    }

    /**
     * 修改文档记录
     * @param fileDocumentEntity
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    @CacheEvict(value = "FileDocumentServiceCache",allEntries = true)
    public void update(FileDocumentEntity fileDocumentEntity) {
        fileDocumentDao.save(fileDocumentEntity);
    }
}
