进出入完善组织机构并加入导入人员和机构功能
554325746@qq.com
2019-08-07 07a66e53d2b4126c2004870d81a379d8ef0071da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
%%%-------------------------------------------------------------------
%%% @author pansen
%%% @copyright (C) 2018, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 01. 八月 2018 16:01
%%%-------------------------------------------------------------------
-module(syncDB_mnesia).
-author("pansen").
 
-compile(export_all).
 
%%-include("DbTool.hrl").
-include("nodeTool.hrl").
 
test() ->
  io:format("start test 12333 ~n"),
  mnesia:start(),
  lager:start(),
  ok.
 
setCNode(CNode) ->
  Ret = mochiglobal:put('cNodeName', CNode),
  case Ret of
    ok ->
      Res = [{atomic, ok}];
    _ ->
      Res = [{aborted, notok}]
  end,
  Res.
 
% setCNode(CNode) ->
%   io:format("setCNode name ~p ",[CNode]),
 
%   % BerPid = mochiglobal:get('sendMessToCNodePID'),
%   % case BerPid of
%   %   undefined ->
%   %       io:format("BerPid is ~p ",[BerPid]),
%   %       Pid = spawn(fun() ->
%   %           callCNode:loop(CNode)
%   %                     end),
%   %       mochiglobal:put('sendMessToCNodePID',Pid);
%   %   _->
%   %       io:format("BerPid is ~p ",[BerPid]),
%   %       BerPid ! stop,
%   %       Pid = spawn(fun() ->
%   %           callCNode:loop(CNode)
%   %                     end),
%   %       mochiglobal:put('sendMessToCNodePID',Pid)
%   % end,
%   Pid = spawn(fun() ->
%       callCNode:loop(CNode)
%                 end),
%   case whereis(sendMessToCNodePID) of
%     undefined ->
%       ok;
%     _->
%       unregister(sendMessToCNodePID)
%   end,
%   Ret = register(sendMessToCNodePID, Pid),
%   case Ret of
%     true ->
%         io:format("register staus ~p, Pid is ~p",[Ret,Pid]),
%         Res =[{atomic, ok}];
%     _->
%         io:format("register staus ~p, Pid is ~p",[Ret,Pid]),
%         Res=[{aborted,notok}]
%   end,
%   Res. %%注册进程名字
% %%  mochiglobal:put('cNPid', Pid).
 
sendMessage() ->
  Nodes = mnesia:system_info(running_db_nodes),
  io:format("sendMessage ~p", [Nodes]),
  Res = syncTool:rpcSendMessToCNode(Nodes, {call, 'update', 1}),
  case Res of
    null ->
      io:format("send ok"),
      Ret = [{atomic, ok}];
    _ ->
      io:format("send error"),
      Ret = [{aborted, notok}]
  end,
  Ret.
 
% sendMessage()->
%   Nodes=mnesia:system_info(running_db_nodes),
%   io:format("sendMessage ~p",[Nodes]),
%   Res = syncTool:rpcSendMessToCNode(Nodes, {call, 'update', 1}),
%   case Res of
%       null ->
%       io:format("send ok"),
%       Ret=[{atomic, ok}];
%     _ ->
%       io:format("send error"),
%       Ret=[{aborted,notok}]
%   end,
%   Ret.
 
 
%%
%%  启动节点并自动连接到集群
%%  新节点自动进行初始化
%%
startNode(NodeName, FatherNodeName, DeviceId, ClusterId, ClusterName) ->
  lager:start(),
  io:format("start Node"),
%%  setCNode('c1@127.0.0.1'),
  mnesiaDbStart(1000),
  TableList = mnesia:system_info(tables),
  try
    case TableList of
      [schema] ->
        if
          FatherNodeName == ' ' ->
            Ret = initLocalFathNodeDB(NodeName),
            io:format("initLocalFathNodeDB is : ~p   ~p~n", [FatherNodeName, Ret]),
            %%%#todo add device_rec to db
            clusterInfo:add_ClusterInfo(ClusterId, ClusterName),
            device_info:add_deviceInfo(uuid:to_string(uuid:uuid5(uuid:uuid4(), "NodeName")), DeviceId, NodeName, ClusterId);
          true ->
            Ret = initLocalSubNodeDB(NodeName, FatherNodeName),
            io:format("initLocalSubNodeDB is : ~p   ~p~n", [FatherNodeName, Ret]),
            %%%#todo add device_rec to db
            device_info:add_deviceInfo(uuid:to_string(uuid:uuid5(uuid:uuid4(), "NodeName")), DeviceId, NodeName, ClusterId)
        end,
        mnesiaDbStart(10000)
    end
  catch
    Ex:Type ->
      {Ex, Type, erlang:get_stacktrace()},
      io:format("table list is  ~p,~p,~p ~n", [Ex, Type, TableList])
  end,
 
  io:format("mnesia info tables ~p~n", [mnesia:info()]),
%%  try
  VE = syncDB:findAllNode(),
  if
    is_tuple(VE) == true ->
      RSRET = {faile, notok};
    is_list(VE) == true ->
      RSRET = {atomic, ok}
  end,
  RSRET.
 
modifyCluName(CluId, CluName) ->
  Tmp = clusterInfo:add_ClusterInfo(CluId, CluName),
  if
    Tmp == ok ->
      RET = [{atomic, ok}];
    true ->
      RET = [{aborted, message}]
  end,
  RET.
 
removeNode() ->
%%  #todo 集群的最后一个节点退出时,删除集群信息
  [Node | Nodes] = mnesia:system_info(running_db_nodes),
  if
    Node == node() ->
      io:format("Node eq  node() ~n"),
      device_info:del_DevInfo(Node),
      mnesia:stop(),
      {atomic, ok};
    true ->
      rpc:call(Node, syncDB, rpcDeleteNode, [node()])
  end.
 
findAllNode() ->
  device_info:deviceOnlineList2Maps(device_info:findDeviceOnlineList('device_info', mnesia:system_info(running_db_nodes))).
 
%%????
getNodeOrTable(TableName, SyncType) ->
  if
    SyncType == '1' ->
      NewTableName = TableName,
      Nodes = mnesia:system_info(running_db_nodes);
    true ->
      %% #todo
      NewTableName = "",
      Nodes = [node()]
  end,
  {Nodes, NewTableName}.
 
 
%%  创建底库
%% 创建底库 表类型,表名,同步状态,黑白名单
createDatabase(TableType, TableName, SyncType, BwType, StartTime, EndTime) ->
  io:format("createDatabase ~p ~p ~p ~p ~p ~p ~n", [TableType, TableName, SyncType, BwType, StartTime, EndTime]),
  Uuid = "",
  Tup = getNodeOrTable(TableName, SyncType),
  if
    TableType == 'person' ->
%%      Ret = person_info:create_personInfo(element(1, Tup), element(2, Tup)),
%%      Ret2 = person_info_feature:create_personFeature(element(1, Tup), element(2, Tup)),
      Ret = person_info:create_personInfo(element(1, Tup), TableName),
      PerFea = addFea2TableName(TableName),
      Ret2 = person_info_feature:create_personFeature(element(1, Tup), PerFea),
      io:format("Ret ~p ~n", [Ret]),
      if
        Ret == {atomic, ok} ->
          tableType:add_tableType(Uuid, TableName, BwType),
          %%todo time
          sys_o_tables:add_sys_table(Uuid, TableName, 'test', TableType, StartTime, EndTime),
%%      #todo send message
          PerRet = [Ret | [Ret2]];
        true ->
          Z = element(1, element(2, Ret)),
          io:format("Z ~p ~n", [Z]),
          if
            Z == already_exists ->
              tableType:add_tableType(Uuid, TableName, BwType),
              %%todo time
              sys_o_tables:add_sys_table(Uuid, TableName, 'test', TableType, StartTime, EndTime),
%%      #todo send message
              PerRet = [{atomic, ok} | [{atomic, ok}]];
            true ->
              PerRet = [Ret | [Ret2]]
          end
      end;
    TableType == 'car' ->
%%      Ret = car_info:create_carInfo(element(1, Tup), element(2, Tup)),
      Ret = car_info:create_carInfo(element(1, Tup), TableName),
      tableType:add_tableType(Uuid, TableName, BwType),
%%todo time
      sys_o_tables:add_sys_table(Uuid, TableName, 'test', TableType, StartTime, EndTime),
%%      PerRet = [Ret],
      io:format("Ret ~p ~n", [Ret]),
      if
        Ret == {atomic, ok} ->
          PerRet = [Ret];
        true ->
          Z = element(1, element(2, Ret)),
          io:format("Z ~p ~n", [Z]),
          if
            Z == already_exists ->
              PerRet = [{atomic, ok} | [{atomic, ok}]];
            true ->
              PerRet = [Ret]
          end
      end;
    true ->
      PerRet = [{aborted, {'tableType_error', TableType}}],
      io:format("Error ~p ~n", [TableType])
  end,
%%  tableType:add_tableType(element(2, Tup), BwType),
%%  if
%%    SyncType == '1' ->
%%
%%  end,
  io:format("PerRet ~p ~n", [PerRet]),
  PerRet.
 
%%  c底库
%% 创建底库 表类型,表名,同步状态,黑白名单
updateDatabase(Uuid, TableType, TableName, SyncType, BwType, StartTime, EndTime) ->
  io:format("createDatabase ~p ~p ~p ~p ~p ~p ~n", [TableType, TableName, SyncType, BwType, StartTime, EndTime]),
  Tup = getNodeOrTable(TableName, SyncType),
  if
    TableType == 'person' ->
      tableType:add_tableType(Uuid, TableName, BwType),
      %%todo time
      sys_o_tables:add_sys_table(Uuid, TableName, 'test', TableType, StartTime, EndTime),
%%      #todo send message
      PerRet = [{atomic, ok}];
    TableType == 'car' ->
%%      Ret = car_info:create_carInfo(element(1, Tup), element(2, Tup)),
%%      Ret = car_info:create_carInfo(element(1, Tup), TableName),
      tableType:add_tableType(Uuid, TableName, BwType),
      %%todo time
      sys_o_tables:add_sys_table(Uuid, TableName, 'test', TableType, StartTime, EndTime),
%%      #todo send message
      PerRet = [{atomic, ok}];
    true ->
      PerRet = [{aborted, {'tableType_error', TableType}}],
      io:format("Error ~p ~n", [TableType])
  end,
  io:format("PerRet ~p ~n", [PerRet]),
  PerRet.
 
 
%% shanchu底库
%% shanchu底库 表类型,表名
deleteDatabase(TableType, TableName) ->
  io:format("deleteDatabase ~p ~p  ~n", [TableType, TableName]),
%%  case  of
%%      ->;
%%end,
  Ret = mnesia:clear_table(TableName),
  if
    TableType == 'person' ->
      case Ret of
        {atomic, ok} ->
          PerFea = addFea2TableName(TableName),
          Ret2 = mnesia:clear_table(PerFea),
          case Ret2 of
            {atomic, ok} ->
              tableType:del_TableTypeInfo(TableName),
              sys_o_tables:del_SysTabInfo(TableName),
              mnesia:delete_table(TableName),
              mnesia:delete_table(PerFea);
%%      #todo send message
            _ ->
              io:format("")
          end;
        _ ->
          Ret2 = {aborted, notok},
          io:format("")
      end,
      PerRet = [Ret | [Ret2]];
    TableType == 'car' ->
      tableType:del_TableTypeInfo(TableName),
      sys_o_tables:del_SysTabInfo(TableName),
      mnesia:delete_table(TableName),
      PerRet = [Ret];
    true ->
      PerRet = [{aborted, {'tableType_error', TableType}}],
      tableType:del_TableTypeInfo(TableName),
      sys_o_tables:del_SysTabInfo(TableName),
      io:format("Error ~p ~n", [TableType])
  end,
%%  tableType:add_tableType(element(2, Tup), BwType),
  io:format("PerRet ~p ~n", [PerRet]),
  PerRet.
 
findAllDatabase() ->
  sys_o_tables:sysTabformat2Mat(sys_o_tables:findAllSysTableInfo('sys_o_tables')).
 
findAllTypeInfo() ->
  tableType:tableTypeformat2Mat(tableType:findAllTableTypeInfo('tableType')).
 
 
%%  插入人
%%  TableName, ID, PersonName, Age, Sex, IdCard, PhoneNum
%%  TableName, Feature
addPersonData(TableName, PersonName, Age, Sex, IdCard, PhoneNum, Img, UUID, Feature) ->
  if
    UUID == '' ->
      io:format("UUID is ~p ~n", [UUID]),
      ID = syncTool:getUUIDStr();
    true ->
      ID = UUID
  end,
  PerFea = addFea2TableName(TableName),
  Uid = person_info_feature:add_personFea(ID, PerFea, Feature, Img, IdCard),
  if
    Uid == '' ->
      Ret = {aborted, {add_error, Feature}};
    true ->
      Ret = person_info:add_personInfo(TableName, Uid, PersonName, Age, Sex, IdCard, PhoneNum)
  % case Ret of
  %   '' ->
  %     ok;
  %   _ ->
  %     syncTool:rpcSendMessToCNode(mnesia:system_info(running_db_nodes), {call, 'bar', 65})
  % end
  end,
  io:format("Ret ~p ~n", [Ret]),
  Ret.
 
%%  删除人
%%  TableName, UUID
deletePersonData(TableName, UUID) ->
  PerFea = addFea2TableName(TableName),
  DelRet = person_info_feature:del_personFea(PerFea, UUID),
  if
    DelRet == {atomic, ok} ->
      Ret = [person_info:del_personInfo(TableName, UUID)];
    true ->
      Ret = {aborted, {del_error, {TableName, UUID}}}
  end,
  io:format("Ret ~p ~n", [Ret]),
  Ret.
 
findSinglePersonInfo(TableName, UUID) ->
  person_info:personInfoFormat2Mat(person_info:findPersonInfo(TableName, UUID)).
 
 
testFunc(PersonId, []) ->
  [];
testFunc(PersonId, [TN | T]) ->
  io:format("table is  ~p ", [TN]),
  TableName = element(1, TN),
  Records = person_info:findPersonInfo(TableName, PersonId),
  case Records of
    [] ->
      TemMaps = #{"tableName"=>TableName, "Uuid"=>"null"};
    _ ->
      [TRco | TRecords] = Records,
      io:format("TRco is  ~p ", [TRco]),
      TemMaps = #{"tableName"=>TableName, "Uuid"=>element(1, TRco)}
  end,
  [maps:to_list(TemMaps) | testFunc(PersonId, T)].
 
%% 判断集群内某人是否存在
singlePersonIsExists(PersonId) ->
  TableLists = sys_o_tables:findAllSysTableInfo('sys_o_tables'),
  testFunc(PersonId, TableLists).
 
 
testFunc2(NewPerId, OldId, Idcard, PerPicUrl, []) ->
  [];
testFunc2(NewPerId, OldId, Idcard, PerPicUrl, [TableRec | T]) ->
  io:format("table is  ~p ~n", [TableRec]),
  TableName = element(1, TableRec),
  NewRecords = person_info:findPersonInfo(TableName, NewPerId),
  case NewRecords of
    [] ->
      io:format("~p have no newId ~p will search oldId ~n", [TableName, NewPerId]),
      OldRecords = person_info:findPersonInfo(TableName, OldId),
      case OldRecords of
        [] ->
          io:format("~p have no oldId ~p ~n", [TableName, OldId]),
          ResRetMaps = #{"tableName"=>TableName, "result"=>"notexist"},
%%          no action
          ok;
        _ ->
          io:format("~p have oldId ~p will update record~n", [TableName, OldId]),
%%          update oldRecord
          Ret = {},
          case Ret of
            [] ->
%%              ok?
              ResRetMaps = #{"tableName"=>TableName, "result"=>"true"},
              ok;
            _ ->
%%              not ok
              ResRetMaps = #{"tableName"=>TableName, "result"=>"false"},
              ok
          end,
          ok
      end;
    _ ->
      io:format("~p have newId ~p will update newId ~n", [TableName, NewPerId]),
%%      update newRecord
      OldRecords = person_info:findPersonInfo(TableName, OldId),
      case OldRecords of
        [] ->
%%          no action
          io:format("~p have no oldId ~p ~n", [TableName, OldId]),
          ok;
        _ ->
%%          delete oldRecord
          io:format("~p have oldId ~p will delete record~n", [TableName, OldId]),
          deletePersonData(TableName, OldId),
          ok
      end,
      ResRetMaps = #{"tableName"=>TableName, "result"=>"true"},
      ok
  end,
  [maps:to_list(ResRetMaps) | testFunc2(NewPerId, OldId, Idcard, PerPicUrl, T)].
 
singlePersonUpdate(NewPerId, OldId, Idcard, PerPicUrl) ->
  TableLists = sys_o_tables:findAllSysTableInfo('sys_o_tables'),
  testFunc2(NewPerId, OldId, Idcard, PerPicUrl, TableLists).
 
%%  插入车
addCarData() ->
  ok.
 
%%  加载人脸数据库
loadFaceFeaData(TableName) ->
  PerFea = addFea2TableName(TableName),
  Results = person_info_feature:findAllPersonFea(PerFea),
  person_info_feature:personFeaFormat2Mat(Results).
 
%%  加载人脸数据库2 no feature
loadFaceFeaData2(TableName) ->
  PerFea = addFea2TableName(TableName),
  Results = person_info_feature:findAllPersonFea(PerFea),
  person_info_feature:personNoFeaFormat2Mat(Results).