product.js 5.9 KB

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