package com.yihu.jw.thirdUpload.service;

import com.yihu.jw.entity.thirdUpload.UpMedicalOnlineDO;
import com.yihu.jw.entity.thirdUpload.UpnsDoctorRecordDO;
import com.yihu.jw.thirdUpload.UpMedicalOnlineDao;
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 UpMedicalOnlineService extends BaseJpaService<UpMedicalOnlineDO, UpMedicalOnlineDao> {
    @Autowired
    private UpMedicalOnlineDao upMedicalOnlineDao;

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

    public void deleteById(String id){
        UpMedicalOnlineDO upMedicalOnlineDO =  upMedicalOnlineDao.findOne(id);
        if (upMedicalOnlineDO!=null){
            upMedicalOnlineDO.setIs_del("0");
            upMedicalOnlineDao.save(upMedicalOnlineDO);
        }

    }

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

    public List<UpMedicalOnlineDO> findAll(){
        return upMedicalOnlineDao.findAll();
    }

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

}
