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

import one.kii.kiimate.model.core.dai.IntensionDai;
import one.kii.kiimate.model.core.dai.ModelSubscriptionDai;
import one.kii.kiimate.status.core.api.VisitStatusApi;
import one.kii.kiimate.status.core.dai.InstanceDai;
import one.kii.kiimate.status.core.fui.InstanceTransformer;
import one.kii.summer.beans.utils.KeyFactorTools;
import one.kii.summer.beans.utils.ValueMapping;
import one.kii.summer.io.context.ReadContext;
import one.kii.summer.io.exception.NotFound;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * Created by WangYanJiong on 4/6/17.
 */

@Component
public class DefaultVisitStatusApi implements VisitStatusApi {

    @Autowired
    private InstanceDai instanceDai;

    @Autowired
    private IntensionDai intensionDai;

    @Autowired
    private ModelSubscriptionDai modelSubscriptionDai;

    @Autowired
    private InstanceTransformer instanceTransformer;

    @Override
    public Receipt visit(ReadContext context, Form form) throws NotFound {
        ModelSubscriptionDai.ChannelSubId channel = ValueMapping.from(ModelSubscriptionDai.ChannelSubId.class, form, context);

        ModelSubscriptionDai.ModelPubSet model = modelSubscriptionDai.getModelPubSetByOwnerSubscription(channel);

        IntensionDai.ChannelLatestExtension latestExtension = new IntensionDai.ChannelLatestExtension();
        latestExtension.setId(model.getRootExtId());

        List<IntensionDai.Record> records = intensionDai.load(latestExtension);
        List<Intension> intensions = ValueMapping.from(Intension.class, records);


        List<InstanceDai.Instance> instances = instanceDai.selectLatestInstanceBySubId(form.getSubId());

        Map<String, Object> map = instanceTransformer.toTimedValue(instances, model);

        Receipt receipt = ValueMapping.from(Receipt.class, form, context);
        receipt.setMap(map);
        receipt.setIntensions(intensions);

        return receipt;
    }


}

@@ -1391,34 +1391,32 @@
 ionDai.ChannelLa
-te
 stExtension late
@@ -1413,29 +1413,18 @@
 nsion la
+s
 t
-estExtension
  = new I
@@ -1444,18 +1444,16 @@
 hannelLa
-te
 stExtens
@@ -1469,29 +1469,18 @@
       la
+s
 t
-estExtension
 .setId(m
@@ -1496,24 +1496,73 @@
 ootExtId());
+%0A        last.setBeginTime(model.getBeginTime());
 %0A%0A        Li
@@ -1616,24 +1616,17 @@
 load
-(latestExtension
+Last(last
 );%0A 

fix bug