package com.dsk.search.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
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.common.utils.redis.RedisUtils;
import com.dsk.jsk.domain.JskCombineSearchDto;
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 lombok.extern.slf4j.Slf4j;
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.time.Duration;
import java.util.*;

/**
 * @Author lc
 * @ClassName ExportService
 * @Date 2023/12/06 16:40
 **/
@Service
@Slf4j
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 (CollectionUtil.isEmpty(companyNameList)) {
            throw new ServiceException("表格中不存在待查询数据!");
        }
        List<String> list = new ArrayList<>();
        for (int i = 0; i < companyNameList.size(); i++) {
            if (null==companyNameList.get(i)){
                return AjaxResult.error("请使用正确的模板");
            }
            if (i<500){
                list.add(companyNameList.get(i).getCompanyName());
            }
        }
        composeQueryDto.setKeyword(String.join(",",list));
        SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd");
        String dayformat = sdf.format(new Date());
        SysUserFileRecord fileRecord = new SysUserFileRecord();
        String fileName = "供应商资质批量查询".concat(dayformat);
        fileRecord.setFileName(fileName);
        fileRecord.setType(1);
        //生成中
        fileRecord.setStatus(2);
        fileRecord.setRemark("资质批量导出");
        fileRecord.setCreateTime(new DateTime());
        Long fileId = sysUserFileRecordService.exportAdd(fileRecord);
        composeQueryDto.setExportUniqueCode(fileId.toString());
        //回调函数
        composeQueryDto.setExportBackUrl(EXPORT_BACK_URL);
        composeQueryDto.setExportExeclName(fileName);
        dskOpenApiUtil.requestBody("/operate/export/zjyj/aptitude", BeanUtil.beanToMap(composeQueryDto,false,false));
        return AjaxResult.success();
    }

    //集团成员导出
    public AjaxResult memberListExport(JskCombineSearchDto dto) {
        SysUserFileRecord fileRecord = new SysUserFileRecord();
        fileRecord.setFileName(dto.getExportExeclName().concat(Constants.SUFFIX_XLSX));
        fileRecord.setType(1);
        //生成中
        fileRecord.setStatus(2);
        fileRecord.setRemark("集团成员列表导出");
        fileRecord.setCreateTime(new DateTime());
        Long fileId = sysUserFileRecordService.exportAdd(fileRecord);
        dto.setExportUniqueCode(fileId.toString());
        //回调函数
        dto.setExportBackUrl(EXPORT_BACK_URL);
        dskOpenApiUtil.requestBody("/operate/export/member", BeanUtil.beanToMap(dto, false, false));
        return AjaxResult.success();
    }

    //集团中标业绩导出
    public AjaxResult exportWinBid(JskCombineSearchDto dto) {
        SysUserFileRecord fileRecord = new SysUserFileRecord();
        fileRecord.setFileName(dto.getExportExeclName().concat(Constants.SUFFIX_XLSX));
        fileRecord.setType(1);
        //生成中
        fileRecord.setStatus(2);
        fileRecord.setRemark("集团中标业绩导出");
        fileRecord.setCreateTime(new DateTime());
        Long fileId = sysUserFileRecordService.exportAdd(fileRecord);
        dto.setExportUniqueCode(fileId.toString());
        //回调函数
        dto.setExportBackUrl(EXPORT_BACK_URL);
        dskOpenApiUtil.requestBody("/operate/export/combineBid", BeanUtil.beanToMap(dto, false, false));
        return AjaxResult.success();
    }

    //集团招标业绩
    public AjaxResult exportBid(JskCombineSearchDto dto) {
        SysUserFileRecord fileRecord = new SysUserFileRecord();
        fileRecord.setFileName(dto.getExportExeclName().concat(Constants.SUFFIX_XLSX));
        fileRecord.setType(1);
        //生成中
        fileRecord.setStatus(2);
        fileRecord.setRemark("集团招标业绩导出");
        fileRecord.setCreateTime(new DateTime());
        Long fileId = sysUserFileRecordService.exportAdd(fileRecord);
        dto.setExportUniqueCode(fileId.toString());
        //回调函数
        dto.setExportBackUrl(EXPORT_BACK_URL);
        dskOpenApiUtil.requestBody("/operate/export/combine/tender", BeanUtil.beanToMap(dto, false, false));
        return AjaxResult.success();
    }

}
