package com.izouma.awesomeadmin.service.impl;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dao.CommonQueryMapper;
import com.izouma.awesomeadmin.dao.CommonQuerySqlServerMapper;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.CommonQuery;
import com.izouma.awesomeadmin.model.TestAaa;
import com.izouma.awesomeadmin.service.CommonQueryService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * service接口实现类
 */
@Service
public class CommonQueryServiceImpl implements CommonQueryService {

    private static Logger logger = Logger.getLogger(CommonQueryServiceImpl.class);

    @Autowired
    private CommonQueryMapper commonQueryMapper;

    @Autowired
    private CommonQuerySqlServerMapper cmmonQuerySqlServerMapper;

    @Override
    public List<Map> getTableListByPage(Page page, CommonQuery record) {

        logger.info("getTableListByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put(AppConstant.PAGE, page);

            if ("Mysql".equals(record.getDatabasetype())) {
                return commonQueryMapper.getTableListByPage(parameter);
            } else if ("SqlServer".equals(record.getDatabasetype())) {
                return cmmonQuerySqlServerMapper.getTableListByPage(parameter);
            }


        } catch (Exception e) {
            logger.error("getTableListByPage", e);
        }

        return null;
    }

    @Override
    public List<Map> getTableListAll(CommonQuery record) {

        logger.info("getTableListAll");
        try {

            if ("Mysql".equals(record.getDatabasetype())) {

                return commonQueryMapper.getTableListAll(record);
            } else if ("SqlServer".equals(record.getDatabasetype())) {
                return cmmonQuerySqlServerMapper.getTableListAll(record);
            }


        } catch (Exception e) {
            logger.error("getTableListAll", e);
        }

        return null;
    }

    @Override
    public Map selectByPrimaryKey(CommonQuery record) {

        logger.info("selectByPrimaryKey");
        try {

            if ("Mysql".equals(record.getDatabasetype())) {
                return commonQueryMapper.selectByPrimaryKey(record);
            } else if ("SqlServer".equals(record.getDatabasetype())) {
                return cmmonQuerySqlServerMapper.selectByPrimaryKey(record);
            }


        } catch (Exception e) {
            logger.error("selectByPrimaryKey", e);
        }

        return null;
    }

    @Override
    public boolean create(Map<String, Object> parameter) {

        logger.info("create");
        try {
            int updates = 0;
            if ("Mysql".equals(parameter.get("databasetype").toString())) {
                updates = commonQueryMapper.insertSelective(parameter);
            } else if ("SqlServer".equals(parameter.get("databasetype").toString())) {
                updates = cmmonQuerySqlServerMapper.insertSelective(parameter);
            }


            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("create", e);
        }

        return false;
    }


    @Override
    public boolean update(Map<String, Object> parameter) {

        logger.info("update");
        try {
            int updates = 0;
            if ("Mysql".equals(parameter.get("databasetype").toString())) {
                updates = commonQueryMapper.updateByPrimaryKeySelective(parameter);
            } else if ("SqlServer".equals(parameter.get("databasetype").toString())) {
                updates = cmmonQuerySqlServerMapper.updateByPrimaryKeySelective(parameter);
            }


            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("update", e);
        }

        return false;
    }


    @Override
    public boolean delete(CommonQuery record) {

        logger.info("delete");
        try {
            int updates = 0;
            if ("Mysql".equals(record.getDatabasetype())) {
                updates = commonQueryMapper.delete(record);
            } else if ("SqlServer".equals(record.getDatabasetype())) {
                updates = cmmonQuerySqlServerMapper.delete(record);
            }


            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("delete", e);
        }

        return false;
    }


}

