Commit 171ce4b7 by 蒋勇

d

parent 21109c83
......@@ -5,5 +5,10 @@ class ProductpriceCtl extends CtlBase {
super("product", CtlBase.getServiceName(ProductpriceCtl));
// this.pricestrategyService=system.getObject("service.product.pricestrategySve")
}
async updownProduct(p,q,req){
let updownid=p.curid
let rtn=await this.service.updownProduct(updownid)
return system.getResult(rtn)
}
}
module.exports = ProductpriceCtl;
......@@ -48,9 +48,15 @@ class Dao {
return this.model.findAll({ where: w, attributes: qobj.fields });
}
}
async bulkDelete(ids) {
var en = await this.model.destroy({ where: { id: { [this.db.Op.in]: ids } } });
return en;
async bulkDelete(ids,t) {
if(t){
var en = await this.model.destroy({ where: { id: { [this.db.Op.in]: ids } },transaction:t});
return en;
}else{
var en = await this.model.destroy({ where: { id: { [this.db.Op.in]: ids } }});
return en
}
}
async bulkDeleteByWhere(whereParam, t) {
var en = null;
......@@ -270,6 +276,9 @@ class Dao {
async findOne(obj) {
return this.model.findOne({ "where": obj });
}
async findOneWithTm(obj,t) {
return this.model.findOne({ "where": obj ,transaction:t});
}
async findById(oid) {
return this.model.findById(oid);
}
......
const system = require("../../../system");
const Dao = require("../../dao.base");
class ProductDao extends Dao {
constructor() {
super(Dao.getModelName(ProductDao));
}
extraModelFilter() {
//return {"key":"include","value":[{model:this.db.models.app,},{model:this.db.models.role,as:"Roles",attributes:["id","name"],joinTableAttributes:['created_at']}]};
return {
"key": "include", "value": [
{ model: this.db.models.productprice, as: "skus", attributes: ["id", "pricestrategy_id"] }]
}
}
}
module.exports = ProductDao;
\ No newline at end of file
......@@ -24,10 +24,6 @@ module.exports = (db, DataTypes) => {
type: DataTypes.STRING,
allowNull: false,
},//和user的from
stragetyids: {//不需要持久化
type: DataTypes.STRING,
allowNull: false,
},//和user的from
}, {
paranoid: true,//假的删除
underscored: true,
......
......@@ -4,15 +4,21 @@ const appconfig=system.getSysConfig();
module.exports = (db, DataTypes) => {
//定价类型
return db.define("productprice", {
pname:{//产品名称
type: DataTypes.STRING,
allowNull: true,
},
strategyitems:{//定价策略
type: DataTypes.STRING,
allowNull: true,
},
lowpriceref:{
type: DataTypes.DECIMAL(10, 2) ,
allowNull: false,
defaultValue: 0.00
allowNull: true,
},
hignpriceref:{
type: DataTypes.DECIMAL(10, 2) ,
allowNull: false,
defaultValue: 0.00
allowNull: true,
},
deliverfile:{
type: DataTypes.STRING,
......
const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const settings = require("../../../../config/settings");
const appconfig = system.getSysConfig();
const fs = require("fs")
class ProductService extends ServiceBase {
constructor() {
super("product", ServiceBase.getDaoName(ProductService));
this.priceDao = system.getObject("db.product.productpriceDao")
}
async findPriceStrategys(stragetyids,t){
//按照策略id查询出定价策略集合
let pts=await this.db.models.pricestrategy.findAll({ where: { id: { [this.db.Op.in]: stragetyids } },attributes:['id','optionunion'],transaction:t})
let tmpdic={}
pts.forEach(p=>{
tmpdic[p.id+'']=p.optionunion
})
return tmpdic
}
async create(p) {
if (!p.name || p.name == "") {
p.name = p.regionpath + "~" + p.productcatpath
}
let stragetyids = p.stragetyids
//策略ids
let stragetyids = p.sts
var self = this;
return this.db.transaction(async function (t) {
p.stragetyids=p.stragetyids.join(",")
let pnew = await self.dao.create(p, t)
let productprices = []
//按照策略id查询出定价策略集合
let tmpdic=await self.findPriceStrategys(stragetyids,t)
stragetyids.forEach(stragetyid => {
let pps = {
product_id: pnew.id,
pricestrategy_id: stragetyid,
pname:p.name,
strategyitems:tmpdic[stragetyid+'']
}
productprices.push(pps)
})
......@@ -29,22 +39,59 @@ class ProductService extends ServiceBase {
return pnew;
});
}
async update(p) {
if (!p.name || p.name == "") {
p.name = p.regionpath + "~" + p.productcatpath
}
//策略ids
let stragetyids = p.sts
var self = this;
return this.db.transaction(async function (t) {
let pupdate = await self.dao.update(p, t)
//先按照产品id检查出当前产品的价格策略,检查出不在传入中的,需要删除
//在传入中的不做处理
let currentProduct = await self.dao.model.findOne({
where: { id: p.id }, include: [
{model: self.db.models.productprice, as:"skus",attributes: ['id', 'pricestrategy_id'],raw:true},
], transaction: t
})
let notInInput=[]//需要删除
let skusarray=[]
currentProduct.skus.forEach(sku=>{
skusarray.push(sku.pricestrategy_id)
if(stragetyids.indexOf(sku.pricestrategy_id)<0){
notInInput.push(sku.id)
}
})
//删除
if(notInInput.length>0){
await self.priceDao.bulkDelete(notInInput,t)
}
//传入不在查出中的,需要新增
let notintables=[]
stragetyids.forEach(st=>{
if(skusarray.indexOf(st)<0){
notintables.push(st)
}
})
//新增
let productprices = []
let tmpdic=await self.findPriceStrategys(stragetyids,t)
notintables.forEach(stragetyid => {
let pps = {
product_id: p.id,
pricestrategy_id: stragetyid,
pname:p.name,
strategyitems:tmpdic[stragetyid+'']
}
productprices.push(pps)
})
if(productprices.length>0){
await self.priceDao.bulkCreate(productprices, t)
}
return {};
});
}
}
module.exports = ProductService;
// (async ()=>{
// let u=new AppService();
// // let x=await u.cregister("jiangong")
// // console.log(x)
// // let x=await u.cunregister("jiangong")
// // console.log(x)
// // let t=await u.cmakejwt()
// // console.log(t)
// //let ux=await u.cjsonregister(AppService.newRouteUrl("test-service2"),{name:"test-service2",hosts:["ttest1.com"]})
// //let ux=await u.cjsonregister(AppService.newServiceUrl(),{name:"test-service3",url:"http://zhichan.gongsibao.com"})
// //let ux=await u.cdel(AppService.routeUrl("test-service2"))
// //let ux=await u.cdel(AppService.serviceUrl("test-service2"))
// // let ux=await u.create({name:"test4-service",backend:"zhichan-service",domainName:"domain.com"})
// // console.log(ux);
// // let delrtn=await u.delete({id:2,name:"test4-service"})
// // console.log(delrtn);
// })()
\ No newline at end of file
module.exports = ProductService;
\ No newline at end of file
const system = require("../../../system");
const ServiceBase = require("../../sve.base");
class ProductpriceService extends ServiceBase {
constructor() {
super("product", ServiceBase.getDaoName(ProductpriceService));
}
async updownProduct(uid){
let p= await this.dao.findById(uid)
p.isEnabled=!p.isEnabled
await p.save()
return {}
}
}
module.exports = ProductpriceService;
\ No newline at end of file
......@@ -217,7 +217,7 @@ class System {
try {
ClassObj = require(objabspath);
} catch (e) {
// console.log(e)
//console.log(e)
let fname = objsettings[packageName + "base"];
ClassObj = require(fname);
}
......
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