package com.ruoyi.api;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.ruoyi.base.service.IBaseCategoryService;
import com.ruoyi.busi.domain.BusiNotice;
import com.ruoyi.busi.service.IBusiNoticeService;
import com.ruoyi.busi.utils.WeChatUtils;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.GzhLoginBody;
import com.ruoyi.member.service.IMemberUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/wxApi")
public class WxApi {
    @Autowired
    private WeChatUtils weChatUtils;
    @Autowired
    private IMemberUserService memberUserService;
    @Autowired
    private IBusiNoticeService busiNoticeService;
    @Autowired
    private IBaseCategoryService baseCategoryService;


    @GetMapping("/getCodeUrl")
    @Anonymous
    public Map<String, String> getCodeUrl(String userType, String url) {
        Map<String, String> res = new HashMap<>();
        res.put("codeUrl", weChatUtils.getCodeUrl("https://www.ddtg.site/" + url, userType));
        return res;
    }

    @GetMapping("/getWebAccessTokenAndOpenid")
    @Anonymous
    public JSONObject getWebAccessTokenAndOpenid(String code) {
        return weChatUtils.getWebAccessTokenAndOpenid(code);
    }

    @PostMapping("/gzhLogin")
    @Anonymous
    public AjaxResult gzhLogin(@RequestBody GzhLoginBody gzhLoginBody) {
        JSONObject userInfo = weChatUtils.getUserInfo(gzhLoginBody.getAccess_token(), gzhLoginBody.getOpenid());
        //如果解析成功,获取token
        String token = memberUserService.gzhLogin(gzhLoginBody.getOpenid(), userInfo);
        AjaxResult ajax = AjaxResult.success();
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @PostMapping("/getUrlLink")
    @Anonymous
    public AjaxResult getUrlLink(String path,String query) {
        BusiNotice busiNotice = busiNoticeService.getById(query);
        String platformName = baseCategoryService.selectByCode(busiNotice.getPlatformCode()).get(0).getTitle();
        String token = weChatUtils.getToken();
        Map<String, Object> map = new HashMap<>();
        map.put("path", path);
        map.put("query", "noticeId="+query+"&navigatePath=index");
        map.put("expire_type", 1);
        map.put("expire_interval", 30);
        String post = HttpUtil.post("https://api.weixin.qq.com/wxa/generate_urllink?access_token=" + token, com.alibaba.fastjson2.JSONObject.toJSONString(map));
        com.alibaba.fastjson2.JSONObject jsonObject = com.alibaba.fastjson2.JSONObject.parseObject(post);
        if (jsonObject.get("errcode").equals(0)) {
            String res="通告快接\n" +
                    "【"+platformName+"】【"+busiNotice.getTitle()+"】\n" +
                    "招募人数："+(busiNotice.getNeedNum()==null?"无上限":busiNotice.getNeedNum()+"名\n") +
                    "通告明细："+busiNotice.getDetail()+"\n" +
                    "报名链接："+jsonObject.get("url_link")+"\n" +
                    "大量小红书、B站、抖音、快手、微博高质量通告！";
            return AjaxResult.success("ok",res);
        }else {
            return AjaxResult.error(jsonObject.get("errmsg").toString());
        }

    }



}
