package com.glxp.mipsdl.client.phxyy;

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.phxyy.PhxyyCorpEntity;
import com.glxp.mipsdl.vo.file.phxyy.PhxyyProductEntity;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * 平和县医院文件接口
 */
@Slf4j
@Service(value = "100005FileClient") //bean注册名称，使用编码 + HttpClient，文件接口加 FileClient
public class PhxyyFileClient 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<PhxyyProductEntity> util = new ExcelUtil(PhxyyProductEntity.class);
                List<PhxyyProductEntity> phxyyProductEntities = util.importExcel("字典", inputStream);
                List<UdiwmsProductInfoResponse> udiwmsProductInfoResponses = new ArrayList<>();
                for (PhxyyProductEntity phxyyProductEntity : phxyyProductEntities) {
                    UdiwmsProductInfoResponse udiwmsProductInfoResponse = new UdiwmsProductInfoResponse();
                    BeanUtils.copyProperties(phxyyProductEntity, 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<PhxyyCorpEntity> util = new ExcelUtil(PhxyyCorpEntity.class);
                List<PhxyyCorpEntity> phxyyCorpEntities = util.importExcel("单位", inputStream);
                for (PhxyyCorpEntity phxyyCorpEntity : phxyyCorpEntities) {
                    UdiwmsUnitResponse udiwmsUnitResponse = new UdiwmsUnitResponse();
                    udiwmsUnitResponse.setName(phxyyCorpEntity.getCorpName());
                    udiwmsUnitResponse.setUnitId(phxyyCorpEntity.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, "暂不支持业务单据上传！");
    }
}
