package com.yihu.jw.hospital.message.service;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.jw.doctor.dao.BaseDoctorDao;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.entity.base.im.ConsultDo;
import com.yihu.jw.entity.hospital.message.SystemMessageDO;
import com.yihu.jw.entity.hospital.prescription.WlyyOutpatientDO;
import com.yihu.jw.hospital.message.dao.SystemMessageDao;
import com.yihu.jw.hospital.prescription.dao.OutpatientDao;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.utils.StringUtil;
import com.yihu.jw.utils.YkyySMSService;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zmk
 * @vsrsion 1.0
 * Created at 2019/11/13
 */
@Service
@Transactional
public class SystemMessageService extends BaseJpaService<SystemMessageDO,SystemMessageDao> {

    @Autowired
    private SystemMessageDao systemMessageDao;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private OutpatientDao outpatientDao;
    @Autowired
    private BaseDoctorDao baseDoctorDao;
    @Autowired
    private YkyySMSService ykyySMSService;
    @Value("${wechat.id}")
    private String wechatId;
    @Autowired
    private HibenateUtils hibenateUtils;


    /**
     * 新增线上就诊消息
     * @param systemMessageDO
     * @return
     * @throws Exception
     */
    public SystemMessageDO addMessage(SystemMessageDO systemMessageDO) throws Exception{
        systemMessageDO.setIsRead("0");
        systemMessageDO.setDel("1");
        systemMessageDO.setCreateTime((new Date()));
        systemMessageDO = systemMessageDao.save(systemMessageDO);
        return systemMessageDO;
    }


    /**
     * 更新线上就诊消息
     * @param id
     * @param read
     * @param del
     * @return
     * @throws Exception
     */
    public SystemMessageDO updateMessage(String id,String read,String del) throws Exception{
        SystemMessageDO systemMessageDO = systemMessageDao.findOne(id);
        if (null == systemMessageDO){
            return null;
        }
        if (!StringUtil.isEmpty(read)){
            systemMessageDO.setIsRead(read);
            systemMessageDO.setReadTime(new Date());
        }
        if (!StringUtil.isEmpty(del)){
            systemMessageDO.setDel(del);
        }
        systemMessageDao.save(systemMessageDO);
        return systemMessageDO;
    }


    /**
     * 保存线上就诊消息（外部调用）
     * @param systemMessageDO
     * @throws Exception
     */
    public void saveMessage(SystemMessageDO systemMessageDO) throws Exception{
        systemMessageDO.setIsRead("0");
        systemMessageDO.setDel("1");
        systemMessageDO.setCreateTime(new Date());
        systemMessageDao.save(systemMessageDO);
    }


    /**
     * 根据id查询消息详情
     * @param id
     * @return
     * @throws Exception
     */
    public  SystemMessageDO queryById(String id) throws Exception{
        SystemMessageDO systemMessageDO = systemMessageDao.queryById(id);
        return systemMessageDO;
    }

    /**
     * 发送系统消息通用接口
     */
    public  SystemMessageDO commenSystemMessage(String jsonParam,String jsonContent) throws Exception{
        if (StringUtils.isNoneBlank(jsonParam)){
            SystemMessageDO systemMessageDO = objectMapper.readValue(jsonParam,SystemMessageDO.class);
            systemMessageDO.setDel("1");
            systemMessageDO.setIsRead("0");
            if (StringUtils.isNoneBlank(jsonContent)){
                JSONObject jsonObject = JSONObject.parseObject(jsonContent);
                systemMessageDO.setData(jsonObject.toJSONString());
            }
            systemMessageDao.save(systemMessageDO);
            return systemMessageDO;
        }
        return null;
    }
    public String  sendDoctorRemindSms(ConsultDo consultDo){
        String msg = "";
        if ("xm_ykyy_wx".equalsIgnoreCase(wechatId)){
            if (consultDo!=null){
                WlyyOutpatientDO wlyyOutpatientDO = outpatientDao.findById(consultDo.getRelationCode());
                if (wlyyOutpatientDO!=null){
                    BaseDoctorDO baseDoctorDO = baseDoctorDao.findById(wlyyOutpatientDO.getDoctor());
                    if (baseDoctorDO!=null){
                        ykyySMSService.sendSmsByTempcode("message_remind_paitent",wlyyOutpatientDO,null,baseDoctorDO.getMobile());
                        msg="发送成功";
                    }else{
                        msg="发送失败，医生不存在";
                    }
                }else {
                    msg="发送失败，门诊不存在";
                }
            }else {
                msg="发送失败，consult不存在";
            }
        }

        return msg;
    }
    /*private MixEnvelop findOutPatientMessage(String reciver ,Integer page ,Integer pageSize){
        String sql = "select b.id as \"id\"," +
                " b.content as \"content\"," +
                "b.saas_id as \"saasId\"," +
                "b.is_read as \"isRead\"," +
                "b.type as \"type\"," +
                "b.sender as \"sender\"," +
                "b.sender_name as \"senderName\"," +
                "b.receiver as \"receiver\"," +
                "b.receiver_name as \"receiverName\"," +
                "b.relation_type as \"relationType\"," +
                "b.relation_code as \"relationCode\"," +
                "b.relation_name as \"relationName\"," +
                "b.status as \"status\"," +
                "b.del as \"del\"," +
                "b.create_user as \"createUser\"," +
                "b.create_user_name as \"createUserName\"," +
                "b.create_time as \"createTime\"," +
                "b.update_user as \"updateUser\"," +
                "b.update_user_name as \"updateUserName\"," +
                "b.update_time as \"updateTime\" " +
                " from wlyy_outpatient t left join base_user_message b " +
                " on t.id = b.relation_code " +
                " where b.del=1 ";
        if (StringUtils.isNoneBlank(reciver)){
            sql+=" and b.reciver = '"+reciver+"'";
        }
        sql+=" and t.status = '0' order by b.create_time asc ";
        String sqlAppend = sql+" and t.status != '0' order by b.create_time desc";
        List<Map<String,Object>> listfront = hi
    }*/


}
