Commit 854d8b12 by sxy

feat: 建立公共表model

parent 042d8db4
const Sequelize = require('sequelize'); const Sequelize = require('sequelize');
const settings=require("../../../../config/settings") const settings = require("../../../../config/settings")
const fs=require("fs") const fs = require("fs")
const path=require("path"); const path = require("path");
var glob = require("glob"); var glob = require("glob");
class DbFactory{ class DbFactory {
constructor(){ constructor() {
const dbConfig=settings.database(); const dbConfig = settings.database();
this.db=new Sequelize(dbConfig.dbname, this.db = new Sequelize(dbConfig.dbname,
dbConfig.user, dbConfig.user,
dbConfig.password, dbConfig.password,
dbConfig.config); dbConfig.config);
this.db.Sequelize=Sequelize; this.db.Sequelize = Sequelize;
this.db.Op=Sequelize.Op; this.db.Op = Sequelize.Op;
this.initModels(); this.initModels();
this.initRelations(); this.initRelations();
} }
async initModels(){ async initModels() {
var self=this; var self = this;
var modelpath=path.normalize(path.join(__dirname, '../..'))+"/models/"; var modelpath = path.normalize(path.join(__dirname, '../..')) + "/models/";
var models=glob.sync(modelpath+"/**/*.js"); var models = glob.sync(modelpath + "/**/*.js");
console.log(models.length); console.log(models.length);
models.forEach(function(m){ models.forEach(function (m) {
console.log(m); console.log(m);
self.db.import(m); self.db.import(m);
}); });
console.log("init models...."); console.log("init models....");
} }
async initRelations(){ async initRelations() {
this.db.models.dataauth.belongsTo(this.db.models.user,{constraints: false,}); this.db.models.dataauth.belongsTo(this.db.models.user, { constraints: false, });
/*建立用户和角色之间的关系*/ /*建立用户和角色之间的关系*/
this.db.models.user.belongsToMany(this.db.models.role, {as:"Roles",through: 'p_userrole',constraints: false,}); this.db.models.user.belongsToMany(this.db.models.role, { as: "Roles", through: 'p_userrole', constraints: false, });
this.db.models.role.belongsToMany(this.db.models.user, {as:"Users",through: 'p_userrole',constraints: false,}); this.db.models.role.belongsToMany(this.db.models.user, { as: "Users", through: 'p_userrole', constraints: false, });
/*组织机构自引用*/ /*组织机构自引用*/
//this.db.models.org.belongsTo(this.db.models.org,{constraints: false,}); //this.db.models.org.belongsTo(this.db.models.org,{constraints: false,});
//this.db.models.org.hasMany(this.db.models.org,{constraints: false,}); //this.db.models.org.hasMany(this.db.models.org,{constraints: false,});
//组织机构和角色是多对多关系,建立兼职岗位,给岗位赋予多个角色,从而同步修改用户的角色 //组织机构和角色是多对多关系,建立兼职岗位,给岗位赋予多个角色,从而同步修改用户的角色
//通过岗位接口去修改用户的角色 //通过岗位接口去修改用户的角色
//this.db.models.role.belongsToMany(this.db.models.org,{through: this.db.models.orgrole,constraints: false,}); //this.db.models.role.belongsToMany(this.db.models.org,{through: this.db.models.orgrole,constraints: false,});
//this.db.models.org.belongsToMany(this.db.models.role,{through: this.db.models.orgrole,constraints: false,}); //this.db.models.org.belongsToMany(this.db.models.role,{through: this.db.models.orgrole,constraints: false,});
//组织机构和用户是1对多, //组织机构和用户是1对多,
// this.db.models.user.belongsTo(this.db.models.org,{constraints: false,}); // this.db.models.user.belongsTo(this.db.models.org,{constraints: false,});
// this.db.models.org.hasMany(this.db.models.user,{constraints: false,}); // this.db.models.org.hasMany(this.db.models.user,{constraints: false,});
this.db.models.user.belongsTo(this.db.models.app,{constraints: false,}); this.db.models.user.belongsTo(this.db.models.app, { constraints: false, });
this.db.models.role.belongsTo(this.db.models.app, {constraints: false,}); this.db.models.role.belongsTo(this.db.models.app, { constraints: false, });
this.db.models.auth.belongsTo(this.db.models.app,{constraints: false,});
this.db.models.auth.belongsTo(this.db.models.company,{constraints: false,});
this.db.models.auth.belongsTo(this.db.models.role,{constraints: false,});
this.db.models.app.belongsTo(this.db.models.user,{as:"creator",constraints: false,}); this.db.models.auth.belongsTo(this.db.models.app, { constraints: false, });
this.db.models.auth.belongsTo(this.db.models.company, { constraints: false, });
this.db.models.auth.belongsTo(this.db.models.role, { constraints: false, });
this.db.models.app.belongsTo(this.db.models.user, { as: "creator", constraints: false, });
this.db.models.user.belongsTo(this.db.models.company,{constraints: false,});
this.db.models.role.belongsTo(this.db.models.company, {constraints: false,}); this.db.models.user.belongsTo(this.db.models.company, { constraints: false, });
this.db.models.role.belongsTo(this.db.models.company, { constraints: false, });
// this.db.models.org.belongsTo(this.db.models.company,{constraints: false,}); // this.db.models.org.belongsTo(this.db.models.company,{constraints: false,});
this.db.models.route.belongsTo(this.db.models.app,{constraints: false,}); this.db.models.route.belongsTo(this.db.models.app, { constraints: false, });
this.db.models.plugin.belongsTo(this.db.models.app,{constraints: false,}); this.db.models.plugin.belongsTo(this.db.models.app, { constraints: false, });
// 商机表 1:1 方案表
this.db.models.scheme.belongsTo(this.db.models.bussiness_opportunity, { constraints: false, });
this.db.models.bussiness_opportunity.hasOne(this.db.models.scheme, { constraints: false, });
} }
//async getCon(){,用于使用替换table模型内字段数据使用 //async getCon(){,用于使用替换table模型内字段数据使用
getCon(){ getCon() {
var that=this; var that = this;
// await this.db.authenticate().then(()=>{ // await this.db.authenticate().then(()=>{
// console.log('Connection has been established successfully.'); // console.log('Connection has been established successfully.');
// }).catch(err => { // }).catch(err => {
...@@ -73,7 +78,7 @@ class DbFactory{ ...@@ -73,7 +78,7 @@ class DbFactory{
// throw err; // throw err;
// }); // });
//同步模型 //同步模型
if(settings.env=="dev"){ if (settings.env == "dev") {
//console.log(pa); //console.log(pa);
// pconfigObjs.forEach(p=>{ // pconfigObjs.forEach(p=>{
...@@ -90,7 +95,7 @@ class DbFactory{ ...@@ -90,7 +95,7 @@ class DbFactory{
return this.db; return this.db;
} }
} }
module.exports=DbFactory; module.exports = DbFactory;
// const dbf=new DbFactory(); // const dbf=new DbFactory();
// dbf.getCon().then((db)=>{ // dbf.getCon().then((db)=>{
// //console.log(db); // //console.log(db);
......
const system=require("../system"); const system = require("../system");
const settings=require("../../config/settings.js"); const settings = require("../../config/settings.js");
const reclient=system.getObject("util.redisClient"); const reclient = system.getObject("util.redisClient");
const md5 = require("MD5"); const md5 = require("MD5");
//获取平台配置兑换率 var dbf = system.getObject("db.common.connection");
//初次登录的赠送数量 var db = dbf.getCon();
//创建一笔交易 db.sync({ force: true }).then(async () => {
//同时增加账户数量,增加系统平台账户 console.log("init 完毕");
var dbf=system.getObject("db.common.connection");
var db=dbf.getCon();
db.sync({force:true}).then(async ()=>{
const apps=await system.getObject("service.common.appSve");
const usS=await system.getObject("service.auth.userSve");
let appnew=await apps.create( {
"name":"center-app",
"domainName":"t9.com",
"backend":"192.168.4.1",
"isSystem":true
});
let Role=db.models["role"];
await Role.create({code:"ta",name:"租户",isSystem:true,app_id:appnew.id,company_id:settings.pmcompanyid})
await Role.create({code:"pr",name:"个人",isSystem:true,app_id:appnew.id,company_id:settings.pmcompanyid})
let usuper=await usS.pmregister({userName:"sm",password:"951753",isSuper:true,isAdmin:true,isSystem:true,isEnabled:true,nickName:"superman",app_id:appnew.id,company_id:settings.id})
appnew.creator_id=usuper.user.id
await appnew.save()
//创建role
// if(settings.env=="prod"){
// reclient.flushall(()=>{
// console.log("clear caches ok.....");
// });
// }
// reclient.flushall(()=>{
// console.log("clear caches ok.....");
// });
}); });
......
const system = require("../../../system");
const settings = require("../../../../config/settings");
const appconfig = system.getSysConfig();
/**
* 商机表
*/
module.exports = (db, DataTypes) => {
return db.define("bussiness_opportunity", {
demand_code: { // 需求编码
allowNull: false,
type: DataTypes.STRING
},
business_type: { // 商机类型
allowNull: false,
type: DataTypes.STRING
},
business_status: { // 商机状态
allowNull: false,
type: DataTypes.STRING
},
business_info: { // 商机详情
allowNull: false,
type: DataTypes.JSON
},
sourse_number: { // 来源单号 (下单时产生的编号)
allowNull: true,
type: DataTypes.STRING
},
address: { // 区域地址
allowNull: false,
type: DataTypes.STRING
},
close_reason: { // 关闭理由
allowNull: true,
type: DataTypes.STRING
},
service_company_id: { // 公司id
allowNull: false,
type: DataTypes.STRING
},
service_company_name: { // 公司名称
allowNull: false,
type: DataTypes.STRING
},
salesman_opcode: { // 组织架构路径
allowNull: true,
type: DataTypes.STRING
},
salesman_id: {// 业务员id
allowNull: true,
type: DataTypes.STRING
},
salesman_name: { // 业务员姓名
allowNull: true,
type: DataTypes.STRING
},
salesman_phone_number: { // 业务员联系方式
allowNull: true,
type: DataTypes.STRING
}
}, {
paranoid: false,//假的删除
underscored: true,
version: true,
freezeTableName: true,
//freezeTableName: true,
// define the table's name
tableName: 'bussiness_opportunity',
validate: {
},
indexes: [
// Create a unique index on email
// {
// unique: true,
// fields: ['email']
// },
//
// // Creates a gin index on data with the jsonb_path_ops operator
// {
// fields: ['data'],
// using: 'gin',
// operator: 'jsonb_path_ops'
// },
//
// // By default index name will be [table]_[fields]
// // Creates a multi column partial index
// {
// name: 'public_by_author',
// fields: ['author', 'status'],
// where: {
// status: 'public'
// }
// },
//
// // A BTREE index with a ordered field
// {
// name: 'title_index',
// method: 'BTREE',
// fields: ['author', {attribute: 'title', collate: 'en_US', order: 'DESC', length: 5}]
// }
]
});
}
const system = require("../../../system");
const settings = require("../../../../config/settings");
const appconfig = system.getSysConfig();
/**
* 交付单表
*/
module.exports = (db, DataTypes) => {
return db.define("delivery_bill", {
delivery_code: { //交付单编号
allowNull: false,
type: DataTypes.STRING
},
sourse_number: { // 来源单号 (订单编号)
allowNull: false,
type: DataTypes.STRING
},
product_code: { // 产品编码
allowNull: false,
type: DataTypes.STRING
},
product_name: { // 产品名称
allowNull: false,
type: DataTypes.STRING
},
address: { // 区域地址
allowNull: false,
type: DataTypes.STRING
},
service_info: { //服务概况
allowNull: false,
type: DataTypes.JSON
},
status: {// 服务单流转状态
allowNull: false,
type: DataTypes.STRING
},
selling_price: {//售价
allowNull: false,
type: DataTypes.INTEGER
},
cost_price: {//成本价
allowNull: true,
type: DataTypes.INTEGER
},
close_reason: {//关闭理由
allowNull: true,
type: DataTypes.STRING
},
service_company_id: { // 公司id
allowNull: false,
type: DataTypes.STRING
},
service_company_name: { // 公司名称
allowNull: false,
type: DataTypes.STRING
},
salesman_opcode: { // 组织架构路径
allowNull: true,
type: DataTypes.STRING
},
salesman_id: {// 业务员id
allowNull: true,
type: DataTypes.STRING
},
salesman_name: { // 业务员姓名
allowNull: true,
type: DataTypes.STRING
},
salesman_phone_number: { // 业务员联系方式
allowNull: true,
type: DataTypes.STRING
}
}, {
paranoid: false,//假的删除
underscored: true,
version: true,
freezeTableName: true,
//freezeTableName: true,
// define the table's name
tableName: 'delivery_bill',
validate: {
},
indexes: [
// Create a unique index on email
// {
// unique: true,
// fields: ['email']
// },
//
// // Creates a gin index on data with the jsonb_path_ops operator
// {
// fields: ['data'],
// using: 'gin',
// operator: 'jsonb_path_ops'
// },
//
// // By default index name will be [table]_[fields]
// // Creates a multi column partial index
// {
// name: 'public_by_author',
// fields: ['author', 'status'],
// where: {
// status: 'public'
// }
// },
//
// // A BTREE index with a ordered field
// {
// name: 'title_index',
// method: 'BTREE',
// fields: ['author', {attribute: 'title', collate: 'en_US', order: 'DESC', length: 5}]
// }
]
});
}
const system = require("../../../system");
const moment = require('moment');
const settings = require("../../../../config/settings");
const appconfig = system.getSysConfig();
/**
* 方案表
*/
module.exports = (db, DataTypes) => {
return db.define("scheme", {
demand_code: { // 需求编码
allowNull: false,
type: DataTypes.STRING
},
scheme_number: { //方案编号
allowNull: false,
type: DataTypes.STRING
},
scheme_info: {//方案详情
allowNull: false,
type: DataTypes.JSON
},
status: { //方案状态
allowNull: false,
type: DataTypes.STRING
},
reject_reason: {//驳回理由
allowNull: true,
type: DataTypes.STRING
},
remark: { //备注
allowNull: false,
type: DataTypes.STRING
}
}, {
paranoid: false,//假的删除
underscored: true,
version: true,
freezeTableName: true,
//freezeTableName: true,
// define the table's name
tableName: 'scheme',
validate: {
},
indexes: [
// Create a unique index on email
// {
// unique: true,
// fields: ['email']
// },
//
// // Creates a gin index on data with the jsonb_path_ops operator
// {
// fields: ['data'],
// using: 'gin',
// operator: 'jsonb_path_ops'
// },
//
// // By default index name will be [table]_[fields]
// // Creates a multi column partial index
// {
// name: 'public_by_author',
// fields: ['author', 'status'],
// where: {
// status: 'public'
// }
// },
//
// // A BTREE index with a ordered field
// {
// name: 'title_index',
// method: 'BTREE',
// fields: ['author', {attribute: 'title', collate: 'en_US', order: 'DESC', length: 5}]
// }
]
});
}
const system = require("../../../system");
const settings = require("../../../../config/settings");
const appconfig = system.getSysConfig();
/**
* 状态流转记录表
*/
module.exports = (db, DataTypes) => {
return db.define("status_log", {
flow_type: { // 流程类型 (商机、方案、交付单)
allowNull: false,
type: DataTypes.STRING
},
flow_id: { // 流程对应id
allowNull: false,
type: DataTypes.STRING
},
status: { // 流转状态
allowNull: false,
type: DataTypes.STRING
}
}, {
paranoid: false,//假的删除
underscored: true,
version: true,
freezeTableName: true,
//freezeTableName: true,
// define the table's name
tableName: 'status_log',
validate: {
},
indexes: [
// Create a unique index on email
// {
// unique: true,
// fields: ['email']
// },
//
// // Creates a gin index on data with the jsonb_path_ops operator
// {
// fields: ['data'],
// using: 'gin',
// operator: 'jsonb_path_ops'
// },
//
// // By default index name will be [table]_[fields]
// // Creates a multi column partial index
// {
// name: 'public_by_author',
// fields: ['author', 'status'],
// where: {
// status: 'public'
// }
// },
//
// // A BTREE index with a ordered field
// {
// name: 'title_index',
// method: 'BTREE',
// fields: ['author', {attribute: 'title', collate: 'en_US', order: 'DESC', length: 5}]
// }
]
});
}
...@@ -6,11 +6,11 @@ var settings = { ...@@ -6,11 +6,11 @@ var settings = {
db: 9, db: 9,
}, },
database: { database: {
dbname: "center-manage", dbname: "ent_deliver",
user: "root", user: "root",
password: "123456", password: "123456",
config: { config: {
host: '192.168.4.119', host: '127.0.0.1',
port: 3306, port: 3306,
dialect: 'mysql', dialect: 'mysql',
operatorsAliases: false, operatorsAliases: false,
......
...@@ -31,10 +31,12 @@ ...@@ -31,10 +31,12 @@
"exif-js": "^2.3.0", "exif-js": "^2.3.0",
"express": "^4.16.2", "express": "^4.16.2",
"express-session": "^1.15.6", "express-session": "^1.15.6",
"glob": "^7.1.6",
"gm": "^1.23.1", "gm": "^1.23.1",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"log4js": "^2.10.0", "log4js": "^2.10.0",
"method-override": "^2.3.10", "method-override": "^2.3.10",
"moment": "^2.26.0",
"morgan": "^1.9.0", "morgan": "^1.9.0",
"multer": "^1.3.0", "multer": "^1.3.0",
"mysql2": "^1.5.3", "mysql2": "^1.5.3",
......
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