package com.jpsoft.shinestar.mq.listener;

import cn.hutool.core.date.DateUtil;
import com.jpsoft.shinestar.config.WxConfig;
import com.jpsoft.shinestar.modules.base.entity.MedicationInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonPopedom;
import com.jpsoft.shinestar.modules.base.service.MedicationInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonPopedomService;
import com.jpsoft.shinestar.modules.common.utils.WechatMessageUtil;
import com.jpsoft.shinestar.modules.sys.entity.SysLog;
import com.jpsoft.shinestar.modules.sys.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 墨鱼_mo
 * @date 2020-10-29 14:57
 */
@Slf4j
@Component
@RabbitListener(queues = "jpMedicationQueue",concurrency = "10")
public class MedicationListener {
    @Autowired
    private MedicationInfoService medicationInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private WxConfig wxConfig;

    @RabbitHandler
    public void process(String id) {

        try{
            log.warn("用药提醒,id={}",id);

            SysLog sysLog = new SysLog();
            sysLog.setCreateTime(new Date());
            sysLog.setRemark("用药提醒发送模板消息");
            sysLog.setPointcut("用药");
            sysLogService.insert(sysLog);


            MedicationInfo medicationInfo = medicationInfoService.get(id);
            if (medicationInfo != null){
                Boolean isMedication = medicationInfoService.isMedication(medicationInfo,new Date());
                if (medicationInfo.getNeedRemind() && isMedication){
                    PersonPopedom personPopedom = personPopedomService.get(medicationInfo.getBeRemindPopedomId());
                    if (personPopedom != null){
                        PersonInfo personInfo = personInfoService.get(personPopedom.getPersonId());
                        if (personInfo != null & StringUtils.isNotBlank(personInfo.getOpenId()) & personInfo.getWechatNoticeEnabled()){
                            WechatMessageUtil.sendMedicationMessage(personInfo.getOpenId(),"尊敬的"+personInfo.getName()+"，您好",medicationInfo.getMedicationName(),medicationInfo.getUsage(),"祝您身体健康！",wxConfig.getAppId(),wxConfig.getAppSecret());
                        }
                    }



                }
            }


        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            SysLog sysLog = new SysLog();
            sysLog.setCreateTime(new Date());
            sysLog.setRemark("用药提醒发送模板消息失败");
            sysLog.setPointcut("用药");
            sysLogService.insert(sysLog);
        }

    }
}
