package com.yihu.jw.hospital.doctor.dao.service;

import com.yihu.jw.doctor.dao.BaseDoctorDao;
import com.yihu.jw.doctor.dao.BaseDoctorMessageDao;
import com.yihu.jw.entity.base.doctor.BaseDoctorBackgroundDO;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.entity.base.doctor.BaseDoctorMessageDO;
import com.yihu.jw.hospital.doctor.dao.BaseDoctorBackgroundDao;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BaseDoctorBGService extends BaseJpaService<BaseDoctorBackgroundDO, BaseDoctorBackgroundDao> {
    @Autowired
    private BaseDoctorBackgroundDao baseDoctorBackgroundDao;
    @Autowired
    private BaseDoctorDao baseDoctorDao;
    @Autowired
    private BaseDoctorMessageDao doctorMessageDao;
    @Autowired
    private HibenateUtils hibenateUtils;
    //查询背景
    public List<BaseDoctorBackgroundDO> getBackground(Integer page,Integer pageSize){
        return baseDoctorBackgroundDao.getBackroundAll(page,pageSize);
    }

    //医生选定背景
    @Transactional
    public BaseDoctorDO setDoctorBackground(String doctor,String id){
        BaseDoctorBackgroundDO baseDoctorBackgroundDO = baseDoctorBackgroundDao.findOne(id);
        BaseDoctorDO baseDoctorDO = baseDoctorDao.findById(doctor);
        if(null!=baseDoctorBackgroundDO&&null!=baseDoctorDO){
            baseDoctorDO.setBackgroundId(baseDoctorBackgroundDO.getId());
            baseDoctorDO.setBackground(baseDoctorBackgroundDO.getBackground());
            baseDoctorDO = baseDoctorDao.save(baseDoctorDO);
        }
        return baseDoctorDO;
    }
    //新增背景
    public BaseDoctorBackgroundDO createBackground(String bgName,String bgUrl){
        BaseDoctorBackgroundDO baseDoctorBackgroundDO = new BaseDoctorBackgroundDO();
        if(StringUtils.isNotEmpty(bgName)){
            baseDoctorBackgroundDO.setTitle(bgName);
        }if(StringUtils.isNotEmpty(bgUrl)){
            baseDoctorBackgroundDO.setBackground(bgUrl);
        }
        baseDoctorBackgroundDO =  baseDoctorBackgroundDao.save(baseDoctorBackgroundDO);
        return baseDoctorBackgroundDO;
    }
    //删除背景
    public void deleteBg(String bgId){
        BaseDoctorBackgroundDO baseDoctorBackgroundDO = baseDoctorBackgroundDao.findOne(bgId);
        if(null!=baseDoctorBackgroundDO){
            baseDoctorBackgroundDao.delete(bgId);
        }
    }
    //修改
    public BaseDoctorBackgroundDO updateBackground(String id,String bgUrl,String bgName){
        BaseDoctorBackgroundDO baseDoctorBackgroundDO =baseDoctorBackgroundDao.findOne(id);
        if(null!=baseDoctorBackgroundDO){
            if (StringUtils.isNotEmpty(bgName)){
                baseDoctorBackgroundDO.setTitle(bgName);
            }if (StringUtils.isNotEmpty(bgUrl)){
                baseDoctorBackgroundDO.setBackground(bgUrl);
            }
        }
        baseDoctorBackgroundDO = baseDoctorBackgroundDao.save(baseDoctorBackgroundDO);
        return  baseDoctorBackgroundDO;
    }


    /**
     * 查询某个医生快捷回复列表
     * @param doctor
     * @param page
     * @param size
     * @return
     */
    public List<Map<String,Object>> selectByDoctor(String doctor, Integer contentType, String content, Integer page, Integer size){
        String sql = "select id as \"id\", doctor as \"doctor\",doctor_name as \"doctorName\",content_type as \"contentType\"," +
                "content as \"content\" , content_desc as \"contentDesc\",is_sort as \"isSort\",del as \"del\",extend as \"extend\"  " +
                " from base_doctor_message where del=1 and doctor='"+doctor+"' ";
        if (contentType!=null){
            sql +=" and content_type= "+contentType;
        }

        if (StringUtils.isNotBlank(content)){
            sql +=" and content like '%"+content+"%' ";
        }

        sql +=" order by is_sort desc ";
        List<Map<String,Object>> mapList = hibenateUtils.createSQLQuery(sql,page,size);
        return mapList;
    }


    /**
     * 增加和更新快捷回复信息
     * @param doctorMessageDO
     * @return
     */
    @Transactional
    public BaseDoctorMessageDO saveDoctorMessage(BaseDoctorMessageDO doctorMessageDO){
        if (!StringUtils.isNotBlank(doctorMessageDO.getId())){
            String sql = "select max(dm.is_sort) as total from base_doctor_message dm where dm.del=1 ";
            List<Map<String,Object>> maps = jdbcTemplate.queryForList(sql);
            Integer isSort=0;
            if (maps!=null&&maps.size()!=0){
                if (maps.get(0).get("total")!=null){
                    isSort = Integer.parseInt(maps.get(0).get("total").toString())+1;
                }else {
                    isSort=isSort+1;
                }
            }else {
                isSort=isSort+1;
            }
            doctorMessageDO.setIsSort(isSort);
        }
        doctorMessageDO.setDel(1);
        doctorMessageDO.setCreateTime(new Date());
        doctorMessageDO.setUpdateTime(new Date());
        return  doctorMessageDao.save(doctorMessageDO);
    }


    /**
     * 设置置顶
     * @param id
     * @return
     */
    @Transactional
    public BaseDoctorMessageDO setMessageHead(String id){
        BaseDoctorMessageDO doctorMessageDO = doctorMessageDao.findOne(id);
        String sql = "select max(dm.is_sort) as total from base_doctor_message dm where dm.del=1  ";
        List<Map<String,Object>> maps = jdbcTemplate.queryForList(sql);
        Integer isSort=0;
        if (maps!=null&&maps.size()!=0){
            if (maps.get(0).get("total")!=null){
                isSort = Integer.parseInt(maps.get(0).get("total").toString())+1;
            }else {
                isSort=isSort+1;
            }

        }else {
            isSort=isSort+1;
        }
        doctorMessageDO.setIsSort(isSort);
        doctorMessageDO.setCreateTime(new Date());
        doctorMessageDO.setUpdateTime(new Date());
        return doctorMessageDao.save(doctorMessageDO);
    }


    @Transactional
    public BaseDoctorMessageDO delDoctorMessage(String id){
        BaseDoctorMessageDO doctorMessageDO = doctorMessageDao.findOne(id);
        doctorMessageDO.setDel(0);
        doctorMessageDO.setCreateTime(new Date());
        doctorMessageDO.setUpdateTime(new Date());
        return doctorMessageDao.save(doctorMessageDO);
    }


    public BaseDoctorMessageDO selectById(String id){
        return doctorMessageDao.findByIdAndDel(id);
    }


}
