Commit 3ac74012 by lixinming

优化

parent 0866f6e2
...@@ -3,8 +3,8 @@ import { ERRORENUM } from "../../config/errorEnum"; ...@@ -3,8 +3,8 @@ import { ERRORENUM } from "../../config/errorEnum";
import { modelMap } from "../../model/modelBind"; import { modelMap } from "../../model/modelBind";
import { BizError } from "../../util/bizError"; import { BizError } from "../../util/bizError";
function analysisParamToWhere(param) { function analysisParamToWhere(param, column) {
let where = {}; let where
for (let key in param) {// 模糊查询{"字段名":{"%like%":'三'}} for (let key in param) {// 模糊查询{"字段名":{"%like%":'三'}}
if (param[key]["%like%"]) { if (param[key]["%like%"]) {
where[key] = {[Op.like]:`%${param[key]}%`}; where[key] = {[Op.like]:`%${param[key]}%`};
...@@ -12,7 +12,10 @@ function analysisParamToWhere(param) { ...@@ -12,7 +12,10 @@ function analysisParamToWhere(param) {
where[key] = param[key]; where[key] = param[key];
} }
} }
return where
let selectParam:any = {where};
if (column.length) selectParam.attributes = column;
return selectParam
} }
/** /**
...@@ -21,9 +24,9 @@ function analysisParamToWhere(param) { ...@@ -21,9 +24,9 @@ function analysisParamToWhere(param) {
* @param param * @param param
* @returns * @returns
*/ */
export async function selectOneDataByParam(tableModel, column:any, param) { export async function selectOneDataByParam(tableModel, param, column) {
let where = analysisParamToWhere(param); let selectParam = analysisParamToWhere(param, column);
let data = tableModel.findOne({attributes: column, where}); let data = tableModel.findOne(selectParam);
return {data}; return {data};
} }
...@@ -34,9 +37,9 @@ export async function selectOneDataByParam(tableModel, column:any, param) { ...@@ -34,9 +37,9 @@ export async function selectOneDataByParam(tableModel, column:any, param) {
* @param param * @param param
* @returns * @returns
*/ */
export async function selectDataListByParam(tableModel, column:any, param:any) { export async function selectDataListByParam(tableModel, param, column) {
let where = analysisParamToWhere(param); let selectParam = analysisParamToWhere(param, column);
let data = tableModel.findAll({attributes: column, where}); let data = tableModel.findAll(selectParam);
return {data}; return {data};
} }
...@@ -49,21 +52,22 @@ export async function selectDataListByParam(tableModel, column:any, param:any) { ...@@ -49,21 +52,22 @@ export async function selectDataListByParam(tableModel, column:any, param:any) {
* @param pageSize * @param pageSize
* @returns * @returns
*/ */
export async function selectDataListToPageByParam(tableModel, column:any, param:any, pageNumber:number, pageSize:number) { export async function selectDataListToPageByParam(tableModel, param, column, pageNumber:number, pageSize:number) {
pageSize = pageSize || 10; let selectParam:any = analysisParamToWhere(param, column);
let where = analysisParamToWhere(param); selectParam.limit = pageSize || 10;
let data = tableModel.findAll({attributes: column, where, offset:(pageNumber-1)*10, limit:pageSize}); selectParam.offset = (pageNumber-1)*10;
let data = tableModel.findAll(selectParam);
return {data}; return {data};
} }
export async function selectDataCountByParam(tableModel, param:any) { export async function selectDataCountByParam(tableModel, param) {
let where = analysisParamToWhere(param); let selectParam:any = analysisParamToWhere(param, []);
let data = tableModel.count({where}); let data = tableModel.count(selectParam);
return {data}; return {data};
} }
export async function associationSelect(tableName:string, param:any) { export async function associationSelect(tableName:string, param) {
let model = modelMap[tableName]; let model = modelMap[tableName];
if (!model) throw new BizError(ERRORENUM.不存在表); if (!model) throw new BizError(ERRORENUM.不存在表);
...@@ -75,20 +79,25 @@ export async function associationSelect(tableName:string, param:any) { ...@@ -75,20 +79,25 @@ export async function associationSelect(tableName:string, param:any) {
/** /**
* 多表联查 * 多表联查
* @param tableName * @param tableModel
* @param associatedTable * @param includeConf {"表名":["",""] }
* @param column
* @param param * @param param
* @param column
* @returns * @returns
*/ */
export async function selectDataToTableAssociation(tableModel, associatedTable:string, column:any, param:any) { export async function selectDataToTableAssociation(tableModel, includeConf, param, column) {
let where = analysisParamToWhere(param); let include = [];
let data = tableModel.findAll({ for (let tableName in includeConf) {
include: [{ if (!modelMap[tableName]) throw new BizError(ERRORENUM.不存在表, `尝试进行多表联查,但是不存在${tableName}`);
model: associatedTable, include.push({
attributes: column, //指定关联表查询的字段 attributes : includeConf[tableName],
}], module : modelMap[tableName]
where });
}); }
let selectParam:any = analysisParamToWhere(param, column);
selectParam.include = include;
let data = tableModel.findAll(selectParam);
return {data}; return {data};
} }
\ No newline at end of file
const Sequelize =require('sequelize'); const Sequelize =require('sequelize');
export const TablesConfig = { export const TablesConfig = [
"支部表":{ {
tableName:'branch', tableNameCn:'支部表',
tableName:'branch',
schema:{ schema:{
bId: { bId: {
type:Sequelize.INTEGER(20), //表示属性的数据类型 type:Sequelize.INTEGER(20), //表示属性的数据类型
...@@ -15,6 +16,11 @@ export const TablesConfig = { ...@@ -15,6 +16,11 @@ export const TablesConfig = {
branchName: {type:Sequelize.STRING(100), allowNull:false}, //支部名称 branchName: {type:Sequelize.STRING(100), allowNull:false}, //支部名称
}, },
association: [{}] association: [{}]
}
]; {
"":{
tableName:'branch',
}, },
"后台用户表":{ "后台用户表":{
tableName:'adminUser', tableName:'adminUser',
...@@ -67,10 +73,10 @@ export const TablesConfig = { ...@@ -67,10 +73,10 @@ export const TablesConfig = {
adminTokenMs:{ type:Sequelize.DATE, allowNull:false }, adminTokenMs:{ type:Sequelize.DATE, allowNull:false },
}, },
association: [ association: [
{type: "hasOne", table1: "branch", table2: "partyMember"}, {type: "hasOne", check: "branch"},
{type: "hasOne", table1: "administrativePosition", table2: "partyMember"}, {type: "hasOne", check: "administrativePosition"},
{type: "hasMany", table1: "partyPositions", table2: "partyMember"}, {type: "hasMany", check: "partyPositions"},
{type: "hasMany", table1: "department", table2: "partyMember"}, {type: "hasMany", check: "department"},
] ]
}, },
"行政职务表":{ "行政职务表":{
......
...@@ -2,6 +2,7 @@ import { ERRORENUM } from "../config/errorEnum"; ...@@ -2,6 +2,7 @@ import { ERRORENUM } from "../config/errorEnum";
import { systemConfig } from "../config/serverConfig"; import { systemConfig } from "../config/serverConfig";
import { EccTableConfig } from "../config/mongoTableConfig"; import { EccTableConfig } from "../config/mongoTableConfig";
import { BizError } from "../util/bizError"; import { BizError } from "../util/bizError";
import { modelMap } from "../model/modelBind";
/** /**
* 中间件 校验连接对象token * 中间件 校验连接对象token
...@@ -40,8 +41,8 @@ export async function checkMySqlSign(req, res, next) { ...@@ -40,8 +41,8 @@ export async function checkMySqlSign(req, res, next) {
if (sign != systemConfig.sign) return next( new BizError(ERRORENUM.身份验证失败, `传入的sign值为:${sign}`) ); if (sign != systemConfig.sign) return next( new BizError(ERRORENUM.身份验证失败, `传入的sign值为:${sign}`) );
if (!table) return next( new BizError(ERRORENUM.缺少必要参数_表名, `传入的table值为:${table}`) ); if (!table) return next( new BizError(ERRORENUM.缺少必要参数_表名, `传入的table值为:${table}`) );
if (!EccTableConfig[table]) return next( new BizError(ERRORENUM.不存在表, `传入的table值为:${table}`) ); if (!modelMap[table]) return next( new BizError(ERRORENUM.不存在表, `传入的table值为:${table}`) );
req.tableModel = EccTableConfig[table]; req.tableModel = modelMap[table];
next(); next();
} }
\ No newline at end of file
...@@ -8,8 +8,9 @@ import { mysqlDB } from "../db/mysql/mysqlInit"; ...@@ -8,8 +8,9 @@ import { mysqlDB } from "../db/mysql/mysqlInit";
let baseDB = {}; let baseDB = {};
export async function initMysqlModel() { export async function initMysqlModel() {
for (let tableNameCN in TablesConfig) {
let { tableName, schema, association } = TablesConfig[tableNameCN]; for (let i =0; i < TablesConfig.length; i++) {
let { tableName, schema, association, tableNameCn } = TablesConfig[i];
let schemaConf = { let schemaConf = {
freezeTableName:true, //true表示使用给定的表名,false表示模型名后加s作为表名 freezeTableName:true, //true表示使用给定的表名,false表示模型名后加s作为表名
...@@ -20,18 +21,19 @@ export async function initMysqlModel() { ...@@ -20,18 +21,19 @@ export async function initMysqlModel() {
baseDB[tableName] = model.sync({}).then(); baseDB[tableName] = model.sync({}).then();
association.forEach( item => { association.forEach( (item:any) => {
if (item) { if (item) {
let {type, table1, table2} = item; let {type, check} = item;
if (type == "hasOne") { if (type == "hasOne") {
table1.hasOne(table2); baseDB[check].hasOne(baseDB[tableName]);
table2.belongsTo(table1);
} else if (type == "hasMany") { } else if (type == "hasMany") {
table1.hasMany(table2); baseDB[check].hasMany(baseDB[tableName]);
table2.belongsTo(table1);
} }
baseDB[tableName].belongsTo(baseDB[check]);
} }
}) });
} }
......
...@@ -36,7 +36,7 @@ async function addTableData(req, res) { ...@@ -36,7 +36,7 @@ async function addTableData(req, res) {
async function deleteData(req, res) { async function deleteData(req, res) {
let table = req.headers.table; let table = req.tableModel;
let reqConf = {param: 'Object' }; let reqConf = {param: 'Object' };
let { param } = eccReqParamater(reqConf, req.body); let { param } = eccReqParamater(reqConf, req.body);
...@@ -46,7 +46,7 @@ async function deleteData(req, res) { ...@@ -46,7 +46,7 @@ async function deleteData(req, res) {
} }
async function updateManyData(req, res) { async function updateManyData(req, res) {
let table = req.headers.table; let table = req.tableModel;
let reqConf = {param:'Object', data: 'Object' }; let reqConf = {param:'Object', data: 'Object' };
let { param, data } = eccReqParamater(reqConf, req.body); let { param, data } = eccReqParamater(reqConf, req.body);
...@@ -59,35 +59,35 @@ async function updateManyData(req, res) { ...@@ -59,35 +59,35 @@ async function updateManyData(req, res) {
async function findOneData(req, res) { async function findOneData(req, res) {
let table = req.headers.table; let table = req.tableModel;
let reqConf = {param:'Object'}; let reqConf = {param:'Object', column:"[String]"};
let { param } = eccReqParamater(reqConf, req.body); let { param, column } = eccReqParamater(reqConf, req.body);
let result = await findBiz.selectOneDataByParam(table, param); let result = await findBiz.selectOneDataByParam(table, param, column);
res.success(result); res.success(result);
} }
async function findManyData(req, res) { async function findManyData(req, res) {
let table = req.headers.table; let table = req.tableModel;
let reqConf = {param:'Object'}; let reqConf = {param:'Object', column:"[String]"};
let { param } = eccReqParamater(reqConf, req.body); let { param, column } = eccReqParamater(reqConf, req.body);
let result = await findBiz.selectDataListByParam(table, param); let result = await findBiz.selectDataListByParam(table, param, column);
res.success(result); res.success(result);
} }
async function findManyToPageData(req, res) { async function findManyToPageData(req, res) {
let table = req.headers.table; let table = req.tableModel;
let reqConf = {param:'Object', pageNumber:'Number', pageSize:'Number'}; let reqConf = {param:'Object', pageNumber:'Number', pageSize:'Number', column:"[String]"};
let { param, pageNumber, pageSize } = eccReqParamater(reqConf, req.body, ['pageSize']); let { param, pageNumber, pageSize, column } = eccReqParamater(reqConf, req.body );
let result = await findBiz.selectDataListToPageByParam(table, param, pageNumber, pageSize); let result = await findBiz.selectDataListToPageByParam(table, param, column, pageNumber, pageSize);
res.success(result); res.success(result);
} }
async function findCountData(req, res) { async function findCountData(req, res) {
let table = req.headers.table; let table = req.tableModel;
let reqConf = {param:'Object'}; let reqConf = {param:'Object'};
let { param } = eccReqParamater(reqConf, req.body); let { param } = eccReqParamater(reqConf, req.body);
...@@ -97,12 +97,11 @@ async function findCountData(req, res) { ...@@ -97,12 +97,11 @@ async function findCountData(req, res) {
} }
async function findAggragateData(req, res) { async function findAggragateData(req, res) {
let table = req.headers.table; let table = req.tableModel;
let reqConf = {param:'[Object]' }; let reqConf = {param:'Object', includeConf:'Object', column:"[String]" };
let { param } = eccReqParamater(reqConf, req.body); let { param, includeConf, column } = eccReqParamater(reqConf, req.body);
// let result = await findBiz.aggragateParam(table, param); let result = await findBiz.selectDataToTableAssociation(table, includeConf, param, column);
let result = {};
res.success(result); res.success(result);
} }
\ No newline at end of file
...@@ -65,6 +65,16 @@ export function eccReqParamater(conf:object, param, skipKeys?) { ...@@ -65,6 +65,16 @@ export function eccReqParamater(conf:object, param, skipKeys?) {
} }
} }
break; break;
case '[String]':
if ( !Array.isArray(param[key]) ) isError = true;
for (let i =0; i < param[key].length; i++) {
let item = param[key][i];
if ( typeof item != 'string' ) {
isError = true;
errorStr = `${key}应是String型数组其中下标${i}${typeof item}`;
}
}
break;
// case 'Address': // case 'Address':
// /**地址类型 基本数据类型为数组字符串但是要判断层级关系 */ // /**地址类型 基本数据类型为数组字符串但是要判断层级关系 */
// if ( !Array.isArray(param[key]) ) { // if ( !Array.isArray(param[key]) ) {
......
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