util.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 = /^%\w*%$/;
  95. const keys = Object.keys(filter);
  96. for (const key of keys) {
  97. const res = key.match(str);
  98. if (res) {
  99. const newKey = key.slice(1, key.length - 1);
  100. filter[newKey] = new RegExp(filter[key]);
  101. delete filter[key];
  102. }
  103. }
  104. return filter;
  105. }
  106. /**
  107. * 将时间转换成对应查询Object
  108. * @param {Object} filter 查询条件
  109. */
  110. turnDateRangeQuery(filter) {
  111. const keys = Object.keys(filter);
  112. for (const k of keys) {
  113. if (k.includes('@')) {
  114. const karr = k.split('@');
  115. // 因为是针对处理范围日期,所以必须只有,开始时间和结束时间
  116. if (karr.length === 2) {
  117. const type = karr[1];
  118. if (type === 'start') {
  119. filter[karr[0]] = {
  120. ..._.get(filter, karr[0], {}),
  121. $gte: filter[k],
  122. };
  123. } else {
  124. filter[karr[0]] = {
  125. ..._.get(filter, karr[0], {}),
  126. $lte: filter[k],
  127. };
  128. }
  129. delete filter[k];
  130. }
  131. }
  132. }
  133. return filter;
  134. }
  135. /**
  136. * sql的车辆表转换到mongodb
  137. */
  138. async dataTurnCar() {
  139. const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_car.json');
  140. let res = file.toString();
  141. res = JSON.parse(res);
  142. let arr = res.RECORDS;
  143. arr = arr.map(i => {
  144. const { car_no } = i;
  145. const obj = {
  146. car_no,
  147. onwer: _.get(i, 'car_onwer'),
  148. id_name: _.get(i, 'it'),
  149. id_number: _.get(i, 'it_member'),
  150. organ: _.get(i, 'ro'),
  151. r_date: moment(_.get(i, 'r_time')).format('YYYY-MM-DD'),
  152. c_type: _.get(i, 'car_type'),
  153. c_doc_no: _.get(i, 'cc_no'),
  154. c_weight: _.get(i, 'cc_weight'),
  155. c_scrap_date: moment(_.get(i, 'cc_scrap_time')).format('YYYY-MM-DD'),
  156. c_examine_date: moment(_.get(i, 'cc_examine_time')).format('YYYY-MM-DD'),
  157. r_tran_no: _.get(i, 'rc_no'),
  158. r_examine_date: moment(_.get(i, 'rc_examine_time')).format('YYYY-MM-DD'),
  159. buy_car_taxes_no: _.get(i, 'bcc_no'),
  160. f_company: _.get(i, 'fi_name'),
  161. f_tel: _.get(i, 'fi_tel'),
  162. f_no: _.get(i, 'fi_no'),
  163. f_cost: _.get(i, 'fi_cost'),
  164. f_start_date: moment(_.get(i, 'fi_start_time')).format('YYYY-MM-DD'),
  165. f_end_date: moment(_.get(i, 'fi_end_time')).format('YYYY-MM-DD'),
  166. f_solve: _.get(i, 'fi_dw'),
  167. f_car_taxes: _.get(i, 'fi_vt'),
  168. f_sign_date: moment(_.get(i, 'fi_sign_time')).format('YYYY-MM-DD'),
  169. b_company: _.get(i, 'bi_name'),
  170. b_tel: _.get(i, 'bi_tel'),
  171. b_no: _.get(i, 'bi_no'),
  172. b_start_date: _.get(i, 'bi_start_time') ? moment(_.get(i, 'bi_start_time')).format('YYYY-MM-DD') : undefined,
  173. b_end_date: _.get(i, 'bi_start_time') ? moment(_.get(i, 'bi_end_time')).format('YYYY-MM-DD') : undefined,
  174. b_solve: _.get(i, 'bi_dw'),
  175. b_sign_date: _.get(i, 'bi_start_time') ? moment(_.get(i, 'bi_sign_time')).format('YYYY-MM-DD') : undefined,
  176. };
  177. return obj;
  178. });
  179. // await this.ctx.model.Car.insertMany(arr);
  180. }
  181. /**
  182. * sql的客户/供应商表初步转换
  183. */
  184. async dataTurnClientFirst() {
  185. const file = await fs.readFileSync(
  186. 'C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client.json'
  187. );
  188. const users = await this.ctx.service.util.httpUtil.cget(
  189. '/user',
  190. 'userAuth',
  191. { _tenant: 'zhwl' }
  192. );
  193. let res = file.toString();
  194. res = JSON.parse(res);
  195. let arr = res.RECORDS;
  196. arr = arr.map(i => {
  197. const { name, address, taxes_no, type } = i;
  198. const obj = {
  199. name,
  200. address,
  201. taxes_no,
  202. type: type === '0' ? '客户' : '供应商',
  203. legal: _.get(i, 'legal_person'),
  204. mobile: _.get(i, 'tel'),
  205. account_bank: _.get(i, 'bank'),
  206. account: _.get(i, 'card_account'),
  207. id: _.get(i, 'id'),
  208. };
  209. if (_.get(i, 'login_id')) {
  210. let lid = _.get(i, 'login_id');
  211. if (lid === 'lsyw') lid = 'liushuang';
  212. const user = users.find(f => f.login_id === lid);
  213. if (user) {
  214. obj.owner = user.id;
  215. return obj;
  216. }
  217. }
  218. });
  219. await this.ctx.model.IClient.insertMany(_.compact(arr));
  220. }
  221. /**
  222. * sql的合同表初步转换
  223. */
  224. async dataTurnTreatyFirst() {
  225. const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client_pact.json');
  226. const iclients = await this.ctx.model.IClient.find();
  227. let res = file.toString();
  228. res = JSON.parse(res);
  229. let arr = res.RECORDS;
  230. arr = arr.map(i => {
  231. const { jf, yf, id, cus_id } = i;
  232. const obj = {
  233. number: _.get(i, 'pact_no'),
  234. period: _.get(i, 'cycle'),
  235. settle_up: _.get(i, 'js_type'),
  236. settle_up_period: _.get(i, 'js_cycle'),
  237. jf, yf, id,
  238. };
  239. if (cus_id) {
  240. const client = iclients.find(f => f.id == cus_id);
  241. if (client) {
  242. obj.client = client._id;
  243. obj.owner = client.owner;
  244. return obj;
  245. }
  246. }
  247. });
  248. arr = _.compact(arr);
  249. await this.ctx.model.ITreaty.insertMany(arr);
  250. }
  251. /**
  252. * sql的项目表初步转换
  253. */
  254. async dataTurnItemFirst() {
  255. const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client_pact_item.json');
  256. let res = file.toString();
  257. res = JSON.parse(res);
  258. const iitems = res.RECORDS; // 对应itemconst arr = [];
  259. const treatys = await this.ctx.model.ITreaty.find();
  260. const arr = [];
  261. for (const treaty of treatys) {
  262. const { _id, id, owner } = treaty;
  263. let arritems = iitems.filter(f => f.pact_id == id && f.cess && f.cess !== '');
  264. arritems = arritems.map(i => {
  265. const obj = {
  266. id: _.get(i, 'id'),
  267. treaty: _id,
  268. name: _.get(i, 'item_name'),
  269. taxes: _.get(i, 'cess'),
  270. owner,
  271. };
  272. arr.push(obj);
  273. });
  274. }
  275. await this.ctx.model.IItem.insertMany(arr);
  276. }
  277. /**
  278. * sql的线路表初步转换
  279. */
  280. async dataTurnRouteFirst() {
  281. const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_dly_way.json');
  282. let res = file.toString();
  283. res = JSON.parse(res);
  284. const iway = res.RECORDS; // 对应route
  285. const iitems = await this.ctx.model.IItem.find();
  286. const dic = await this.ctx.model.Dictionary.find({ categroy: 'xzqh' });
  287. const arr = [];
  288. for (const item of iitems) {
  289. const { _id, id, owner } = item;
  290. const ways = iway.filter(f => f.item_id == id);
  291. for (const way of ways) {
  292. const obj = { item: _id, name: way.name, owner, id: way.id, start: way.start_site, end: way.end_site };
  293. const s_p_o = dic.find(f => f.label.includes(way.start_province) && f.pid === '5fcb00a6f6ed0b0b70df95cf');
  294. const s_c_o = dic.find(f => f.label.includes(way.start_city) && f.pid !== '5fcb00a6f6ed0b0b70df95cf');
  295. const e_p_o = dic.find(f => f.label.includes(way.end_province) && f.pid === '5fcb00a6f6ed0b0b70df95cf');
  296. const e_c_o = dic.find(f => f.label.includes(way.end_city) && f.pid !== '5fcb00a6f6ed0b0b70df95cf');
  297. if (s_p_o) obj.s_p = s_p_o.value;
  298. if (s_c_o) obj.s_c = s_c_o.value;
  299. if (e_p_o) obj.e_p = e_p_o.value;
  300. if (e_c_o) obj.e_c = e_c_o.value;
  301. if (obj.s_p && obj.s_c && obj.e_p && obj.e_c) arr.push(obj);
  302. }
  303. }
  304. await this.ctx.model.IRoute.insertMany(arr);
  305. }
  306. /**
  307. * sql的付费方式表转换(没有初步,直接成型,下面需要做的是将之前的初步转换成成品)
  308. */
  309. async dataTurnModeFirst() {
  310. const file = await fs.readFileSync('C:\\Users\\liuruifeng\\Desktop\\暂存文件夹\\zhwl-turn\\zhwl_client_pact_item_way_type.json');
  311. let res = file.toString();
  312. res = JSON.parse(res);
  313. const itype = res.RECORDS; // 对应rmode
  314. const routes = await this.ctx.model.IRoute.find();
  315. const arr = [];
  316. for (const route of routes) {
  317. const { _id, id, owner } = route;
  318. const modes = itype.filter(f => f.dly_way_id == id);
  319. for (const mode of modes) {
  320. const obj = { name: mode.type_name, route: _id, price: mode.price, owner, status: mode.status };
  321. if (mode.send_type) obj.send_type = mode.send_type == '1' ? '整车' : '零担';
  322. if (mode.count_type) obj.computed_type = mode.count_type == '1' ? '按重量' : '按体积';
  323. if (mode.is_lf) obj.is_lf = mode.is_lf == '0';
  324. arr.push(obj);
  325. }
  326. }
  327. await this.ctx.model.Mode.insertMany(arr);
  328. }
  329. }
  330. module.exports = UtilService;