/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.youchain.basicdata.rest;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.basicdata.service.*;
import com.youchain.basicdata.service.impl.ImportPickDetailServiceImpl;
import com.youchain.businessdata.domain.Asn;
import com.youchain.businessdata.domain.PickDetail;
import com.youchain.businessdata.domain.PickTicket;
import com.youchain.businessdata.service.AsnService;
import com.youchain.businessdata.service.PickDetailService;
import com.youchain.businessdata.service.PickTicketService;
import com.youchain.config.FileProperties;
import com.youchain.exception.BadRequestException;
import com.youchain.exception.handler.ApiResult;
import com.youchain.utils.BizStatus;
import com.youchain.utils.CodeUtils;
import com.youchain.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.springframework.http.HttpStatus.OK;


/**
 * @author houjianlan
 * @website https://eladmin.vip
 * @date 2023-08-16
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "导入管理")
@Slf4j
@RequestMapping("/api/importData")
public class ImportDataController {
    private final FileProperties properties;
    private final AsnService asnService;
    private final PickTicketService pickTicketService;
    private final PickDetailService pickDetailService;
    private final ImportDataService importDataService;
    private final ImportAsnService importAsnService;
    private final ImportBomAccountService importBomAccountService;
    private final ImportPointService importPointService;
    private final ImportItemService importItemService;
    private final ImportProductionPlanService importProductionPlanService;
    private final ImportAsnDetailService importAsnDetailService;
    private final ImportPickDetailService importPickDetailService;
    private final ImportCountDetailService importCountDetailService;
    private final ImportXppPlanService importXppPlanService;

    @Log("导入完成品品番")
    @ApiOperation("导入完成品品番")
    @PostMapping(value = "/bigItem")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importBigItem(@RequestParam("file") MultipartFile multipartFile) {
        List<Map<String, Object>> readAll = getMaps(multipartFile);
        int a = 0;//新增
        int b = 0;//修改
        int c = 0;//错误
        int d = 0;//执行
        List<String> cList = new ArrayList<>();
        for (int i = 0; i < readAll.size(); i++) {
            String code = readAll.get(i).get("完成品品番") == null ? null : readAll.get(i).get("完成品品番").toString();
            if (code == null || code.equals("")) {
                break;
            }
            String re = importDataService.importBigItem(readAll.get(i));
        }
        return new ResponseEntity("导入成功", OK);
    }

    @Log("导入部品品番")
    @ApiOperation("导入部品品番")
    @PostMapping(value = "/item")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importItem(@RequestParam("file") MultipartFile multipartFile) {
        log.info("开始导入");
        long start = System.currentTimeMillis();
        importItemService.importData(multipartFile);
        log.info("导入结束，耗时：{}ms", (System.currentTimeMillis() - start));
        return new ResponseEntity<>(ApiResult.success(OK.value(), "导入成功", null), OK);
    }

    @Log("导入其他入库")
    @ApiOperation("导入其他入库")
    @PostMapping(value = "/importAsnDetail")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importAsnDetail(@RequestParam Long asnId, @RequestParam("file") MultipartFile multipartFile) {
        log.info("开始导入");
        long start = System.currentTimeMillis();
        importAsnDetailService.importData(asnId,multipartFile);
        log.info("导入结束，耗时：{}ms", (System.currentTimeMillis() - start));
        return new ResponseEntity<>(ApiResult.success(OK.value(), "导入成功", null), OK);
    }

    @Log("导入其他出库")
    @ApiOperation("导入其他出库")
    @PostMapping(value = "/importPickDetail")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importPickDetail(@RequestParam Long ptId, @RequestParam("file") MultipartFile multipartFile) {
        log.info("开始导入出库明细");
        long start = System.currentTimeMillis();
        importPickDetailService.importData(ptId,multipartFile);
        log.info("导入结束，耗时：{}ms", (System.currentTimeMillis() - start));
        return new ResponseEntity<>(ApiResult.success(OK.value(), "导入成功", null), OK);
    }

    @Log("导入手工指示")
    @ApiOperation("导入手工指示")
    @PostMapping(value = "/importPickInstruction")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importPickInstruction(@RequestParam("file") MultipartFile multipartFile) {
        List<Map<String, Object>> readAll = getMaps(multipartFile);
        importDataService.importPickInstruction(readAll);
        return new ResponseEntity("导入成功", OK);
    }

//    @Log("导入移位明细")
//    @ApiOperation("导入移位明细")
//    @PostMapping(value = "/importCountMoveDetail")
//    @Transactional
//    @AnonymousAccess
//    public ResponseEntity<Object> importCountMoveDetail(@RequestParam Long countId, @RequestParam("file") MultipartFile multipartFile) {
//        List<Map<String, Object>> readAll = getMaps(multipartFile);
//        importDataService.importCountMoveDetail(countId, readAll);
//        return new ResponseEntity("导入成功", OK);
//    }

    @Log("导入BOM工位清单")
    @ApiOperation("导入BOM工位清单")
    @PostMapping(value = "/importBomAccountOld")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importBomAccountOld(@RequestParam("file") MultipartFile multipartFile) {
        List<Map<String, Object>> readAll = getMaps(multipartFile);
        for (int i = 0; i < readAll.size(); i++) {
            importDataService.importBomAccount(i, readAll.get(i));
        }
        return new ResponseEntity("导入成功" + readAll.size() + "行", OK);
    }


    /**
     * 解析导入文件
     *
     * @param multipartFile
     * @return
     */
    private List<Map<String, Object>> getMaps(MultipartFile multipartFile) {
        FileUtil.checkSize(properties.getMaxSize(), multipartFile.getSize());
        String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
        String type = FileUtil.getFileType(suffix);
        log.error("--" + properties.getPath().getPath() + type + File.separator);
        File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + type + File.separator);
        log.error("--" + file.getPath());
        ExcelReader reader = ExcelUtil.getReader(file);
        List<Map<String, Object>> readAll = reader.readAll();
        return readAll;
    }


    @PostMapping("/importAsn")
    @Log("导入asn")
    @ApiOperation("导入asn")
    @AnonymousAccess
    public ResponseEntity<Object> importAsn(@RequestParam("file") MultipartFile file, @RequestParam("templateType") String templateType) {
        log.info("开始导入发票箱单");
        long start = System.currentTimeMillis();
        importAsnService.importAsn(file, templateType);
        log.info("导入结束，耗时：{}ms", (System.currentTimeMillis() - start));
        return new ResponseEntity<>(ApiResult.success(OK.value(), "导入成功", null), OK);
    }

    @Log("导入BOM多列")
    @ApiOperation("导入BOM多列")
    @PostMapping(value = "/importBomAccountBatch")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importBomAccountBatch(@RequestParam("file") MultipartFile multipartFile) {
        log.info("开始导入Bom工位清单批量");
        long start = System.currentTimeMillis();
        importBomAccountService.importBomAccountBatch(multipartFile);
        log.info("导入结束，耗时：{}ms", (System.currentTimeMillis() - start));
        return new ResponseEntity<>(ApiResult.success(OK.value(), "导入成功", null), OK);
    }

    @Log("导入BOM")
    @ApiOperation("导入BOM")
    @PostMapping(value = "/importBomAccount")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importBomAccount(@RequestParam("file") MultipartFile multipartFile) {
        log.info("开始导入Bom工位清单");
        long start = System.currentTimeMillis();
        importBomAccountService.importBomAccount(multipartFile);
        log.info("导入结束，耗时：{}ms", (System.currentTimeMillis() - start));
        return new ResponseEntity<>(ApiResult.success(OK.value(), "导入成功", null), OK);
    }


    @Log("导入ProductionPlan")
    @ApiOperation("导入ProductionPlan")
    @PostMapping(value = "/importProductionPlan")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importProductionPlan(@RequestParam("file") MultipartFile multipartFile) {
        log.info("开始导入生产顺序计划");
        long start = System.currentTimeMillis();
        importProductionPlanService.importData(multipartFile);
        log.info("导入结束，耗时：{}ms", (System.currentTimeMillis() - start));
        return new ResponseEntity<>(ApiResult.success(OK.value(), "导入成功", null), OK);
    }

    @Log("导入库位")
    @ApiOperation("导入库位")
    @PostMapping(value = "/importPoint")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importPoint(@RequestParam("file") MultipartFile multipartFile) {
        log.info("开始导入点位");
        long start = System.currentTimeMillis();
        importPointService.importPoint(multipartFile);
        log.info("导入结束，耗时：{}ms", (System.currentTimeMillis() - start));
        return new ResponseEntity("导入成功", OK);
    }
    @Log("导入库位更新")
    @ApiOperation("导入库位更新")
    @PostMapping(value = "/importPointUpdate")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importPointUpdate(@RequestParam("file") MultipartFile multipartFile) {
        log.info("开始导入点位");
        long start = System.currentTimeMillis();
        importPointService.importPointUpdate(multipartFile);
        log.info("导入结束，耗时：{}ms", (System.currentTimeMillis() - start));
        return new ResponseEntity("导入成功", OK);
    }
    @Log("导入移位明细")
    @ApiOperation("导入移位明细")
    @PostMapping(value = "/importCountMoveDetail")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importCountMoveDetail(@RequestParam Long countId, @RequestParam("file") MultipartFile multipartFile) {
        log.info("开始导入移位明细");
        long start = System.currentTimeMillis();
        importCountDetailService.importData(countId, multipartFile);
        log.info("导入结束，耗时：{}ms", (System.currentTimeMillis() - start));
        return new ResponseEntity("导入成功", OK);
    }

    @Log("导入现品票计划")
    @ApiOperation("导入现品票计划")
    @PostMapping(value = "/importXppPlan")
    @Transactional
    @AnonymousAccess
    public ResponseEntity<Object> importXppPlan(@RequestParam("file") MultipartFile multipartFile) {
        log.info("开始导入");
        long start = System.currentTimeMillis();
        importXppPlanService.importData(multipartFile);
        log.info("导入结束，耗时：{}ms", (System.currentTimeMillis() - start));
        return new ResponseEntity("导入成功", OK);
    }

}