1+ 'use strict' ;
2+ import { createConnections , getConnection , getRepository } from "typeorm" ;
3+ import { Cap } from './entity/jlc/Cap' ;
4+ import path from 'path' ;
5+ import { app , ipcMain } from 'electron' ;
6+ import log from 'electron-log' ;
7+
8+ export default class Db {
9+ constructor ( win ) {
10+ this . win = win ;
11+ this . connected = false ;
12+ }
13+
14+ parseJlc ( val , g ) {
15+
16+ var a = {
17+ pn : '' ,
18+ vendor : '' ,
19+ vendorPn : '' ,
20+ vendorUrl : '' ,
21+ value : '' ,
22+ prices : [ ] ,
23+ imgs : [ ] ,
24+ symbolUrl : '' ,
25+ footprint : '' ,
26+ fpUrl : '' ,
27+ desc : '' ,
28+ datasheet : '' ,
29+ g : 'cap' ,
30+ type : '' ,
31+ }
32+ if ( val != null ) {
33+ let svgs = [ ] ;
34+ if ( val . svgs != null )
35+ svgs = val . svgs . split ( ',' ) ;
36+ a . g = g
37+ a . pn = val . pn ;
38+ if ( val . imgs != null )
39+ a . imgs = val . imgs . split ( ',' ) ;
40+ a . vendorPn = val . jlcpn ;
41+ a . vendorUrl = val . jlcurl ;
42+ a . vendor = 'jlc' ;
43+ a . desc = val . desc ;
44+ a . footprint = val . footprint ;
45+ if ( svgs . length > 0 ) {
46+ a . symbolUrl = svgs [ 0 ] ;
47+ }
48+ if ( svgs . length > 1 ) {
49+ a . fpUrl = svgs [ 1 ] ;
50+ }
51+ if ( val . datasheet != null )
52+ a . datasheet = val . datasheet ;
53+ a . value = val . value ;
54+ if ( val . type_ != null )
55+ a . type = val . type_ ;
56+ if ( val . prices != null ) {
57+ const prices = val . prices . split ( ',' ) ;
58+ a . prices = [ ]
59+ for ( const i in prices ) {
60+ const m = prices [ i ] . split ( ':' ) ;
61+ a . prices . push ( {
62+ num : m [ 0 ] ,
63+ price : m [ 1 ]
64+ } ) ;
65+ }
66+ }
67+ }
68+ log . info ( a )
69+ this . win . webContents . send ( 'data' , a )
70+ return a ;
71+
72+
73+
74+ }
75+ connect ( sync ) {
76+ createConnections ( [ {
77+ name : "jlc" ,
78+ type : "mysql" ,
79+ host : "www.whyengineer.com" ,
80+ port : 3306 ,
81+ username : "jlc" ,
82+ password : "71451085a" ,
83+ database : "jlc" ,
84+ entities : [ Cap ] ,
85+ logging : true ,
86+ synchronize : sync
87+ } , {
88+ name : "bom" ,
89+ type : "sqlite" ,
90+ database : path . join ( app . getPath ( 'userData' ) , 'db' , 'bom.sqlite' ) ,
91+ entities : [ ] ,
92+ logging : true ,
93+ synchronize : sync
94+ } ] ) . then ( ( ) => {
95+ log . info ( "connect database ok" )
96+ this . connected = true ;
97+ this . jlc = getConnection ( 'jlc' ) ;
98+ this . bom = getConnection ( 'bom' ) ;
99+ // ipcMain.on('getData',()=>{
100+ // this.jlc.manager.findOne(Cap).then((val) => {
101+ // this.parseJlc(val, 'cap')
102+ // })
103+ // })
104+ this . jlc . manager . findOne ( Cap ) . then ( ( val ) => {
105+ this . parseJlc ( val , 'cap' )
106+ } )
107+
108+
109+ } ) . catch ( ( err ) => {
110+ this . connected = false ;
111+ log . error ( err ) ;
112+ } )
113+ }
114+
115+ }
0 commit comments