package com.jpsoft.shinestar.mq.listener;

import com.jpsoft.shinestar.config.WxConfig;
import com.jpsoft.shinestar.modules.business.dto.HealthyReportNoticeDTO;
import com.jpsoft.shinestar.modules.common.utils.JwtUtil;
import com.jpsoft.shinestar.modules.common.utils.WechatMessageUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Component
@RabbitListener(queues = "workerHealthyReportNoticeQueue",concurrency = "10")
public class WorkerHealthyReportNoticeListener {
    @Value("${healthyReport.noticeTmplCode}")
    private String noticeTmplCode;

    @Value("${healthyReport.workerReportUrl}")
    private String workerReportUrl;

    @Autowired
    private WxConfig wxConfig;

    @Value("${jwt.secret}")
    private String jwtSecret;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @RabbitHandler
    public void process(HealthyReportNoticeDTO dto) {
        String key = "personHealthLedger_" + dto.getPersonId() + "_" + DateTime.now().toString("yyyy-MM-dd");

        boolean hasKey = redisTemplate.hasKey(key);

        log.warn(key);

        if (hasKey){
            log.warn(dto.getPersonId() + "今日已打卡，不再通知!");
            return;
        }

        String token = JwtUtil.createToken(jwtSecret,String.valueOf(dto.getPersonId()), DateTime.now().plusMonths(1).toDate());

        JSONObject sendData = new JSONObject();

        if (StringUtils.isEmpty(dto.getSubmitTime())){
            dto.setSubmitTime("09:00");
        }

        JSONObject first = new JSONObject();
        first.put("value",  "严防严控，请按本单位要求在" +  dto.getSubmitTime() + "之前及时打卡，点击“详情”立刻进行健康打卡！");
        first.put("color", "#FF0000");

        //通知人
        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", dto.getPersonName());
        keyword1.put("color", "#173177");

        //时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", sdf.format(new Date()));
        keyword2.put("color", "#173177");

        //状态
        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", "待打卡");
        keyword3.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", dto.getCompanyName());
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
//        sendData.put("keyword4", keyword4);
        sendData.put("remark", remark);

        try {
            String reportUrl = workerReportUrl;

            WechatMessageUtil.sendNativeTemplate(
                    wxConfig.getAppId(),
                    wxConfig.getAppSecret(),
                    noticeTmplCode,
                    dto.getOpenId(),
                    sendData,
                    MessageFormat.format(reportUrl, URLEncoder.encode(token, "UTF-8"))
            );
        }
        catch (Exception ex){
            log.warn(ex.getMessage());
        }
    }
}
