package com.xjrsoft.module.dataexpert.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.db.Db;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.utils.DatasourceUtil;
import com.xjrsoft.module.dataexpert.entity.DataExpertTemplate;
import com.xjrsoft.module.dataexpert.entity.DataExpertTemplateField;
import com.xjrsoft.module.dataexpert.mapper.DataExpertTemplateFieldMapper;
import com.xjrsoft.module.dataexpert.mapper.DataExpertTemplateMapper;
import com.xjrsoft.module.dataexpert.service.IDataExpertTemplateService;
import com.xjrsoft.module.dataexpert.vo.DataExpertTemplateFieldVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

/**
* @title: 数据导出-数据模板
* @Author dzx
* @Date: 2024-04-19
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class DataExpertTemplateServiceImpl extends MPJBaseServiceImpl<DataExpertTemplateMapper, DataExpertTemplate> implements IDataExpertTemplateService {
    private final DataExpertTemplateFieldMapper fieldMapper;

    private final DataExpertTemplateMapper templateMapper;
    @Override
    public List<DataExpertTemplateFieldVo> getFieldList(Long id) {
        List<DataExpertTemplateField> list = fieldMapper.selectList(
            new QueryWrapper<DataExpertTemplateField>().lambda()
            .eq(DataExpertTemplateField::getDataExpertTemplateId, id)
        );
        return BeanUtil.copyToList(list, DataExpertTemplateFieldVo.class);
    }

    @Override
    public List<String[]> getDataList(String fields, String viewName, String conditions) throws SQLException {
        DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
        Db use = Db.use(datasource);
        String sql = "SELECT " + fields + " FROM " + viewName + " WHERE 1 = 1 " + conditions;
        List<String[]> list = use.query(sql, String[].class);
        return list;
    }
}
