package cn.ibizlab.core.data.service.impl;

import cn.ibizlab.core.data.domain.DOModel;
import cn.ibizlab.core.data.dto.BaseData;
import cn.ibizlab.core.data.dto.DbDataQuery;
import cn.ibizlab.core.data.dto.RequestData;
import cn.ibizlab.core.data.dto.ResponseData; 
import cn.ibizlab.core.data.service.IDataService;
import cn.ibizlab.core.data.service.ModelService;
import cn.ibizlab.util.filter.SearchContextBase;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 实体[业务实体] 无存储服务对象接口实现
 */
@Slf4j
@Primary
@Service
public  class BaseDataService implements IDataService {

    @Override
    public DOModel getDOModel(String system, String entity)
    {
        return modelService.getDOModel(system,entity);
    }

    @Autowired
    private DbDataServiceImpl dbProxyService;

    @Autowired
    private MongoDataServiceImpl mongoProxyService;

    @Autowired
    private ModelService modelService;

    public IDataService getProxyService(String datasource)
    {
        if(datasource.indexOf("mongo")>0)
            return mongoProxyService;
        else
            return dbProxyService;
    }


    @Override
    public ResponseData call(DOModel model,String scope, String method, String datasource, RequestData requestBody)
    {
        if("create".equalsIgnoreCase(method))
        {
            if(create(model, datasource, requestBody.getBaseData()))
                return ResponseData.ok(requestBody.getBaseData());
            else
                return ResponseData.error(1,"");
        }
        else if("update".equalsIgnoreCase(method))
        {
            if(update(model, datasource, requestBody.getBaseData()))
                return ResponseData.ok(requestBody.getBaseData());
            else
                return ResponseData.error(1,"");
        }
        else if("save".equalsIgnoreCase(method))
        {
            if(save(model, datasource, requestBody.getBaseData()))
                return ResponseData.ok(requestBody.getBaseData());
            else
                return ResponseData.error(1,"");
        }
        else if("remove".equalsIgnoreCase(method))
        {
            Serializable key=requestBody.getKey();
            if(ObjectUtils.isEmpty(key))
                key=requestBody.getBaseData().getSerializableValue(model.getKeyPropertyName());
            if(ObjectUtils.isEmpty(key))
                return ResponseData.error(400,"key 没有输入");
            if(remove(model, datasource, key))
                return ResponseData.ok(requestBody.getBaseData());
            else
                return ResponseData.error(1,"");
        }
        else if("get".equalsIgnoreCase(method))
        {
            Serializable key=requestBody.getKey();
            if(ObjectUtils.isEmpty(key))
                key=requestBody.getBaseData().getSerializableValue(model.getKeyPropertyName());
            if(ObjectUtils.isEmpty(key))
                return ResponseData.error(400,"key 没有输入");
            BaseData data=get(model,datasource,key);
            if(data!=null)
                return ResponseData.ok(data);
            else
                return ResponseData.error(404,"");
        }
        else if("checkKey".equalsIgnoreCase(method))
        {
            if(checkKey(model, datasource, requestBody.getBaseData()))
                return ResponseData.ok(requestBody.getBaseData());
            else
                return ResponseData.error(1,"");
        }
        else if("getDraft".equalsIgnoreCase(method))
        {
            return ResponseData.ok(getDraft(model, datasource, requestBody.getBaseData()));
        }
        else if("createBatch".equalsIgnoreCase(method))
        {
            createBatch(model, datasource, requestBody.getListData());
            return ResponseData.ok(true);
        }
        else if("updateBatch".equalsIgnoreCase(method))
        {
            updateBatch(model, datasource, requestBody.getListData());
            return ResponseData.ok(true);
        }
        else if("saveBatch".equalsIgnoreCase(method))
        {
            saveBatch(model, datasource, requestBody.getListData());
            return ResponseData.ok(true);
        }
        return null;
    }

    @Override
    public boolean create(DOModel model, String datasource, BaseData et)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        return getProxyService(datasource).create(model,datasource,et);
    }

    @Override
    public void createBatch(DOModel model, String datasource, List<BaseData> list)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        getProxyService(datasource).createBatch(model,datasource,list);
    }

    @Override
    public boolean update(DOModel model, String datasource, BaseData et)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        return getProxyService(datasource).update(model,datasource,et);
    }

    @Override
    public void updateBatch(DOModel model, String datasource, List<BaseData> list)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        getProxyService(datasource).updateBatch(model,datasource,list);
    }

    @Override
    public boolean remove(DOModel model, String datasource, Serializable key)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        return getProxyService(datasource).remove(model,datasource,key);
    }

    @Override
    public void removeBatch(DOModel model, String datasource, List<Serializable> idList)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        getProxyService(datasource).removeBatch(model,datasource,idList);
    }

    @Override
    public BaseData get(DOModel model, String datasource, Serializable key)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        return getProxyService(datasource).get(model,datasource,key);
    }

    @Override
    public BaseData getDraft(DOModel model, String datasource, BaseData et)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        return getProxyService(datasource).getDraft(model,datasource,et);
    }

    @Override
    public boolean checkKey(DOModel model, String datasource, BaseData et)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        return getProxyService(datasource).checkKey(model,datasource,et);
    }

    @Override
    public boolean save(DOModel model, String datasource, BaseData et)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        return getProxyService(datasource).save(model,datasource,et);
    }

    @Override
    public void saveBatch(DOModel model, String datasource, List<BaseData> list)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        getProxyService(datasource).saveBatch(model,datasource,list);
    }

    @Override
    public Page<BaseData> fetch(DOModel model, String dataSet, String datasource, SearchContextBase context)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        return getProxyService(datasource).fetch(model,dataSet,datasource,context);
    }

    @Override
    public List<BaseData> list(DOModel model, String dataSet, String datasource, SearchContextBase context)
    {
        if(StringUtils.isEmpty(datasource))datasource=model.getDefaultDataSource();
        return getProxyService(datasource).list(model,dataSet,datasource,context);
    }

}


