package org.example.background.condition;

import org.apache.ibatis.jdbc.SQL;
import org.example.background.entity.EnergyDataEntity;
import org.example.background.utils.FieldUtils;
import org.springframework.cglib.beans.BeanMap;

public class EnergyDataCondition {

    public String insert(EnergyDataEntity data){
        SQL sql = new SQL();
        sql.INSERT_INTO("energy_data");
        BeanMap beanMap = BeanMap.create(data);
        for (Object key : beanMap.keySet()) {
            Object val = beanMap.get(key);
            if (val != null) {
                sql.VALUES(FieldUtils.camelToLine(key + ""), "#{" + key + "}");
            }
        }
        return sql.toString();
    }

    public String updateByPrimaryKey(EnergyDataEntity data) {
        SQL sql = new SQL();
        sql.UPDATE("energy_data");
        BeanMap beanMap = BeanMap.create(data);
        for (Object key : beanMap.keySet()) {
            Object val = beanMap.get(key);
            if (val != null) {
                sql.SET(FieldUtils.camelToLine(key + "") + "=#{" + key + "}");
            }
        }
        sql.WHERE("energy_id=#{energyId}");
        return sql.toString();
    }
}
