package com.tofly.xrtygis.custom.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tofly.xrtygis.custom.entity.ScadaPar;
import com.tofly.xrtygis.custom.mapper.ExportAboutMapper;
import com.tofly.xrtygis.custom.service.ExportAboutService;
import com.tofly.xrtygis.entity.Scadastaitem;
import com.tofly.xrtygis.service.ScadastaitemService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 数据导出相关
 *
 * @author 陈浩
 * @date 2021-4-17
 */
@Service("exportAboutService")
@AllArgsConstructor
public class ExportAboutServiceImpl implements ExportAboutService {

    private final ScadastaitemService scadastaitemService;

    private final ExportAboutMapper exportAboutMapper;

    /**
     * excel获取导出数据
     *
     * @param dto          筛选条件对象
     * @param templateFlag 0为正式导出/1为演示模板导出
     * @return 封装好的导出对象
     */
    @Override
    public List<Map<String, Object>> getExportData(String dto, String templateFlag) {
        List<Map<String, Object>> returnMap = new ArrayList<>();
        Map<String, Object> parentMap = new HashMap<>(12);
        ScadaPar scadaPar = JSONObject.parseObject(dto, ScadaPar.class);

        Scadastaitem item = scadastaitemService.getOne(Wrappers.<Scadastaitem>lambdaQuery()
                .eq(Scadastaitem::getState,1)
                .eq(Scadastaitem::getStationId, Long.parseLong(scadaPar.getStid()))
                .eq(Scadastaitem::getItnm, scadaPar.getItnm()));

        if (null != item){
            List<Map> history = exportAboutMapper.selectHistory(item.getItcd(), scadaPar.getStart(), scadaPar.getEnd());

            parentMap.put("list", history);
            returnMap.add(parentMap);
        }
        return returnMap;
    }

    /**
     * excel导入的数据落库
     *
     * @param dataList 导入的数据
     * @return 是否存储成功
     */
    @Override
    public Boolean excelDataStorage(List<Object> dataList) {
        return null;
    }
}
