Skip to content

Commit

Permalink
fix:factory
Browse files Browse the repository at this point in the history
  • Loading branch information
shifttttttt committed Oct 12, 2022
1 parent d070a27 commit 3b827af
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 48 deletions.
19 changes: 8 additions & 11 deletions apps/dgiot_factory/src/dgiot_factory_channel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ handle_message({sync_parse, _Pid, 'before', put, Token, <<"Device">>, #{<<"conte
#{<<"person">> := #{<<"type">> := PersonType}} ->
case process_data(Content, PersonType, Token, TaskDeviceId) of
{BatchProductId, BatchDeviceId, BatchAddr, NewData} ->
handle_data(TaskProductId, TaskDeviceId, BatchProductId, BatchDeviceId, BatchAddr, PersonType, NewData),
NewContent = handle_data(TaskProductId, TaskDeviceId, BatchProductId, BatchDeviceId, BatchAddr, PersonType, NewData),
MergedContent = maps:merge(Content, NewContent),
dgiot_parse_hook:publish(_Pid, MergedContent),
{ok, State};
_ ->
{ok, State}
Expand Down Expand Up @@ -127,18 +129,16 @@ stop(ChannelType, ChannelId, _State) ->
%%process_content(#{<<"person_type">> := PersonType} = Payload, ProductId, DeviceId, DevAddr, Id) ->
%% save_data(ProductId, Id, DevAddr, DeviceId, PersonType, Payload).


handle_data(_TaskProductId, TaskDeviceId, BatchProductId, BatchDeviceId, BatchAddr, PersonType, NewData) ->
io:format("~s ~p status =~p ~n", [?FILE, ?LINE, maps:get(<<"person_status">>, NewData, error)]),
NewPayLoad = run_factory_hook(_TaskProductId, TaskDeviceId, BatchProductId, BatchDeviceId, PersonType, NewData),
{OldNumData, OldNameData} = get_card_data(BatchProductId, BatchDeviceId),
dgiot_data:insert(?FACTORY_ORDER, {BatchProductId, BatchDeviceId, PersonType}, NewPayLoad),
Content = maps:merge(OldNameData, NewPayLoad),
io:format("~s ~p status =~p ~n", [?FILE, ?LINE, maps:get(<<"person_status">>, Content, error)]),
dgiot_parse:update_object(<<"Device">>, BatchDeviceId, #{<<"content">> => dgiot_map:unflatten(Content)}),
Content = dgiot_map:unflatten(maps:merge(OldNameData, NewPayLoad)),
dgiot_parse:update_object(<<"Device">>, BatchDeviceId, #{<<"content">> => Content}),
NumData = dgiot_factory_utils:turn_num(NewPayLoad, BatchProductId, PersonType),
io:format("~s ~p status =~p ~n", [?FILE, ?LINE, maps:get(<<"person_status">>, NumData, error)]),
%% io:format("~s ~p OldNumData =~p ~n", [?FILE, ?LINE,OldNumData]),
dgiot_task:save_td(BatchProductId, BatchAddr, maps:merge(OldNumData, NumData), #{}).
dgiot_task:save_td(BatchProductId, BatchAddr, maps:merge(OldNumData, NumData), #{}),
Content.
get_card_data(BatchProductId, BatchDeviceId) ->
DevcieTypeList = dgiot_product:get_devicetype(BatchProductId),
lists:foldl(
Expand Down Expand Up @@ -268,6 +268,3 @@ get_new_acl(SessionToken, Acl) ->
end, #{}, AclList);
Err -> {400, Err}
end.



4 changes: 2 additions & 2 deletions apps/dgiot_factory/src/dgiot_factory_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ do_request(get_data, #{<<"productId">> := undefined, <<"objectId">> := DeviceId}
{error, <<"get_data_failed">>}
end;
do_request(get_data, #{<<"productId">> := ProductId, <<"objectId">> := DeviceId, <<"type">> := Type,
<<"function">> := Function, <<"functionmap">> := FunctionMap, <<"group">> := Group,<<"having">> := Having,
<<"function">> := Function, <<"functionmap">> := FunctionMap, <<"group">> := Group, <<"having">> := Having,
<<"order">> := Order, <<"where">> := Where, <<"limit">> := Limit, <<"skip">> := Skip} = _Args,
#{<<"sessionToken">> := SessionToken} = _Context, _Body) ->
io:format("~s ~p _Args = ~p ~n", [?FILE, ?LINE, _Args]),
Expand All @@ -142,7 +142,7 @@ do_request(get_data, #{<<"productId">> := ProductId, <<"objectId">> := DeviceId,
io:format("~s ~p ProductId = ~p ~n", [?FILE, ?LINE, ProductId]),
{error, Error};
{ok, Channel} ->
case dgiot_factory_data:get_history_data(ProductId, DeviceId, Type, Function, FunctionMap, Group,Having, Where, Order, Channel, Limit, Skip) of
case dgiot_factory_data:get_history_data(ProductId, DeviceId, Type, Function, FunctionMap, Group, Having, Where, Order, Channel, Limit, Skip) of
{ok, {Total, Res}} ->
{ok, #{<<"status">> => 0, msg => <<"数据请求成功"/utf8>>, <<"data">> => #{<<"total">> => Total, <<"items">> => Res}}};
_ ->
Expand Down
16 changes: 8 additions & 8 deletions apps/dgiot_factory/src/dgiot_factory_material.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ get_material_record(DeviceId, Depart) ->

{ok, get_usable_material(DepartMaterial)};
_ ->
io:format("~s ~p DeviceId = ~p ~n", [?FILE, ?LINE, DeviceId]),
case dgiot_hook:run_hook({factory, get_material}, [DeviceId]) of
{ok, [{ok, Material}]} ->
{ok, get_usable_material(Material)};
Expand Down Expand Up @@ -69,7 +68,7 @@ get_usable_material(Material) ->
post_material(DeviceId, Data) when is_map(Data) ->
case get_material_record(DeviceId, undefined) of
{ok, Material} ->
io:format("~s ~p Data = ~p ~n", [?FILE, ?LINE, Data]),
%% io:format("~s ~p Data = ~p ~n", [?FILE, ?LINE, Data]),
case hanlde_pickandretrive(DeviceId, Data, Material) of
{ok, NewMaterial} ->
%% io:format("~s ~p NewMaterial = ~ts ~n", [?FILE, ?LINE, unicode:characters_to_list(jiffy:encode(NewMaterial))]),
Expand All @@ -84,32 +83,33 @@ post_material(_, _) ->
error.
%%get_Material(Deviceid, FReplaceGroup, FPrdOrgId,FStockId, FAppQty, FActualQty)
%%FReplaceGroup 行号 FPrdOrgId 组织id FStockId 仓库id FAppQty 申请数量 FActualQty 实际数量
hanlde_pickandretrive(DeviceId, #{ <<"FPrdOrgId">> :=FPrdOrgId,<<"FStockId">> := FStockId, <<"FReplaceGroup">> := FReplaceGroup,
<<"Subitem_BOM_number">> := Name, <<"material_date">> := Date,
hanlde_pickandretrive(DeviceId, #{<<"FPrdOrgId">> := FPrdOrgId, <<"FStockId">> := FStockId, <<"FReplaceGroup">> := FReplaceGroup,
<<"Subitem_BOM_number">> := Name,
<<"material_people">> := People, <<"material_type">> := Type,
<<"material_number">> := Num, <<"material_batchid">> := BatchId, <<"material_weight">> := Weight,
<<"objectId">> := Id}, Material) ->
Date = dgiot_datetime:format("YYYY-MM-DD"),
case maps:find(Name, Material) of
{ok, Res} ->
case Res of
#{<<"material_pick">> := Pick, <<"material_retrive">> := Retrive, <<"material_picked">> := Picked} ->
case Type of
<<"picking">> ->
case dgiot_hook:run_hook({kingdee, post_material, <<"picking">>},[DeviceId, FReplaceGroup, FPrdOrgId, FStockId, Weight, Weight]) of
case dgiot_hook:run_hook({kingdee, post_material, <<"picking">>}, [DeviceId, FReplaceGroup, FPrdOrgId, FStockId, Weight, Weight]) of
{ok, [{ok, _}]} ->
io:format("~s ~p Type = ~p ~n", [?FILE, ?LINE, Type]),
After = dgiot_utils:to_float(Picked) + dgiot_utils:to_float(Weight),
handle_warehouse(Id, Type, Num),
NewRes = maps:merge(Res, #{<<"material_picked">> => After, <<"material_pick">> => Pick ++ [#{<<"material_date">> => Date, <<"material_people">> => People, <<"material_number">> => Num,
<<"material_batchid">> => BatchId, <<"material_weight">> => Weight}]}),
{ok, maps:merge(Material, #{Name => NewRes})};
Type ->
_ ->
io:format("~s ~p Type = ~p ~n", [?FILE, ?LINE, Type]),
error

end;
<<"retriving">> ->
case dgiot_hook:run_hook({kingdee, post_material, <<"retriving">>},[DeviceId, FReplaceGroup, FPrdOrgId, FStockId, Weight, Weight]) of
case dgiot_hook:run_hook({kingdee, post_material, <<"retriving">>}, [DeviceId, FReplaceGroup, FPrdOrgId, FStockId, Weight, Weight]) of
{ok, [{ok, _}]} ->
After = dgiot_utils:to_float(Picked) - dgiot_utils:to_float(Weight),
handle_warehouse(Id, Type, Num),
Expand All @@ -129,7 +129,7 @@ hanlde_pickandretrive(DeviceId, #{ <<"FPrdOrgId">> :=FPrdOrgId,<<"FStockId">> :
_ ->
error
end;
hanlde_pickandretrive(_,_, _) ->
hanlde_pickandretrive(_, _, _) ->
%% io:format("~s ~p R = ~p ~n", [?FILE, ?LINE,R]),
error.

Expand Down
41 changes: 21 additions & 20 deletions apps/dgiot_factory/src/dgiot_factory_shift.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
-author("jonhl").
-include_lib("dgiot/include/logger.hrl").
-include("dgiot_factory.hrl").
-export([get_one_shift/1, save_one_shift/1, updata_id/0, get_shift/3, post_shift/1, get_all_shift/4, get_workshop/2,format_worker/1]).
-export([get_one_shift/1, save_one_shift/1, updata_id/0, get_shift/3, post_shift/1, get_all_shift/4, get_workshop/2, format_worker/1]).
-export([post_one_shift/1, get_shift_time/1]).
-export([ bits_to_workerlist/1, workerlist_to_bits/1]).
-export([bits_to_workerlist/1, workerlist_to_bits/1]).
-define(DAY, 86400).
-define(WORKERCALENDAR, <<"WorkerCanlendar">>).
-define(INTERVAL, 604800).
Expand Down Expand Up @@ -169,7 +169,7 @@ post_shift(Shifts) when is_list(Shifts) ->
end
end, [], Shifts);

post_shift(Shifts) when is_map(Shifts) ->
post_shift(Shifts) when is_map(Shifts) ->
post_one_shift(Shifts);


Expand All @@ -181,7 +181,8 @@ post_shift(_) ->
post_one_shift(#{<<"worker">> := Workers} = Data) ->
%% Bits = workerlist_to_bits(_Workers),
%% Args = maps:merge(Shift, #{<<"worker">> => Bits}),
Args = maps:merge(Data,#{<<"worker">> => dgiot_utils:to_binary(Workers)}),
TeamWorker = dgiot_factory_utils:check_workteam(Workers),
Args = maps:merge(Data, #{<<"worker">> => dgiot_utils:to_binary(TeamWorker)}),
case dgiot_parse_id:get_objectid(<<"shift">>, Args) of
#{<<"objectId">> := ObjectId} ->
case dgiot_parse:get_object(?WORKERCALENDAR, ObjectId) of
Expand Down Expand Up @@ -278,18 +279,18 @@ workerlist_to_bits(Workers) when is_binary(Workers) ->
Acc ++ [dgiot_utils:to_int(Num)]
end, [], List),
SortedList = lists:sort(NumList),
{Bits,_} = lists:foldl(
{Bits, _} = lists:foldl(
fun(X, {Acc, LastNum}) ->
ZeroNum = X - LastNum - 1,
ZeroList = dgiot_factory_utils:get_zero_list(ZeroNum),
{Acc ++ ZeroList ++ [1], X}
end, {[], 0}, SortedList),
Res = case length(Bits) < ?MAXWORKERNUM of
true ->
Bits ++ dgiot_factory_utils:get_zero_list(?MAXWORKERNUM - length(Bits));
_ ->
Bits
end,
true ->
Bits ++ dgiot_factory_utils:get_zero_list(?MAXWORKERNUM - length(Bits));
_ ->
Bits
end,
dgiot_utils:bits_to_binary(Res);
workerlist_to_bits(Workers) ->
Workers.
Expand All @@ -299,7 +300,7 @@ workerlist_to_bits(Workers) ->
bits_to_workerlist(Bits) ->
Binary = dgiot_utils:to_binary(Bits),
List = dgiot_utils:binary_to_bits(Binary),
{Res, _ } = lists:foldl(
{Res, _} = lists:foldl(
fun(X, {Acc, Num}) ->
case X of
1 ->
Expand All @@ -309,7 +310,7 @@ bits_to_workerlist(Bits) ->
Bin = <<Zero/binary, B/binary>>,
case dgiot_data:lookup(?WORKERTREE, Bin) of
{ok, Value} ->
{<<Acc/binary," ", Value/binary>>, Num + 1};
{<<Acc/binary, " ", Value/binary>>, Num + 1};
_ ->
{<<Acc/binary, " ", Bin/binary>>, Num + 1}
end;
Expand All @@ -321,15 +322,15 @@ bits_to_workerlist(Bits) ->


format_worker(Worker) ->
WorkerList = re:split(Worker,<<" ">>),
WorkerList = re:split(Worker, <<" ">>),
lists:foldl(
fun(X, Acc)->
case dgiot_data:get(?WORKERTREE,X) of
fun(X, Acc) ->
case dgiot_data:get(?WORKERTREE, X) of
not_find ->
<<Acc/binary," ",X/binary>>;
{_,_,Name,_} ->
io:format("~s ~p Name = ~p.~n", [?FILE, ?LINE, Name]),
<<Acc/binary, " ", X/binary>>;
{_, _, Name, _} ->
%% io:format("~s ~p Name = ~p.~n", [?FILE, ?LINE, Name]),

<<Acc/binary," ",Name/binary>>
<<Acc/binary, " ", Name/binary>>
end
end,<<"">>,WorkerList).
end, <<"">>, WorkerList).
89 changes: 82 additions & 7 deletions apps/dgiot_factory/src/dgiot_factory_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
-export([get_num/2, get_name/2, turn_name/3, turn_num/3]).
-export([get_usertree/2, getalluser/1, get_ThingMap/2]).
-export([get_zero_list/1, get_zero_binary/1]).
-export([fix_model/1, get_worker/1, get_children/1]).
-export([fix_model/1, get_worker/1, get_children/1, check_workteam/1]).

fix_model(ID) ->
{ok, #{<<"thing">> := Model}} = dgiot_parse:get_object(<<"Product">>, ID),
Expand Down Expand Up @@ -160,7 +160,10 @@ get_usertree(#{<<"id">> := undefined}, SessionToken) ->
[]
end;
get_usertree(#{<<"id">> := Id}, _) ->
get_worker(Id).
Tree = get_worker(Id),
{NewTree, List} = format_tree(Tree),
record_workteam(List),
NewTree.



Expand Down Expand Up @@ -190,7 +193,6 @@ get_worker(Id) ->
{ok, #{<<"results">> := RoleList}} ->
get_children(dgiot_parse_utils:create_tree(RoleList, <<"parent">>));
_ ->
io:format("~s ~p Id = ~p ~n", [?FILE, ?LINE, Id]),
error
%% end
end.
Expand Down Expand Up @@ -228,7 +230,7 @@ getalluser(#{<<"objectId">> := RoleId, <<"name">> := Depname} = Role) ->
#{<<"nick">> := <<"user_for_", _/binary>>} ->
Acc;
#{<<"objectId">> := Id, <<"nick">> := Nick, <<"username">> := UserName} ->
update_worker_ets(Id, UserName, Nick,Depname),
update_worker_ets(Id, UserName, Nick, Depname),
%% Acc ++ [#{<<"label">> => Nick, <<"value">> => <<UserName/binary, "_", Nick/binary>>}];
Acc ++ [#{<<"label">> => <<UserName/binary, "_", Nick/binary>>, <<"value">> => UserName}];
_ ->
Expand Down Expand Up @@ -283,10 +285,83 @@ get_ThingMap(_DeviceType, ProductId) ->
end.

%%dgiot_data:insert(?WORKERTREE, Id, {UserName, Nick,1}),
update_worker_ets(Id, UserName, Nick,Depname) ->
update_worker_ets(Id, UserName, Nick, Depname) ->
case dgiot_data:lookup(?WORKERTREE, Id) of
{ok, {_, _, State}} ->
dgiot_data:insert(?WORKERTREE, UserName, {Id, Depname,Nick, State});
dgiot_data:insert(?WORKERTREE, UserName, {Id, Depname, Nick, State});
{error, not_find} ->
dgiot_data:insert(?WORKERTREE, UserName, {Id,Depname, Nick, 1})
dgiot_data:insert(?WORKERTREE, UserName, {Id, Depname, Nick, 1})
end.
record_workteam(List) ->
Res = lists:foldl(
fun(X, Acc) ->
case is_map(X) of
true ->
maps:merge(Acc, X);
_ ->
Acc
end
end, #{}, List),
dgiot_data:insert(?WORKERTREE, workteam, Res).

check_workteam(Worker) ->
WorkerList = re:split(Worker, <<" ">>),
TeamMap = case dgiot_data:get(?WORKERTREE, workteam) of
not_find ->
[];
L ->
L
end,
TeamList = maps:keys(TeamMap),
lists:foldl(
fun(Shift, Acc) ->
case lists:member(Shift, TeamList) of
true ->
Workers = maps:get(Shift, TeamMap, <<"">>),
BinWorkers = turn_workes2binary(Workers),
<<Acc/binary, BinWorkers/binary>>;
_ ->
<<Acc/binary, " ", Shift/binary>>
end
end, <<"">>, WorkerList).

turn_workes2binary(Workers) ->
FlatternList = lists:flatten(Workers),
lists:foldl(
fun(X, Acc) ->
Bin = dgiot_utils:to_binary(X),
<<Acc/binary, " ", Bin/binary>>
end, <<"">>, FlatternList).

format_tree(Tree) ->
lists:foldl(
fun(Team, {NewTree, List}) ->
case Team of
#{<<"name">> := Value, <<"children">> := Child} ->
{AllChild, ChildList} = get_all_worker(Child),
{NewTree ++ [maps:merge(Team, #{<<"children">> => AllChild})], List ++ [#{Value => ChildList}]};
#{<<"name">> := Lable, <<"value">> := Value} ->
{NewTree ++ [#{<<"label">> => Lable, <<"value">> => Value}], List ++ [Value]};
_ ->

{NewTree, List}
end
end, {[], []}, Tree).

get_all_worker(Child) when is_list(Child) ->
lists:foldl(
fun(X, {NewTree, List}) ->
case X of
#{<<"children">> := SubChild} ->
{SubTree, SubList} = get_all_worker(SubChild),
{NewTree ++ SubTree, List ++ [SubList]};
#{<<"label">> := Lable, <<"value">> := Value} ->
{NewTree ++ [#{<<"label">> => Lable, <<"value">> => Value}], List ++ [Value]};
_ ->
{NewTree, List}
end
end, {[], []}, Child);

get_all_worker(Child) ->
io:format("~s ~p here ~n", [?FILE, ?LINE]),
{Child, []}.

0 comments on commit 3b827af

Please sign in to comment.