| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- const d_product = require('../database/d_product');
- const {searchHandle} = require('../tools/searchSql');
- const {handle} = require('../tools/handle_cjs');
- const codeMap = require("../map/rcodeMap");
- const log = require("../logger").logger("c_product","info")
- const time = require("../tools/time_cjs")
- const typeTool = require("../tools/typeTool_cjs");
- /**
- * 加载产品
- * @param key 产品类别
- * @param p 页码
- * @param l 每页数量
- * @returns {Promise<*[]>} [err,res]
- */
- async function loadProduct(key,p,l)
- {
- p = p || 1;
- l = l || 10;
- let [err,res] = await handle(d_product.loadProducts(key, p, l));
- if(err){
- return [err,null];
- }
- return [null,res];
- }
- /**
- * 获取产品信息
- * @param id 产品id
- * @returns {Promise<*[]>}
- */
- async function getProductInfo(id, lang = 'zh')
- {
- log.info(`获取产品信息:${id}`);
- let productInfoPromise = d_product.getProductInfo(id)
- let [err,res] = await handle(productInfoPromise);
- if(err){
- console.log(err)
- return [err,null];
- }
- let products = res;
- let data = {};
- if(products.length){
- data = products[0];
- }else{
- return [
- {
- eCode: codeMap.NotFound,
- eMsg: `无法找到对应产品`
- }, null]
- }
- if(data.sub_img){
- data.sub_images = data.sub_img.split(';');
- }else {
- data.sub_images = [];
- }
- // todo 英文字段支持
- return [null,data];
- }
- async function addProduct(product)
- {
- product.sub_img = product.sub_images.join(';');
- // 获取产品分类
- let [err,res] = await handle(d_product.getTypeByKey(product.type))
- if(err){
- log.error(`无法获取产品分类:${product.type}`)
- log.error(err)
- return [err,null];
- }
- if(!res.length){
- log.error(`无法获取产品分类:${product.type}`)
- return [
- {
- eCode: codeMap.NotFound,
- eMsg: `无法找到对应产品分类`
- }, null]
- }
- let typeId = res[0].type_id;
- [err,res] = await handle(d_product.addProduct(product, typeId));
- if(err){
- log.error(`添加产品失败:${JSON.stringify(product)}`)
- log.error(err)
- return [err,null];
- }
- console.log(res)
- return [null,res];
- }
- async function editProduct(product)
- {
- product.sub_img = product.sub_images.join(';');
- let [err,res] = await handle(d_product.getTypeByKey(product.type))
- if(err){
- log.error(`无法获取产品分类 ${product.type}`)
- log.error(err)
- return [err,null];
- }
- let typeId = res[0].type_id;
- [err,res] = await handle(d_product.editProduct(product, typeId, product.proid));
- if(err){
- log.error(`更新产品失败:${JSON.stringify(product)}`)
- log.error(err)
- return [err,null];
- }
- return [null,res];
- }
- async function deleteProduct(id)
- {
- // 寻找产品
- let [err,res] = await handle(d_product.getProductInfo(id));
- if(err){
- log.error(`无法获取产品信息:${id}`)
- log.error(err)
- return [err,null];
- }
- if(!res.length){
- log.error(`无法获取产品信息:${id}`)
- return [
- {
- eCode: codeMap.NotFound,
- eMsg: `无法找到对应产品`
- }, null]
- }
- [err,res] = await handle(d_product.deleteProduct(id));
- if(err){
- log.error(`删除产品失败:${id}`)
- log.error(err)
- return [err,null];
- }
- return [null,res];
- }
- async function searchProduct(type, key, p, l)
- {
- p = p || 1;
- l = l || 10;
- let _params = {
- }
- if(type !== 'all'){
- _params.type = type;
- }
- if(key){
- _params.key = key
- }
- return searchHandle(
- '搜索产品失败',
- d_product.searchProducts,
- _params,
- null,
- p,
- l);
- }
- async function searchProductByMini(type, key, p, l){
- p = p || 1;
- l = l || 10;
- let _params = {
- }
- if(type !== 'all'){
- _params.type = type;
- }
- if(key){
- _params.key = key
- }
- return searchHandle(
- '搜索产品失败',
- d_product.searchProductsByMini,
- _params,
- null,
- p,
- l);
- }
- // 获取产品类型
- async function getProductTypes()
- {
- let [err,res] = await handle(d_product.getProductTypeList());
- if(err){
- return [err,null];
- }
- return [null,res];
- }
- async function editType( id, productType)
- {
- // 判断类别关键字是否重复
- let [err,res] = await handle(d_product.getTypeByKey(productType.type_key))
- if(err){
- return [err,null];
- }
- if(res.length){
- let nowType = res.find(item => typeTool.toNumber(item.type_id) === typeTool.toNumber(id))
- if(!nowType){
- return [
- {
- eCode:codeMap.DataRepeat,
- eMsg:`类型关键字重复`
- },null]
- }
- }
- [err,res] = await handle(d_product.editProductType(id, productType));
- if(err){
- return [err,null];
- }
- return [null,res];
- }
- async function addType( id, productType)
- {
- // 判断类别关键字是否重复
- let [err,res] = await handle(d_product.getTypeByKey(productType.type_key))
- if(err){
- return [err,null];
- }
- if(res.length){
- return [
- {
- eCode:codeMap.DataRepeat,
- eMsg:`产品类型关键字重复`
- },null]
- }
- // 数据校验
- [err,res] = await handle(d_product.addProductType(id, productType));
- if(err){
- return [err,null];
- }
- return [null,res];
- }
- // 删除产品类型
- async function deleteType(id) {
- let err, res;
- // 检索产品类型下是否有产品
- [err, res] = await handle(d_product.getProductByTypeId(id));
- if (err) {
- // 删除失败
- log.error('[移除产品分类] 无法找到产品' + err.message);
- return [err, null];
- }
- if (res.length > 0) {
- // 存在产品
- log.warn('[移除产品分类] 存在产品,无法删除');
- return [
- {
- eCode: codeMap.NotPermission,
- eMsg: `该类型下存在产品,无法删除, 请先移除对应产品`
- }, null];
- }
- [err, res] = await handle(d_product.deleteProductType(id));
- if (err) {
- log.error('删除失败' + err.message);
- return [err, null];
- }
- return [null,res];
- }
- module.exports = {
- loadProduct,
- getProductInfo,
- addProduct,
- editProduct,
- deleteProduct,
- searchProduct,
- searchProductByMini,
- getProductTypes,
- editType,
- addType,
- deleteType
- };
|