package com.yihu.jw.thirdUpload.service;

import com.yihu.jw.entity.thirdUpload.UpAppointmentOnlineDO;
import com.yihu.jw.entity.thirdUpload.UpNsOrganBaseDO;
import com.yihu.jw.entity.thirdUpload.UpnsDoctorRecordDO;
import com.yihu.jw.entity.thirdUpload.UpnsDoctorScoreDO;
import com.yihu.jw.thirdUpload.UpnsDoctorRecordDao;
import com.yihu.jw.thirdUpload.UpnsOrganBaseDao;
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 UpnsDoctorRecordService extends BaseJpaService<UpnsDoctorRecordDO, UpnsDoctorRecordDao> {
    @Autowired
    private UpnsDoctorRecordDao upnsDoctorRecordDao;

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

    public void deleteById(String id){
        UpnsDoctorRecordDO upnsDoctorRecordDO =  upnsDoctorRecordDao.findOne(id);
        if (upnsDoctorRecordDO!=null){
            upnsDoctorRecordDO.setIs_del("0");
            upnsDoctorRecordDao.save(upnsDoctorRecordDO);
        }

    }

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

    public List<UpnsDoctorRecordDO> findAll(){
        return upnsDoctorRecordDao.findAll();
    }

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

}
