ProductDao.java 3.6 KB
package vion.dao;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import vion.model.File;
import vion.model.Product;
import vion.model.Store;

import java.util.Date;
import java.util.List;

@Repository
public class ProductDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final String SELECT_PRODUCT_ALL = "SELECT * FROM tbl_product_info";

    public List<Product> getProductAll() {
        return jdbcTemplate.query(SELECT_PRODUCT_ALL, new BeanPropertyRowMapper<Product>(Product.class));
    }

    private static final String SELECT_PRODUCT_BY_ID = "SELECT * FROM tbl_product_info where id=?";

    public Product getProductByID(Integer id) {
        List<Product> products= jdbcTemplate.query(SELECT_PRODUCT_BY_ID,new Object[]{id},
                new BeanPropertyRowMapper<Product>(Product.class));
        if(products.size()>0){
            return products.get(0);
        }else{
            return null;
        }
    }


    private static final String SELECT_PRODUCT_BY_PARM= "SELECT * FROM tbl_product_info WHERE account_id=? and id=? ";

    public List<Product> getProductByParm(Integer accountid, Integer id, String name, Integer brand, Integer functionary) {

        StringBuffer getProductSb = new StringBuffer(SELECT_PRODUCT_BY_PARM);

        if(!StrUtil.isBlank(name)){
            getProductSb.append(" and name='"+name+"'");
        }
        if(brand!=null){
            getProductSb.append(" and brand="+brand+"");
        }
        if(functionary!=null){
            getProductSb.append(" and functionary="+functionary+"");
        }

        return jdbcTemplate.query(getProductSb.toString(),new Object[]{accountid,id}, new BeanPropertyRowMapper<Product>(Product.class));
    }

    private static final String SAVE_PRODUCT= "INSERT INTO tbl_product_info(\n" +
            "            material_no, store_id, name, functionary, brand, product_count, \n" +
            "            product_price, product_subtotal, product_total_price,remark,account_id)\n" +
            "    VALUES (?, ?, ?, ?, ?, ?, ?, \n" +
            "            ?, ?, ?,?);";
/*
    public Integer addProduct(Product product) {
        return jdbcTemplate.update(SAVE_PRODUCT,
                new Object[]{product.getMaterial_no(),product.getStore_id(),product.getName(),
                product.getFunctionary(),product.getBrand(),product.getProduct_count(),
                product.getProduct_price(),product.getProduct_subtotal(),product.getProduct_total_price(),
                product.getRemark(),product.getAccount_id()});
    }

    private static final String UPDATE_PRODUCT= "UPDATE tbl_product_info\n" +
            "   SET material_no=?, store_id=?, name=?, functionary=?, brand=?, \n" +
            "       product_count=?, product_price=?, product_subtotal=?, product_total_price=?, \n" +
            "       modify_time=?, remark=?,account_id=?\n" +
            " WHERE id=?;";

    public Integer updateProduct(Product product) {
        return jdbcTemplate.update(UPDATE_PRODUCT,
                new Object[]{product.getMaterial_no(),product.getStore_id(),product.getName(),
                        product.getFunctionary(),product.getBrand(),product.getProduct_count(),
                        product.getProduct_price(),product.getProduct_subtotal(),
                        product.getProduct_total_price(),product.getModify_time(),
                        product.getRemark(),product.getId(),product.getAccount_id()});
    }*/
}