package com.yihu.hos.resource.service.impl;

import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.resource.dao.IRsDemensionCategoryDao;
import com.yihu.hos.resource.dao.IRsDimensionDao;
import com.yihu.hos.resource.dao.IRsResourceDimensionDao;
import com.yihu.hos.resource.model.RsDemensionCategory;
import com.yihu.hos.resource.model.RsDimension;
import com.yihu.hos.resource.service.IRsDimensionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by chenweida on 2016/1/26.
 */
@Service("rsDimensionService")
public class RsDimensionServiceImpl implements IRsDimensionService {
    @Resource(name = "resourceDimensionDao")
    private IRsResourceDimensionDao resourceDimensionDao;
    @Resource(name = "demensionCategoryDao")
    private IRsDemensionCategoryDao demensionCategoryDao;
    @Resource(name = "dimensionDao")
    private IRsDimensionDao dimensionDao;

    @Override
    public Result getDimensionCategoryList() throws Exception {
        ActionResult actionResult = new ActionResult();
        actionResult.setData(demensionCategoryDao.getDimensionCategoryList());
        return actionResult;
    }

    @Override
    public Result getDimensionList(Map<String, Object> conditionMap) throws Exception {
        return resourceDimensionDao.getDimensionList(conditionMap);
    }

    @Override
    public RsDimension getDimensionById(String id) throws Exception {
        return dimensionDao.getEntity(RsDimension.class, id);
    }

    @Transactional
    public Result updateDimension(RsDimension rsDimension) throws Exception {
        RsDimension dimensionTemp = dimensionDao.getEntity(RsDimension.class, rsDimension.getId());
        dimensionTemp.setCategory(rsDimension.getCategory());
        dimensionTemp.setDictCode(rsDimension.getDictCode());
        dimensionTemp.setType(rsDimension.getType());
        dimensionTemp.setName(rsDimension.getName());
        return Result.success("修改成功");
    }

    @Transactional
    public Result addDimension(RsDimension rsDimension) throws Exception {
        dimensionDao.saveEntity(rsDimension);

        return Result.success("保存成功");
    }

    @Transactional
    public Result deleteDimensionById(String id) throws Exception {
        dimensionDao.deleteEntity(dimensionDao.getEntity(RsDimension.class, id));

        return Result.success("删除成功");
    }

    @Override
    public Result getDimensionCatecoryList(Map<String, Object> conditionMap) throws Exception {
        return demensionCategoryDao.getDimensionCatecoryList(conditionMap);
    }

    @Transactional
    public Result deleteDimensionCatetory(String id) throws Exception {
        demensionCategoryDao.deleteEntity(demensionCategoryDao.getEntity(RsDemensionCategory.class, id));
        return Result.success("删除成功");
    }

    @Override
    public RsDemensionCategory getDimensionCatetoryById(String id) throws Exception {
        return demensionCategoryDao.getEntity(RsDemensionCategory.class, id);
    }

    @Transactional
    public Result addDimensionCatetroy(RsDemensionCategory r) throws Exception {
        demensionCategoryDao.saveEntity(r);
        return Result.success("保存成功");
    }

    @Transactional
    public Result updateDimensionCatetroy(RsDemensionCategory r) throws Exception {
        RsDemensionCategory tsDemensionCategoryTemp = demensionCategoryDao.getEntity(RsDemensionCategory.class, r.getId());
        tsDemensionCategoryTemp.setName(r.getName());
        return Result.success("修改成功");
    }
}
