Commit 447e139b by 王昆

gsb

parent a43b1d99
......@@ -4,11 +4,71 @@ const md5 = require("MD5");
const logCtl = system.getObject("web.oplogCtl");
class EcontractApi {
constructor() {
this.userSve = system.getObject("service.userSve");
this.departmentSve = system.getObject("service.departmentSve");
this.esettleSve = system.getObject("service.esettleSve");
this.busistatSve = system.getObject("service.busistatSve");
this.etemplatebusiSve = system.getObject("service.etemplatebusiSve");
this.restClient = system.getObject("util.restClient");
this.redisClient = system.getObject("util.redisClient");
this.outTradeNoKey = 'bpo_outtradeno';
this.outTradeNoKey = 'bpo_outtradeno';
}
async syncUserDepartment(pobj) {
let map1 = await this.etemplatebusiSve.findOwnerBusiIdMap(1) || {};
let map2 = await this.etemplatebusiSve.findOwnerBusiIdMap(2) || {};
let keys1 = Object.keys(map1);
let keys2 = Object.keys(map2);
let keys3 = keys1.concat(keys2);
let userMap = await this.userSve.findMapByIds(keys3);
let departmentDic = await this.departmentSve.mapDict();
await this.updateStatDepartment(map1, userMap, departmentDic);
await this.updateStatDepartment(map2, userMap, departmentDic);
return system.getResult2(1);
}
async updateStatDepartment(map, userMap, departmentDic) {
for (let k in map) {
let busiIds = map[k] || [];
if (busiIds.length == 0) {
continue;
}
let user = userMap["id_" + k] || {};
let department = departmentDic[user.department_id || 0] || {};
await this.busistatSve.updateDepartmentName({
busiIds: busiIds,
user_name: user.nickName || "",
user_id: Number(k),
user_department_name: department.name || "",
});
}
}
async syncDepartmentStat(pobj) {
// 获取30天内数据
let t1 = new Date().getTime();
for (let i = 0; i < 30; i++) {
try {
let day = moment().subtract(30 - i, 'days').format('YYYY-MM-DD');
await this.syncDay({day: day});
} catch (e) {
console.log(e.stack);
}
}
let t2 = new Date().getTime();
console.log(t2 - t1, "------------------");
await this.syncUserDepartment();
return system.getResult2(1);
}
async syncDay(pobj) {
let day = pobj.day;
let dayNum = Number(day.replace(new RegExp("-", 'g'), ""));
let list = await this.esettleSve.busiStatByDay(day);
console.log(list);
await this.busistatSve.syncDayData(dayNum, list);
}
async settleCommission(obj, req) {
......@@ -280,4 +340,4 @@ class EcontractApi {
return uuid.join('');
}
}
module.exports = EcontractApi;
\ No newline at end of file
module.exports = EcontractApi;
......@@ -45,6 +45,19 @@ class Dao{
var en= await this.model.destroy({where:{id:{[this.db.Op.in]:ids}}});
return en;
}
//批量插入
async bulkCreate(objs, t) {
if (!objs || objs.length == 0) {
return;
}
if (t) {
return await this.model.bulkCreate(objs, { transaction: t });
} else {
return await this.model.bulkCreate(objs);
}
}
async delete(qobj){
var en= await this.model.findOne({where:qobj});
if(en!=null){
......
......@@ -10,6 +10,19 @@ class BusistatDao extends Dao {
qc.raw = true;
return qw;
}
async delByDay(statDay) {
if (!statDay) {
return;
}
var sql = "DELETE FROM `c_busi_stat` WHERE stat_day = " + Number(statDay);
return await this.customExecAddOrPutSql(sql);
}
async updateDepartmentName(params) {
let sql = "UPDATE `c_busi_stat` SET user_name = :user_name, user_id=:user_id, user_department_name= :user_department_name WHERE busi_id IN (:busiIds)";
return await this.customExecAddOrPutSql(sql, {replacements: params});
}
}
module.exports = BusistatDao;
......@@ -5,7 +5,17 @@ class BusistatService extends ServiceBase {
constructor() {
super(ServiceBase.getDaoName(BusistatService));
}
async syncDayData(statDay, list) {
if (!list || list.length == 0) {
return;
}
await this.dao.delByDay(statDay);
await this.dao.bulkCreate(list);
}
async updateDepartmentName(params) {
return await this.dao.updateDepartmentName(params);
}
}
module.exports = BusistatService;
......@@ -9,6 +9,20 @@ class DepartmentService extends ServiceBase {
async suggestByName(name){
return await this.dao.suggest(name);
}
async dict() {
let sql = "SELECT id, name FROM p_department WHERE deleted_at IS NULL";
return await this.customQuery(sql) || [];
}
async mapDict() {
let result = {};
let list = await this.dict();
for (let item of list) {
result[item.id] = item;
}
return result;
}
}
module.exports = DepartmentService;
......@@ -826,6 +826,23 @@ class EsettleService extends ServiceBase {
}
return list;
}
async mapBusiByIds(ids) {
let result = {};
if (!ids || ids.length == 0) {
return result;
}
var sql = "SELECT id, company_name FROM `tbl_busi` WHERE id IN (:ids)";
var list = await this.settledb.query(sql, { replacements: { ids: ids } });
if (list && list.length > 0) {
list = list[0, 0] || [];
for (let b of list) {
result[b.id] = b.company_name;
}
}
return result;
}
async findBybusiId(busiId) {
if (!busiId) {
return [];
......@@ -921,5 +938,46 @@ class EsettleService extends ServiceBase {
}
return list;
}
async busiStatByDay(day) {
if (!day) {
return [];
}
let dayNum = Number(day.replace(new RegExp("-", 'g'), ""))
let begin = day + " 00:00:00";
let end = day + " 23:59:59";
let sql = [];
sql.push("SELECT ");
sql.push(" busi_id,");
sql.push(" COUNT(busi_id) AS times,");
sql.push(" SUM(amt) AS amt,");
sql.push(" SUM(actual_amt) AS actual_amt,");
sql.push(" SUM(deduct_amt) AS deduct_amt,");
sql.push(" SUM(income_tax) AS income_tax,");
sql.push(" SUM(service_tax) AS service_tax,");
sql.push(" SUM(added_value_tax) AS added_value_tax");
sql.push("FROM");
sql.push(" `tbl_order_item` ");
sql.push("WHERE trade_status = '00' ");
sql.push(" AND create_time >= :begin ");
sql.push(" AND create_time <= :end ");
sql.push("GROUP BY busi_id");
let list = await this.settledb.query(sql.join(" "), { replacements: { begin: begin, end: end } });
if (!list || list.length == 0) {
return [];
}
list = list[0, 0] || [];
let ids = [];
for (let item of list) {
item.stat_day = dayNum;
ids.push(item.busi_id);
}
let busiMap = await this.mapBusiByIds(ids);
for (let item of list) {
item.busi_name = busiMap[item.busi_id];
}
return list;
}
}
module.exports = EsettleService;
......@@ -23,6 +23,26 @@ class EtemplatebusiService extends ServiceBase {
return 1;
}
async findOwnerBusiIdMap(template_type) {
template_type = template_type || 0;
let sql = [];
sql.push("SELECT t1.`busi_id`, t3.`owner_id` FROM `c_etemplate_busi` t1");
sql.push("INNER JOIN `c_etemplate` t2 ON t1.`template_id` = t2.`id` AND template_type = :template_type");
sql.push("INNER JOIN c_ecompany t3 ON t2.`ecompany_id` = t3.`id`");
sql.push("WHERE t3.`owner_id` > 0");
let list = await this.customQuery(sql.join(" "), {template_type: template_type});
let result = {};
if (!list || list.length == 0) {
return result;
}
for (let item of list) {
let busiIds = result[item.owner_id] || [];
busiIds.push(item.busi_id);
result[item.owner_id] = busiIds;
}
return result;
}
async suggest(name, ownerIds) {
return await this.dao.suggest(name, ownerIds);
}
......
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