package com.yihu.jw.thirdUpload.service;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.yihu.jw.doctor.dao.BaseDoctorHospitalDao;
import com.yihu.jw.entity.base.dict.BaseFrequencyDictDO;
import com.yihu.jw.entity.base.doctor.BaseDoctorHospitalDO;
import com.yihu.jw.entity.thirdUpload.AchnsDoctorRecordDO;
import com.yihu.jw.entity.thirdUpload.UpAppointmentOnlineDO;
import com.yihu.jw.restmodel.iot.device.IotDeviceImportVO;
import com.yihu.jw.thirdUpload.AchnsDoctorRecordDao;
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 AchnsDoctorRecordService extends BaseJpaService<AchnsDoctorRecordDO, AchnsDoctorRecordDao> {
    @Autowired
    private AchnsDoctorRecordDao achnsDoctorRecordDao;

    public void saveAchns(List<AchnsDoctorRecordDO> achnsDoctorRecordDOs) throws Exception{
        /*List<IotDeviceImportVO> importVOList = objectMapper.readValue(JSONObject.toJSONString(correctLs), new TypeReference<List<IotDeviceImportVO>>() {
        });*/
        for (AchnsDoctorRecordDO achnsDoctorRecordDO:achnsDoctorRecordDOs){
            if (StringUtils.isNotBlank(achnsDoctorRecordDO.getId())){
                AchnsDoctorRecordDO oldFrequency= achnsDoctorRecordDao.findOne(achnsDoctorRecordDO.getId());
                BeanUtils.copyProperties(achnsDoctorRecordDO, oldFrequency, getNullPropertyNames(achnsDoctorRecordDO));
                oldFrequency.setUpdate_time(new Date());
                oldFrequency.setIs_del("1");
                achnsDoctorRecordDao.save(oldFrequency);
            }else {
                achnsDoctorRecordDO.setCreate_time(new Date());
                achnsDoctorRecordDO.setIs_del("1");
                achnsDoctorRecordDao.save(achnsDoctorRecordDO);
            }
        }
    }

    public void deleteById(String id){
        AchnsDoctorRecordDO achnsDoctorRecordDO =  achnsDoctorRecordDao.findOne(id);
        if (achnsDoctorRecordDO!=null){
            achnsDoctorRecordDO.setIs_del("0");
            achnsDoctorRecordDao.save(achnsDoctorRecordDO);
        }

    }

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

    public List<AchnsDoctorRecordDO> findAllByDate(String startDate, String endDate){

        Date startTime= DateUtil.strToDate(startDate+" 00:00:00");
        Date endTime= DateUtil.strToDate(endDate+" 23:59:59");
        return achnsDoctorRecordDao.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 achnsDoctorRecordDao.findCount(startTime, endTime);
    }

}
