package com.yihu.jw.thirdUpload.service;

import com.yihu.jw.entity.thirdUpload.BaseNsOnlineAskDO;
import com.yihu.jw.entity.thirdUpload.BaseNsPrescriptionDO;
import com.yihu.jw.entity.thirdUpload.BaseNsPrescriptionDrugDO;
import com.yihu.jw.thirdUpload.BaseNsPrescriptionDao;
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 BaseNsPrescriptionAService extends BaseJpaService<BaseNsPrescriptionDO, BaseNsPrescriptionDao> {


    @Autowired
    private BaseNsPrescriptionDao baseNsPrescriptionDao;

    public void savePrescription(List<BaseNsPrescriptionDO> baseNsPrescriptionDOS) throws Exception{

        for (BaseNsPrescriptionDO baseNsPrescriptionDO : baseNsPrescriptionDOS){
            if (StringUtils.isNoneBlank(baseNsPrescriptionDO.getId())){
                BaseNsPrescriptionDO old = baseNsPrescriptionDao.findOne(baseNsPrescriptionDO.getId());
                BeanUtils.copyProperties(baseNsPrescriptionDO, old, getNullPropertyNames(baseNsPrescriptionDO));
                baseNsPrescriptionDao.save(old);
            }else {
                baseNsPrescriptionDao.save(baseNsPrescriptionDO);
            }

        }

    }


    public void deleteById(String id) {
        BaseNsPrescriptionDO baseNsPrescriptionDO = baseNsPrescriptionDao.findOne(id);
        if (baseNsPrescriptionDO != null){
            baseNsPrescriptionDao.delete(id);
        }
    }

    public List<BaseNsPrescriptionDO> findAll(){
        return baseNsPrescriptionDao.findAll();
    }

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

}
