'use strict'; const _ = require('lodash'); const moment = require('moment'); const { CrudService } = require('naf-framework-mongoose/lib/service'); const fs = require('fs'); class UtilService extends CrudService { constructor(ctx) { super(ctx); this.mq = this.ctx.mq; } async utilMethod(query, body) { const routes = await this.ctx.model.Route.find(); const dic = await this.ctx.model.Dictionary.find({ categroy: 'xzqh' }); console.log(routes.length); for (const route of routes) { const { s_p, s_c, e_p, e_c } = route; const s_p_o = dic.find(f => f.value == s_p); if (s_p_o) route.s_p = s_p_o._id; const s_c_o = dic.find(f => f.value == s_c); if (s_c_o) route.s_c = s_c_o._id; const e_p_o = dic.find(f => f.value == e_p); if (e_p_o) route.e_p = e_p_o._id; const e_c_o = dic.find(f => f.value == e_c); if (e_c_o) route.e_c = e_c_o._id; route.save(); } } /** * 获取字段前置函数 * @param {String} param0 {model字段名 */ async findModel({ model }) { if (model !== 'all') return await this.getModel(model); const modelList = this.ctx.model; const keys = Object.keys(modelList); const arr = []; for (const key of keys) { if (key !== 'Test') { const zh = this.modelToZh(key); if (!zh) continue; arr.push({ zh, table: key, value: await this.getModel(key), }); } } return arr; } /** * 获取字段 * @param {String} model 表名 */ async getModel(model) { const _model = _.capitalize(model); const data = this.ctx.model[_model].prototype.schema.obj; const keys = Object.keys(data); let res = []; for (const k of keys) { const field = _.get(data[k], 'field'); if (field && _.get(field, 'label')) { field.model = k; res.push(field); } } res = _.orderBy(res, [ 'row' ], [ 'asc' ]); return res; } /** * 表名换成中文 * @param {String} model 表名 */ modelToZh(model) { const obj = { car: '车辆表', client: '客户/供应商表', daily: '车辆日常维修表', dictionary: '字典表', driver: '司机表', item: '项目表', mode: '计费方式表', order: '订单表', route: '路线表', transport: '运输表', treaty: '合同表', }; return obj[_.lowerCase(model)]; } /** * 将查询条件中模糊查询的标识转换成对应object * @param {Object} filter 查询条件 */ turnFilter(filter) { const str = /^%\w*%$/; const keys = Object.keys(filter); for (const key of keys) { const res = key.match(str); if (res) { const newKey = key.slice(1, key.length - 1); filter[newKey] = new RegExp(filter[key]); delete filter[key]; } } return filter; } /** * 将时间转换成对应查询Object * @param {Object} filter 查询条件 */ turnDateRangeQuery(filter) { const keys = Object.keys(filter); for (const k of keys) { if (k.includes('@')) { const karr = k.split('@'); // 因为是针对处理范围日期,所以必须只有,开始时间和结束时间 if (karr.length === 2) { const type = karr[1]; if (type === 'start') { filter[karr[0]] = { ..._.get(filter, karr[0], {}), $gte: filter[k], }; } else { filter[karr[0]] = { ..._.get(filter, karr[0], {}), $lte: filter[k], }; } delete filter[k]; } } } return filter; } /** * sql的车辆表转换到mongodb */ async dataTurnCar() { const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_car.json'); let res = file.toString(); res = JSON.parse(res); let arr = res.RECORDS; arr = arr.map(i => { const { car_no } = i; const obj = { car_no, onwer: _.get(i, 'car_onwer'), id_name: _.get(i, 'it'), id_number: _.get(i, 'it_member'), organ: _.get(i, 'ro'), r_date: moment(_.get(i, 'r_time')).format('YYYY-MM-DD'), c_type: _.get(i, 'car_type'), c_doc_no: _.get(i, 'cc_no'), c_weight: _.get(i, 'cc_weight'), c_scrap_date: moment(_.get(i, 'cc_scrap_time')).format('YYYY-MM-DD'), c_examine_date: moment(_.get(i, 'cc_examine_time')).format('YYYY-MM-DD'), r_tran_no: _.get(i, 'rc_no'), r_examine_date: moment(_.get(i, 'rc_examine_time')).format('YYYY-MM-DD'), buy_car_taxes_no: _.get(i, 'bcc_no'), f_company: _.get(i, 'fi_name'), f_tel: _.get(i, 'fi_tel'), f_no: _.get(i, 'fi_no'), f_cost: _.get(i, 'fi_cost'), f_start_date: moment(_.get(i, 'fi_start_time')).format('YYYY-MM-DD'), f_end_date: moment(_.get(i, 'fi_end_time')).format('YYYY-MM-DD'), f_solve: _.get(i, 'fi_dw'), f_car_taxes: _.get(i, 'fi_vt'), f_sign_date: moment(_.get(i, 'fi_sign_time')).format('YYYY-MM-DD'), b_company: _.get(i, 'bi_name'), b_tel: _.get(i, 'bi_tel'), b_no: _.get(i, 'bi_no'), b_start_date: _.get(i, 'bi_start_time') ? moment(_.get(i, 'bi_start_time')).format('YYYY-MM-DD') : undefined, b_end_date: _.get(i, 'bi_start_time') ? moment(_.get(i, 'bi_end_time')).format('YYYY-MM-DD') : undefined, b_solve: _.get(i, 'bi_dw'), b_sign_date: _.get(i, 'bi_start_time') ? moment(_.get(i, 'bi_sign_time')).format('YYYY-MM-DD') : undefined, }; return obj; }); // await this.ctx.model.Car.insertMany(arr); } /** * sql的客户/供应商表初步转换 */ async dataTurnClientFirst() { const file = await fs.readFileSync( 'C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client.json' ); const users = await this.ctx.service.util.httpUtil.cget( '/user', 'userAuth', { _tenant: 'zhwl' } ); let res = file.toString(); res = JSON.parse(res); let arr = res.RECORDS; arr = arr.map(i => { const { name, address, taxes_no, type } = i; const obj = { name, address, taxes_no, type: type === '0' ? '客户' : '供应商', legal: _.get(i, 'legal_person'), mobile: _.get(i, 'tel'), account_bank: _.get(i, 'bank'), account: _.get(i, 'card_account'), id: _.get(i, 'id'), }; if (_.get(i, 'login_id')) { let lid = _.get(i, 'login_id'); if (lid === 'lsyw') lid = 'liushuang'; const user = users.find(f => f.login_id === lid); if (user) { obj.owner = user.id; return obj; } } }); await this.ctx.model.IClient.insertMany(_.compact(arr)); } /** * sql的合同表初步转换 */ async dataTurnTreatyFirst() { const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client_pact.json'); const iclients = await this.ctx.model.IClient.find(); let res = file.toString(); res = JSON.parse(res); let arr = res.RECORDS; arr = arr.map(i => { const { jf, yf, id, cus_id } = i; const obj = { number: _.get(i, 'pact_no'), period: _.get(i, 'cycle'), settle_up: _.get(i, 'js_type'), settle_up_period: _.get(i, 'js_cycle'), jf, yf, id, }; if (cus_id) { const client = iclients.find(f => f.id == cus_id); if (client) { obj.client = client._id; obj.owner = client.owner; return obj; } } }); arr = _.compact(arr); await this.ctx.model.ITreaty.insertMany(arr); } /** * sql的项目表初步转换 */ async dataTurnItemFirst() { const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client_pact_item.json'); let res = file.toString(); res = JSON.parse(res); const iitems = res.RECORDS; // 对应itemconst arr = []; const treatys = await this.ctx.model.ITreaty.find(); const arr = []; for (const treaty of treatys) { const { _id, id, owner } = treaty; let arritems = iitems.filter(f => f.pact_id == id && f.cess && f.cess !== ''); arritems = arritems.map(i => { const obj = { id: _.get(i, 'id'), treaty: _id, name: _.get(i, 'item_name'), taxes: _.get(i, 'cess'), owner, }; arr.push(obj); }); } await this.ctx.model.IItem.insertMany(arr); } /** * sql的线路表初步转换 */ async dataTurnRouteFirst() { const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_dly_way.json'); let res = file.toString(); res = JSON.parse(res); const iway = res.RECORDS; // 对应route const iitems = await this.ctx.model.IItem.find(); const dic = await this.ctx.model.Dictionary.find({ categroy: 'xzqh' }); const arr = []; for (const item of iitems) { const { _id, id, owner } = item; const ways = iway.filter(f => f.item_id == id); for (const way of ways) { const obj = { item: _id, name: way.name, owner, id: way.id, start: way.start_site, end: way.end_site }; const s_p_o = dic.find(f => f.label.includes(way.start_province) && f.pid === '5fcb00a6f6ed0b0b70df95cf'); const s_c_o = dic.find(f => f.label.includes(way.start_city) && f.pid !== '5fcb00a6f6ed0b0b70df95cf'); const e_p_o = dic.find(f => f.label.includes(way.end_province) && f.pid === '5fcb00a6f6ed0b0b70df95cf'); const e_c_o = dic.find(f => f.label.includes(way.end_city) && f.pid !== '5fcb00a6f6ed0b0b70df95cf'); if (s_p_o) obj.s_p = s_p_o.value; if (s_c_o) obj.s_c = s_c_o.value; if (e_p_o) obj.e_p = e_p_o.value; if (e_c_o) obj.e_c = e_c_o.value; if (obj.s_p && obj.s_c && obj.e_p && obj.e_c) arr.push(obj); } } await this.ctx.model.IRoute.insertMany(arr); } /** * sql的付费方式表转换(没有初步,直接成型,下面需要做的是将之前的初步转换成成品) */ async dataTurnModeFirst() { const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client_pact_item_way_type.json'); let res = file.toString(); res = JSON.parse(res); const itype = res.RECORDS; // 对应rmode const routes = await this.ctx.model.IRoute.find(); const arr = []; for (const route of routes) { const { _id, id, owner } = route; const modes = itype.filter(f => f.dly_way_id == id); for (const mode of modes) { const obj = { name: mode.type_name, route: _id, price: mode.price, owner, status: mode.status }; if (mode.send_type) obj.send_type = mode.send_type == '1' ? '整车' : '零担'; if (mode.count_type) obj.computed_type = mode.count_type == '1' ? '按重量' : '按体积'; if (mode.is_lf) obj.is_lf = mode.is_lf == '0'; arr.push(obj); } } await this.ctx.model.Mode.insertMany(arr); } } module.exports = UtilService;