package com.yihu.jw.dict.service;

import com.yihu.jw.entity.hospital.manage.BaseDiseaseHomepageBannerDO;
import com.yihu.jw.hospital.dict.BaseDiseaseHomepageBannerDao;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang.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 DiseaseHomepageBannerService extends BaseJpaService<BaseDiseaseHomepageBannerDO, BaseDiseaseHomepageBannerDao> {
    @Autowired
    private BaseDiseaseHomepageBannerDao diseaseHomepageBannerDao;

    public BaseDiseaseHomepageBannerDO updateDiseaseHomepageBanner(BaseDiseaseHomepageBannerDO diseaseHomepageBannerDO) {
        //判断新增或编辑
        if (diseaseHomepageBannerDO.getId() == null) {
            diseaseHomepageBannerDO.setCreateTime(new Date());
            diseaseHomepageBannerDO.setDel(1);
            return diseaseHomepageBannerDao.save(diseaseHomepageBannerDO);
        } else {
            BaseDiseaseHomepageBannerDO oldDiseaseHomepageBanner = diseaseHomepageBannerDao.findOne(diseaseHomepageBannerDO.getId());
            BeanUtils.copyProperties(diseaseHomepageBannerDO, oldDiseaseHomepageBanner, getNullPropertyNames(diseaseHomepageBannerDO));
            return diseaseHomepageBannerDao.save(oldDiseaseHomepageBanner);
        }
    }

    public void delDiseaseHomepageBanner(Integer id) {
        diseaseHomepageBannerDao.delete(id);
    }

    public MixEnvelop findDiseaseHomepageBanner(Integer id, String name, Integer page, Integer pageSize) throws Exception {
        MixEnvelop mixEnvelop = new MixEnvelop();
        String filters = "";
        if (id != null) {
            filters += "id=" + id + ";";
        }else {
            if (StringUtils.isNotBlank(name)) {
                filters += "name?" + name + ";";
            }
        }
        Integer count = this.search(null, filters, null).size();
        List<BaseDiseaseHomepageBannerDO> list;
        if(page!= null && pageSize!=null) {
            list = this.search(null, filters, null, page, pageSize);
        }else {
            list = this.search(null, filters, null);
        }
        mixEnvelop.setMessage("获取成功！");
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setTotalCount(count);
        mixEnvelop.setDetailModelList(list);
        return mixEnvelop;
    }
}
