d_product.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. const mysql = require('./mysql');
  2. const {searchSql,limitSql} = require("../tools/searchSql");
  3. const time = require("../tools/time_cjs");
  4. const log = require("../logger").logger("d_product","info")
  5. function loadProducts(key, page, limit) {
  6. let sql = ``;
  7. let values = [];
  8. sql += `SELECT
  9. p.proid as id,p.remark,p.name,p.image,p.source,p.sourceType,
  10. p_type.type_name
  11. FROM
  12. hfy_product as p ,
  13. hfy_product_type as p_type
  14. WHERE
  15. p.type_id = p_type.type_id
  16. and p_type.type_key = ?`;
  17. values = [key];
  18. let _limitSql = limitSql(limit,page);
  19. sql += _limitSql.sql;
  20. values.push(..._limitSql.values);
  21. return mysql.pq(sql, values);
  22. }
  23. function getProductInfo(id) {
  24. let sql = ``;
  25. let values = [];
  26. sql += `SELECT
  27. p.*,
  28. p_type.type_key
  29. FROM
  30. hfy_product as p,
  31. hfy_product_type as p_type
  32. WHERE
  33. p.type_id = p_type.type_id
  34. and p.proid = ?`;
  35. values = [id];
  36. return mysql.pq(sql, values);
  37. }
  38. function searchProducts(type='array',searchParam,sort,page,limit){
  39. let sql = ``;
  40. let values = [];
  41. if(type === 'count'){
  42. sql = `select count(*) as total `;
  43. }else{
  44. sql = `select
  45. p.proid as id,
  46. p.remark,p.name,
  47. p.image,p.source,
  48. p.sourceType,
  49. p_type.type_key
  50. `;
  51. }
  52. sql += `
  53. from
  54. hfy_product as p,
  55. hfy_product_type as p_type
  56. `
  57. sql += `where p.type_id = p_type.type_id`
  58. if(searchParam.key){
  59. sql += ` and p.name like '%${searchParam.key}%'`
  60. }
  61. if(searchParam.type){
  62. sql += ` and p_type.type_key = ?`
  63. values.push(searchParam.type)
  64. }
  65. return searchSql(mysql.pq,type,sql,values,limit,page);
  66. }
  67. /**
  68. * 搜索产品,只返回id和name等信息,节约流量
  69. * @param type
  70. * @param searchParam
  71. * @param page
  72. * @param limit
  73. * @returns {*}
  74. */
  75. function searchProductsByMini(type='array',searchParam,sort,page,limit){
  76. let sql = ``;
  77. let values = [];
  78. if(type === 'count'){
  79. sql = `select count(*) as total `;
  80. }else{
  81. sql = `select
  82. p.proid as id,
  83. p.name,
  84. p_type.type_key
  85. `;
  86. }
  87. sql += `
  88. from
  89. hfy_product as p,
  90. hfy_product_type as p_type
  91. `
  92. sql += `where p.type_id = p_type.type_id`
  93. if(searchParam.key){
  94. sql += ` and p.name like '%${searchParam.key}%'`
  95. }
  96. if(searchParam.type){
  97. sql += ` and p_type.type_key = ?`
  98. values.push(searchParam.type)
  99. }
  100. return searchSql(mysql.pq,type,sql,values,limit,page);
  101. }
  102. function loadTypes() {
  103. let sql = `SELECT * FROM hfy_product_type`;
  104. return mysql.pq(sql, []);
  105. }
  106. function getProductById(id){
  107. let sql = `SELECT *,name as title FROM hfy_product WHERE proid = ? limit 1`;
  108. return mysql.pq(sql,[id]);
  109. }
  110. function getProductByTypeId(id){
  111. let sql = `SELECT * FROM hfy_product WHERE type_id = ?`;
  112. return mysql.pq(sql,[id]);
  113. }
  114. // 获取产品类型列表
  115. function getProductTypeList() {
  116. let sql = `SELECT * FROM hfy_product_type`;
  117. return mysql.pq(sql, []);
  118. }
  119. // 编辑产品类型
  120. function editProductType(id, typeChange) {
  121. let sql = ``
  122. let values = [];
  123. sql += `UPDATE hfy_product_type SET date_time = ?`;
  124. values.push(time.getUnixTimeStamp());
  125. if(typeChange.type_name){
  126. sql += `,type_name = ?`;
  127. values.push(typeChange.type_name);
  128. }
  129. if(typeChange.type_key){
  130. sql += `,type_key = ?`;
  131. values.push(typeChange.type_key);
  132. }
  133. if(typeChange.type_sort){
  134. sql += `,type_sort = ?`;
  135. values.push(typeChange.type_sort);
  136. }
  137. if(typeChange.type_logo){
  138. sql += `,type_logo = ?`;
  139. values.push(typeChange.type_logo);
  140. }
  141. sql += ` WHERE type_id = ?`;
  142. values.push(id);
  143. return mysql.pq(sql, values);
  144. }
  145. // 删除产品类型
  146. function deleteProductType(id) {
  147. let sql = `DELETE FROM hfy_product_type WHERE type_id = ?`;
  148. return mysql.pq(sql, [id]);
  149. }
  150. // 新增产品类型
  151. function addProductType(type) {
  152. let sql = `INSERT INTO hfy_product_type (date_time, type_name, type_key, type_sort, type_logo) VALUES (?, ?, ?, ?, ?)`;
  153. return mysql.pq(sql, [time.getUnixTimeStamp(), type.type_name, type.type_key, type.type_sort, type.type_logo]);
  154. }
  155. module.exports = {
  156. loadProducts,
  157. getProductInfo,
  158. searchProducts,
  159. searchProductsByMini,
  160. loadTypes,
  161. getProductByTypeId,
  162. getProductById,
  163. getProductTypeList,
  164. editProductType,
  165. deleteProductType,
  166. addProductType
  167. }