You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

productDao.js 2.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * @Description: 商品模块数据持久层
  3. * @Author: hai-27
  4. * @Date: 2020-02-07 16:51:56
  5. * @LastEditors: hai-27
  6. * @LastEditTime: 2020-02-27 15:42:52
  7. */
  8. const db = require('./db.js');
  9. module.exports = {
  10. // 连接数据库获取商品分类
  11. GetCategory: async () => {
  12. const sql = "select * from category";
  13. return await db.query(sql, []);
  14. },
  15. // 连接数据库根据商品分类名称获取分类id
  16. GetCategoryId: async (categoryName) => {
  17. const sql = "select * from category where category_name = ?";
  18. const category = await db.query(sql, [categoryName]);
  19. return category[0].category_id;
  20. },
  21. // 连接数据库,根据商品分类id获取首页展示的商品信息
  22. GetPromoProduct: async (categoryID) => {
  23. const sql = "select * from product where category_id = ? order by product_sales desc limit 7";
  24. return await db.query(sql, categoryID);
  25. },
  26. // 连接数据库,分页获取所有的商品信息
  27. GetAllProduct: async (offset = 0, rows = 0) => {
  28. let sql = "select * from product ";
  29. if (rows != 0) {
  30. sql += "limit " + offset + "," + rows;
  31. }
  32. return await db.query(sql, []);
  33. },
  34. // 连接数据库,根据商品分类id,分页获取商品信息
  35. GetProductByCategory: async (categoryID, offset = 0, rows = 0) => {
  36. let sql = "select * from product where category_id = ? ";
  37. for (let i = 0; i < categoryID.length - 1; i++) {
  38. sql += "or category_id = ? ";
  39. }
  40. if (rows != 0) {
  41. sql += "order by product_sales desc limit " + offset + "," + rows;
  42. }
  43. return await db.query(sql, categoryID);
  44. },
  45. // 连接数据库,根据搜索条件,分页获取商品信息
  46. GetProductBySearch: async (search, offset = 0, rows = 0) => {
  47. let sql = `select * from product where product_name like "%${ search }%" or product_title like "%${ search }%" or product_intro like "%${ search }%"`;
  48. if (rows != 0) {
  49. sql += "order by product_sales desc limit " + offset + "," + rows;
  50. }
  51. return await db.query(sql, []);
  52. },
  53. // 连接数据库,根据商品id,获取商品详细信息
  54. GetProductById: async (id) => {
  55. const sql = 'select * from product where product_id = ?';
  56. return await db.query(sql, id);
  57. },
  58. // 连接数据库,根据商品id,获取商品图片
  59. GetDetailsPicture: async (productID) => {
  60. const sql = "select * from product_picture where product_id = ? ";
  61. return await db.query(sql, productID);
  62. }
  63. }