package org.jeecg.modules.wps.service.impl;

import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.wps.config.Context;
import org.jeecg.modules.wps.dto.*;
import org.jeecg.modules.wps.entity.*;
import org.jeecg.modules.wps.mapper.WpsFileMapper;
import org.jeecg.modules.wps.service.*;
import org.jeecg.modules.wps.util.Token;
import org.jeecg.modules.wps.util.WpsUtil;
import org.jeecg.modules.wps.util.file.FileUtil;
import org.jeecg.modules.wps.util.upload.ResFileDTO;
import org.jeecg.modules.wps.util.upload.oss.WpsOSSUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * wps文件
 * @author jeecg-boot
 * @date   2020-06-01
 * @version V1.0
 */
@Slf4j
@Service
public class WpsFileServiceImpl extends ServiceImpl<WpsFileMapper, WpsFile> implements IWpsFileService {
    @Autowired
    private IWpsUserAclService wpsUserAclService;
    @Autowired
    private IWpsFileWatermarkService wpsFileWatermarkService;
    @Autowired
    private IWpsFileVersionService wpsFileVersionService;
    @Autowired
    private IWpsUserService wpsUserService;
    @Autowired
    private WpsUtil wpsUtil;
    @Override
    public Token getViewUrl(String fileUrl, boolean checkToken){
        Token t = new Token();

        String fileType = FileUtil.getFileTypeByPath(fileUrl);
        // fileId使用uuid保证出现同样的文件而是最新文件
        UUID randomUUID = UUID.randomUUID();
        String uuid = randomUUID.toString().replace("-","");

        Map<String,String> values = new HashMap<String,String>(){
            {
                put("_w_appid", appid);
                if (checkToken){
                    put("_w_tokentype","1");
                }
                put("_w_userid", "-1");
                put("_w_filepath",fileUrl);
                put("_w_filetype","web");
            }
        };

        String wpsUrl = wpsUtil.getWpsUrl(values,fileType,uuid);

        t.setToken(uuid);
        t.setExpires_in(600);
        t.setWpsUrl(wpsUrl);

        return t;
    }
    @Value("wps.domain")
    private String domain;
    @Value("wps.appid")
    private String appid;
    @Value("wps.appsecret")
    private String appsecret;
    @Value("wps.download_host")
    private String downloadHost;
    @Value("wps.local_Dir")
    private String localDir;

    @Override
    public Token getViewUrl(String fileId,String userId,boolean checkToken){
        WpsFile fileEntity = this.getById(fileId);
        if (fileEntity != null){
            Token t = new Token();
            String fileName = fileEntity.getName();
            String fileType = FileUtil.getFileTypeByName(fileName);

            UUID randomUuid = UUID.randomUUID();
            String uuid = randomUuid.toString().replace("-","");

            Map<String,String> values = new HashMap<>();
            values.put("_w_appid", appid);
            if (checkToken){
                values.put("_w_tokentype","1");
            }
            values.put("_w_filepath",fileName);
            values.put("_w_userid", userId);
            values.put("_w_filetype","db");

            String wpsUrl = wpsUtil.getWpsUrl(values,fileType,fileEntity.getId());
            t.setToken(uuid);
            t.setExpires_in(600);
            t.setWpsUrl(wpsUrl);

            return t;
        }
        return null;
    }

    @Override
    public Map<String,Object> getFileInfo(String userId, String filePath,String _w_filetype){
        if ("web".equalsIgnoreCase(_w_filetype)){
            return getWebFileInfo(filePath);
        }else if ("db".equalsIgnoreCase(_w_filetype)){
            return getDbFileInfo(userId);
        }
        return null;
    }

    private Map<String,Object> getWebFileInfo(String filePath){
        Map<String,Object>result = new HashMap<>();
        log.info("_w_filepath:{}",filePath);
        // 构建默认user信息
        UserDTO wpsUser = new UserDTO(
                "-1","我","read","https://zmfiletest.oss-cn-hangzhou.aliyuncs.com/user0.png"
        );

        // 构建文件
        FileDTO file = new FileDTO(
                Context.getFileId(), FileUtil.getFileName(filePath),
                1,2835,"-1",System.currentTimeMillis(), filePath,
                // 默认设置为无水印，只读权限
                new UserAclBO(),new WatermarkBO()
        );
        result.put("file",file);
        result.put("user",wpsUser);
        return result;
    }

    private Map<String,Object> getDbFileInfo(String userId){
        String fileId = Context.getFileId();
        // 获取文件信息
        WpsFile fileEntity = this.getById(fileId);

        // 初始化文件读写权限为read
        String permission = "read";

        // 增加用户权限
        WpsUserAcl userAclEntity = wpsUserAclService.findFirstByFileIdAndUserId(fileId,userId);
        UserAclBO userAcl = new UserAclBO();
        if (userAclEntity != null){
            BeanUtils.copyProperties(userAclEntity,userAcl);
            permission = userAclEntity.getPermission();
        }

        // 增加水印
        WpsFileWatermark watermarkEntity = wpsFileWatermarkService.findFirstByFileId(fileId);
        WatermarkBO watermark = new WatermarkBO();
        if (watermarkEntity != null){
            BeanUtils.copyProperties(watermarkEntity,watermark);
        }

        //获取user
        WpsUser wpsUser = wpsUserService.getById(userId);
        UserDTO user = new UserDTO();
        if (wpsUser != null){
            BeanUtils.copyProperties(wpsUser,user);
            user.setPermission(permission);
        }

        // 构建fileInfo
        FileDTO file = new FileDTO();
        BeanUtils.copyProperties(fileEntity,file);
        file.setUser_acl(userAcl);
        file.setWatermark(watermark);

        return new HashMap<String, Object>(){
            {
                put("file", file);
                put("user", user);
            }
        };
    }

    @Override
    public void fileRename(String fileName, String userId){
        String fileId = Context.getFileId();
        WpsFile file = this.getById(fileId);
        if (file != null){
            file.setName(fileName);
            file.setModifier(userId);
            Date date = new Date();
            file.setModifyTime(date.getTime());
            this.updateById(file);
        }
    }

    @Override
    public Map<String,Object> fileNew(MultipartFile file, String userId){
        ResFileDTO resFileDTO = WpsOSSUtil.uploadMultipartFile(file);

        String fileName = resFileDTO.getFileName();
        String fileUrl = resFileDTO.getFileUrl();
        int fileSize = (int) file.getSize();
        Date date = new Date();
        long dataTime = date.getTime();
        // 保存文件
        WpsFile f = new WpsFile(fileName,1,fileSize,userId,userId,dataTime,dataTime,fileUrl);
        this.save(f);

        // 处理权限
        wpsUserAclService.saveUserFileAcl(userId,f.getId());

        // 处理水印
        wpsFileWatermarkService.saveWatermark(f.getId());

        // 处理返回
        Map<String,Object> map = new HashMap<>();
        map.put("redirect_url",this.getViewUrl(f.getId(),userId,false).getWpsUrl());
        map.put("user_id",userId);
        return map;
    }

    @Override
    public Map<String,Object> fileHistory(FileReqDTO req){
        Map<String,Object> map = new HashMap<>();
        List<FileHisDTO> result = new ArrayList<>(12);
        if (req.getId() == null){
            map.put("histories",result);
            return map;
        }
        // 目前先实现获取所有的历史记录
        List<WpsFileVersion> versionList = wpsFileVersionService.findByFileIdOrderByVersionDesc(req.getId());
        if (versionList == null || versionList.isEmpty()){
            map.put("histories",result);
            return map;
        }
        Set<String> userIdSet = new HashSet<>();
        for (WpsFileVersion fileVersion : versionList){
            userIdSet.add(fileVersion.getModifier());
            userIdSet.add(fileVersion.getCreator());
        }
        List<String> userIdList = new ArrayList<>(userIdSet);
        // 获取所有关联的人
        List<WpsUser> userList = wpsUserService.findByIdIn(userIdList);
        if (userList == null || userList.isEmpty()){
            map.put("histories",result);
            return map;
        }
        for (WpsFileVersion fileVersion : versionList){
            FileHisDTO fileHis = new FileHisDTO();
            BeanUtils.copyProperties(fileVersion,fileHis);
            fileHis.setId(fileVersion.getFileId());
            UserDTO creator = new UserDTO();
            UserDTO modifier = new UserDTO();
            for (WpsUser user : userList){
                if (user.getId().equals(fileVersion.getCreator())){
                    BeanUtils.copyProperties(user,creator);
                }
                if (user.getId().equals(fileVersion.getModifier())){
                    BeanUtils.copyProperties(user,modifier);
                }
            }
            fileHis.setModifier(modifier);
            fileHis.setCreator(creator);
            result.add(fileHis);
        }
        map.put("histories",result);
        return map;
    }

    @Override
    public Map<String,Object> fileSave(MultipartFile mFile,String userId){
        Date date = new Date();
        // 上传
        ResFileDTO resFileDTO = WpsOSSUtil.uploadMultipartFile(mFile);
        int size = (int) resFileDTO.getFileSize();

        String fileId = Context.getFileId();
        WpsFile file = this.getById(fileId);
        FileDTO fileInfo = new FileDTO();

        String oldFileUrl = file.getDownloadUrl();

        // 更新当前版本
        file.setVersion(file.getVersion() + 1);
        file.setDownloadUrl(resFileDTO.getFileUrl());
        file.setModifier(userId);
        file.setModifyTime(date.getTime());
        file.setSize(size);
        this.updateById(file);

        // 保存历史版本
        WpsFileVersion fileVersion = new WpsFileVersion();
        BeanUtils.copyProperties(file,fileVersion);
        fileVersion.setFileId(fileId);
        fileVersion.setVersion(file.getVersion() - 1);
        fileVersion.setDownloadUrl(oldFileUrl);
        fileVersion.setSize(size);
        wpsFileVersionService.save(fileVersion);

        // 返回当前版本信息
        BeanUtils.copyProperties(file,fileInfo);

        Map<String,Object> map = new HashMap<>();
        map.put("file",fileInfo);
        return map;
    }


    @Override
    public Map<String,Object> fileVersion(Integer version){
        FileDTO fileInfo = new FileDTO();
        String fileId = Context.getFileId();
        WpsFileVersion fileVersion =
                wpsFileVersionService.findByFileIdAndVersion(fileId,version);
        if (fileVersion != null){
            BeanUtils.copyProperties(fileVersion,fileInfo);
            fileInfo.setId(fileVersion.getFileId());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("file",fileInfo);
        return map;
    }
    @Autowired
    private WpsFileMapper wpsFileMapper;

    @Override
    public List<FileListDTO> getFileList(){
        return wpsFileMapper.findAllFile();
    }
    @Override
    public Map<String,Object> getFileListByPage(int pageNum, int pageSize){
        Map<String,Object>result = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<FileListDTO> fileListDTOS = wpsFileMapper.getAllFileByPage();
        PageInfo<FileListDTO> pageInfo = new PageInfo<>(fileListDTOS);
        result.put("data",pageInfo);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }

    @Override
    public int delFile(String id){
        WpsFile file = this.getById(id);
        if (file != null){
            if ("Y".equalsIgnoreCase(file.getCanDelete())){
                // del
                this.removeById(id);
                return 1;
            }else {
                return 0;
            }
        }else {
            return -1;
        }
    }

    @Override
    public void uploadFile(MultipartFile file){
        String uploadUserId = "3";
        ResFileDTO resFileDTO = WpsOSSUtil.uploadMultipartFile(file);
        // 上传成功后，处理数据库记录值
        Date date = new Date();
        long dataTime = date.getTime();
        // 保存文件
        WpsFile f = new WpsFile(resFileDTO.getFileName(),1,((int) resFileDTO.getFileSize()),
                uploadUserId,uploadUserId,dataTime,dataTime, resFileDTO.getFileUrl());
        this.save(f);

        // 处理权限
        wpsUserAclService.saveUserFileAcl(uploadUserId,f.getId());

        // 处理水印
        wpsFileWatermarkService.saveWatermark(f.getId());
    }

    @Override
    public String createTemplateFile(String template){
        boolean typeTrue = FileUtil.checkCode(template);
        if (typeTrue){
            return wpsUtil.getTemplateWpsUrl(template,"3");
        }
        return "";
    }
}
