util.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. 'use strict';
  2. const _ = require('lodash');
  3. const moment = require('moment');
  4. const { CrudService } = require('naf-framework-mongoose/lib/service');
  5. const fs = require('fs');
  6. class UtilService extends CrudService {
  7. constructor(ctx) {
  8. super(ctx);
  9. this.mq = this.ctx.mq;
  10. }
  11. async utilMethod(query, body) {
  12. const routes = await this.ctx.model.Route.find();
  13. const dic = await this.ctx.model.Dictionary.find({ categroy: 'xzqh' });
  14. console.log(routes.length);
  15. for (const route of routes) {
  16. const { s_p, s_c, e_p, e_c } = route;
  17. const s_p_o = dic.find(f => f.value == s_p);
  18. if (s_p_o) route.s_p = s_p_o._id;
  19. const s_c_o = dic.find(f => f.value == s_c);
  20. if (s_c_o) route.s_c = s_c_o._id;
  21. const e_p_o = dic.find(f => f.value == e_p);
  22. if (e_p_o) route.e_p = e_p_o._id;
  23. const e_c_o = dic.find(f => f.value == e_c);
  24. if (e_c_o) route.e_c = e_c_o._id;
  25. route.save();
  26. }
  27. }
  28. /**
  29. * 获取字段前置函数
  30. * @param {String} param0 {model字段名
  31. */
  32. async findModel({ model }) {
  33. if (model !== 'all') return await this.getModel(model);
  34. const modelList = this.ctx.model;
  35. const keys = Object.keys(modelList);
  36. const arr = [];
  37. for (const key of keys) {
  38. if (key !== 'Test') {
  39. const zh = this.modelToZh(key);
  40. if (!zh) continue;
  41. arr.push({
  42. zh,
  43. table: key,
  44. value: await this.getModel(key),
  45. });
  46. }
  47. }
  48. return arr;
  49. }
  50. /**
  51. * 获取字段
  52. * @param {String} model 表名
  53. */
  54. async getModel(model) {
  55. const _model = _.capitalize(model);
  56. const data = this.ctx.model[_model].prototype.schema.obj;
  57. const keys = Object.keys(data);
  58. let res = [];
  59. for (const k of keys) {
  60. const field = _.get(data[k], 'field');
  61. if (field && _.get(field, 'label')) {
  62. field.model = k;
  63. res.push(field);
  64. }
  65. }
  66. res = _.orderBy(res, [ 'row' ], [ 'asc' ]);
  67. return res;
  68. }
  69. /**
  70. * 表名换成中文
  71. * @param {String} model 表名
  72. */
  73. modelToZh(model) {
  74. const obj = {
  75. car: '车辆表',
  76. client: '客户/供应商表',
  77. daily: '车辆日常维修表',
  78. dictionary: '字典表',
  79. driver: '司机表',
  80. item: '项目表',
  81. mode: '计费方式表',
  82. order: '订单表',
  83. route: '路线表',
  84. transport: '运输表',
  85. treaty: '合同表',
  86. };
  87. return obj[_.lowerCase(model)];
  88. }
  89. /**
  90. * 将查询条件中模糊查询的标识转换成对应object
  91. * @param {Object} filter 查询条件
  92. */
  93. turnFilter(filter) {
  94. const str = /^%\S*%$/;
  95. const keys = Object.keys(filter);
  96. for (const key of keys) {
  97. const res = key.match(str);
  98. console.log(res);
  99. if (res) {
  100. const newKey = key.slice(1, key.length - 1);
  101. filter[newKey] = new RegExp(filter[key]);
  102. delete filter[key];
  103. }
  104. }
  105. return filter;
  106. }
  107. /**
  108. * 将时间转换成对应查询Object
  109. * @param {Object} filter 查询条件
  110. */
  111. turnDateRangeQuery(filter) {
  112. const keys = Object.keys(filter);
  113. for (const k of keys) {
  114. if (k.includes('@')) {
  115. const karr = k.split('@');
  116. // 因为是针对处理范围日期,所以必须只有,开始时间和结束时间
  117. if (karr.length === 2) {
  118. const type = karr[1];
  119. if (type === 'start') {
  120. filter[karr[0]] = {
  121. ..._.get(filter, karr[0], {}),
  122. $gte: filter[k],
  123. };
  124. } else {
  125. filter[karr[0]] = {
  126. ..._.get(filter, karr[0], {}),
  127. $lte: filter[k],
  128. };
  129. }
  130. delete filter[k];
  131. }
  132. }
  133. }
  134. return filter;
  135. }
  136. /**
  137. * sql的车辆表转换到mongodb
  138. */
  139. async dataTurnCar() {
  140. const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_car.json');
  141. let res = file.toString();
  142. res = JSON.parse(res);
  143. let arr = res.RECORDS;
  144. arr = arr.map(i => {
  145. const { car_no } = i;
  146. const obj = {
  147. car_no,
  148. onwer: _.get(i, 'car_onwer'),
  149. id_name: _.get(i, 'it'),
  150. id_number: _.get(i, 'it_member'),
  151. organ: _.get(i, 'ro'),
  152. r_date: moment(_.get(i, 'r_time')).format('YYYY-MM-DD'),
  153. c_type: _.get(i, 'car_type'),
  154. c_doc_no: _.get(i, 'cc_no'),
  155. c_weight: _.get(i, 'cc_weight'),
  156. c_scrap_date: moment(_.get(i, 'cc_scrap_time')).format('YYYY-MM-DD'),
  157. c_examine_date: moment(_.get(i, 'cc_examine_time')).format('YYYY-MM-DD'),
  158. r_tran_no: _.get(i, 'rc_no'),
  159. r_examine_date: moment(_.get(i, 'rc_examine_time')).format('YYYY-MM-DD'),
  160. buy_car_taxes_no: _.get(i, 'bcc_no'),
  161. f_company: _.get(i, 'fi_name'),
  162. f_tel: _.get(i, 'fi_tel'),
  163. f_no: _.get(i, 'fi_no'),
  164. f_cost: _.get(i, 'fi_cost'),
  165. f_start_date: moment(_.get(i, 'fi_start_time')).format('YYYY-MM-DD'),
  166. f_end_date: moment(_.get(i, 'fi_end_time')).format('YYYY-MM-DD'),
  167. f_solve: _.get(i, 'fi_dw'),
  168. f_car_taxes: _.get(i, 'fi_vt'),
  169. f_sign_date: moment(_.get(i, 'fi_sign_time')).format('YYYY-MM-DD'),
  170. b_company: _.get(i, 'bi_name'),
  171. b_tel: _.get(i, 'bi_tel'),
  172. b_no: _.get(i, 'bi_no'),
  173. b_start_date: _.get(i, 'bi_start_time') ? moment(_.get(i, 'bi_start_time')).format('YYYY-MM-DD') : undefined,
  174. b_end_date: _.get(i, 'bi_start_time') ? moment(_.get(i, 'bi_end_time')).format('YYYY-MM-DD') : undefined,
  175. b_solve: _.get(i, 'bi_dw'),
  176. b_sign_date: _.get(i, 'bi_start_time') ? moment(_.get(i, 'bi_sign_time')).format('YYYY-MM-DD') : undefined,
  177. };
  178. return obj;
  179. });
  180. // await this.ctx.model.Car.insertMany(arr);
  181. }
  182. /**
  183. * sql的客户/供应商表初步转换
  184. */
  185. async dataTurnClientFirst() {
  186. const file = await fs.readFileSync(
  187. 'C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client.json'
  188. );
  189. const users = await this.ctx.service.util.httpUtil.cget(
  190. '/user',
  191. 'userAuth',
  192. { _tenant: 'zhwl' }
  193. );
  194. let res = file.toString();
  195. res = JSON.parse(res);
  196. let arr = res.RECORDS;
  197. arr = arr.map(i => {
  198. const { name, address, taxes_no, type } = i;
  199. const obj = {
  200. name,
  201. address,
  202. taxes_no,
  203. type: type === '0' ? '客户' : '供应商',
  204. legal: _.get(i, 'legal_person'),
  205. mobile: _.get(i, 'tel'),
  206. account_bank: _.get(i, 'bank'),
  207. account: _.get(i, 'card_account'),
  208. id: _.get(i, 'id'),
  209. };
  210. if (_.get(i, 'login_id')) {
  211. let lid = _.get(i, 'login_id');
  212. if (lid === 'lsyw') lid = 'liushuang';
  213. const user = users.find(f => f.login_id === lid);
  214. if (user) {
  215. obj.owner = user.id;
  216. return obj;
  217. }
  218. }
  219. });
  220. await this.ctx.model.IClient.insertMany(_.compact(arr));
  221. }
  222. /**
  223. * sql的合同表初步转换
  224. */
  225. async dataTurnTreatyFirst() {
  226. const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client_pact.json');
  227. const iclients = await this.ctx.model.IClient.find();
  228. let res = file.toString();
  229. res = JSON.parse(res);
  230. let arr = res.RECORDS;
  231. arr = arr.map(i => {
  232. const { jf, yf, id, cus_id } = i;
  233. const obj = {
  234. number: _.get(i, 'pact_no'),
  235. period: _.get(i, 'cycle'),
  236. settle_up: _.get(i, 'js_type'),
  237. settle_up_period: _.get(i, 'js_cycle'),
  238. jf, yf, id,
  239. };
  240. if (cus_id) {
  241. const client = iclients.find(f => f.id == cus_id);
  242. if (client) {
  243. obj.client = client._id;
  244. obj.owner = client.owner;
  245. return obj;
  246. }
  247. }
  248. });
  249. arr = _.compact(arr);
  250. await this.ctx.model.ITreaty.insertMany(arr);
  251. }
  252. /**
  253. * sql的项目表初步转换
  254. */
  255. async dataTurnItemFirst() {
  256. const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client_pact_item.json');
  257. let res = file.toString();
  258. res = JSON.parse(res);
  259. const iitems = res.RECORDS; // 对应itemconst arr = [];
  260. const treatys = await this.ctx.model.ITreaty.find();
  261. const arr = [];
  262. for (const treaty of treatys) {
  263. const { _id, id, owner } = treaty;
  264. let arritems = iitems.filter(f => f.pact_id == id && f.cess && f.cess !== '');
  265. arritems = arritems.map(i => {
  266. const obj = {
  267. id: _.get(i, 'id'),
  268. treaty: _id,
  269. name: _.get(i, 'item_name'),
  270. taxes: _.get(i, 'cess'),
  271. owner,
  272. };
  273. arr.push(obj);
  274. });
  275. }
  276. await this.ctx.model.IItem.insertMany(arr);
  277. }
  278. /**
  279. * sql的线路表初步转换
  280. */
  281. async dataTurnRouteFirst() {
  282. const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_dly_way.json');
  283. let res = file.toString();
  284. res = JSON.parse(res);
  285. const iway = res.RECORDS; // 对应route
  286. const iitems = await this.ctx.model.IItem.find();
  287. const dic = await this.ctx.model.Dictionary.find({ categroy: 'xzqh' });
  288. const arr = [];
  289. for (const item of iitems) {
  290. const { _id, id, owner } = item;
  291. const ways = iway.filter(f => f.item_id == id);
  292. for (const way of ways) {
  293. const obj = { item: _id, name: way.name, owner, id: way.id, start: way.start_site, end: way.end_site };
  294. const s_p_o = dic.find(f => f.label.includes(way.start_province) && f.pid === '5fcb00a6f6ed0b0b70df95cf');
  295. const s_c_o = dic.find(f => f.label.includes(way.start_city) && f.pid !== '5fcb00a6f6ed0b0b70df95cf');
  296. const e_p_o = dic.find(f => f.label.includes(way.end_province) && f.pid === '5fcb00a6f6ed0b0b70df95cf');
  297. const e_c_o = dic.find(f => f.label.includes(way.end_city) && f.pid !== '5fcb00a6f6ed0b0b70df95cf');
  298. if (s_p_o) obj.s_p = s_p_o.value;
  299. if (s_c_o) obj.s_c = s_c_o.value;
  300. if (e_p_o) obj.e_p = e_p_o.value;
  301. if (e_c_o) obj.e_c = e_c_o.value;
  302. if (obj.s_p && obj.s_c && obj.e_p && obj.e_c) arr.push(obj);
  303. }
  304. }
  305. await this.ctx.model.IRoute.insertMany(arr);
  306. }
  307. /**
  308. * sql的付费方式表转换(没有初步,直接成型,下面需要做的是将之前的初步转换成成品)
  309. */
  310. async dataTurnModeFirst() {
  311. const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client_pact_item_way_type.json');
  312. let res = file.toString();
  313. res = JSON.parse(res);
  314. const itype = res.RECORDS; // 对应rmode
  315. const routes = await this.ctx.model.IRoute.find();
  316. const arr = [];
  317. for (const route of routes) {
  318. const { _id, id, owner } = route;
  319. const modes = itype.filter(f => f.dly_way_id == id);
  320. for (const mode of modes) {
  321. const obj = { name: mode.type_name, route: _id, price: mode.price, owner, status: mode.status };
  322. if (mode.send_type) obj.send_type = mode.send_type == '1' ? '整车' : '零担';
  323. if (mode.count_type) obj.computed_type = mode.count_type == '1' ? '按重量' : '按体积';
  324. if (mode.is_lf) obj.is_lf = mode.is_lf == '0';
  325. arr.push(obj);
  326. }
  327. }
  328. await this.ctx.model.Mode.insertMany(arr);
  329. }
  330. }
  331. module.exports = UtilService;