package weaver.interfaces.htsc.cowork.util;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.htsc.cowork.entity.TaskEntity;

import java.lang.reflect.Field;

/**
 * Created by K1810006 on 2020/12/17.
 */
public class TaskUtil {


    /**
     * 创建任务
     * @param taskEntity
     * @return
     */
    public static boolean createTask(TaskEntity taskEntity){
        if(taskEntity!=null){
            Class<?> clazz = taskEntity.getClass();
            Field[] fields = clazz.getDeclaredFields();
            String insertFields = " insert into uf_xzsy(";
            String paramsStr  = " values(";
            Object[] values = new Object[fields.length];
            for(int i=0;i<fields.length;i++){
                fields[i].setAccessible( true );
                if(i==fields.length-1){
                    insertFields += fields[i].getName()+" )";
                    paramsStr += "? )";
                }else{
                    insertFields += fields[i].getName()+",";
                    paramsStr += "?,";
                }
                try{
                    values[i] = fields[i].get(taskEntity);
                }catch (Exception e){
                    new BaseBean().writeLog("保存任务数据错误："+e);
                }
            }
            String insertSql = insertFields + paramsStr;
            RecordSet rs = new RecordSet();
            boolean result = rs.executeQuery(insertSql,values);
            return result;

        }
        return false;
    }

    public static void main(String[] args) {
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setBt("ces");
        taskEntity.setCjr(123);
        taskEntity.setWcqk("fdasfasfas");
        createTask(taskEntity);
    }
}
