package com.hfkj.service.file.impl;

import com.hfkj.dao.FileRecordsMapper;
import com.hfkj.entity.FileRecords;
import com.hfkj.entity.FileRecordsExample;
import com.hfkj.service.file.FileRecordsService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

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

@Service("fileRecordsService")
public class FileRecordsServiceImpl implements FileRecordsService {

    @Resource
    private FileRecordsMapper fileRecordsMapper;

    @Override
    public void create(FileRecords fileRecords) {
        fileRecordsMapper.insert(fileRecords);
    }

    @Override
    public void update(FileRecords fileRecords) {
        fileRecordsMapper.updateByPrimaryKeySelective(fileRecords);
    }

    @Override
    public void delete(Long id, Boolean fullDelete) {
        fileRecordsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public FileRecords queryDetail(Long id) {
        return fileRecordsMapper.selectByPrimaryKey(id);
    }

    @Override
    public FileRecords queryDetailByMap(Map<String, Object> map) {
        return null;
    }

    @Override
    public List<FileRecords> getList(Map<String, Object> map) {

        FileRecordsExample example = new FileRecordsExample();
        FileRecordsExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "opId") != null) {
            criteria.andOpIdEqualTo(MapUtils.getLong(map, "opId"));
        }
        example.setOrderByClause("create_time desc");
        return fileRecordsMapper.selectByExample(example);
    }

    @Override
    public Boolean findExported(String content , Long opId) {

        FileRecordsExample example = new FileRecordsExample();
        FileRecordsExample.Criteria criteria = example.createCriteria();

        criteria.andDownloadConditionsEqualTo(content).andOpIdEqualTo(opId).andStatusEqualTo(2);

        List<FileRecords> list = fileRecordsMapper.selectByExample(example);

        return !list.isEmpty();
    }
}
