package com.tofly.codegen.service.impl;

import com.tofly.codegen.entity.TableDetailGen;
import com.tofly.codegen.entity.TableGen;
import com.tofly.codegen.service.TableProcessService;
import com.tofly.codegen.util.DataSourceUtil;
import com.tofly.codegen.util.DataTypeSqlInfo;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author tuonbed
 * @date 2020/1/2  15:54
 */
@Service
public class TableProcessServiceImpl implements TableProcessService {

    @Override
    @Transactional
    public int insertTableGen(final TableGen tableGen) {
        final List<TableDetailGen> detailList = tableGen.getDetailGenList();
        JdbcTemplate jdbcTemplate= DataSourceUtil.getJdbcTemplate();
        DataTypeSqlInfo dataTypeSqlInfo=DataSourceUtil.getDataTypeSqlInfo();
        Long tableGenid= jdbcTemplate.queryForObject(dataTypeSqlInfo.getQUERYMAXSQL()+dataTypeSqlInfo.getTABLEGEN_TABLENAME(),Long.class);
        Long tableDetailGenid= jdbcTemplate.queryForObject(dataTypeSqlInfo.getQUERYMAXSQL()+dataTypeSqlInfo.getTABLEDETAILGEN_TABLENAME(),Long.class);
        tableGen.setId(tableGenid);

        if(tableGen.getDetailGenList().size()>0){
            jdbcTemplate.update(dataTypeSqlInfo.getDELETE_TABLEGEN_SQL(),tableGen.getTableName());
            jdbcTemplate.update(dataTypeSqlInfo.getDELETE_TABLEDETAILGEN_SQL(),tableGen.getTableName());
        }

        jdbcTemplate.update(dataTypeSqlInfo.getINSERT_TABLEGEN_SQL(),new PreparedStatementSetter(){

            @Override
            public void setValues(PreparedStatement preparedStatement) throws SQLException {
                preparedStatement.setLong(1,tableGenid);
                preparedStatement.setString(2,tableGen.getTableName());
                preparedStatement.setString(3,tableGen.getCreate_user());
                preparedStatement.setShort(4,Short.valueOf("1"));
            }
        });

//        for(TableDetailGen tableDetailGen:detailList){
//            tableDetailGen.setId(tableDetailGenid);
//            tableDetailGen.setTablegenId(tableGenid);
//            tableDetailGenid++;
//        }

        int[] ints = jdbcTemplate.batchUpdate(dataTypeSqlInfo.getBATCHINSERT_TABLEDETAILGEN_SQL(), new BatchPreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                preparedStatement.setLong(1, tableDetailGenid + i);
                preparedStatement.setString(2, detailList.get(i).getTableName());
                preparedStatement.setString(3, detailList.get(i).getCreateUser());
                preparedStatement.setShort(4, Short.valueOf("1"));
                preparedStatement.setString(5, detailList.get(i).getType());
                preparedStatement.setLong(6, tableGenid);
                preparedStatement.setString(7, detailList.get(i).getColumnName());
                preparedStatement.setString(8, detailList.get(i).getDefaultValue());
                preparedStatement.setString(9, detailList.get(i).getQueryLike());
                preparedStatement.setString(10, detailList.get(i).getCodeKey());
                preparedStatement.setString(11,detailList.get(i).getCustomsql());
            }

            @Override
            public int getBatchSize() {
                return detailList.size();
            }
        });
        return ints.length;
    }

    @Override
    public int updateTableGen(TableGen tableGen) {
        return 0;
    }
}
