Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新后立即查询返回老的对象 #19

Open
wv1124 opened this issue Oct 18, 2018 · 0 comments
Open

更新后立即查询返回老的对象 #19

wv1124 opened this issue Oct 18, 2018 · 0 comments

Comments

@wv1124
Copy link

wv1124 commented Oct 18, 2018

public async updateConfig(param: IPrinterConfigParam) {
await this.ctx.model.CashierPrinterConfig.sync();
if (param && param.id) {
// 修改
await this.ctx.model.CashierPrinterConfig.upsert({
adminId: param.adminId,
id: param.id,
jsonConfig: param.configJson,
});
} else {
// 新增;
let printerConfig: PlainObject[] = [];
// 查询所有记录
const sqlIntstance = await this.ctx.model.CashierPrinterConfig.findAll({
where: {
adminId: param.adminId,
},
});
if (sqlIntstance) {
sqlIntstance.map(ins => {
let temp = (ins as PlainObject).dataValues;
// 判断当前设备是否已经保存过 deviceNo key
let tempJson = JSON.parse(temp.jsonConfig);
let newJson = JSON.parse(param.configJson);
if (tempJson.deviceNo === newJson.deviceNo && tempJson.key === newJson.key) {
printerConfig.push(temp);
}
});
// 不存在记录
if (printerConfig.length == 0) {
await this.ctx.model.CashierPrinterConfig.create({
adminId: param.adminId,
jsonConfig: param.configJson,
});
} else {
await this.ctx.model.CashierPrinterConfig.upsert({
adminId: param.adminId,
id: printerConfig[0].id,
jsonConfig: param.configJson,
});
}
}
}
}

public async getConfigByAmdinId(adminId: string) {
console.log('getConfigByAmdinId', adminId);
let printerConfig: PlainObject[] = [];

await this.ctx.model.CashierPrinterConfig.sync()

const sqlIntstance = await this.ctx.model.CashierPrinterConfig.findAll({
  where: {
    adminId,
  },
});
if (sqlIntstance) {
  sqlIntstance.map(ins => {
    let temp = (ins as PlainObject).dataValues;
    printerConfig.push({ id: temp.id, config: JSON.parse(temp.jsonConfig) });
  });
}
return printerConfig;

}

2018-10-18 09:00:54,334 INFO 15542 egg-sequelize Executed (default): CREATE TABLE IF NOT EXISTS cashier_printer_config (id INTEGER auto_increment UNIQUE , admin_id VARCHAR(20) NOT NULL, json_config VARCHAR(2000) NOT NULL, modify_time DATETIME NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB;
2018-10-18 09:00:54,336 INFO 15542 egg-sequelize Executed (default): SHOW INDEX FROM cashier_printer_config
2018-10-18 09:00:54,348 INFO 15542 egg-sequelize Executed (default): INSERT INTO cashier_printer_config (id,admin_id,json_config,modify_time) VALUES (186,'A882080','{"printMode":"manual","state":1,"deviceType":"wifi"}','2018-10-18 09:00:54') ON DUPLICATE KEY UPDATE admin_id=VALUES(admin_id), id=VALUES(id), json_config=VALUES(json_config), modify_time=VALUES(modify_time);

2018-10-18 09:00:54,335 INFO 15541 egg-sequelize Executed (default): CREATE TABLE IF NOT EXISTS cashier_printer_config (id INTEGER auto_increment UNIQUE , admin_id VARCHAR(20) NOT NULL, json_config VARCHAR(2000) NOT NULL, modify_time DATETIME NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB;
2018-10-18 09:00:54,337 INFO 15541 egg-sequelize Executed (default): SHOW INDEX FROM cashier_printer_config
2018-10-18 09:00:54,339 INFO 15541 egg-sequelize Executed (default): SELECT id, admin_id AS adminId, json_config AS jsonConfig, modify_time AS modifyTime, modify_time FROM cashier_printer_config AS cashier_printer_config WHERE cashier_printer_config.admin_id = 'A882080';
结果还是:
[{"id":186,"config":{"printMode":"auto","state":1,"deviceType":"wifi"}}]

2018-10-18 09:02:49,889 INFO 15542 egg-sequelize Executed (default): CREATE TABLE IF NOT EXISTS cashier_printer_config (id INTEGER auto_increment UNIQUE , admin_id VARCHAR(20) NOT NULL, json_config VARCHAR(2000) NOT NULL, modify_time DATETIME NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB;
2018-10-18 09:02:49,890 INFO 15542 egg-sequelize Executed (default): SHOW INDEX FROM cashier_printer_config
2018-10-18 09:02:49,892 INFO 15542 egg-sequelize Executed (default): SELECT id, admin_id AS adminId, json_config AS jsonConfig, modify_time AS modifyTime, modify_time FROM cashier_printer_config AS cashier_printer_config WHERE cashier_printer_config.admin_id = 'A882080';
过一会查就正常了
[{"id":186,"config":{"printMode":"manual","state":1,"deviceType":"wifi"}}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant