package weaver.interfaces.htxc.tycgsq.action;

import weaver.conn.RecordSet;
import weaver.general.GCONST;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-09-21 16:54
 * @Description:
 * @ModifyLog:
 **/

/**
 * c:返回值类型
 */
public class SelectToEntityImpl implements  SelectToEntity {
    @Override
    public <T> T select(String sql, List<String> param, Class<T> c) {
        T t =null;
        try {
            t = c.newInstance();
            RecordSet rs = new RecordSet();
            rs.executeQuery(sql,param);
            if(rs.next()){
                String[] columnName = rs.getColumnName();
                for (String s : columnName) {
                    Field field = c.getDeclaredField(s.toLowerCase());
                    field.setAccessible(true);
                    field.set(t,rs.getString(s));
                }
                return t;
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return t;
    }

//    public static void main(String[] args) {
//        GCONST.setRootPath("WEB-INF/");
//        GCONST.setServerName("ecology");
//        SelectToEntityImpl entity = new SelectToEntityImpl();
//        String sql ="select lxlx from formtable_main_493_dt5 where mainid = ?";
//        ArrayList<String> list = new ArrayList<>();
//        list.add("7");
//        TycgsqDt5 select = entity.select(sql, list, TycgsqDt5.class);
//        System.out.println(select.getLxlx());
//    }
}
