package com.foreverwin.mesnac.dataimport.controller;

import com.foreverwin.mesnac.dataimport.util.APResult;
import com.foreverwin.mesnac.dataimport.util.ImportRequest;
import com.foreverwin.mesnac.common.model.ExportTemplate;
import com.foreverwin.mesnac.common.service.ExportTemplateService;
import com.foreverwin.mesnac.dataimport.service.impl.MasterDataImportServiceImpl;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.SpringUtil;
import com.sap.me.frame.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dataImport")
public class DataImportController {

    @Autowired
    private MasterDataImportServiceImpl masterDataImportService;

    @PostMapping("/import")
    public String importFile(@PathParam("fileType") String fileType, @PathParam("site") String site, @PathParam("handleType") final String handleType, @PathParam("mode") final String mode, @PathParam("user") String user, @RequestParam("file") MultipartFile multipartFile) {
        ImportRequest importRequest = new ImportRequest();
        importRequest.setFileType(fileType);
        importRequest.setSite(site);
        importRequest.setUser(user);
        importRequest.setMode(mode);
        importRequest.setMasterType(handleType);
        //-----------------------------------------------------------------------------------------------------------------------------------------
        try {
            String transInfo = masterDataImportService.importFile(multipartFile, importRequest);
            return new APResult().getResult(0, transInfo, transInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new APResult().getResult(9999, e.getMessage(), e.getMessage());
        }
    }

    @GetMapping("/getTowRowTemplate")
    public R getTowRowTemplate(String templateCode, String isDoExport) {
        try {
            String site = CommonMethods.getSite();
            ExportTemplateService exportTemplateService = SpringUtil.getBean(ExportTemplateService.class);
            List<ExportTemplate> exportTemplates = exportTemplateService.selectListBySiteCodeLanguage(site, templateCode + "", LocaleContextHolder.getLocale().getLanguage());
            if (Utils.isEmpty(exportTemplates)) {

            } else {
                Map<String, String> map = new HashMap<>();
                exportTemplates.stream().forEach(o -> map.put(o.getColumnName(), o.getColumnLabel()));
                List list = new ArrayList<>();
                list.add(map);
                return R.ok(list);
            }
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
        return R.failed();
    }
}
