d_product.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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,
  125. p.proid as id,
  126. p.remark,p.name,
  127. p.image,p.source,
  128. p.sourceType,
  129. p_type.type_key
  130. `;
  131. }
  132. sql += `
  133. from
  134. hfy_product as p,
  135. hfy_product_type as p_type
  136. `
  137. sql += `where p.type_id = p_type.type_id`
  138. if(searchParam.key){
  139. sql += ` and p.name like '%${searchParam.key}%'`
  140. }
  141. if(searchParam.type){
  142. sql += ` and p_type.type_key = ?`
  143. values.push(searchParam.type)
  144. }
  145. sql += ` ORDER BY p.sort DESC, p.add_time DESC `;
  146. return searchSql(mysql.pq, type, sql, values, limit, page);
  147. }
  148. /**
  149. * 搜索产品,只返回id和name等信息,节约流量
  150. * @param type
  151. * @param searchParam
  152. * @param page
  153. * @param limit
  154. * @returns {*}
  155. */
  156. function searchProductsByMini(type='array',searchParam,sort,page,limit){
  157. let sql = ``;
  158. let values = [];
  159. if(type === 'count'){
  160. sql = `select count(*) as total `;
  161. }else{
  162. sql = `select
  163. p.proid as id,
  164. p.name,
  165. p_type.type_key
  166. `;
  167. }
  168. sql += `
  169. from
  170. hfy_product as p,
  171. hfy_product_type as p_type
  172. `
  173. sql += `where p.type_id = p_type.type_id`
  174. if(searchParam.key){
  175. sql += ` and p.name like '%${searchParam.key}%'`
  176. }
  177. if(searchParam.type){
  178. sql += ` and p_type.type_key = ?`
  179. values.push(searchParam.type)
  180. }
  181. sql += ` ORDER BY p.sort DESC, p.add_time DESC `;
  182. return searchSql(mysql.pq,type,sql,values,limit,page);
  183. }
  184. function loadTypes() {
  185. let sql = `SELECT * FROM hfy_product_type ORDER BY type_sort DESC`;
  186. return mysql.pq(sql, []);
  187. }
  188. function getProductById(id){
  189. let sql = `SELECT *,name as title FROM hfy_product WHERE proid = ? limit 1`;
  190. return mysql.pq(sql,[id]);
  191. }
  192. function getProductByTypeId(id){
  193. let sql = `SELECT * FROM hfy_product WHERE type_id = ?`;
  194. return mysql.pq(sql,[id]);
  195. }
  196. // 获取产品类型列表
  197. function getProductTypeList() {
  198. let sql = `SELECT * FROM hfy_product_type ORDER BY type_sort DESC`;
  199. return mysql.pq(sql, []);
  200. }
  201. // 编辑产品类型
  202. function editProductType(id, typeChange) {
  203. let sql = ``
  204. let values = [];
  205. sql += `UPDATE hfy_product_type SET date_time = ?`;
  206. values.push(time.getUnixTimeStamp());
  207. if(typeChange.type_name){
  208. sql += `,type_name = ?`;
  209. values.push(typeChange.type_name);
  210. }
  211. if(typeChange.type_key){
  212. sql += `,type_key = ?`;
  213. values.push(typeChange.type_key);
  214. }
  215. if(typeChange.sub_text){
  216. sql += `,sub_text = ?`;
  217. values.push(typeChange.sub_text);
  218. }
  219. if(typeChange.type_sort){
  220. sql += `,type_sort = ?`;
  221. values.push(typeChange.type_sort);
  222. }
  223. if(typeChange.type_logo){
  224. sql += `,type_logo = ?`;
  225. values.push(typeChange.type_logo);
  226. }
  227. if(typeChange.shop_addr){
  228. sql += `,shop_addr = ?`;
  229. values.push(typeChange.shop_addr);
  230. }
  231. if(typeChange.seo_key){
  232. sql += `,seo_key = ?`;
  233. values.push(typeChange.seo_key);
  234. }
  235. sql += ` WHERE type_id = ?`;
  236. values.push(id);
  237. return mysql.pq(sql, values);
  238. }
  239. // 删除产品类型
  240. function deleteProductType(id) {
  241. let sql = `DELETE FROM hfy_product_type WHERE type_id = ?`;
  242. return mysql.pq(sql, [id]);
  243. }
  244. // 新增产品类型
  245. function addProductType(type) {
  246. let sql = `INSERT INTO hfy_product_type
  247. (date_time, type_name, type_key, type_sort, type_logo, seo_key, sub_text)
  248. VALUES (?, ?, ?, ?, ?, ?)`;
  249. let values = [];
  250. values.push(time.getUnixTimeStamp());
  251. values.push(type.type_name);
  252. values.push(type.type_key);
  253. values.push(type.type_sort);
  254. values.push(type.type_logo);
  255. values.push(type.seo_key);
  256. values.push(type.sub_text);
  257. return mysql.pq(sql, values);
  258. }
  259. // 获取类型
  260. function getTypeByKey(key) {
  261. let sql = `SELECT * FROM hfy_product_type WHERE type_key = ?`;
  262. return mysql.pq(sql, [key]);
  263. }
  264. module.exports = {
  265. loadProducts,
  266. getProductInfo,
  267. addProduct,
  268. editProduct,
  269. deleteProduct,
  270. getSubImages,
  271. searchProducts,
  272. searchProductsByMini,
  273. loadTypes,
  274. getProductByTypeId,
  275. getProductById,
  276. getProductTypeList,
  277. editProductType,
  278. deleteProductType,
  279. addProductType,
  280. getTypeByKey
  281. }