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.text.SimpleDateFormat;
import java.util.*;

/**
 * @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);
        SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd");
        String dayformat = sdf.format(new Date());
        composeQueryDto.setExportExeclName("供应商资质批量查询"+dayformat+".xlsx");
        dskOpenApiUtil.requestBody("/operate/export/zjyj/aptitude", BeanUtil.beanToMap(composeQueryDto,false,false));
        return AjaxResult.success();
    }

}
