package com.emr.service.emrDictionary;

import com.emr.dao.Emr_DictionaryMapper;
import com.emr.entity.Emr_Dictionary;
import com.emr.entity.Power_User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EmrDictionaryServiceImpl implements EmrDictionaryService {
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private Emr_DictionaryMapper dictionaryMapper;
    @Override
    public void add(Emr_Dictionary emrDictionary, HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        emrDictionary.setCreater(user.getUserName());
        emrDictionary.setUpdater(user.getUserName());
        emrDictionary.setCreateTime(fmt.format(new Date()));
        emrDictionary.setUpdateTime(fmt.format(new Date()));
        dictionaryMapper.insert(emrDictionary);
    }

    @Override
    public void update(Emr_Dictionary emrDictionary, HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        emrDictionary.setUpdater(user.getUserName());
        emrDictionary.setUpdateTime(fmt.format(new Date()));
        dictionaryMapper.updateByPrimaryKeySelective(emrDictionary);
    }

    @Override
    public void delete(Integer id) {
        dictionaryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public String getDictionaryByParentId(Integer parentId) {
        return dictionaryMapper.getDictionaryByParentId(parentId);
    }

    @Override
    public List<Emr_Dictionary> selectDictionaryByTypeCode(String typeName) {
        return dictionaryMapper.selectDictionaryByTypeCode(typeName);
    }
}
