Commit 5c4b9559 by wkliang

接口基础功能完成

parent 3310631d
...@@ -14,18 +14,21 @@ class ProductAPI extends APIBase { ...@@ -14,18 +14,21 @@ class ProductAPI extends APIBase {
let result let result
switch (pobj.actionType) { switch (pobj.actionType) {
case 'getPage': case 'getPage':
result = await this.productSve.getPage(pobj.actionBody.page, pobj.actionBody.offset, pobj.actionBody.type) result = await this.productSve.getPage(pobj.actionBody.page, pobj.actionBody.offset,
return result pobj.actionBody.type, pobj.actionBody.keywords)
return system.getResult(result)
case 'createOrUpdate': case 'createOrUpdate':
break result = await this.productSve.createOrUpdate(pobj.actionBody)
case 'getAll': return system.getResult(result)
case 'getAllDic':
result = await this.productSve.getAllDic(pobj.actionBody.type) result = await this.productSve.getAllDic(pobj.actionBody.type)
return result return system.getResult(result)
case 'getByIds': case 'getByIds':
result = await this.productSve.getByIds(pobj.actionBody.ids) result = await this.productSve.getByIds(pobj.actionBody.ids)
break return system.getResult(result)
case 'getItems': case 'getItems':
break result = await this.productSve.getItems(pobj.actionBody.id)
return system.getResult(result)
default: default:
break break
} }
......
...@@ -4,25 +4,38 @@ class productDao extends Dao { ...@@ -4,25 +4,38 @@ class productDao extends Dao {
super(Dao.getModelName(productDao)); super(Dao.getModelName(productDao));
} }
async getPage (page = 1, offset = 10, type = [0, 1]) { async getPage (page = 1, offset = 10, type = [0, 1], keywords) {
return await this.model.findAndCountAll({ let params = {
where: { product_type: { [this.db.Op.in]: type } }, where: { product_type: { [this.db.Op.in]: type } },
limit: offset, limit: offset,
offset: (page - 1) * offset offset: (page - 1) * offset
}) }
if (keywords) {
params.where.product_name = { [this.db.Op.like]: `%${keywords}%` }
}
return await this.model.findAndCountAll(params)
} }
async getAllDic (type = [0, 1]) { async getAllDic (type = [0, 1]) {
// TODO: 筛选字段
return await this.model.findAll({ return await this.model.findAll({
where: { product_type: { [this.db.Op.in]: type } } where: { product_type: { [this.db.Op.in]: type } },
attributes: ['id', 'product_type', 'product_name']
}) })
} }
async getByIds (ids) { async getByIds (ids) {
return await this.model.getAll({ return await this.model.findAll({
where: { id: { [this.db.Op.in]: ids } } where: { id: { [this.db.Op.in]: ids } }
}) })
} }
async create (params) {
return await this.model.create(params)
}
async update (params) {
console.log(params)
return await this.model.update(params, { where: { id: params.id } })
}
} }
module.exports = productDao; module.exports = productDao;
\ No newline at end of file
...@@ -23,5 +23,13 @@ class productItemDao extends Dao { ...@@ -23,5 +23,13 @@ class productItemDao extends Dao {
} }
}) })
} }
async createMany (params) {
return await this.model.bulkCreate(params)
}
async delete (params) {
return await this.model.destroy({where: params})
}
} }
module.exports = productItemDao; module.exports = productItemDao;
\ No newline at end of file
module.exports = (db, DataTypes) => { module.exports = (db, DataTypes) => {
return db.define("productItem", { return db.define("productitem", {
parent_id: DataTypes.BIGINT, // 父产品 id parent_id: DataTypes.BIGINT, // 父产品 id
product_id: DataTypes.BIGINT, // 单产品 id product_id: DataTypes.BIGINT, // 单产品 id
}, { }, {
......
const ServiceBase = require("../../sve.base"); const ServiceBase = require("../../sve.base");
const system = require('../../../system')
class ProductService extends ServiceBase { class ProductService extends ServiceBase {
constructor() { constructor() {
super("product", ServiceBase.getDaoName(ProductService)); super("product", ServiceBase.getDaoName(ProductService));
this.itemDao = system.getObject("db.product.productitemDao");
} }
async getPage (page, offset, type) { async getPage (page, offset, type, keywords) {
return await this.dao.getPage(page, offset, type) return await this.dao.getPage(page, offset, type, keywords)
} }
async getAllDic (type) { async getAllDic (type) {
...@@ -17,11 +19,58 @@ class ProductService extends ServiceBase { ...@@ -17,11 +19,58 @@ class ProductService extends ServiceBase {
} }
async getItems (id) { async getItems (id) {
// TODO: relation const querys = {
where: { parent_id: id },
attributes: ['id'],
distinct: true,
include: [{
model: this.dao.model,
require: false,
}],
};
this.itemDao.model.hasMany(this.dao.model, { foreignKey: 'id', sourceKey: "parent_id" });
return await this.itemDao.model.findAll(querys)
} }
async createOrUpdate (params) { async createOrUpdate (params) {
// TODO: if (params.id) {
return await this.update(params)
} else {
return await this.create(params)
}
}
async create (params) {
let items
if (params.product_type == 1) {
items = JSON.parse(JSON.stringify(params.items))
delete params.items
}
params.source_id = params.source_id || 10001
let result = await this.dao.create(params)
let itemArr = []
for (let i of items) {
itemArr.push({ parent_id: result.id, product_id: i })
}
let itemResult = await this.itemDao.createMany(itemArr)
return result
}
async update (params) {
let items
if (params.product_type == 1) {
items = JSON.parse(JSON.stringify(params.items))
delete params.items
}
params.source_id = params.source_id || 10001
let result = await this.dao.update(params)
let itemArr = []
for (let i of items) {
itemArr.push({ parent_id: params.id, product_id: i })
}
await this.itemDao.delete({ parent_id: params.id })
let itemResult = await this.itemDao.createMany(itemArr)
return result
} }
} }
module.exports = ProductService; module.exports = ProductService;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment