package com.bonus.system.service.impl;

import com.bonus.common.core.domain.R;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.system.api.domain.SysFileSource;
import com.bonus.system.mapper.SysFileSourceMapper;
import com.bonus.system.service.SysFileSourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文件资源存储
 * @author 黑子
 */
@Slf4j
@Service
public class SysFileSourceServiceImpl implements SysFileSourceService {


    @Autowired
    private SysFileSourceMapper mapper;
    /**
     *
     * @param fileSource
     * @return
     */
    @Override
    public R<Boolean> addFileSource(SysFileSource fileSource) {
       try{
           if(StringUtils.isEmpty(fileSource.getFilePath())){
               System.err.println("文件id为空");
               return R.fail(false);
           }
           int num=mapper.addFileSource(fileSource);
           if (num>0){
               return R.ok(true);
           }
           return R.fail(false);
       }catch (Exception e){
           log.error(e.toString(),e);
       }
       return R.fail(false);
    }

    /**
     *
     * @param fileSource
     * @return
     */
    @Override
    public R<Boolean> delSourceFile(SysFileSource fileSource) {
         try{
             int num=0;
             if(ObjectUtils.isEmpty(fileSource.getId()) && fileSource.getId()!=0L){
                 num= mapper.delSourceFileById(fileSource);
             }else if(StringUtils.isNotEmpty(fileSource.getSourceId())){
                 num= mapper.delSourceFileBySourceId(fileSource);
            }
             if (num>0){
                 return R.ok(true);
             }
             return R.fail(false);
         }catch (Exception e){
             log.error(e.toString(),e);
         }

        return R.fail(false);
    }

    @Override
    public R<List<SysFileSource>> getSourceFileList(String sourceId) {
      List<SysFileSource> list=mapper.getSourceFileList(sourceId);
        return R.ok(list);
     }
}
