package com.yihu.hos.standard.service.adapter;

import com.yihu.hos.standard.model.adapter.AdapterSchemeDispatchModel;
import com.yihu.hos.web.framework.constant.ErrorCode;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.exception.ApiException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @created lingfeng 2015/12/25.
 */
@Transactional
@Service("AdapterSchemeDispatchService")
public class AdapterSchemeDispatchService extends SQLGeneralDAO {

    public static final String BEAN_ID = "AdapterSchemeDispatchService";

    public AdapterSchemeDispatchModel add(String dispatcher) {
        try {
            AdapterSchemeDispatchModel model = objectMapper.readValue(dispatcher, AdapterSchemeDispatchModel.class);
            saveEntity(model);
            return model;
        } catch (Exception e) {
            throw new ApiException(ErrorCode.SaveSchemeFailed);
        }
    }

    public void delete(Integer dispatchId) {
        try {
            AdapterSchemeDispatchModel model = getEntity(AdapterSchemeDispatchModel.class, dispatchId);
            deleteEntity(model);
        } catch (Exception e) {
            throw new ApiException(ErrorCode.DeleteSchemeFailed);
        }
    }

    public AdapterSchemeDispatchModel update(String dispatcher) {
        try {
            AdapterSchemeDispatchModel model = objectMapper.readValue(dispatcher, AdapterSchemeDispatchModel.class);
            updateEntity(model);
            return model;
        } catch (Exception e) {
            throw new ApiException(ErrorCode.UpdateSchemeFailed);
        }
    }

    public AdapterSchemeDispatchModel get(Integer dispatchId) {
        try {
            AdapterSchemeDispatchModel model = getEntity(AdapterSchemeDispatchModel.class, dispatchId);
            return model;
        } catch (Exception e) {
            throw new ApiException(ErrorCode.GetSchemeFailed);
        }
    }
}
