Commit af2e3cef by wkliang

参数校验 接口调整

parent 5c4b9559
......@@ -14,21 +14,33 @@ class ProductAPI extends APIBase {
let result
switch (pobj.actionType) {
case 'getPage':
result = await this.productSve.getPage(pobj.actionBody.page, pobj.actionBody.offset,
pobj.actionBody.type, pobj.actionBody.keywords)
return system.getResult(result)
result = await this.productSve.getPage(pobj.actionBody.page, pobj.actionBody.limit,
pobj.actionBody.types, pobj.actionBody.keywords)
return system.getResultSuccess(result)
case 'createOrUpdate':
if (pobj.actionBody.product_type == 0 && pobj.actionBody.items) {
return system.getResult(null, '单产品不能有子产品')
}
if (pobj.actionBody.product_type == 1 && (!pobj.actionBody.items || pobj.actionBody.items.length == 0)) {
return system.getResult(null, '未选择子产品')
}
if (pobj.actionBody.product_type == 1) {
let checkRes = await this.productSve.checkSitem(pobj.actionBody.items)
if (!checkRes) {
return system.getResult(null, '不能选择组合产品为子产品')
}
}
result = await this.productSve.createOrUpdate(pobj.actionBody)
return system.getResult(result)
return system.getResultSuccess(result)
case 'getAllDic':
result = await this.productSve.getAllDic(pobj.actionBody.type)
return system.getResult(result)
result = await this.productSve.getAllDic(pobj.actionBody.types)
return system.getResultSuccess(result)
case 'getByIds':
result = await this.productSve.getByIds(pobj.actionBody.ids)
return system.getResult(result)
return system.getResultSuccess(result)
case 'getItems':
result = await this.productSve.getItems(pobj.actionBody.id)
return system.getResult(result)
return system.getResultSuccess(result)
default:
break
}
......
......@@ -4,11 +4,11 @@ class productDao extends Dao {
super(Dao.getModelName(productDao));
}
async getPage (page = 1, offset = 10, type = [0, 1], keywords) {
async getPage (page = 1, limit = 10, type = [0, 1], keywords) {
let params = {
where: { product_type: { [this.db.Op.in]: type } },
limit: offset,
offset: (page - 1) * offset
where: { product_type: { [this.db.Op.in]: type }, source_id: 10001 },
limit: limit,
offset: (page - 1) * limit
}
if (keywords) {
params.where.product_name = { [this.db.Op.like]: `%${keywords}%` }
......@@ -18,14 +18,14 @@ class productDao extends Dao {
async getAllDic (type = [0, 1]) {
return await this.model.findAll({
where: { product_type: { [this.db.Op.in]: type } },
where: { product_type: { [this.db.Op.in]: type }, source_id: 10001 },
attributes: ['id', 'product_type', 'product_name']
})
}
async getByIds (ids) {
async getByIds (ids, type = [0, 1]) {
return await this.model.findAll({
where: { id: { [this.db.Op.in]: ids } }
where: { id: { [this.db.Op.in]: ids }, source_id: 10001, product_type: { [this.db.Op.in]: type } }
})
}
......@@ -34,7 +34,6 @@ class productDao extends Dao {
}
async update (params) {
console.log(params)
return await this.model.update(params, { where: { id: params.id } })
}
}
......
......@@ -4,6 +4,10 @@ class productItemDao extends Dao {
super(Dao.getModelName(productItemDao));
}
async getAll (params) {
return await this.model.findAll(params)
}
async getById (id) {
return await this.model.findOne(id)
}
......@@ -29,7 +33,7 @@ class productItemDao extends Dao {
}
async delete (params) {
return await this.model.destroy({where: params})
return await this.model.destroy({ where: params })
}
}
module.exports = productItemDao;
\ No newline at end of file
......@@ -6,8 +6,8 @@ class ProductService extends ServiceBase {
this.itemDao = system.getObject("db.product.productitemDao");
}
async getPage (page, offset, type, keywords) {
return await this.dao.getPage(page, offset, type, keywords)
async getPage (page, limit, type, keywords) {
return await this.dao.getPage(page, limit, type, keywords)
}
async getAllDic (type) {
......@@ -21,15 +21,29 @@ class ProductService extends ServiceBase {
async getItems (id) {
const querys = {
where: { parent_id: id },
attributes: ['id'],
distinct: true,
include: [{
model: this.dao.model,
require: false,
}],
attributes: ['product_id'],
raw: true
};
this.itemDao.model.hasMany(this.dao.model, { foreignKey: 'id', sourceKey: "parent_id" });
return await this.itemDao.model.findAll(querys)
let ids = await this.itemDao.getAll(querys)
let arr = []
for (let i of ids) {
arr.push(i.product_id)
}
let result = await this.dao.getByIds(arr)
return result
}
async checkSitem(ids) {
let res = await this.dao.getByIds(ids, [0])
let arr = []
for (let i of res) {
arr.push(i.id)
}
if (JSON.stringify(ids) == JSON.stringify(arr)) {
return true
} else {
return false
}
}
async createOrUpdate (params) {
......@@ -49,10 +63,12 @@ class ProductService extends ServiceBase {
params.source_id = params.source_id || 10001
let result = await this.dao.create(params)
let itemArr = []
if (items) {
for (let i of items) {
itemArr.push({ parent_id: result.id, product_id: i })
}
let itemResult = await this.itemDao.createMany(itemArr)
}
return result
}
......@@ -65,11 +81,13 @@ class ProductService extends ServiceBase {
params.source_id = params.source_id || 10001
let result = await this.dao.update(params)
let itemArr = []
if (items) {
for (let i of items) {
itemArr.push({ parent_id: params.id, product_id: i })
itemArr.push({ parent_id: result.id, product_id: i })
}
await this.itemDao.delete({ parent_id: params.id })
let itemResult = await this.itemDao.createMany(itemArr)
}
return result
}
}
......
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