123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const SpreadConst = require('../app/const/spread');
- const BaseUtil = require('./baseUtils');
- const querySql = BaseUtil.querySql;
- const generateSpreadSetting = function(colSet, emptyBase, BaseSetCol, BaseSpreadColSetting, spreadType = 'bills') {
- const spreadSetting = JSON.parse(JSON.stringify(emptyBase));
- for (const col of colSet) {
- if (!col.valid) continue;
- const dc = BaseSetCol.find(x => { return x.key === col.key; });
- if (!dc) continue;
- const orgBaseCols = BaseSpreadColSetting[col.key];
- if (!orgBaseCols) continue;
- const baseCols = JSON.parse(JSON.stringify(orgBaseCols));
- if (dc.fixed.indexOf('alias') < 0 && col.alias) {
- if (baseCols.length === 1) {
- if (baseCols[0].aliasFormat) {
- baseCols[0].title = baseCols[0].aliasFormat.replace('{%s}', col.alias);
- delete baseCols[0].aliasFormat;
- } else {
- baseCols[0].title = col.alias;
- }
- } else {
- baseCols.forEach(x => {
- if (x.aliasFormat) {
- x.title = x.aliasFormat.replace('{%s}', col.alias);
- delete x.aliasFormat;
- }
- });
- }
- }
- spreadSetting.cols.push(...baseCols);
- }
- return spreadSetting;
- };
- const updateSpreadWithSpec = function(spreadSetting, specSetting) {
- if (!specSetting) return;
- for (const s of specSetting) {
- for (const c of spreadSetting.cols) {
- if (s.condition.value.indexOf(c[s.condition.key]) >= 0) {
- BaseUtil._.assignIn(c, s.update);
- }
- }
- }
- };
- const generateRelaSpread = function (colSetType, colSet) {
- const baseSetCol = SpreadConst.BaseSetCol[colSetType];
- const baseSpreadColSetting = SpreadConst.BaseSpreadColSetting[colSetType];
- const billsSpread = generateSpreadSetting(colSet, SpreadConst.EmptySpreadSetting[colSetType].bills, baseSetCol, baseSpreadColSetting.bills);
- const posSpread = generateSpreadSetting(colSet, SpreadConst.EmptySpreadSetting[colSetType].pos, baseSetCol, baseSpreadColSetting.pos, 'pos');
- const spreadSpec = SpreadConst.SpreadSpec[colSetType];
- if (spreadSpec) {
- updateSpreadWithSpec(billsSpread, spreadSpec.bills);
- updateSpreadWithSpec(posSpread, spreadSpec.pos);
- }
- return [billsSpread, posSpread];
- };
- const InsertSql = function (tableName, data) {
- const column = [], query = [], value = [];
- for (const prop in data) {
- column.push(prop);
- query.push('?');
- value.push(data[prop]);
- }
- return [`INSERT INTO ${tableName} (${column.join(',')}) VALUES (${query.join(',')})`, value]
- };
- const initProjectSpread = async function (project) {
- const exist = await querySql('Select * From zh_project_spread WHERE id = ?', [project.id]);
- if (exist.length > 0) {
- await querySql('DELETE FROM zh_project_spread WHERE id = ?', [project.id]);
- }
- for (const SpreadTemplate of SpreadConst.ProjectSpreadTemplate) {
- const DefaultSet = JSON.parse(JSON.stringify(SpreadTemplate.template));
- const sjsRela = project.sjs_rela ? JSON.parse(project.sjs_rela) : null;
- if (sjsRela) {
- for (const prop in DefaultSet) {
- const template = DefaultSet[prop];
- for (const lc of sjsRela.ledgerCol) {
- const col = template.find(x => { return x.key === lc.field; });
- if (lc.show) col.valid = 1;
- if (lc.alias) col.alias = lc.alias;
- if (lc.pos === 'name') {
- const lColIndex = template.findIndex(x => { return x.key === lc.field; });
- const lCol = template[lColIndex];
- const preColIndex = template.findIndex(x => { return x.key === lc.pos; });
- const preCol = template[preColIndex];
- if (preCol && preColIndex !== lColIndex - 1) {
- template.splice(lColIndex, 1);
- template.splice(preColIndex + 1, 0, lCol);
- }
- }
- }
- }
- }
- [DefaultSet.tz_ledger_bills_spread, DefaultSet.tz_ledger_pos_spread] = generateRelaSpread('tz_ledger_set', DefaultSet.tz_ledger_set);
- [DefaultSet.tz_stage_bills_spread, DefaultSet.tz_stage_pos_spread] = generateRelaSpread('tz_stage_set', DefaultSet.tz_stage_set);
- [DefaultSet.gcl_ledger_bills_spread, DefaultSet.gcl_ledger_pos_spread] = generateRelaSpread('gcl_ledger_set', DefaultSet.gcl_ledger_set);
- [DefaultSet.gcl_stage_bills_spread, DefaultSet.gcl_stage_pos_spread] = generateRelaSpread('gcl_stage_set', DefaultSet.gcl_stage_set);
- for (const prop in DefaultSet) {
- DefaultSet[prop] = JSON.stringify(DefaultSet[prop]);
- }
- DefaultSet.pid = project.id;
- DefaultSet.code = SpreadTemplate.code;
- DefaultSet.name = SpreadTemplate.name;
- DefaultSet.is_default = SpreadTemplate.isDefault || 0;
- const [sql, sqlParams] = InsertSql('zh_project_spread', DefaultSet);
- await querySql(sql, sqlParams);
- };
- };
- const doComplete = async function(projectCode) {
- try {
- const whereSql = projectCode ? ` WHERE code = "${projectCode}"` : '';
- const project = await querySql(`Select * From zh_project ${whereSql}`);
- for (const p of project) {
- console.log(`Update Project ${p.code}(${p.id}):`);
- await initProjectSpread(p);
- }
- } catch (err) {
- console.log(err);
- }
- BaseUtil.closePool();
- };
- // doComplete(process.argv[3]);
- // 更新所有工程量清单
- const updateProjectSpread = async function (project) {
- const projectSpread = await querySql('Select * From zh_project_spread WHERE pid = ?', [project.id]);
- for (const spread of projectSpread) {
- if (!spread.gcl_stage_set) continue;
- const gcl_stage_set = JSON.parse(spread.gcl_stage_set);
- const tz_calc = gcl_stage_set.find(x => { return x.key === 'tz_calc'});
- if (!tz_calc) continue;
- tz_calc.pos_valid = 0;
- await querySql(`UPDATE zh_project_spread SET gcl_stage_set = ? WHERE id = ?`, [JSON.stringify(gcl_stage_set), spread.id]);
- }
- };
- const doComplete2 = async function (projectCode) {
- try {
- const whereSql = projectCode ? ` WHERE code = "${projectCode}"` : '';
- const project = await querySql(`Select * From zh_project ${whereSql}`);
- for (const p of project) {
- console.log(`Update Project ${p.code}(${p.id}):`);
- await updateProjectSpread(p);
- }
- } catch (err) {
- console.log(err);
- }
- BaseUtil.closePool();
- };
- doComplete2(process.argv[3]);
|