package com.yihu.jw.thirdUpload.service;

import com.yihu.jw.entity.thirdUpload.AchnsDoctorRecordDO;
import com.yihu.jw.entity.thirdUpload.BaseNsOnlineAskDO;
import com.yihu.jw.entity.thirdUpload.UpNsOrganBaseDO;
import com.yihu.jw.entity.thirdUpload.UpnsDoctorScoreDO;
import com.yihu.jw.thirdUpload.AchnsDoctorRecordDao;
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 UpnsOrganBaseService extends BaseJpaService<UpNsOrganBaseDO, UpnsOrganBaseDao> {
    @Autowired
    private UpnsOrganBaseDao upnsOrganBaseDao;

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

    public void deleteById(String id){
        UpNsOrganBaseDO upNsOrganBaseDO =  upnsOrganBaseDao.findOne(id);
        if (upNsOrganBaseDO!=null){
            upNsOrganBaseDO.setIs_del("0");
            upnsOrganBaseDao.save(upNsOrganBaseDO);
        }

    }

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

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

}
