package com.chinaitop.depot.intelligent.grainsituation.controller;

import com.chinaitop.depot.intelligent.grainsituation.service.TemperImportService;
import com.chinaitop.depot.intelligent.utils.ImportServiceImpl;
import com.unissoft.model.TypeEnum;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qingsong.han
 * @description: 粮温历史数据导入
 * @create 2020-08-03 9:39
 */
@RestController
@RequestMapping(value = "/intelligentTemperature")
public class TemperImportController {
//    final static Logger logger = LoggerFactory.getLogger(GrainPushsServiceImpl.class);

    @Resource
    private ImportServiceImpl importService;
    @Autowired
    private TemperImportService temperImportService;

    /**
     * 导入excel
     *
     * @param file     文件
     * @param fileType 类型：如xlsx
     * @param orgId    组织机构id
     * @return
     */
    @RequestMapping(value = "/importFile", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "导入excel", notes = "导入excel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件数据", paramType = "query"),
            @ApiImplicitParam(name = "fileType", value = "文件后缀", paramType = "query"),
            @ApiImplicitParam(name = "rootPath", value = "路径", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织机构id", paramType = "query")
    })
    public Map<String, Object> importFile(MultipartFile file, String fileType, HttpServletRequest request, Integer orgId) {

        String[] titleArr = {"测温线缆列号(X)", "测温线缆行号(Y)", "测温点高度(Z)", "温度值"};//第一行的title
        boolean[] emptyArr = {false, false, false, false};//指定列可为空，不可为空值为false.

        List<List<String>> list = new ArrayList<List<String>>();//该list为数据返回的list
        Map<String, Object> fileMap = new HashMap<>();
        String errormsg = null;
        try {
            errormsg = importService.importFile(file, fileType, request, titleArr, emptyArr, list, 3);

            if (errormsg != null && !"".equals(errormsg)) {
                fileMap.put("errormsg", errormsg);
                return fileMap;
            }

            //查询代储点名称和代储库名称对应的id
            Map<String, Object> agentMap = importService.temperData(file, fileType, request);

            if (list.size() > 0) {
                errormsg = temperImportService.importData(list, orgId, agentMap);
            }
        } catch (Exception e) {
            fileMap.put("errormsg", TypeEnum.ED.getCode()+",请检查模板数据!");
            e.printStackTrace();
            return fileMap;
        }
        fileMap.put("errormsg", errormsg);
        return fileMap;
    }

}
