package com.njcn.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.oss.constant.OssPath;
import com.njcn.oss.utils.FileStorageUtil;
import com.njcn.system.mapper.ResourceAdministrationMapper;
import com.njcn.system.pojo.po.Resinformation;
import com.njcn.system.service.IResourceAdministrationService;
import com.njcn.web.utils.RequestUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @version 1.0.0
 * @author: zbj
 * @date: 2023/04/17
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ResourceAdministrationServiceImpl extends ServiceImpl<ResourceAdministrationMapper, Resinformation> implements IResourceAdministrationService {

    private final ResourceAdministrationMapper resourceAdministrationMapper;

    private final FileStorageUtil fileStorageUtil;

    @Override
    public Boolean uploadFile(MultipartFile multipartFile, String name, String type, String description, String systemType) {
        //通过封装好的文件工具类来传入文件和文件夹名称来获取文件路径
        String url = fileStorageUtil.uploadMultipart(multipartFile, OssPath.RESOURCEADMINISTRATION);
        //创建对象
        Resinformation resinformation = new Resinformation();
        resinformation.setName(name);
        resinformation.setDescription(description);
        resinformation.setUrl(url);
        //获取用户id
        String userIndex = RequestUtil.getUserIndex();
        resinformation.setUpdateUser(userIndex);
        resinformation.setUpdateTime(LocalDateTime.now());
        resinformation.setState(DataStateEnum.ENABLE.getCode());
        resinformation.setFileName(multipartFile.getOriginalFilename());
        if (StringUtils.isNotBlank(systemType)) {
            resinformation.setSystemType(systemType);
        }
        resinformation.setType(type);
        return this.save(resinformation);
    }

    @Override
    public List<Resinformation> queryData(Resinformation resinformation) {
        if (StringUtils.isNotBlank(resinformation.getType()) && StringUtils.isNotBlank(resinformation.getSystemType())) {
            LambdaQueryWrapper<Resinformation> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Resinformation::getType, resinformation.getType())
                    .eq(Resinformation::getSystemType, resinformation.getSystemType());
            return resourceAdministrationMapper.selectList(wrapper);
        } else if (StringUtils.isBlank(resinformation.getType()) && StringUtils.isBlank(resinformation.getSystemType())) {
            return this.list();
        } else if (StringUtils.isNotBlank(resinformation.getType()) && StringUtils.isBlank(resinformation.getSystemType())) {
            LambdaQueryWrapper<Resinformation> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Resinformation::getType, resinformation.getType());
            return resourceAdministrationMapper.selectList(wrapper);
        } else {
            LambdaQueryWrapper<Resinformation> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Resinformation::getSystemType, resinformation.getSystemType());
            return resourceAdministrationMapper.selectList(wrapper);
        }
    }

    @Override
    public Boolean updateFile(MultipartFile multipartFile, String id, String name, String type, String description, String systemType) {
        Resinformation resinformation = resourceAdministrationMapper.selectById(id);
        if (null != multipartFile && !multipartFile.isEmpty()) {
            //删除文件
            fileStorageUtil.deleteFile(resinformation.getUrl());
            //获取路径
            String url = fileStorageUtil.uploadMultipart(multipartFile, OssPath.RESOURCEADMINISTRATION);
            resinformation.setUrl(url);
            resinformation.setFileName(multipartFile.getOriginalFilename());
        }
        resinformation.setName(name);
        resinformation.setDescription(description);
        //获取用户id
        String userIndex = RequestUtil.getUserIndex();
        resinformation.setUpdateUser(userIndex);
        resinformation.setUpdateTime(LocalDateTime.now());
        resinformation.setState(1);
        if (StringUtils.isNotBlank(systemType)) {
            resinformation.setSystemType(systemType);
        }
        resinformation.setType(type);
        int i = resourceAdministrationMapper.updateById(resinformation);
        return i > 0;
    }

    @Override
    public Boolean deleteFile(String id) {
        Resinformation resinformation = resourceAdministrationMapper.selectById(id);
        //删除文件
        fileStorageUtil.deleteFile(resinformation.getUrl());
        //删除数据
        int i = resourceAdministrationMapper.deleteById(id);
        return i > 0;
    }

    @Override
    public void downloadFile(String id, HttpServletResponse response) {
        Resinformation resinformation = resourceAdministrationMapper.selectById(id);
        //下载文件
        fileStorageUtil.downloadStream(response, resinformation.getUrl());
    }
}
