package com.tofly.xrtymis.custom.service.export.impl;

import com.alibaba.fastjson.JSONArray;
import com.tofly.xrtymis.custom.service.export.ExportCommonCodeService;
import com.tofly.xrtymis.em.Constant;
import com.tofly.xrtymis.em.ExcelExportFileTypeEnum;
import com.tofly.xrtymis.strategy.ExcelStrategyContext;
import com.tofly.xrtymis.util.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class ExportCommonCodeServiceImpl implements ExportCommonCodeService {

    private final ExcelStrategyContext excelStrategyContext;

    @Override
    public Object exportCode(HttpServletResponse response,String service, List list) {
        // 根据templateServiceType,templateFlag获取对应模板数据
        ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum(service, "1");
        if (null == excelExportFileTypeEnum) {
            return null;
        }
        //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
        String resultListToString = JSONArray.toJSONString(list);
        // 从模型类型枚举根据templateServiceType获取策略对应模板数据
        List<Map<String, Object>> dataList = excelStrategyContext.getExportData(service, "1", resultListToString);
        ExcelUtil.export(dataList.get(0), excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
        return null;
    }
}
