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


import com.fasterxml.jackson.databind.JsonNode;
import com.yihu.hos.standard.model.adapter.AdapterDatasetRelationModel;
import com.yihu.hos.standard.service.bo.AdapterVersion;
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 com.yihu.hos.web.framework.util.sql.SqlCreator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("AdapterDatasetRelationService")
public class AdapterDatasetRelationService extends SQLGeneralDAO {

    public static final String BEAN_ID = "AdapterDatasetRelationService";

    public AdapterDatasetRelationService() {
    }

    public void finalize() throws Throwable {
        super.finalize();
    }

    public AdapterDatasetRelationModel add(String adapterVersion, String datasetRelation) {
        try {
            AdapterVersion version = new AdapterVersion(adapterVersion);
            AdapterDatasetRelationModel adapterDatasetRelationModel = objectMapper.readValue(datasetRelation, AdapterDatasetRelationModel.class);
            Session session = getCurrentSession();
            String sql = "select max(id) from " + version.getMetaDataTableName();
            Query query = session.createSQLQuery(sql);
            Object object = query.uniqueResult();
            adapterDatasetRelationModel.setId(object == null ? 1 : Integer.parseInt(object.toString()) + 1);

            JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(adapterDatasetRelationModel));
            SqlCreator sqlCreator = new SqlCreator(AdapterDatasetRelationModel.class);
            sql = sqlCreator.insertData(version.getMetaDataTableName(), jsonNode);
            query = getExeuteQuery(sqlCreator, sql);
            query.executeUpdate();
            adapterDatasetRelationModel.setSuccessFlg(true);
            adapterDatasetRelationModel.setMessage("保存数据集关联成功");
            return adapterDatasetRelationModel;
        } catch (Exception e) {
            throw new ApiException(ErrorCode.SaveDatasetRelationFailed);
        }
    }

    public void delete(String adapterVersion, Integer datasetRelationId) {
        try {
            if (adapterVersion == null || adapterVersion.length() == 0) {
                throw new ApiException(ErrorCode.InvalidStdVersion);
            }
            AdapterVersion version = new AdapterVersion(adapterVersion);
            Session session = getCurrentSession();
            String sql = "delete from " + version.getMetaDataTableName() + " where id = :id";
            Query query = session.createSQLQuery(sql);
            query.setInteger("id", datasetRelationId);
            query.executeUpdate();
        } catch (Exception e) {
            throw new ApiException(ErrorCode.DeleteDataSetRelationFailed);
        }
    }

    public AdapterDatasetRelationModel modify(String adapterVersion, String datasetRelation) {
        try {
            AdapterVersion version = new AdapterVersion(adapterVersion);
            AdapterDatasetRelationModel adapterDatasetRelationModel = objectMapper.readValue(datasetRelation, AdapterDatasetRelationModel.class);
            JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(adapterDatasetRelationModel));
            SqlCreator sqlCreator = new SqlCreator(AdapterDatasetRelationModel.class);
            String sql = sqlCreator.updateDataByTableKey(version.getMetaDataTableName(), jsonNode);
            Query query = getExeuteQuery(sqlCreator, sql);
            query.executeUpdate();
            return adapterDatasetRelationModel;
        } catch (Exception e) {
            throw new ApiException(ErrorCode.UpdateatasetRelationFailed);
        }
    }
}