package com.gxwebsoft.hjm.task;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import com.gxwebsoft.hjm.dto.TemplateMessageRequest;
import com.gxwebsoft.hjm.entity.HjmCar;
import com.gxwebsoft.hjm.service.HjmCarService;
import com.gxwebsoft.hjm.service.WxNotificationService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

/**
 * 定时任务
 *
 * @author 科技小王子
 * @since 2022-12-15 19:11:07
 */
@Tag(name = "定时任务")
@RestController
@RequestMapping("/api/hjm/scheduling")
public class PushHjmFenceOutController extends BaseController {
  @Resource
  private HjmCarService hjmCarService;
  @Resource
  private UserService userService;
  @Resource
  private WxNotificationService wxNotificationService;
  @Value("${spring.profiles.active}")
  String active;

  /**
   * 定时推送订阅消息
   * @Scheduled(fixedDelay = 2000, initialDelay = 2000)
   * @Scheduled(cron = "0 0 9 * * ?")
   */
//  @Scheduled(cron = "0 0/10 * * * ?")
  public void index() {
    final List<HjmCar> list = hjmCarService.list(new LambdaQueryWrapper<HjmCar>()
      .eq(HjmCar::getStatus, 1)
      .eq(HjmCar::getDeleted, 0)
      .eq(HjmCar::getInFence, false)
    );

    // 开发环境
    if (active.equals("dev")){
      return;
    }

    // 获取所有的邮政协会/管局工作人员
    final List<User> users = userService.listByAlert();

    list.forEach(item -> {
      // 执行推送
      users.forEach(d -> {
        if(StrUtil.isNotBlank(d.getOfficeOpenid())){
          item.setToUser(d.getOfficeOpenid());
          item.setAppId("wxd2723d1afd9c4553");
          sendTemplateMessage(item);
        }
      });
    });
  }

  public void sendTemplateMessage(HjmCar item) {
    // 发送模板消息
    final TemplateMessageRequest templateMessageRequest = new TemplateMessageRequest();
    templateMessageRequest.setToUser(item.getToUser());
    templateMessageRequest.setTemplateId("oMckHaNgNT-ivInYF5DtCcqyd9O-i1hP_G0jQALsx54");
//      templateMessageRequest.setUrl("https://mp.websoft.top");
    final TemplateMessageRequest.MiniProgram miniProgram = new TemplateMessageRequest.MiniProgram();
    miniProgram.setAppid(item.getAppId());
    miniProgram.setPagepath("hjm/query?id=".concat(item.getCode()));
    templateMessageRequest.setMiniprogram(miniProgram);
    HashMap<String, TemplateMessageRequest.TemplateDataItem> map = new HashMap<>();
    map.put("phrase6", new TemplateMessageRequest.TemplateDataItem(item.getDriverName()));
    map.put("time4", new TemplateMessageRequest.TemplateDataItem(
      new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getCreateTime()), "#173177"));
    map.put("phrase7", new TemplateMessageRequest.TemplateDataItem("离开围栏"));
    map.put("thing11", new TemplateMessageRequest.TemplateDataItem(item.getFenceName()));
    map.put("car_number12", new TemplateMessageRequest.TemplateDataItem(item.getCode()));
    templateMessageRequest.setData(map);
    boolean success = wxNotificationService.sendTemplateMessage(10519, templateMessageRequest);
    System.out.println("向 = " + item.getDriverName() + "发送消息成功：" + success);
  }

}
