package com.casic.missiles.modular.system.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.system.dao.MetaModuleMapper;
import com.casic.missiles.modular.system.dao.ModuleConfigMapper;
import com.casic.missiles.modular.system.entity.MetaModule;
import com.casic.missiles.modular.system.enums.DeviceDataExportEnum;
import com.casic.missiles.modular.system.service.AbstactCustomizeDataList;
import com.casic.missiles.modular.system.service.MetaModuleService;
import com.casic.missiles.modular.system.util.ApplicationContextUtil;
import com.casic.missiles.modular.system.util.ExportMapToExcel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.nio.channels.AcceptPendingException;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@DS("log")
@AllArgsConstructor
public class MetaModuleServiceImpl extends ServiceImpl<ModuleConfigMapper, MetaModule> implements MetaModuleService {

    private final MetaModuleMapper metaModuleMapper;

    /**
     * 1、查询表及字段
     * 2、自定义流程
     *
     * @param optionType 操作类型
     * @param deviceType 设备类型
     * @return
     */
    @Override
    public Object getDataList(String optionType, String deviceType, String keyWords, String beginTime, String endTime, Long deptid) {
        ResponseData responseData = new ResponseData();
        try {
            responseData.setMessage("查询成功");
            QueryWrapper<MetaModule> moduleQueryWrapper = new QueryWrapper<MetaModule>()
                    .eq("device_type", deviceType)
                    .eq("operation_type", optionType);
            MetaModule metaModule = this.baseMapper.selectOne(moduleQueryWrapper);
            List<Map<String, String>> deviceDataList = metaModuleMapper.getDeviceDataList(metaModule.getTableName(), metaModule.getFields(),
                    beginTime, endTime, keyWords, deptid);
            Boolean deviceListBean = ApplicationContextUtil.containsBean("deviceList");
            if (deviceListBean) {
                AbstactCustomizeDataList customizeProcessor = (AbstactCustomizeDataList) ApplicationContextUtil.getBean("deviceList");
                customizeProcessor.customizeBuilder(deviceDataList);
            }
            responseData.setData(deviceDataList);
            responseData.setSuccess(true);
            responseData.setCode(200);
        } catch (AcceptPendingException ex) {
            log.error("主题:数据列表查询异常，异常信息{}", ex);
            responseData.setMessage("查询异常");
            responseData.setSuccess(false);
            responseData.setCode(500);
        }
        return responseData;
    }

    @Override
    public Object dataListImport(HttpServletResponse response, String optionType, String deviceType, String keyWords, String beginTime, String endTime) {
        ResponseData responseData = new ResponseData();
        try {
            QueryWrapper<MetaModule> moduleQueryWrapper = new QueryWrapper<MetaModule>()
                    .eq("device_type", deviceType)
                    .eq("operation_type", optionType);
            MetaModule metaModule = this.baseMapper.selectOne(moduleQueryWrapper);
            List<Map<String, String>> deviceDataList = metaModuleMapper.getDeviceDataList(metaModule.getTableName(), metaModule.getFields(),
                    beginTime, endTime, keyWords, null);
            AbstactCustomizeDataList customizeProcessor = (AbstactCustomizeDataList) ApplicationContextUtil.getBean("fileCompareService");
            ExportMapToExcel.exportExcel(response, DeviceDataExportEnum.DEVICE_EXPORT_MAP.getExportMap(Integer.valueOf(deviceType)).getFileName(), "标题", deviceDataList);
            customizeProcessor.customizeBuilder(deviceDataList);
            responseData.setMessage("导出异常");
            responseData.setData(deviceDataList);
            responseData.setSuccess(true);
            responseData.setCode(200);
        } catch (Exception ex) {
            log.error("主题:数据导出异常，异常信息{}", ex);
            responseData.setMessage("导出异常");
            responseData.setSuccess(false);
            responseData.setCode(500);
        }
        return responseData;
    }

}
