package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.entity.CommonEntity;
import com.ruoyi.system.entity.DragTable;
import com.ruoyi.system.entity.DragTableCondition;
import com.ruoyi.system.entity.TableSql;
import com.ruoyi.system.mapper.DragTableConditionMapper;
import com.ruoyi.system.mapper.TableSqlMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DragTableMapper;
import com.ruoyi.system.service.IDragTableService;

import javax.annotation.Resource;

/**
 * 动态格Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-31
 */
@Service
public class DragTableServiceImpl implements IDragTableService 
{
    @Autowired
    private DragTableMapper dragTableMapper;

    @Resource
    private DragTableConditionMapper dragTableConditionMapper;

    @Resource
    private TableSqlMapper tableSqlMapper;

    /**
     * 查询动态格
     * 
     * @param tId 动态格主键
     * @return 动态格
     */
    @Override
    public DragTable selectDragTableByTId(Long tId)
    {
        return dragTableMapper.selectDragTableByTId(tId);
    }

    /**
     * 查询动态格列表
     * 
     * @param dragTable 动态格
     * @return 动态格
     */
    @Override
    public List<DragTable> selectDragTableList(DragTable dragTable)
    {
        return dragTableMapper.selectDragTableList(dragTable);
    }

    /**
     * 新增动态格
     * 
     * @param dragTable 动态格
     * @return 结果
     */
    @Override
    public int insertDragTable(DragTable dragTable)
    {
        dragTable.setCreateTime(DateUtils.getNowDate());
        return dragTableMapper.insertDragTable(dragTable);
    }

    /**
     * 修改动态格
     * 
     * @param dragTable 动态格
     * @return 结果
     */
    @Override
    public int updateDragTable(DragTable dragTable)
    {
        dragTable.setUpdateTime(DateUtils.getNowDate());
        return dragTableMapper.updateDragTable(dragTable);
    }

    /**
     * 批量删除动态格
     * 
     * @param tIds 需要删除的动态格主键
     * @return 结果
     */
    @Override
    public int deleteDragTableByTIds(Long[] tIds)
    {
        return dragTableMapper.deleteDragTableByTIds(tIds);
    }

    /**
     * 删除动态格信息
     * 
     * @param tId 动态格主键
     * @return 结果
     */
    @Override
    public int deleteDragTableByTId(Long tId)
    {
        return dragTableMapper.deleteDragTableByTId(tId);
    }

    @Override
    public CommonEntity dragTableInfo(String sqlKey) {
        DragTable dragTable = dragTableMapper.selectDragTableBySqlKey(sqlKey);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("template",dragTable);
        resultMap.put("where",dragTableConditionMapper.selectDragTableConditionByTid(dragTable.gettId()));
        resultMap.put("querySql",tableSqlMapper.selectTableSqlByTSqlKey(sqlKey));
        CommonEntity commonEntity = new CommonEntity();
        commonEntity.setResultMap(resultMap);
        return commonEntity;
    }
}
