package com.yihu.jw.thirdUpload.service;

import com.yihu.jw.entity.thirdUpload.AchnsDoctorRecordDO;
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.AchnsDoctorRecordDao;
import com.yihu.jw.thirdUpload.UpnsDoctorScoreDao;
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 UpnsDoctorScoreService extends BaseJpaService<UpnsDoctorScoreDO, UpnsDoctorScoreDao> {
    @Autowired
    private UpnsDoctorScoreDao upnsDoctorScoreDao;

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

    public void deleteById(String id){
        UpnsDoctorScoreDO upnsDoctorScoreDO =  upnsDoctorScoreDao.findOne(id);
        if (upnsDoctorScoreDO!=null){
            upnsDoctorScoreDO.setIs_del("0");
            upnsDoctorScoreDao.save(upnsDoctorScoreDO);
        }

    }

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

    public List<UpnsDoctorScoreDO> findAll(){
        return upnsDoctorScoreDao.findAll();
    }

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

}
