package com.foreverwin.mesnac.dataimport.service.impl;

import cn.hutool.core.util.StrUtil;
import com.foreverwin.mesnac.dataimport.handler.HandlerFactory;
import com.foreverwin.mesnac.dataimport.util.ImportRequest;
import com.foreverwin.mesnac.dataimport.handler.base.BaseHandler;
import com.foreverwin.mesnac.dataimport.handler.base.Handler;
import com.foreverwin.mesnac.dataimport.service.MasterDataImportService;
import com.foreverwin.mesnac.dataimport.service.MasterImportLogService;
import com.foreverwin.modular.core.util.CommonMethods;
import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Ervin Chen
 * @date 2020/9/22 20:45
 */
@Service("masterDataImportService")
public class MasterDataImportServiceImpl implements MasterDataImportService {

    @Autowired
    private AutowireCapableBeanFactory factory;
    @Autowired
    MasterImportLogService logService;

    @Transactional(rollbackFor = Exception.class)
    public String importFile(FileItem fileItem, HttpServletRequest req) throws Exception {
        String fileType = req.getParameter("fileType");
        String site = req.getParameter("site");
        String masterType = req.getParameter("masterType");
        String mode = req.getParameter("mode");
        String user = req.getParameter("user");
        //---------------------------------------------------------------------------------------------------------------------------------------
        InputStream inputStream = null;
        try {
            if (!StrUtil.isBlank(site)) CommonMethods.setSite(site);
            if (!StrUtil.isBlank(user)) CommonMethods.setUser(user);
            //---------------------------------------------------------------------------------------------------------------------------------------
            inputStream = fileItem.getInputStream();
            Handler handler = HandlerFactory.getHandler(masterType);
            ((BaseHandler) handler).setInit(site, mode);
            //---------------------------------------------------------------------------------------------------------------------------------------
            factory.autowireBean(handler);
            String transInfo = handler.importFile(site, inputStream, fileType, mode);
            //---------------------------------------------------------------------------------------------------------------------------------------
            return transInfo;
        } finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                    fileItem.delete();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public String importFile(MultipartFile multipartFile, ImportRequest importRequest) throws Exception {
        String fileType = importRequest.getFileType();
        String site = importRequest.getSite();
        String masterType = importRequest.getMasterType();
        String mode = importRequest.getMode();
        String user = importRequest.getUser();
        //---------------------------------------------------------------------------------------------------------------------------------------
        InputStream inputStream = null;
        try {
            if (!StrUtil.isBlank(site)) CommonMethods.setSite(site);
            if (!StrUtil.isBlank(user)) CommonMethods.setUser(user);
            //---------------------------------------------------------------------------------------------------------------------------------------
            inputStream = multipartFile.getInputStream();
            Handler handler = HandlerFactory.getHandler(masterType);
            ((BaseHandler) handler).setInit(site, mode);
            //---------------------------------------------------------------------------------------------------------------------------------------
            factory.autowireBean(handler);
            String transInfo = handler.importFile(site, inputStream, fileType, mode);
            //---------------------------------------------------------------------------------------------------------------------------------------
            return transInfo;
        } finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
