package com.yihu.hos.system.service;

import com.yihu.hos.system.dao.DictDao;
import com.yihu.hos.system.dao.DictListDao;
import com.yihu.hos.system.model.SystemDict;
import com.yihu.hos.system.model.SystemDictList;
import com.yihu.hos.web.framework.model.Result;
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/20.
 */
@Service("dictManager")
public class DictManager  {

    public static final String BEAN_ID = "dictManager";

    @Resource(name =DictDao.BEAN_ID )
    private DictDao dictItemDao;//字典项
    @Resource(name = DictListDao.BEAN_ID)
    private DictListDao dictDao;//字典

    @Transactional
    public Result getDictList(Map<String, Object> params) throws Exception {
        return dictDao.getDictList(params);
    }

    @Transactional
    public Result getDictItemList(Map<String, Object> params) throws Exception {
        return dictItemDao.getDictList(params);
    }

   
    public SystemDict getSystemDictById(String id) throws Exception {
        return dictItemDao.getEntity(SystemDict.class, id);
    }

   
    public SystemDictList getSystemDictListById(String id) throws Exception {
        return dictDao.getEntity(SystemDictList.class, id);
    }

    @Transactional
    public Result deleteDict(String id) throws Exception {
        SystemDictList sd = dictDao.getEntity(SystemDictList.class, id);
        dictItemDao.delDictItemByDictName(sd.getDictName());
        dictDao.deleteEntity(sd);
        return Result.success("删除成功");
    }

    @Transactional
    public Result addDict(SystemDictList rr) throws Exception {
        SystemDictList systemDictListTemp = dictDao.getDitcByName(rr.getDictName());
        if (systemDictListTemp != null) {
            return Result.error("字典代码重复");
        }
        dictDao.saveEntity(rr);
        return Result.success("保存成功");
    }

    @Transactional
    public Result updateDict(SystemDictList rr) throws Exception {
        SystemDictList systemDictListTemp = dictDao.getDitcByNameWithOutId(rr.getDictName(), rr.getId());
        if (systemDictListTemp != null) {
            return Result.error("字典代码重复");
        }
        SystemDictList sd = dictDao.getEntity(SystemDictList.class, rr.getId());
        sd.setPid(rr.getPid());
        sd.setChineseName(rr.getChineseName());
        sd.setDictName(rr.getDictName());
        sd.setPyCode(rr.getPyCode());
        sd.setRelationColCode(rr.getRelationColCode());
        sd.setRelationColValue(rr.getRelationColValue());
        sd.setRelationTable(rr.getRelationTable());
        sd.setRemark(rr.getRemark());
        sd.setRelationColExtend(rr.getRelationColExtend());
        return Result.success("更新成功");
    }

    @Transactional
    public Result deleteDictItem(String id) throws Exception {
        SystemDict sd = dictItemDao.getEntity(SystemDict.class, id);
        dictItemDao.deleteEntity(sd);
        return Result.success("删除成功");
    }

    @Transactional
    public Result addDictItem(SystemDict rr) throws Exception {
       // SystemDict rrTemp = dictItemDao.getDictItemByCode(rr.getCode());
       // if (rrTemp != null) {
       //     return Result.error("字典项代码已经存在");
      //  }
        dictItemDao.saveEntity(rr);

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

    @Transactional
    public Result updateDictItem(SystemDict rr) throws Exception {
      //  SystemDict rrTemp = dictItemDao.getDictItemByCodeWithOutId(rr.getCode(),rr.getId());
       // if (rrTemp != null) {
       //    return Result.error("字典项代码已经存在");
      //  }
        SystemDict sd = dictItemDao.getEntity(SystemDict.class, rr.getId());
        sd.setPyCode(rr.getPyCode());
        sd.setDictName(rr.getDictName());
        sd.setCode(rr.getCode());
        sd.setSort(rr.getSort());
        sd.setValue(rr.getValue());
        return Result.success("更新成功");
    }
}
