package com.yanzhu.jh.work.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.yanzhu.jh.work.domain.WorkFile;
import com.yanzhu.jh.work.mapper.WorkFileMapper;
import com.yanzhu.jh.work.service.IWorkFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * 文件传达Service业务层处理
 *
 * @author JIangYuQi
 * @date 2023-08-06
 */
@Service
public class WorkFileServiceImpl implements IWorkFileService
{
    @Autowired
    private WorkFileMapper workFileMapper;

    /**
     * 查询文件传达
     *
     * @param id 文件传达主键
     * @return 文件传达
     */
    @Override
    public WorkFile selectWorkFileById(Long id)
    {
        return workFileMapper.selectWorkFileById(id);
    }

    /**
     * 查询文件传达列表
     *
     * @param workFile 文件传达
     * @return 文件传达
     */
    @Override
    public List<WorkFile> selectWorkFileList(WorkFile workFile)
    {
        return workFileMapper.selectWorkFileList(workFile);
    }

    /**
     * 查询文件传达列表
     *
     * @param workFile 文件传达
     * @return 文件传达
     */
    @Override
    public List<WorkFile> selectWorkFileListLimit20(WorkFile workFile)
    {
        return workFileMapper.selectWorkFileListLimit20(workFile);
    }

    /**
     * 新增文件传达
     *
     * @param workFile 文件传达
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWorkFile(WorkFile workFile)
    {

        List<Map<String,Object>> fileList = workFile.getFileList();
        int res = 0;
        DecimalFormat df = new DecimalFormat ("#.#");
        if(fileList!=null && fileList.size()>0){
            for(Map<String,Object> fileMap:fileList){
                WorkFile lastingModel = new WorkFile();
                lastingModel.setFileBelong(workFile.getFileBelong());
                lastingModel.setFileType(FileUtils.getFileLastNameUpCase(Convert.toStr(fileMap.get("original"))));
                lastingModel.setFileName(Convert.toStr(fileMap.get("original")));
                lastingModel.setFilePath(Convert.toStr(fileMap.get("name")));
                String filesize = df.format(Convert.toInt(fileMap.get("size"))/1024/1024);
                if(filesize.equals("0")){
                    filesize = "0.1";
                }
                lastingModel.setFileSize(filesize);
                lastingModel.setCreateBy(SecurityUtils.getUsername());
                lastingModel.setCreateTime(DateUtils.getNowDate());
                if(workFileMapper.insertWorkFile(lastingModel)>0){
                    res++;
                }else{
                    throw new RuntimeException("新增文件传达异常");
                }
            }
        }
        return res;
    }

    /**
     * 修改文件传达
     *
     * @param workFile 文件传达
     * @return 结果
     */
    @Override
    public int updateWorkFile(WorkFile workFile)
    {
        workFile.setUpdateTime(DateUtils.getNowDate());
        return workFileMapper.updateWorkFile(workFile);
    }

    /**
     * 批量删除文件传达
     *
     * @param ids 需要删除的文件传达主键
     * @return 结果
     */
    @Override
    public int deleteWorkFileByIds(Long[] ids)
    {
        return workFileMapper.deleteWorkFileByIds(ids);
    }

    /**
     * 删除文件传达信息
     *
     * @param id 文件传达主键
     * @return 结果
     */
    @Override
    public int deleteWorkFileById(Long id)
    {
        return workFileMapper.deleteWorkFileById(id);
    }
}
