package com.dsk.search.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dsk.common.constant.Constants;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.excel.ExcelUtils;
import com.dsk.common.exception.ServiceException;
import com.dsk.jsk.domain.bo.ComposeQueryDto;
import com.dsk.jsk.service.EnterpriseService;
import com.dsk.system.domain.SysUserFileRecord;
import com.dsk.system.domain.vo.AptitudeVo;
import com.dsk.system.service.ISysUserFileRecordService;
import com.dsk.system.utils.DskOpenApiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lc
 * @ClassName ExportService
 * @Date 2023/12/06 16:40
 **/
@Service
public class ExportService {

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;
    @Autowired
    private ISysUserFileRecordService sysUserFileRecordService;
    @Value("${dsk.exportBackUrl}")
    private String EXPORT_BACK_URL;

    public AjaxResult aptitudeFile(MultipartFile file) throws Exception {
        ComposeQueryDto composeQueryDto = new ComposeQueryDto();
        //识别Excel内容
        List<AptitudeVo> companyNameList = new ExcelUtils<>(AptitudeVo.class).importExcel(file.getInputStream(), 2);
        if (companyNameList.isEmpty()) {
            throw new ServiceException("表格中不存在待导入数据!");
        }
        List<String> list = new ArrayList<>();
        for (int i = 0; i < companyNameList.size(); i++) {
            if (i>499){
                break;
            }
            list.add(companyNameList.get(i).getCompanyName());
        }
        composeQueryDto.setKeyword(String.join(",",list));
        SysUserFileRecord fileRecord = new SysUserFileRecord();
        fileRecord.setFileName("批量导出".concat(Constants.SUFFIX_XLSX));
        fileRecord.setType(1);
        //生成中
        fileRecord.setStatus(2);
        fileRecord.setRemark("资质批量导出");
        Long fileId = sysUserFileRecordService.exportAdd(fileRecord);
        composeQueryDto.setExportUniqueCode(fileId.toString());
        //回调函数
        composeQueryDto.setExportBackUrl(EXPORT_BACK_URL);
        composeQueryDto.setExportExeclName("资质批量导出.xlsx");
        dskOpenApiUtil.requestBody("/nationzj/marketAnalysis/areaGroupByProvince", BeanUtil.beanToMap(composeQueryDto,false,false));
        return AjaxResult.success();
    }

}
