package com.glxp.mipsdl.client.zaxyy;

import cn.hutool.core.thread.ThreadUtil;
import com.glxp.mipsdl.client.BaseFileClient;
import com.glxp.mipsdl.req.post.PostThrCorpRequest;
import com.glxp.mipsdl.req.post.PostThrProductsRequest;
import com.glxp.mipsdl.res.BaseResponse;
import com.glxp.mipsdl.res.udiwms.UdiwmsProductInfoResponse;
import com.glxp.mipsdl.res.udiwms.UdiwmsUnitResponse;
import com.glxp.mipsdl.service.file.UdiInfoUploadService;
import com.glxp.mipsdl.util.ExcelUtil;
import com.glxp.mipsdl.util.ExecutorUtil;
import com.glxp.mipsdl.util.ResultVOUtils;
import com.glxp.mipsdl.vo.file.zazyy.ZyCorpEntity;
import com.glxp.mipsdl.vo.file.zazyy.ZyProductEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 诏安，职业V1文件规范
 */
@Slf4j
@Service
public class ZazyyFileClient implements BaseFileClient {

    @Resource
    private UdiInfoUploadService udiInfoUploadService;
    @Resource
    private ExecutorUtil executorUtil;

    @Override
    public BaseResponse uploadPi(List<MultipartFile> files, String thirdSys) {
        for (int i = 0; i < files.size(); i++) {
            MultipartFile file = files.get(i);
            if (file.isEmpty()) {
                return ResultVOUtils.error(500, "上传第" + (i++) + "个文件失败");
            }
            try {
                InputStream inputStream = file.getInputStream();
                ExcelUtil<ZyProductEntity> util = new ExcelUtil(ZyProductEntity.class);
                List<ZyProductEntity> zyProductEntities = util.importExcel(inputStream);
                List<UdiwmsProductInfoResponse> udiwmsProductInfoResponses = new ArrayList<>();
                for (ZyProductEntity zyProductEntity : zyProductEntities) {
                    UdiwmsProductInfoResponse udiwmsProductInfoResponse = new UdiwmsProductInfoResponse();
                    BeanUtils.copyProperties(zyProductEntity, udiwmsProductInfoResponse);
                    udiwmsProductInfoResponses.add(udiwmsProductInfoResponse);
                }
                PostThrProductsRequest postThrProductsRequest = new PostThrProductsRequest();
                postThrProductsRequest.setDatas(udiwmsProductInfoResponses);
                postThrProductsRequest.setUploadType("文件导入");
                postThrProductsRequest.setThirdSys(thirdSys);
                executorUtil.getUploadExecutor().execute(ThreadUtil.newThread(() -> {
                    udiInfoUploadService.postProducts(postThrProductsRequest);
                }, "uploadPi"));
                log.error("上传完" + System.currentTimeMillis());
            } catch (IOException e) {
                log.error("解析上传excel文件IO异常，异常信息：{}", e);
                return ResultVOUtils.error(500, "数据异常");
            } catch (Exception e) {
                log.error("处理产品信息excel文件异常，异常信息：{}", e);
                return ResultVOUtils.error(500, "数据异常");
            }
        }
        return ResultVOUtils.success("文件解析成功，正在上传，请稍后刷新查看");
    }

    @Override
    public BaseResponse uploadCorps(List<MultipartFile> files, String thirdSys) {
        for (int i = 0; i < files.size(); i++) {
            MultipartFile file = files.get(i);
            if (file.isEmpty()) {
                return ResultVOUtils.error(500, "上传第" + (i++) + "个文件失败");
            }
            List<UdiwmsUnitResponse> udiwmsUnitResponses = new ArrayList<>();
            try {
                InputStream inputStream = file.getInputStream();
                ExcelUtil<ZyCorpEntity> util = new ExcelUtil(ZyCorpEntity.class);
                List<ZyCorpEntity> zyCorpEntities = util.importExcel(inputStream);
                for (ZyCorpEntity zyCorpEntity : zyCorpEntities) {
                    UdiwmsUnitResponse udiwmsUnitResponse = new UdiwmsUnitResponse();
                    udiwmsUnitResponse.setName(zyCorpEntity.getCorpName());
                    udiwmsUnitResponse.setUnitId(zyCorpEntity.getCorpCode());
                    udiwmsUnitResponses.add(udiwmsUnitResponse);
                }
                PostThrCorpRequest postThrProductsRequest = new PostThrCorpRequest();
                postThrProductsRequest.setCorps(udiwmsUnitResponses);
                postThrProductsRequest.setThirdSys(thirdSys);
                executorUtil.getUploadExecutor().execute(ThreadUtil.newThread(() -> {
                    udiInfoUploadService.postCorps(postThrProductsRequest);
                }, "uploadCorps"));
            } catch (IOException e) {
                log.error("解析往来单位excel文件IO异常，错误信息：{}", e);
                return ResultVOUtils.error(500, "数据异常");
            } catch (Exception e) {
                log.error("解析往来单位excel文件异常，异常信息：{}", e);
                return ResultVOUtils.error(500, "数据异常");
            }
        }
        return ResultVOUtils.success("文件解析成功，正在上传，请稍后刷新查看");
    }

    @Override
    public BaseResponse uploadInvPi(List<MultipartFile> files, String thirdSys) {
        return ResultVOUtils.error(500, "暂不支持库存产品信息上传！");
    }

    @Override
    public BaseResponse uploadOrders(List<MultipartFile> files, String thirdSys) {
        return ResultVOUtils.error(500, "暂不支持业务单据上传！");
    }

    public boolean filterEmpty(Row row) {

        if (row.getCell(0) != null) {
            row.getCell(0).setCellType(CellType.STRING);
        }
        if (row.getCell(1) != null) {
            row.getCell(1).setCellType(CellType.STRING);
        }
        if (row.getCell(2) != null) {
            row.getCell(2).setCellType(CellType.STRING);
        }
        if (row.getCell(3) != null) {
            row.getCell(3).setCellType(CellType.STRING);
        }
        if (row.getCell(4) != null) {
            row.getCell(4).setCellType(CellType.STRING);
        }
        if (row.getCell(5) != null) {
            row.getCell(5).setCellType(CellType.STRING);
        }
        if (row.getCell(6) != null) {
            row.getCell(6).setCellType(CellType.STRING);
        }
        if ((row.getCell(0) == null && row.getCell(1) == null && row.getCell(2) == null
                && row.getCell(3) == null && row.getCell(4) == null && row.getCell(5) == null && row.getCell(6) == null) ||
                (
                        (row.getCell(0) != null && row.getCell(1) != null && row.getCell(2) != null
                                && row.getCell(3) != null && row.getCell(4) != null && row.getCell(5) != null && row.getCell(6) != null) &&
                                ("".equals(row.getCell(0).getStringCellValue())
                                        && "".equals(row.getCell(1).getStringCellValue())
                                        && "".equals(row.getCell(2).getStringCellValue())
                                        && "".equals(row.getCell(3).getStringCellValue())
                                        && "".equals(row.getCell(4).getStringCellValue())
                                        && "".equals(row.getCell(5).getStringCellValue())
                                        && "".equals(row.getCell(6).getStringCellValue()))
                )) {
            return true;
        }
        return false;
    }
}
