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


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.config.ModuleParamConfig;
import com.casic.missiles.modular.system.dao.ModuleConfigMapper;
import com.casic.missiles.modular.system.entity.MetaModule;
import com.casic.missiles.modular.system.model.MetaModuleParam;
import com.casic.missiles.modular.system.service.ModuleConfigService;
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 javax.xml.ws.Response;
import java.nio.channels.AcceptPendingException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@AllArgsConstructor
@Service
public class ModuleConfigServiceImpl extends ServiceImpl<ModuleConfigMapper, MetaModule> implements ModuleConfigService {

    private final ModuleParamConfig moduleParamConfig;

    /**
     * 1、先查询表名及相应的表名字段
     * 2、转化表对应字段的数据列表
     */
    @Override
    public Object getTableNameField() {
        ResponseData responseData = new ResponseData();
        try {
            List<Map<String, String>> tableNameFieldList = this.baseMapper.getTableNameField(moduleParamConfig.getDatabaseTable());
            Map<String, List<Map<String,String>>> tableNameFieldMap = tableNameFieldList.stream()
                    .collect(Collectors.groupingBy(e->e.get("tableName")));
            responseData.setMessage("查询成功");
            responseData.setSuccess(true);
            responseData.setCode(200);
            responseData.setData(tableNameFieldMap);
        } catch (Exception ex) {
            log.error("主题:模块初始化查询异常，异常信息{}", ex);
            responseData.setMessage("查询异常");
            responseData.setSuccess(false);
            responseData.setCode(500);
        }
        return responseData;
    }

    @Override
    public Object saveMetaModule(MetaModuleParam metaModuleParam) {
        ResponseData responseData = new ResponseData();
        try {
            QueryWrapper<MetaModule> moduleQueryWrapper = new QueryWrapper<MetaModule>()
                    .eq("device_type", metaModuleParam.getDeviceType())
                    .eq("operation_type", metaModuleParam.getOperationType());
            this.baseMapper.delete(moduleQueryWrapper);
            MetaModule metaModule = metaModuleBuilder(metaModuleParam);
            this.baseMapper.insert(metaModule);
            responseData.setMessage("success");
            responseData.setCode(200);
        } catch (AcceptPendingException ex) {
            log.error("主题:报警内容类型查询异常，异常信息{}", ex);
            responseData.setMessage("查询异常");
            responseData.setCode(500);
        }
        return responseData;
    }

    @Override
    public Object deleteMetaModule(MetaModuleParam metaModuleParam) {
        ResponseData responseData = new ResponseData();
        try {
            QueryWrapper<MetaModule> moduleQueryWrapper = new QueryWrapper<MetaModule>()
                    .eq("device_type", metaModuleParam.getDeviceType())
                    .eq("operation_type", metaModuleParam.getOperationType());
            this.baseMapper.delete(moduleQueryWrapper);
            responseData.setMessage("success");
            responseData.setCode(200);
        } catch (AcceptPendingException ex) {
            log.error("主题:报警内容类型查询异常，异常信息{}", ex);
            responseData.setMessage("查询异常");
            responseData.setCode(500);
        }
        return responseData;
    }

    private MetaModule metaModuleBuilder(MetaModuleParam metaModuleParam) {
        return MetaModule.builder()
                .tableName(metaModuleParam.getTableName())
                .deviceType(metaModuleParam.getDeviceType())
                .fields(metaModuleParam.getFieldJson())
                .metaModuleName(metaModuleParam.getMetaModuleName())
                .operationType(metaModuleParam.getOperationType())
                .enable("1")
                .lastTime(new Date())
                .createTime(new Date())
                .build();
    }
}
