123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- '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;
|