package com.gxwebsoft.sdy.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.entity.UserVerify;
import com.gxwebsoft.common.system.param.UserParam;
import com.gxwebsoft.common.system.service.UserService;
import com.gxwebsoft.hjm.dto.TemplateMessageRequest;
import com.gxwebsoft.hjm.entity.HjmCar;
import com.gxwebsoft.hjm.entity.HjmCourses;
import com.gxwebsoft.hjm.service.HjmCarService;
import com.gxwebsoft.hjm.service.WxNotificationService;
import com.gxwebsoft.pwl.entity.PwlProject;
import com.gxwebsoft.shop.entity.ShopDealerUser;
import com.gxwebsoft.shop.entity.ShopDealerWithdraw;
import com.gxwebsoft.shop.service.ShopDealerUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;

/**
 * 定时任务
 *
 * @author 科技小王子
 * @since 2022-12-15 19:11:07
 */
@Tag(name = "定时任务")
@RestController
@RequestMapping("/api/sdy/sdy-template-message")
public class PushTemplateMessageController extends BaseController {
  @Resource
  private UserService userService;
  @Resource
  private WxNotificationService wxNotificationService;
  @Resource
  private ShopDealerUserService shopDealerUserService;


  final static Integer tenantId = 10560;
  final static String appId = "wx51962d6ac21f2ed2";
  final static String toUser = "oKGr42It8OcS1Bl-KpiQj7MM43o8";

  @Operation(summary = "升级为管理员")
  @GetMapping("/{id}")
  public ApiResult<?> pushByUpdateAdmin(@PathVariable("id") Integer id) {
    // 发送模板消息
    boolean success = updateToAdmin();
    System.out.println("success = " + success);
//    try {
//      // 查询分销商用户信息
//      final ShopDealerUser dealerUser = shopDealerUserService.getByUserIdRel(id);
//
//      // 判断用户是否存在
//      if (ObjectUtil.isEmpty(dealerUser)) {
////        return fail("用户不存在");
//      }
//
//      // 发送模板消息
//      boolean success = updateToAdmin(dealerUser);
//
//      if (success) {
//        return success("模板消息发送成功");
//      } else {
//        return fail("模板消息发送失败");
//      }
//
//    } catch (Exception e) {
//      System.err.println("发送升级管理员通知失败: " + e.getMessage());
//      e.printStackTrace();
//      return fail("发送失败：" + e.getMessage());
//    }
    return success("模板消息发送成功",success);
  }

  /**
   * 升级为管理员
   *
   * @return 发送是否成功
   */
  public boolean updateToAdmin() {
    try {
      // 发送模板消息
      final TemplateMessageRequest templateMessageRequest = new TemplateMessageRequest();
      templateMessageRequest.setToUser(toUser);
      templateMessageRequest.setTemplateId("KxGoeBpHW60QFUIU7Vo3c_48g_3V55tWWr23tUUl8gI");
//      templateMessageRequest.setUrl("https://mp.websoft.top");
      final TemplateMessageRequest.MiniProgram miniProgram = new TemplateMessageRequest.MiniProgram();
      miniProgram.setAppid(appId);
      miniProgram.setPagepath("pages/index/index");
      templateMessageRequest.setMiniprogram(miniProgram);
      HashMap<String, TemplateMessageRequest.TemplateDataItem> map = new HashMap<>();
      map.put("thing1", new TemplateMessageRequest.TemplateDataItem("唐任节"));
      map.put("thing2", new TemplateMessageRequest.TemplateDataItem("升级为管理员"));
      templateMessageRequest.setData(map);
      System.out.println("发送升级管理员通知，数据: " + map);

      // 调用微信通知服务发送模板消息
      return wxNotificationService.sendTemplateMessage(tenantId, templateMessageRequest);

    } catch (Exception e) {
      System.err.println("发送模板消息异常: " + e.getMessage());
      e.printStackTrace();
      return false;
    }
  }

  @Operation(summary = "发送实名审核提醒")
  @PostMapping("/pushReviewReminder")
  public ApiResult<?> pushReviewReminder(@RequestBody UserVerify userVerify) {
    try {
      // 发送模板消息
      final TemplateMessageRequest templateMessageRequest = new TemplateMessageRequest();
      templateMessageRequest.setToUser(toUser);
      templateMessageRequest.setTemplateId("0FBKFCWXe8WyjReYXwSDEXf1-pxYKQXE0quZre3GYIM");
      final TemplateMessageRequest.MiniProgram miniProgram = new TemplateMessageRequest.MiniProgram();
      miniProgram.setAppid(appId);
      miniProgram.setPagepath("admin/userVerify/index");
      templateMessageRequest.setMiniprogram(miniProgram);
      HashMap<String, TemplateMessageRequest.TemplateDataItem> map = new HashMap<>();
      map.put("thing5", new TemplateMessageRequest.TemplateDataItem(userVerify.getRealName()));
      // 当前日期
      map.put("time6", new TemplateMessageRequest.TemplateDataItem(new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())));
      templateMessageRequest.setData(map);
      System.out.println("map = " + map);
      // 调用微信通知服务发送模板消息
      wxNotificationService.sendTemplateMessage(tenantId, templateMessageRequest);
      return success("发送成功");

    } catch (Exception e) {
      System.err.println("发送模板消息异常: " + e.getMessage());
      e.printStackTrace();
      return fail("发送失败：" + e.getMessage());
    }
  }

  @Operation(summary = "提现审核提醒")
  @PostMapping("/pushWithdrawalReviewReminder")
  public ApiResult<?> pushWithdrawalReviewReminder(@RequestBody ShopDealerWithdraw shopDealerWithdraw) {
    try {
      // 发送模板消息
      final TemplateMessageRequest templateMessageRequest = new TemplateMessageRequest();
      templateMessageRequest.setToUser(toUser);
      templateMessageRequest.setTemplateId("fJOb0ZPs_HCQO6tdqPRTfaELGgjH5s8a6Vm9X9Hxgrk");
      final TemplateMessageRequest.MiniProgram miniProgram = new TemplateMessageRequest.MiniProgram();
      miniProgram.setAppid(appId);
      miniProgram.setPagepath("dealer/withdraw/admin");
//      miniProgram.setPagepath("pages/index/index");
      templateMessageRequest.setMiniprogram(miniProgram);
      HashMap<String, TemplateMessageRequest.TemplateDataItem> map = new HashMap<>();
      map.put("amount1", new TemplateMessageRequest.TemplateDataItem(shopDealerWithdraw.getMoney().toString()));
      map.put("thing7", new TemplateMessageRequest.TemplateDataItem(shopDealerWithdraw.getBankAccount()));
      templateMessageRequest.setData(map);
      System.out.println("map = " + map);
      // 调用微信通知服务发送模板消息
      wxNotificationService.sendTemplateMessage(tenantId, templateMessageRequest);
      return success("发送成功");

    } catch (Exception e) {
      System.err.println("发送模板消息异常: " + e.getMessage());
      e.printStackTrace();
      return fail("发送失败：" + e.getMessage());
    }
  }


  @Operation(summary = "提现到账通知提醒")
  @PostMapping("/pushNoticeOfWithdrawalToAccount")
  public ApiResult<?> pushNoticeOfWithdrawalToAccount(@RequestBody ShopDealerWithdraw shopDealerWithdraw) {
    if (StrUtil.isBlank(shopDealerWithdraw.getOfficeOpenid())) {
      return fail("请传入公众号openid");
    }
    try {
      // 发送模板消息
      final TemplateMessageRequest templateMessageRequest = new TemplateMessageRequest();
      templateMessageRequest.setToUser(shopDealerWithdraw.getOfficeOpenid());
      templateMessageRequest.setTemplateId("QnmEjyEBmodtfnDsflDwVS1mmh4v_hql-TCDxi2ADs8");
      final TemplateMessageRequest.MiniProgram miniProgram = new TemplateMessageRequest.MiniProgram();
      miniProgram.setAppid(appId);
      miniProgram.setPagepath("dealer/withdraw/admin");
      templateMessageRequest.setMiniprogram(miniProgram);
      HashMap<String, TemplateMessageRequest.TemplateDataItem> map = new HashMap<>();
      map.put("thing13", new TemplateMessageRequest.TemplateDataItem(shopDealerWithdraw.getBankAccount()));
      map.put("amount4", new TemplateMessageRequest.TemplateDataItem(shopDealerWithdraw.getMoney().toString()));
      map.put("amount5", new TemplateMessageRequest.TemplateDataItem(shopDealerWithdraw.getMoney().subtract(new BigDecimal(3)).toString()));
      map.put("amount9", new TemplateMessageRequest.TemplateDataItem(new BigDecimal(3).toString()));

      templateMessageRequest.setData(map);
      System.out.println("map = " + map);
      // 调用微信通知服务发送模板消息
      wxNotificationService.sendTemplateMessage(tenantId, templateMessageRequest);
      return success("发送成功");

    } catch (Exception e) {
      System.err.println("发送模板消息异常: " + e.getMessage());
      e.printStackTrace();
      return fail("发送失败：" + e.getMessage());
    }
  }

}
