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


import com.yihu.hos.standard.model.standard.StandardModel;
import com.yihu.hos.standard.model.standard.StdCDACatalogModel;
import com.yihu.hos.web.framework.constant.ErrorCode;
import com.yihu.hos.web.framework.constant.SqlConstants;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.exception.ApiException;
import com.yihu.hos.web.framework.util.sql.RequestParamTransformer;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author AndyCai
 * @version 1.0
 * @created 11-12月-2015 15:53:02
 */
@Transactional
@Service("StdCDACatalogService")
public class StdCDACatalogService extends SQLGeneralDAO {

    public static final String BEAN_ID = "StdCDACatalogService";

    public StdCDACatalogService() {

    }

    public void finalize() throws Throwable {

    }

    public List<StdCDACatalogModel> getList(String condition, String order, Integer limit, Integer offset) {
        try {
            Map queryMap = RequestParamTransformer.parseJsonToMap(condition);
            Map orderMap = RequestParamTransformer.parseJsonToMap(order);
            return this.getList(queryMap, orderMap, limit, offset);
        } catch (Exception e) {
            e.printStackTrace();
        }

        throw new ApiException(ErrorCode.StandardCDACatalogGetListFailed);
    }

    public Object add(String catalog) {
        try {
            StdCDACatalogModel standardModel = objectMapper.readValue(catalog, StdCDACatalogModel.class);
            this.add(standardModel);

            return standardModel.getId();
        } catch (Exception e) {
            e.printStackTrace();
        }

        throw new ApiException(ErrorCode.StandardCDACatalogSaveFailed);
    }

    public void delete(Integer catalogId) throws Exception {
        try {
            if (catalogId != null) {
                StdCDACatalogModel stdCdaCatalogModel = get(catalogId);
                deleteEntity(stdCdaCatalogModel);
            }
        } catch (Exception e) {
            throw new ApiException(ErrorCode.StandardCDACatalogDeleteFailed);
        }
    }

    public Object update(String catalog) {
        try {
            StdCDACatalogModel standardModel = objectMapper.readValue(catalog, StdCDACatalogModel.class);
            this.update(standardModel);

            return standardModel;
        } catch (Exception e) {
            e.printStackTrace();
        }

        throw new ApiException(ErrorCode.StandardCDACatalogUpdateFailed);
    }

    public StdCDACatalogModel get(Integer catalogId) {
        try {
            return getEntity(StdCDACatalogModel.class, catalogId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        throw new ApiException(ErrorCode.StandardCDACatalogGetFailed);
    }

    private void update(StdCDACatalogModel stdCDACatalogModel) throws Exception {
        updateEntity(stdCDACatalogModel);
    }

    private void add(StdCDACatalogModel stdCDACatalogModel) throws Exception {
        saveEntity(stdCDACatalogModel);
    }

    private List<StdCDACatalogModel> getList(Map<String, Object> query, Map<String, String> order, Integer limit, Integer offset) {

        Session session = getCurrentSession();
        Criteria criteria = session.createCriteria(StandardModel.class);
        for (String key : query.keySet()) {
            criteria.add(Restrictions.eq(key, query.get(key)));
        }
        if (limit != null) {
            criteria.setMaxResults(limit);
            if (offset != null) {
                criteria.setFirstResult((offset - 1) * limit);
            }
        }
        if (order != null) {
            for (String key : order.keySet()) {
                if (order.get(key).equals(SqlConstants.ASC)) {
                    criteria.addOrder(Order.asc(key));
                } else if (order.get(key).equals(SqlConstants.DESC)) {
                    criteria.addOrder(Order.desc(key));
                }
            }
        }
        return (List<StdCDACatalogModel>) criteria.list();
    }
}