package com.sinewang.kiimate.status.core.api;

import one.kii.kiimate.model.core.dai.ModelSubscriptionDai;
import one.kii.kiimate.status.core.api.SearchAssetsApi;
import one.kii.kiimate.status.core.dai.AssetPublicationDai;
import one.kii.kiimate.status.core.dai.InstanceDai;
import one.kii.kiimate.status.core.fui.InstanceTransformer;
import one.kii.summer.beans.utils.BasicCopy;
import one.kii.summer.io.context.ReadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by WangYanJiong on 20/05/2017.
 */
@Component
public class DefaultSearchAssetsApi implements SearchAssetsApi {

    @Autowired
    private AssetPublicationDai assetPublicationDai;

    @Autowired
    private InstanceDai instanceDai;

    @Autowired
    private ModelSubscriptionDai modelSubscriptionDai;

    @Autowired
    private InstanceTransformer instanceTransformer;

    @Override
    public List<Assets> search(ReadContext context, QueryForm form) {
        List<AssetPublicationDai.Assets> assetsList = assetPublicationDai.queryAssets(context.getOwnerId(), form.getQuery());

        List<Assets> assets = new ArrayList<>();
        for (AssetPublicationDai.Assets assetDb : assetsList) {
            List<InstanceDai.Instance> instances = instanceDai.selectInstanceByPubSet(assetDb.getPubSet());
            Assets asset = BasicCopy.from(Assets.class, assetDb);
            String rootExtId = modelSubscriptionDai.getLatestRootExtIdByOwnerSubscription(context.getOwnerId(), assetDb.getModelSubId());
            Map<String, Object> map = instanceTransformer.from(instances, rootExtId);
            assets.add(asset);
        }
        return assets;
    }


}

@@ -44,68 +44,8 @@
 i;%0A%0A
-import one.kii.kiimate.model.core.dai.ModelSubscriptionDai;%0A
 impo
@@ -160,120 +160,8 @@
 ai;%0A
-import one.kii.kiimate.status.core.dai.InstanceDai;%0Aimport one.kii.kiimate.status.core.fui.InstanceTransformer;%0A
 impo
@@ -381,62 +381,12 @@
 til.
-ArrayList;%0Aimport java.util.List;%0Aimport java.util.Map
+List
 ;%0A%0A/
@@ -584,201 +584,8 @@
 i;%0A%0A
-    @Autowired%0A    private InstanceDai instanceDai;%0A%0A    @Autowired%0A    private ModelSubscriptionDai modelSubscriptionDai;%0A%0A    @Autowired%0A    private InstanceTransformer instanceTransformer;%0A%0A
     
@@ -790,25 +790,24 @@
 ery());%0A
-%0A
         
 List%3CAss
@@ -802,573 +802,55 @@
     
-List%3CAssets%3E assets = new ArrayList%3C%3E();%0A        for (AssetPublicationDai.Assets assetDb : assetsList) %7B%0A            List%3CInstanceDai.Instance%3E instances = instanceDai.selectInstanceByPubSet(assetDb.getPubSet());%0A            Assets asset = BasicCopy.from(Assets.class, assetDb);%0A            String rootExtId = modelSubscriptionDai.getLatestRootExtIdByOwnerSubscription(context.getOwnerId(), assetDb.getModelSubId());%0A            Map%3CString, Object%3E map = instanceTransformer.from(instances, rootExtId);%0A            assets.add(asset);%0A        %7D%0A        return assets
+return BasicCopy.from(Assets.class, assetsList)
 ;%0A  

remove unused code