package com.mcwl.communityCenter.service.impl;

import cn.hutool.Hutool;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.page.PageDomain;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.common.utils.obs.ObsUtils;
import com.mcwl.communityCenter.domain.CommunityFile;
import com.mcwl.communityCenter.domain.CommunityFileLog;
import com.mcwl.communityCenter.domain.CommunityUser;
import com.mcwl.communityCenter.domain.dto.*;
import com.mcwl.communityCenter.domain.vo.CommunityFileVo;
import com.mcwl.communityCenter.domain.vo.DownloadFileUserVo;
import com.mcwl.communityCenter.mapper.CommunityFileMapper;
import com.mcwl.communityCenter.mapper.CommunityUserMapper;
import com.mcwl.communityCenter.service.CommunityFileLogService;
import com.mcwl.communityCenter.service.CommunityFileService;
import com.mcwl.communityCenter.service.CommunityUserService;
import com.mcwl.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service
@RequiredArgsConstructor
public class CommunityFileServiceImpl extends ServiceImpl<CommunityFileMapper, CommunityFile> implements CommunityFileService {

    private final ObsUtils obsUtils;

    private final ISysUserService userService;

    private final CommunityFileLogService communityFileLogService;

    private final CommunityUserMapper communityUserMapper;

    private static final String[] UN_UPLOAD_FILE_TYPE = new String[]{
            "bmp", "dib",
            "jpg", "jpeg", "jpe", "jfif",
            "gif",
            "tif", "tiff",
            "png",
            "heic",
            "webp"
    };


    @Override
    public R<String> upload(CommunityUploadFileRes communityUploadFileRes) {
        Long tenantId = communityUploadFileRes.getTenantId();
        Long communityId = communityUploadFileRes.getCommunityId();
        Long currentUserId = SecurityUtils.getUserId();

        CommunityUser communityUser = communityUserMapper.selectCommunityUser(tenantId, communityId, currentUserId);
        if (Objects.isNull(communityUser)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "您不是该社区成员");
        }

        CommunityFile communityFile = BeanUtil.toBean(communityUploadFileRes, CommunityFile.class);
        communityFile.setUserId(currentUserId);

        baseMapper.insert(communityFile);
        return R.ok();
    }

    @Override
    public TableDataInfo getCommunityFileList(CommunityFilePageListRes communityFilePageListRes) {
        Page<CommunityFile> page = new Page<>(communityFilePageListRes.getPageNum(), communityFilePageListRes.getPageSize());
        List<CommunityFileVo> communityFileList = baseMapper.getCommunityFileList(page, communityFilePageListRes);
        for (CommunityFileVo communityFileVo : communityFileList) {
            DownloadFileUserVo downloadFileUserVo = new DownloadFileUserVo();

            Long tenantId = communityFileVo.getTenantId();
            Long communityId = communityFileVo.getCommunityId();
            Long fileId = communityFileVo.getId();
            List<String> avatarList = communityFileLogService.getUserAvatarList(tenantId, communityId, fileId);
            Integer count = communityFileLogService.getDownloadCount(tenantId, communityId, fileId);

            downloadFileUserVo.setAvatarList(avatarList);
            downloadFileUserVo.setCount(count);
            communityFileVo.setDownloadFileUser(downloadFileUserVo);
        }
        // 封装返回
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(communityFileList);
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

    @Override
    public R<String> download(CommunityDownloadFileRes communityDownloadFileRes) {

        Long tenantId = communityDownloadFileRes.getTenantId();
        Long communityId = communityDownloadFileRes.getCommunityId();
        Long fileId = communityDownloadFileRes.getFileId();
        Long currentUserId = SecurityUtils.getUserId();

        CommunityUser communityUser = communityUserMapper.selectCommunityUser(tenantId, communityId, currentUserId);
        if (Objects.isNull(communityUser)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "您不是该社区成员");
        }

        CommunityFile communityFile = baseMapper.selectCommunityFile(tenantId, communityId, fileId);

        if (Objects.isNull(communityFile)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "文件不存在");
        }

        CommunityFileLog communityFileLog = new CommunityFileLog();
        communityFileLog.setTenantId(tenantId);
        communityFileLog.setCommunityId(communityId);
        communityFileLog.setCommunityFileId(fileId);
        communityFileLog.setDownloadUserId(currentUserId);


        communityFileLogService.saveCommunityFileLog(communityFileLog);

        return R.ok(communityFile.getFileUrl());
    }
}
