d_product.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. sql += ` ORDER BY p.sort DESC, p.add_time DESC `;
  19. let _limitSql = limitSql(limit,page);
  20. sql += _limitSql.sql;
  21. // 添加排序
  22. values.push(..._limitSql.values);
  23. return mysql.pq(sql, values);
  24. }
  25. function getProductInfo(id) {
  26. let sql = ``;
  27. let values = [];
  28. sql += `SELECT
  29. p.*,
  30. p_type.type_key
  31. FROM
  32. hfy_product as p,
  33. hfy_product_type as p_type
  34. WHERE
  35. p.type_id = p_type.type_id
  36. and p.proid = ?`;
  37. values = [id];
  38. return mysql.pq(sql, values);
  39. }
  40. function addProduct(data, type_id) {
  41. let sql = ``;
  42. let values = [];
  43. sql += `INSERT INTO hfy_product (
  44. type_id,
  45. name,
  46. remark,
  47. sort,
  48. seo_key,
  49. image,
  50. sub_img,
  51. detail,
  52. overview,
  53. parameter,
  54. add_time) VALUES
  55. (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
  56. values.push(type_id);
  57. values.push(data.name);
  58. values.push(data.remark);
  59. values.push(data.sort);
  60. values.push(data.seo_key);
  61. values.push(data.image);
  62. values.push(data.sub_img);
  63. values.push(data.detail);
  64. values.push(data.overview);
  65. values.push(data.parameter);
  66. values.push(time.getUnixTimeStamp());
  67. return mysql.pq(sql, values);
  68. }
  69. function editProduct(data, type_id, id) {
  70. let sql = ``;
  71. let values = [];
  72. sql += `UPDATE hfy_product SET
  73. type_id = ?,
  74. name = ?,
  75. remark = ?,
  76. sort = ?,
  77. seo_key = ?,
  78. image = ?,
  79. sub_img = ?,
  80. detail = ?,
  81. overview = ?,
  82. parameter = ?
  83. WHERE
  84. proid = ?`;
  85. values.push(type_id);
  86. values.push(data.name);
  87. values.push(data.remark);
  88. values.push(data.sort);
  89. values.push(data.seo_key);
  90. values.push(data.image);
  91. values.push(data.sub_img);
  92. values.push(data.detail);
  93. values.push(data.overview);
  94. values.push(data.parameter);
  95. values.push(id);
  96. return mysql.pq(sql, values);
  97. }
  98. function deleteProduct(id) {
  99. let sql = ``;
  100. let values = [];
  101. sql += `DELETE FROM hfy_product WHERE proid = ? limit 1`;
  102. values = [id];
  103. return mysql.pq(sql, values);
  104. }
  105. function getSubImages(id) {
  106. let sql = ``;
  107. let values = [];
  108. sql += `SELECT
  109. id, path
  110. FROM
  111. hfy_product_images
  112. WHERE
  113. proid = ?`;
  114. values = [id];
  115. return mysql.pq(sql, values);
  116. }
  117. function searchProducts(type='array',searchParam,sort,page,limit){
  118. let sql = ``;
  119. let values = [];
  120. if(type === 'count'){
  121. sql = `select count(*) as total `;
  122. }else{
  123. sql = `select
  124. p.proid as id,
  125. p.remark,p.name,
  126. p.image,p.source,
  127. p.sourceType,
  128. p_type.type_key
  129. `;
  130. }
  131. sql += `
  132. from
  133. hfy_product as p,
  134. hfy_product_type as p_type
  135. `
  136. sql += `where p.type_id = p_type.type_id`
  137. if(searchParam.key){
  138. sql += ` and p.name like '%${searchParam.key}%'`
  139. }
  140. if(searchParam.type){
  141. sql += ` and p_type.type_key = ?`
  142. values.push(searchParam.type)
  143. }
  144. sql += ` ORDER BY p.sort DESC, p.add_time DESC `;
  145. return searchSql(mysql.pq, type, sql, values, limit, page);
  146. }
  147. /**
  148. * 搜索产品,只返回id和name等信息,节约流量
  149. * @param type
  150. * @param searchParam
  151. * @param page
  152. * @param limit
  153. * @returns {*}
  154. */
  155. function searchProductsByMini(type='array',searchParam,sort,page,limit){
  156. let sql = ``;
  157. let values = [];
  158. if(type === 'count'){
  159. sql = `select count(*) as total `;
  160. }else{
  161. sql = `select
  162. p.proid as id,
  163. p.name,
  164. p_type.type_key
  165. `;
  166. }
  167. sql += `
  168. from
  169. hfy_product as p,
  170. hfy_product_type as p_type
  171. `
  172. sql += `where p.type_id = p_type.type_id`
  173. if(searchParam.key){
  174. sql += ` and p.name like '%${searchParam.key}%'`
  175. }
  176. if(searchParam.type){
  177. sql += ` and p_type.type_key = ?`
  178. values.push(searchParam.type)
  179. }
  180. sql += ` ORDER BY p.sort DESC, p.add_time DESC `;
  181. return searchSql(mysql.pq,type,sql,values,limit,page);
  182. }
  183. function loadTypes() {
  184. let sql = `SELECT * FROM hfy_product_type ORDER BY type_sort DESC`;
  185. return mysql.pq(sql, []);
  186. }
  187. function getProductById(id){
  188. let sql = `SELECT *,name as title FROM hfy_product WHERE proid = ? limit 1`;
  189. return mysql.pq(sql,[id]);
  190. }
  191. function getProductByTypeId(id){
  192. let sql = `SELECT * FROM hfy_product WHERE type_id = ?`;
  193. return mysql.pq(sql,[id]);
  194. }
  195. // 获取产品类型列表
  196. function getProductTypeList() {
  197. let sql = `SELECT * FROM hfy_product_type ORDER BY type_sort DESC`;
  198. return mysql.pq(sql, []);
  199. }
  200. // 编辑产品类型
  201. function editProductType(id, typeChange) {
  202. let sql = ``
  203. let values = [];
  204. sql += `UPDATE hfy_product_type SET date_time = ?`;
  205. values.push(time.getUnixTimeStamp());
  206. if(typeChange.type_name){
  207. sql += `,type_name = ?`;
  208. values.push(typeChange.type_name);
  209. }
  210. if(typeChange.type_key){
  211. sql += `,type_key = ?`;
  212. values.push(typeChange.type_key);
  213. }
  214. if(typeChange.sub_text){
  215. sql += `,sub_text = ?`;
  216. values.push(typeChange.sub_text);
  217. }
  218. if(typeChange.type_sort){
  219. sql += `,type_sort = ?`;
  220. values.push(typeChange.type_sort);
  221. }
  222. if(typeChange.type_logo){
  223. sql += `,type_logo = ?`;
  224. values.push(typeChange.type_logo);
  225. }
  226. if(typeChange.shop_addr){
  227. sql += `,shop_addr = ?`;
  228. values.push(typeChange.shop_addr);
  229. }
  230. if(typeChange.seo_key){
  231. sql += `,seo_key = ?`;
  232. values.push(typeChange.seo_key);
  233. }
  234. sql += ` WHERE type_id = ?`;
  235. values.push(id);
  236. return mysql.pq(sql, values);
  237. }
  238. // 删除产品类型
  239. function deleteProductType(id) {
  240. let sql = `DELETE FROM hfy_product_type WHERE type_id = ?`;
  241. return mysql.pq(sql, [id]);
  242. }
  243. // 新增产品类型
  244. function addProductType(type) {
  245. let sql = `INSERT INTO hfy_product_type
  246. (date_time, type_name, type_key, type_sort, type_logo, seo_key, sub_text)
  247. VALUES (?, ?, ?, ?, ?, ?)`;
  248. let values = [];
  249. values.push(time.getUnixTimeStamp());
  250. values.push(type.type_name);
  251. values.push(type.type_key);
  252. values.push(type.type_sort);
  253. values.push(type.type_logo);
  254. values.push(type.seo_key);
  255. values.push(type.sub_text);
  256. return mysql.pq(sql, values);
  257. }
  258. // 获取类型
  259. function getTypeByKey(key) {
  260. let sql = `SELECT * FROM hfy_product_type WHERE type_key = ?`;
  261. return mysql.pq(sql, [key]);
  262. }
  263. module.exports = {
  264. loadProducts,
  265. getProductInfo,
  266. addProduct,
  267. editProduct,
  268. deleteProduct,
  269. getSubImages,
  270. searchProducts,
  271. searchProductsByMini,
  272. loadTypes,
  273. getProductByTypeId,
  274. getProductById,
  275. getProductTypeList,
  276. editProductType,
  277. deleteProductType,
  278. addProductType,
  279. getTypeByKey
  280. }