package com.bonus.system.file.service;

import com.bonus.common.core.utils.DateTimeHelper;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.att.entity.AttDataDetailsBean;
import com.bonus.system.file.dao.FileUploadDao;
import com.bonus.system.file.entity.FileSourceVo;
import com.bonus.system.file.util.FileUtils;
import com.bonus.system.file.util.IDUtils;
import com.bonus.system.file.util.SystemUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className:FileUploadService
 * @author:cwchen
 * @date:2025-02-19-11:30
 * @version:1.0
 * @description:文件上传-业务层
 */
@Service(value = "FileUploadService")
@Slf4j
public class FileUploadServiceImpl implements FileUploadService{

    @Resource(name = "FileUploadDao")
    private FileUploadDao dao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAttUpdateFileSource(List<AttDataDetailsBean> list) {
        try {
            List<FileSourceVo> fileSourceVoList = new ArrayList<>();
            List<String> delFileList  = new ArrayList<>();
            for (AttDataDetailsBean bean : list) {
                List<AttDataDetailsBean.FilesVo> filesVo = bean.getFilesVoList();
                if(CollectionUtils.isNotEmpty(filesVo)){
                    for (AttDataDetailsBean.FilesVo vo : filesVo) {
                        FileSourceVo fileSourceVo = new FileSourceVo();
                        fileSourceVo.setFileName(vo.getFileName());
                        fileSourceVo.setFileSuffix(FileUtils.getFileExtension(vo.getFileName()));
                        fileSourceVo.setFilePath(vo.getFilePath());
                        fileSourceVo.setFileType(FileUtils.getFileType(vo.getFileName()));
                        fileSourceVo.setSourceId(bean.getUserId()+"@"+bean.getAttCurrentDay()+"@"+vo.getAttType());
                        fileSourceVo.setSourceType(1);
                        fileSourceVo.setCreateUser(SecurityUtils.getUserId());
                        fileSourceVo.setUpdateUser(SecurityUtils.getUserId());
                        // 更新图片ID
                        delFileList.add(bean.getUserId()+"@"+bean.getAttCurrentDay()+"@"+vo.getAttType());
                        fileSourceVoList.add(fileSourceVo);
                    }
                }
            }
            delFileResource(delFileList,1);
            addFileResource(fileSourceVoList);
        } catch (Exception e) {
            log.error(e.toString(),e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    public void addFileResource(List<FileSourceVo> list){
        dao.addFileResource(list);
    }

    public void delFileResource(List<String> list,int sourceType){
        dao.delFileResource(list,sourceType);
    }

    @Override
    public List<AttDataDetailsBean.FilesVo> getFiles(AttDataDetailsBean vo) {
        try {
            // 上班附件和下班附件
            List<String> list = new ArrayList<>();
            list.add(vo.getUserId()+"@"+vo.getAttCurrentDay()+"@"+"1");
            list.add(vo.getUserId()+"@"+vo.getAttCurrentDay()+"@"+"2");
            return dao.getFiles(list);
        } catch (Exception e) {
            log.error(e.toString(),e);
            return null;
        }
    }
}
