package com.gxwebsoft.apps.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 报餐系统工具类
 * @author 科技小王子
 *
 */
@Component
public class BcUtil {
  private final StringRedisTemplate stringRedisTemplate;
  public Integer categoryId;
  public Integer tenantId;

  public BcUtil(StringRedisTemplate stringRedisTemplate){
    this.stringRedisTemplate = stringRedisTemplate;
  }

  // 判断当前餐段
  public Integer getCurrentPeriod(Integer tenantId) {
    // 今天开始时间 2023-05-01 00:00:00
    DateTime parse = DateUtil.parse(DateUtil.today());
    // 早餐开始时间 2023-05-01 07:00:00
    DateTime breakfastStart = DateUtil.offsetMinute(parse, 7*60);
    // 早餐截止时间 2023-05-01 07:55:00
    DateTime breakfastEnd = DateUtil.offsetMinute(parse, 7*60 + 55);
    // 午餐开始时间 2023-05-01 13:00:00
    DateTime lunchStart = DateUtil.offsetMinute(parse, 11*60 + 50);
    // 午餐截止时间 2023-05-01 13:00:00
    DateTime lunchEnd = DateUtil.offsetMinute(parse, 13*60);
    // 晚餐截止时间 2023-05-01 19:30:00
    DateTime dinnerStart = DateUtil.offsetMinute(parse, 17*60 + 30);
    // 晚餐截止时间 2023-05-01 19:30:00
    DateTime dinnerEnd = DateUtil.offsetMinute(parse, 19*60 + 30);

    // 读取后台配置
//    String key = "cache" + tenantId + ":setting:BaoCan";
//    System.out.println("读取后台配置 = " + key);
//    final String cache = stringRedisTemplate.opsForValue().get(key);
//    if(cache != null){
//      DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//      final JSONObject jsonObject = JSONObject.parseObject(cache);
//      System.out.println("jsonObject = " + jsonObject);
//      final String breakfastStartTime = jsonObject.getString("breakfastStartTime");
//      final String breakfastEndTime = jsonObject.getString("breakfastEndTime");
//      final String lunchStartTime = jsonObject.getString("lunchStartTime");
//      final String lunchEndTime = jsonObject.getString("lunchEndTime");
//      final String dinnerStartTime = jsonObject.getString("dinnerStartTime");
//      final String dinnerEndTime = jsonObject.getString("dinnerEndTime");
//      breakfastStart = DateUtil.parse(DateUtil.format(DateUtil.parse(breakfastStartTime), df));
//      breakfastEnd = DateUtil.parse(DateUtil.format(DateUtil.parse(breakfastEndTime), df));
//      lunchStart = DateUtil.parse(DateUtil.format(DateUtil.parse(lunchStartTime), df));
//      lunchEnd = DateUtil.parse(DateUtil.format(DateUtil.parse(lunchEndTime), df));
//      dinnerStart = DateUtil.parse(DateUtil.format(DateUtil.parse(dinnerStartTime), df));
//      dinnerEnd = DateUtil.parse(DateUtil.format(DateUtil.parse(dinnerEndTime), df));
//
//      System.out.println("breakfastStart = " + breakfastStart);
//      System.out.println("breakfastEndTime = " + breakfastEndTime);
//      System.out.println("lunchStartTime = " + lunchStartTime);
//      System.out.println("lunchEndTime = " + lunchEndTime);
//      System.out.println("dinnerStartTime = " + dinnerStartTime);
//      System.out.println("dinnerEndTime = " + dinnerEndTime);
//
//    }

    // 早餐时间段
    if(DateUtil.date().compareTo(breakfastStart) > 0 && DateUtil.date().compareTo(breakfastEnd) < 0){
      System.out.println("早餐时间段 = ");
      this.categoryId = 25;
      return 25;
    }
    // 午餐时间段
    if(DateUtil.date().compareTo(lunchStart) > 0 && DateUtil.date().compareTo(lunchEnd) < 0){
      System.out.println("午餐时间段 = ");
      this.categoryId = 26;
      return 26;
    }
    // 晚餐时间段
    if(DateUtil.date().compareTo(dinnerStart) > 0 && DateUtil.date().compareTo(dinnerEnd) < 0){
      System.out.println("晚餐时间段 = ");
      this.categoryId = 27;
      return 27;
    }
    return 0;
  }

  // 发送消息提醒
  public void send(String content) {
    // 1.获取access_token
    String key = "cache10048:ww:access_token";
    String access_token = stringRedisTemplate.opsForValue().get(key);
    System.out.println("access_token = " + access_token);
    String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + access_token;
    final ArrayList<String> toUsers = new ArrayList<>();
    toUsers.add("ZhaiLing");
    toUsers.add("LiangYun");
    toUsers.add("zhaozhonglin");
    final HashMap<String, Object> map = new HashMap<>();
    map.put("msgtype","text");
    map.put("agentid","wxa040285bc1c223ae");
//    map.put("toall",1);
    map.put("touser",toUsers);
    final HashMap<Object, Object> text = new HashMap<>();
    text.put("content",content);
    map.put("text",text);
    System.out.println("发送消息提醒 = " + JSONObject.toJSONString(map));
    final String post = HttpUtil.post(url, JSONObject.toJSONString(map));
  }
}
