package com.unis.base.server.domain;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.DYpxxHisVO;
import com.unis.base.common.vo.DYpxxQueryVO;
import com.unis.base.common.vo.DYpxxVO;
import com.unis.base.common.vo.KdBaseInfoVO;
import com.unis.base.server.dao.*;
import com.unis.base.server.entity.*;
import com.unis.common.utils.DateUtil;
import com.unis.common.utils.LsBeanUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：yangshengjie
 * @date ：Created in 2019/9/19 20:26
 * @Title DYpxxDomain
 * @ProjectName 持久化
 */
@Component
public class DYpxxDomain extends BaseDomain{

    @Autowired
	private DYpxxDao dYpxxDao;
	@Autowired
	private FqwxhxpcszclyclqkbDao fqwxhxpcszclyclqkbDao;
	@Autowired
	private WxhxphjglpcjbbDao wxhxphjglpcjbbDao;
	@Autowired
	private ClhxyjbgtzDao clhxyjbgtzDao;
    @Autowired
    private KcBaseInfoDao kcBaseInfoDao;
    @Autowired
    private DYpxxHisDomain dYpxxHisDomain;

    /**
     * create time: 2019/9/19 20:30
     * 根据参数获取药剂信息
     * @param pageSize 
     * @param pageNum 
     * @Param: DYpxxQueryVO
     * @return: List
     */

    public PageInfo<DYpxxVO> findYpxxList(DYpxxQueryVO dYpxxQueryVO, Integer pageNum, Integer pageSize){
        PageInfo<DYpxxVO> pageInfoVO = new PageInfo<DYpxxVO>();
		PageHelper.startPage(pageNum, pageSize);
		DYpxxDO dYpxxDO = new DYpxxDO();
        LsBeanUtils.copyAllProperties(dYpxxQueryVO,dYpxxDO);
		List<DYpxxDO> dYpxxHisDOs =  dYpxxDao.findYpxxList(dYpxxDO);
		PageInfo<DYpxxDO> PageInfoDO = new PageInfo<DYpxxDO>(dYpxxHisDOs);
		LsBeanUtils.copyAllProperties(PageInfoDO,pageInfoVO);
		return pageInfoVO;
    }

//	public Map<String, Object> findFqwxhxpcszclyclqkbList(FqwxhxpcszclyclqkbVO VO, Integer page, Integer size){
//		Example example = new Example(FqwxhxpcszclyclqkbDO.class);
//		Example.Criteria criteria = example.createCriteria();
//		// 添加按库点名称查询
//		if (!"".equals(VO.getQymc()) && null != VO.getQymc()) {
//			criteria.andLike("qymc", "%" + VO.getQymc() + "%");
//		}
//		// 添加按企业名称查询
//		if (!"".equals(VO.getKdmc()) && null != VO.getKdmc()) {
//			criteria.andLike("kdmc", "%" + VO.getKdmc() + "%");
//		}
//		// 添加按年度查询
//		if (!"".equals(VO.getJhnd()) && null != VO.getJhnd()) {
//			criteria.andEqualTo("areaCode",  VO.getJhnd());
//		}
//		PageHelper.startPage(page, size);
//		List<FqwxhxpcszclyclqkbDO> resultDOList = fqwxhxpcszclyclqkbDao.selectByExample(example);
//		PageInfo<FqwxhxpcszclyclqkbDO> pageInfo = new PageInfo<FqwxhxpcszclyclqkbDO>(resultDOList);
//		Map<String, Object> resultMap = new HashMap<String, Object>();
//		List<FqwxhxpcszclyclqkbVO> resultVOList = new ArrayList<>();
//		if (!resultDOList.isEmpty()) {
//			for (FqwxhxpcszclyclqkbDO resultDO : resultDOList) {
//				FqwxhxpcszclyclqkbVO resultVO = LsBeanUtils.copyProperties(resultDO, FqwxhxpcszclyclqkbVO.class);
//				resultVOList.add(resultVO);
//			}
//		}
//		resultMap.put("total", pageInfo.getTotal());
//		resultMap.put("resultList", resultVOList);
//		return resultMap;
//	}

//	public Map<String, Object> findWxhxphjglpcjbbList(WxhxphjglpcjbbVO VO, Integer page, Integer size){
//		Example example = new Example(WxhxphjglpcjbbDO.class);
//		Example.Criteria criteria = example.createCriteria();
//		// 添加按库点名称查询
//		if (!"".equals(VO.getQymc()) && null != VO.getQymc()) {
//			criteria.andLike("qymc", "%" + VO.getQymc() + "%");
//		}
//		// 添加按企业名称查询
//		if (!"".equals(VO.getKdmc()) && null != VO.getKdmc()) {
//			criteria.andLike("kdmc", "%" + VO.getKdmc() + "%");
//		}
//		// 添加按年度查询
//		if (!"".equals(VO.getJhnd()) && null != VO.getJhnd()) {
//			criteria.andEqualTo("jhnd",  VO.getJhnd());
//		}
//		PageHelper.startPage(page, size, "XH");
//		List<WxhxphjglpcjbbDO> resultDOList = wxhxphjglpcjbbDao.selectByExample(example);
//		PageInfo<WxhxphjglpcjbbDO> pageInfo = new PageInfo<WxhxphjglpcjbbDO>(resultDOList);
//		Map<String, Object> resultMap = new HashMap<String, Object>();
//		List<WxhxphjglpcjbbVO> resultVOList = new ArrayList<>();
//		if (!resultDOList.isEmpty()) {
//			for (WxhxphjglpcjbbDO resultDO : resultDOList) {
//				WxhxphjglpcjbbVO resultVO = LsBeanUtils.copyProperties(resultDO, WxhxphjglpcjbbVO.class);
//				resultVOList.add(resultVO);
//			}
//		}
//		resultMap.put("total", pageInfo.getTotal());
//		resultMap.put("resultList", resultVOList);
//		return resultMap;
//	}

	public Map<String, Object> findReportsInfoList(KdBaseInfoVO VO, Integer page, Integer size){
		Example example = new Example(KdBaseInfoDO.class);
		Example.Criteria criteria = example.createCriteria();
		if(StringUtils.isEmpty(VO.getTzqfCode())){
		    throw new RuntimeException("未在KdBaseInfo中填写tzqfCode字段。");
        }
        criteria.andEqualTo("tzqfCode", VO.getTzqfCode());
		// 添加按库点名称查询
		if (!"".equals(VO.getQymc()) && null != VO.getQymc()) {
			criteria.andLike("qymc", "%" + VO.getQymc() + "%");
		}
		// 添加按企业名称查询
		if (!"".equals(VO.getKddm()) && null != VO.getKddm()) {
			criteria.andLike("kdmc",  VO.getKddm());
		}
		// 添加按填报时间查询
		if (!"".equals(VO.getTbsjFrom()) && null != VO.getTbsjFrom() && !"".equals(VO.getTbsjTo()) && null != VO.getTbsjTo()) {
			criteria.andBetween("tbsj", VO.getTbsjFrom(), VO.getTbsjTo());
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();

        PageHelper.startPage(page, size);
        List<KdBaseInfoDO> resultDOList = kcBaseInfoDao.selectByExample(example);
        PageInfo<KdBaseInfoDO> pageInfo = new PageInfo<KdBaseInfoDO>(resultDOList);

        List<KdBaseInfoVO> resultVOList = new ArrayList<>();
        if (!resultDOList.isEmpty()) {
            for (KdBaseInfoDO resultDO : resultDOList) {
                KdBaseInfoVO resultVO = LsBeanUtils.copyProperties(resultDO, KdBaseInfoVO.class);
                resultVOList.add(resultVO);
            }
        }
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("resultList", resultVOList);
        return resultMap;
	}

	public KdBaseInfoDO findKdBaseInfoById(String id) throws Exception {
		Example example = new Example(KdBaseInfoDO.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("id", id);
		KdBaseInfoDO resDO = kcBaseInfoDao.selectOneByExample(example);
		if (ObjectUtils.equals(resDO, null)) {
			return null;
		}
		return resDO;
	}

	public KdBaseInfoDO findWxhxphjglpcjbbApp(KdBaseInfoVO vo) throws Exception {
    	if(StringUtils.isEmpty(vo.getKddm())){
    		throw new RuntimeException("请填写库点代码");
		}
		if(vo.getTbsj() == null){
			throw new RuntimeException("请填写填报时间");
		}
		Example example = new Example(KdBaseInfoDO.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("kddm", vo.getKddm());
		criteria.andEqualTo("tzqfCode", "2");
		criteria.andCondition("to_char(tbsj,'yyyy-MM-dd') = "+ "'"+DateUtil.turnJavaUtilDateToStrDate(vo.getTbsj(), DateUtil.YYYYMMDD)+ "'");
		KdBaseInfoDO resDO = kcBaseInfoDao.selectOneByExample(example);
		if (ObjectUtils.equals(resDO, null)) {
			return null;
		}
		return resDO;
	}

	public List<ClhxyjbgtzDO> findClhxyjbgtzByKddm(String id) throws Exception {
		Example example = new Example(ClhxyjbgtzDO.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("kddm", id);
		List<ClhxyjbgtzDO> resDO = clhxyjbgtzDao.selectByExample(example);
		if (ObjectUtils.equals(resDO, null)) {
			return null;
		}
		return resDO;
	}

	public List<FqwxhxpcszclyclqkbDO> findFqwxhxpcszclyclqkbByKddm(String id) throws Exception {
		Example example = new Example(FqwxhxpcszclyclqkbDO.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("kddm", id);
		List<FqwxhxpcszclyclqkbDO> resDO = fqwxhxpcszclyclqkbDao.selectByExample(example);
		if (ObjectUtils.equals(resDO, null)) {
			return null;
		}
		return resDO;
	}

	public List<WxhxphjglpcjbbDO> findWxhxphjglpcjbbByKdmc(String id) throws Exception {
		Example example = new Example(WxhxphjglpcjbbDO.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("kddm", id);
		List<WxhxphjglpcjbbDO> resDO = wxhxphjglpcjbbDao.selectByExample(example);
		if (ObjectUtils.equals(resDO, null)) {
			return null;
		}
		return resDO;
	}

	/**
	 * 
	 * @param dYpxxHisVO
	 * @return
	 */
	public Integer updateMedicineinfoStatus(DYpxxHisVO dYpxxHisVO) {
		Map<String, Object> updateMap = new HashMap<>();
		updateMap.put("ypid",dYpxxHisVO.getYpid());
		updateMap.put("sfba",dYpxxHisVO.getSfba());
    	int i = dYpxxDao.updateMedicineinfoStatus(updateMap);
		if (i == 0){
			return i;
		}
		return dYpxxHisDomain.insert(dYpxxHisVO);
	}

	/**
	 * 根据ID获取
	 * @param ypId
	 * @return
	 */
	public DYpxxVO getDYpxxInfoByLsId(String ypId) {
		DYpxxVO dYpxxVO=new DYpxxVO();
		DYpxxDO dYpxxDO=new DYpxxDO();
		dYpxxDO.setYpid(ypId);
		dYpxxDO=dYpxxDao.selectById(dYpxxDO);
		LsBeanUtils.copyAllProperties(dYpxxDO,dYpxxVO);
		return dYpxxVO;
	}
}
