package com.sckw.file.dubbo;

import com.sckw.core.model.constant.NumberConstant;
import com.sckw.core.common.enums.enums.FileEnum;
import com.sckw.core.model.file.FileInfo;
import com.sckw.core.utils.FileUtils;
import com.sckw.core.utils.IdWorker;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.DateUtil;
import com.sckw.file.api.dto.FileInfoDTO;
import com.sckw.file.api.dubbo.FileApiDubboService;
import com.sckw.file.dao.KwsFileInfoDao;
import com.sckw.file.model.kwfFileInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Map;

/**
 * @author lfdc
 * @description fileDubbo调用
 * @date 2023/6/20 0020
 */

@Slf4j
@DubboService(group = "design", version = "1.0.0",timeout = 50000)
public class FileDubboApiServiceImpl implements FileApiDubboService {

    @Autowired
    KwsFileInfoDao fileInfoDao;

    @Override
    public HttpResult fileUpload(String str, byte[] fileByte) {
        return null;
    }

    @Override
    public HttpResult fileUploadTodubbo(MultipartFile file) {
        return null;
    }

    @Override
    public HttpResult selectAll() {
        BigDecimal bigDecimal = new BigDecimal(NumberConstant.ZERO);
        BigDecimal bigDecimal1 = new BigDecimal(NumberConstant.SIX);
        BigDecimal divide = bigDecimal1.divide(bigDecimal);
        return null;
    }

    @Override
    public HttpResult fileDubboUpload(InputStream inputStream, FileInfoDTO fileInfo) {
        Map<String, String> map = FileUtils.uploadFileByInfo(inputStream, fileInfo.getOriginalName(), FileEnum.Local_Address);
        return HttpResult.ok(map);
    }

    @Override
    public HttpResult fileInfoInsert(FileInfo fileInfo) throws ParseException {
        kwfFileInfo kwfFileInfo = new kwfFileInfo();
        kwfFileInfo.setOriginalName(fileInfo.getFileOriginalName());
        kwfFileInfo.setFileSuffix(FilenameUtils.getExtension(fileInfo.getFileOriginalName()));
        kwfFileInfo.setFileSize(StringUtils.isBlank(fileInfo.getFileSize()) ? new BigDecimal(NumberConstant.ZERO) : fileInfo.getFileSize());
        kwfFileInfo.setId(new IdWorker(1).nextId());
        kwfFileInfo.setType(fileInfo.getType());
        kwfFileInfo.setFileName(fileInfo.getFileName());
        kwfFileInfo.setMd5(fileInfo.getFileMd5());
        kwfFileInfo.setFilePath(fileInfo.getFilePath());
        kwfFileInfo.setCreateTime(DateUtil.strToDate(fileInfo.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
        kwfFileInfo.setUpdateTime(DateUtil.strToDate(fileInfo.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
        kwfFileInfo.setStatus(fileInfo.getStatus());
        kwfFileInfo.setDelFlag(fileInfo.getDelFlag());
        kwfFileInfo.setCreateBy(fileInfo.getCreateBy());
        kwfFileInfo.setUpdateBy(fileInfo.getUpdateBy());
        fileInfoDao.insert(kwfFileInfo);
        return HttpResult.ok();
    }
}
