package com.jwipc.order.logic.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.order.convert.FileLogConvert;
import com.jwipc.order.entity.FileLog;
import com.jwipc.order.logic.FileLogLogic;
import com.jwipc.order.model.po.FileLogDTO;
import com.jwipc.order.model.query.BasicPlanQuery;
import com.jwipc.order.service.IFileLogService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.prop.SysProp;
import com.jwipc.sys.dto.FileResposeDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p> </p>
 *
 * @author 文件日志操作记录
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class FileLogLogicImpl implements FileLogLogic {
    private final IFileLogService fileLogService;
    private final SysProp sysProp;
    private final FileLogConvert fileLogConvert;

    /**
     * @param fileLogDTO 文件名称
     * @return 文件
     */
    @Override
    public Long createFile(FileLogDTO fileLogDTO) {
        FileLog fileLog = fileLogConvert.entity2DTO(fileLogDTO);
        fileLogService.save(fileLog);
        return fileLog.getId();
    }

    @Override
    public FileResposeDTO getForDownload2(Long fileId) {
        final FileLog byId = fileLogService.getById(fileId);
        final FileResposeDTO fileResposeDTO = new FileResposeDTO();
        fileResposeDTO.setFileName(byId.getFileName());
        fileResposeDTO.setFilePath(byId.getFilePath());
        return fileResposeDTO;
    }

    @Override
    public PageView selectListByPage(BasicPlanQuery query) {
        final Page<FileLog> dbPage = PageUtil.toPage(query);
        List<FileLogDTO> poList = fileLogService.selectListByPage(dbPage, query);
        poList.stream().forEach(item -> {
            if (StringUtils.isNotEmpty(item.getFileName())) {
                item.setFileSizeStr(item.getFileSize() / 1024 / 1024 + "M");
            }
        });
        return PageUtil.getPV(poList, dbPage.getTotal());
    }
}