package com.yihu.wlyy.service.app.followup;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.wlyy.entity.dict.DmDrugsGroupItem;
import com.yihu.wlyy.entity.followup.FollowupContent;
import com.yihu.wlyy.entity.followup.FollowupDrugs;
import com.yihu.wlyy.repository.dict.DmDrugsGroupItemDao;
import com.yihu.wlyy.repository.dict.SystemDictDao;
import com.yihu.wlyy.repository.followup.FollowupContentDao;
import com.yihu.wlyy.repository.followup.FollowupDrugsDao;
import com.yihu.wlyy.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.*;

/**
 * 随访用药服务
 * @author hzp add 2016-12-08
 *
 */
@Component
@Transactional(rollbackOn = Exception.class)
public class FollowupDrugsService extends BaseService {


	@Autowired
	private FollowupContentDao followupContentDao;

	@Autowired
	private FollowupDrugsDao followupDrugsDao;

	@Autowired
	private DmDrugsGroupItemDao dmDrugsGroupItemDao;

	@Autowired
	private SystemDictDao systemDictDao;

	@Autowired
	private ObjectMapper objectMapper;

	private String drugsProject = "6";

	/**
	 * 获取面访用药数据
	 */
	public Map<String,Object> getFollowupDrugs(String id) throws Exception
	{
		Map<String,Object> re = new HashMap<>();
		List<FollowupContent> dataList =  followupContentDao.findByFollowupIdAndFollowupProject(Long.valueOf(id),drugsProject);
		for(FollowupContent item:dataList)
		{
			re.put(item.getFollowupKey(),item.getFollowupValue());
		}

		//获取用药记录
		List<FollowupDrugs> drugsList = followupDrugsDao.findByFollowupId(Long.valueOf(id));
		re.put("DRUG_LIST",drugsList);

		return re;
	}


	/**
	 *保存面访用药数据
	 */
	@Transactional
	public void saveFollowupDrugs(String id,String drugsData) throws Exception {
		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Map.class);
		List<Map<String,String>> list = objectMapper.readValue(drugsData,javaType);

		Long followupId = Long.valueOf(id);
		//删除原有用药记录
		followupDrugsDao.deleteByFollowupId(Long.valueOf(followupId));

		if(list!=null && list.size()>0)
		{
			List<FollowupDrugs> newList = new ArrayList<>();
			for(Map<String,String> item:list)
			{
				FollowupDrugs drug = new FollowupDrugs();
				drug.setFollowupId(followupId);
				drug.setDrugsGroup(item.get("drugsGroup"));
				drug.setDrugsCode(item.get("drugsCode"));
				drug.setDrugsName(item.get("drugsName"));
				drug.setDose(Double.valueOf(String.valueOf(item.get("dose"))));
				drug.setUnit(item.get("unit"));
				drug.setFrequency(item.get("frequency"));
				drug.setCreateTime(new Date());

				newList.add(drug);
			}
			followupDrugsDao.save(newList);
		}
	}

	/*********************************** 疾病用药 *****************************************************************/
	/**
	 *获取疾病用药
	 */
	public List<DmDrugsGroupItem> getFollowupClassDrugs(String followupClass) throws Exception {
		return dmDrugsGroupItemDao.findByFollowupClass(followupClass);
	}




}
