package com.yihu.jw.thirdUpload.service;

import com.yihu.jw.entity.thirdUpload.AchnsDoctorRecordDO;
import com.yihu.jw.entity.thirdUpload.BaseNsPrescriptionDrugDO;
import com.yihu.jw.thirdUpload.BaseNsPrescriptionDrugDao;
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 BaseNsPrescriptionDrugService extends BaseJpaService<BaseNsPrescriptionDrugDO, BaseNsPrescriptionDrugDao> {

    @Autowired
    private BaseNsPrescriptionDrugDao baseNsPrescriptionDrugDao;

    public void savePrescriptionDrug(List<BaseNsPrescriptionDrugDO> baseNsPrescriptionDrugDOS) throws Exception{

        for (BaseNsPrescriptionDrugDO baseNsPrescriptionDrugDO : baseNsPrescriptionDrugDOS){
            if (StringUtils.isNoneBlank(baseNsPrescriptionDrugDO.getId())){
                BaseNsPrescriptionDrugDO old = baseNsPrescriptionDrugDao.findOne(baseNsPrescriptionDrugDO.getId());
                BeanUtils.copyProperties(baseNsPrescriptionDrugDO, old, getNullPropertyNames(baseNsPrescriptionDrugDO));
                baseNsPrescriptionDrugDao.save(old);
            }else {
                baseNsPrescriptionDrugDao.save(baseNsPrescriptionDrugDO);
            }
        }

    }

    public void deleteById(String id) {
        BaseNsPrescriptionDrugDO baseNsPrescriptionDrugDO = baseNsPrescriptionDrugDao.findOne(id);
        if (baseNsPrescriptionDrugDO != null){
            baseNsPrescriptionDrugDao.delete(id);
        }
    }

    public List<BaseNsPrescriptionDrugDO> findAll(){
        return baseNsPrescriptionDrugDao.findAll();
    }

    public List<BaseNsPrescriptionDrugDO> findAllByDate(String startDate, String endDate){
        Date startTime= DateUtil.strToDate(startDate+" 00:00:00");
        Date endTime= DateUtil.strToDate(endDate+" 23:59:59");
        return baseNsPrescriptionDrugDao.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 baseNsPrescriptionDrugDao.findCount(startTime, endTime);
    }

}
