package com.yihu.jw.thirdUpload.service;

import com.yihu.jw.entity.thirdUpload.AchnsDoctorRecordDO;
import com.yihu.jw.entity.thirdUpload.BaseNsOnlineMedDO;
import com.yihu.jw.thirdUpload.BaseNsOlineMedDao;
import com.yihu.jw.util.date.DateUtil;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.yihu.jw.util.common.BeanUtils.getNullPropertyNames;

@Service
public class BaseNsOlineMedService extends BaseJpaService<BaseNsOnlineMedDO, BaseNsOlineMedDao> {

    @Autowired
    private BaseNsOlineMedDao baseNsOlineMedDao;

    public void saveNsOnlinemed(List<BaseNsOnlineMedDO> baseNsOnlineMedDOList)throws Exception{
        for (BaseNsOnlineMedDO baseNsOlineMedDO : baseNsOnlineMedDOList){
            if(StringUtils.isNoneBlank(baseNsOlineMedDO.getId())){
                BaseNsOnlineMedDO old = baseNsOlineMedDao.findOne(baseNsOlineMedDO.getId());
                BeanUtils.copyProperties(baseNsOlineMedDO,old,getNullPropertyNames(baseNsOlineMedDO));
                baseNsOlineMedDao.save(old);
            }else{
                baseNsOlineMedDao.save(baseNsOlineMedDO);
            }
        }
    }

    public void deleteById(String id) {
        BaseNsOnlineMedDO baseNsOnlineMedDO = baseNsOlineMedDao.findOne(id);
        if(baseNsOnlineMedDO != null){
            baseNsOlineMedDao.delete(id);
        }
    }

    public List<BaseNsOnlineMedDO> findAll(){
        return baseNsOlineMedDao.findAll();
    }

    public List<BaseNsOnlineMedDO> findAllByDate(String startDate, String endDate){
        Date startTime= DateUtil.strToDate(startDate+" 00:00:00");
        Date endTime= DateUtil.strToDate(endDate+" 23:59:59");
        return baseNsOlineMedDao.finAllByDate(startTime,endTime);
    }

    public String findCount(String startDate, String endDate){
        Date startTime= DateUtil.strToDate(startDate+" 00:00:00");
        Date endTime= DateUtil.strToDate(endDate+" 23:59:59");
        return baseNsOlineMedDao.findCount(startTime, endTime);
    }

}
