package com.yihu.jw.thirdUpload.service;

import com.yihu.jw.entity.thirdUpload.BaseNsPrescriptionDrugDO;
import com.yihu.jw.entity.thirdUpload.UpAppointmentOnlineDO;
import com.yihu.jw.entity.thirdUpload.UpMedicalOnlineDO;
import com.yihu.jw.entity.thirdUpload.UpnsDoctorRecordDO;
import com.yihu.jw.thirdUpload.UpAppointmentOnlineDao;
import com.yihu.jw.thirdUpload.UpnsDoctorRecordDao;
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 UpAppointmentOnlineService extends BaseJpaService<UpAppointmentOnlineDO, UpAppointmentOnlineDao> {
    @Autowired
    private UpAppointmentOnlineDao upAppointmentOnlineDao;

    public void saveUpAppointment(List<UpAppointmentOnlineDO> upAppointmentOnlineDOS) throws Exception{
        /*List<IotDeviceImportVO> importVOList = objectMapper.readValue(JSONObject.toJSONString(correctLs), new TypeReference<List<IotDeviceImportVO>>() {
        });*/
        for (UpAppointmentOnlineDO upAppointmentOnlineDO:upAppointmentOnlineDOS){
            if (StringUtils.isNotBlank(upAppointmentOnlineDO.getId())){
                UpAppointmentOnlineDO old= upAppointmentOnlineDao.findOne(upAppointmentOnlineDO.getId());
                BeanUtils.copyProperties(upAppointmentOnlineDO, old, getNullPropertyNames(upAppointmentOnlineDO));
                old.setUpdate_time(new Date());
                old.setIs_del("1");
                upAppointmentOnlineDao.save(old);
            }else {
                upAppointmentOnlineDO.setCreate_time(new Date());
                upAppointmentOnlineDO.setIs_del("1");
                upAppointmentOnlineDao.save(upAppointmentOnlineDO);
            }
        }
    }

    public void deleteById(String id){
        UpAppointmentOnlineDO upAppointmentOnlineDO =  upAppointmentOnlineDao.findOne(id);
        if (upAppointmentOnlineDO!=null){
            upAppointmentOnlineDO.setIs_del("0");
            upAppointmentOnlineDao.save(upAppointmentOnlineDO);
        }

    }

    public List<UpAppointmentOnlineDO> findByCreateTime(String createTime){
        Date startTime= DateUtil.strToDate(createTime+" 00:00:00");
        Date endTime= DateUtil.strToDate(createTime+" 23:59:59");
        return upAppointmentOnlineDao.findByCreateTime(startTime,endTime);
    }

    public List<UpAppointmentOnlineDO> findAll(){
        return upAppointmentOnlineDao.findAll();
    }

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

}
