package com.yihu.jw.thirdUpload.service;

import com.yihu.jw.entity.thirdUpload.BaseNsOnlineAskDO;
import com.yihu.jw.entity.thirdUpload.BaseNsOnlineMedDO;
import com.yihu.jw.thirdUpload.BaseNsOnlineAskDao;
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 BaseNsOnlineAskService extends BaseJpaService<BaseNsOnlineAskDO, BaseNsOnlineAskDao> {

    @Autowired
    private BaseNsOnlineAskDao baseNsOnlineAskDao;

    public void saveNsOlineAsk(List<BaseNsOnlineAskDO> baseNsOnlineAskDOS) throws Exception{

        for (BaseNsOnlineAskDO baseNsOnlineAskDO : baseNsOnlineAskDOS){
            if (StringUtils.isNotBlank(baseNsOnlineAskDO.getId())){
                BaseNsOnlineAskDO old = baseNsOnlineAskDao.findOne(baseNsOnlineAskDO.getId());
                BeanUtils.copyProperties(baseNsOnlineAskDO, old, getNullPropertyNames(baseNsOnlineAskDO));
                //待完善
                baseNsOnlineAskDao.save(old);
            }else{
//                baseNsOnlineAskDO.setCreateTime(new Date());
//                baseNsOnlineAskDO.setUpload("1");
                baseNsOnlineAskDao.save(baseNsOnlineAskDO);
            }
        }

    }

    public void deleteById(String id){
        BaseNsOnlineAskDO baseNsOnlineAskDO =  baseNsOnlineAskDao.findOne(id);
        if (baseNsOnlineAskDO != null){
            baseNsOnlineAskDao.delete(id);
            //baseNsOnlineAskDO.setIs_del("0");
            //baseNsOnlineAskDO.save(baseNsOnlineAskDO);
        }
    }

    public List<BaseNsOnlineAskDO> findAll(){
        return baseNsOnlineAskDao.findAll();
    }

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

}
