product.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. const d_product = require('../database/d_product');
  2. const {searchHandle} = require('../tools/searchSql');
  3. const {handle} = require('../tools/handle_cjs');
  4. const codeMap = require("../map/rcodeMap");
  5. const log = require("../logger").logger("c_product","info")
  6. const time = require("../tools/time_cjs")
  7. /**
  8. * 加载产品
  9. * @param key 产品类别
  10. * @param p 页码
  11. * @param l 每页数量
  12. * @returns {Promise<*[]>} [err,res]
  13. */
  14. async function loadProduct(key,p,l)
  15. {
  16. p = p || 1;
  17. l = l || 10;
  18. let [err,res] = await handle(d_product.loadProducts(key, p, l));
  19. if(err){
  20. return [err,null];
  21. }
  22. return [null,res];
  23. }
  24. /**
  25. * 获取产品信息
  26. * @param id 产品id
  27. * @returns {Promise<*[]>}
  28. */
  29. async function getProductInfo(id, lang = 'zh')
  30. {
  31. log.info(`获取产品信息:${id}`);
  32. let productInfoPromise = d_product.getProductInfo(id)
  33. let [err,res] = await handle(productInfoPromise);
  34. if(err){
  35. console.log(err)
  36. return [err,null];
  37. }
  38. let products = res;
  39. let data = {};
  40. if(products.length){
  41. data = products[0];
  42. }else{
  43. return [
  44. {
  45. eCode: codeMap.NotFound,
  46. eMsg: `无法找到对应产品`
  47. }, null]
  48. }
  49. if(data.sub_img){
  50. data.sub_images = data.sub_img.split(';');
  51. }else {
  52. data.sub_images = [];
  53. }
  54. // todo 英文字段支持
  55. return [null,data];
  56. }
  57. async function addProduct(product)
  58. {
  59. product.sub_img = product.sub_images.join(';');
  60. // 获取产品分类
  61. let [err,res] = await handle(d_product.getTypeByKey(product.type))
  62. if(err){
  63. log.error(`无法获取产品分类:${product.type}`)
  64. log.error(err)
  65. return [err,null];
  66. }
  67. if(!res.length){
  68. log.error(`无法获取产品分类:${product.type}`)
  69. return [
  70. {
  71. eCode: codeMap.NotFound,
  72. eMsg: `无法找到对应产品分类`
  73. }, null]
  74. }
  75. let typeId = res[0].type_id;
  76. [err,res] = await handle(d_product.addProduct(product, typeId));
  77. if(err){
  78. log.error(`添加产品失败:${JSON.stringify(product)}`)
  79. log.error(err)
  80. return [err,null];
  81. }
  82. console.log(res)
  83. return [null,res];
  84. }
  85. async function editProduct(product)
  86. {
  87. product.sub_img = product.sub_images.join(';');
  88. let [err,res] = await handle(d_product.getTypeByKey(product.type))
  89. if(err){
  90. log.error(`无法获取产品分类 ${product.type}`)
  91. log.error(err)
  92. return [err,null];
  93. }
  94. let typeId = res[0].type_id;
  95. [err,res] = await handle(d_product.editProduct(product, typeId, product.proid));
  96. if(err){
  97. log.error(`更新产品失败:${JSON.stringify(product)}`)
  98. log.error(err)
  99. return [err,null];
  100. }
  101. return [null,res];
  102. }
  103. async function deleteProduct(id)
  104. {
  105. // 寻找产品
  106. let [err,res] = await handle(d_product.getProductInfo(id));
  107. if(err){
  108. log.error(`无法获取产品信息:${id}`)
  109. log.error(err)
  110. return [err,null];
  111. }
  112. if(!res.length){
  113. log.error(`无法获取产品信息:${id}`)
  114. return [
  115. {
  116. eCode: codeMap.NotFound,
  117. eMsg: `无法找到对应产品`
  118. }, null]
  119. }
  120. [err,res] = await handle(d_product.deleteProduct(id));
  121. if(err){
  122. log.error(`删除产品失败:${id}`)
  123. log.error(err)
  124. return [err,null];
  125. }
  126. return [null,res];
  127. }
  128. async function searchProduct(type, key, p, l)
  129. {
  130. p = p || 1;
  131. l = l || 10;
  132. let _params = {
  133. }
  134. if(type !== 'all'){
  135. _params.type = type;
  136. }
  137. if(key){
  138. _params.key = key
  139. }
  140. return searchHandle(
  141. '搜索产品失败',
  142. d_product.searchProducts,
  143. _params,
  144. null,
  145. p,
  146. l);
  147. }
  148. async function searchProductByMini(type, key, p, l){
  149. p = p || 1;
  150. l = l || 10;
  151. let _params = {
  152. }
  153. if(type !== 'all'){
  154. _params.type = type;
  155. }
  156. if(key){
  157. _params.key = key
  158. }
  159. return searchHandle(
  160. '搜索产品失败',
  161. d_product.searchProductsByMini,
  162. _params,
  163. null,
  164. p,
  165. l);
  166. }
  167. // 获取产品类型
  168. async function getProductTypes()
  169. {
  170. let [err,res] = await handle(d_product.getProductTypeList());
  171. if(err){
  172. return [err,null];
  173. }
  174. return [null,res];
  175. }
  176. async function editType( id, productType)
  177. {
  178. // 判断类别关键字是否重复
  179. let [err,res] = await handle(d_product.getTypeByKey(productType.type_key))
  180. if(err){
  181. return [err,null];
  182. }
  183. if(res.length){
  184. return [
  185. {
  186. eCode:codeMap.DataRepeat,
  187. eMsg:`产品类型关键字重复`
  188. },null]
  189. }
  190. [err,res] = await handle(d_product.editProductType(id, productType));
  191. if(err){
  192. return [err,null];
  193. }
  194. return [null,res];
  195. }
  196. async function addType( id, productType)
  197. {
  198. // 判断类别关键字是否重复
  199. let [err,res] = await handle(d_product.getTypeByKey(productType.type_key))
  200. if(err){
  201. return [err,null];
  202. }
  203. if(res.length){
  204. return [
  205. {
  206. eCode:codeMap.DataRepeat,
  207. eMsg:`产品类型关键字重复`
  208. },null]
  209. }
  210. // 数据校验
  211. [err,res] = await handle(d_product.addProductType(id, productType));
  212. if(err){
  213. return [err,null];
  214. }
  215. return [null,res];
  216. }
  217. // 删除产品类型
  218. async function deleteType(id) {
  219. let err, res;
  220. // 检索产品类型下是否有产品
  221. [err, res] = await handle(d_product.getProductByTypeId(id));
  222. if (err) {
  223. // 删除失败
  224. log.error('[移除产品分类] 无法找到产品' + err.message);
  225. return [err, null];
  226. }
  227. if (res.length > 0) {
  228. // 存在产品
  229. log.warn('[移除产品分类] 存在产品,无法删除');
  230. return [
  231. {
  232. eCode: codeMap.NotPermission,
  233. eMsg: `该类型下存在产品,无法删除, 请先移除对应产品`
  234. }, null];
  235. }
  236. [err, res] = await handle(d_product.deleteProductType(id));
  237. if (err) {
  238. log.error('删除失败' + err.message);
  239. return [err, null];
  240. }
  241. return [null,res];
  242. }
  243. module.exports = {
  244. loadProduct,
  245. getProductInfo,
  246. addProduct,
  247. editProduct,
  248. deleteProduct,
  249. searchProduct,
  250. searchProductByMini,
  251. getProductTypes,
  252. editType,
  253. addType,
  254. deleteType
  255. };