package com.bohuikeji.delivery.backstage.controller.views;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.bohuikeji.delivery.backstage.constants.WxAppEm;
import com.bohuikeji.delivery.backstage.entity.wx.TWxUser;
import com.bohuikeji.delivery.backstage.pay.AliPay;
import com.bohuikeji.delivery.backstage.service.IWxUserService;
import com.bohuikeji.delivery.backstage.util.WxUtils;
import com.bohuikeji.frame.module.admin.entity.back.PageEntity;
import com.bohuikeji.frame.module.admin.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * 开放页面访问控制器，无需登录
 * @author 孙劲辉
 */
@Slf4j
@Controller
@RequestMapping("/views/open")
@RequiredArgsConstructor
public class OpenPage {

    final IWxUserService wxUserService;

    /**
     * 微信公众号授权回调
     * @param path
     * @param code
     * @param id
     * @param type
     * @param appid
     * @return
     */
    @GetMapping("/wx/oauth")
    public PageEntity wxAuth(String path,String code,Integer id,String type,String appid) {
        log.info("#########################微信公众号授权回调信息：code:{}，path:{}",code,path);
        WxAppEm app= WxAppEm.GZH_TCJS;
        TWxUser wxUser=wxUserService.getUserByType(id,type,app);
        if (wxUser==null){
            JSONObject token=WxUtils.getGzhOauthToken(app,code);
            wxUser=new TWxUser();
            wxUser.setUserType(type);
            wxUser.setUserId(Long.valueOf(id));
            wxUser.setWxAppType(app.getType());
            wxUser.setWxAppId(app.getAppId());
            wxUser.setWxAppName(app.getName());
            wxUser.setWxUserOpenid(token.getString("openid"));
        }
        //该用户是否关注公众号
        if(WxUtils.isFollowGzh(app,wxUser.getWxUserOpenid())){
            wxUserService.saveOrUpdate(wxUser);
            return PageEntity.page(path);
        }
        return PageEntity.page("/open/default/fail");
    }

    /**
     * 支付宝授权回调进入
     * @param auth_code
     * @param id
     * @param type
     * @return
     */
    @GetMapping("/zfb/oauth")
    public PageEntity afuAuth(String auth_code,Integer id,String type) {
        log.error("支付宝授权回调成功，auth_code：[{}]，app_id=[{}]，scope=[{}]",auth_code,id,type);
        AlipayUserInfoShareResponse response= AliPay.getUserInfo(auth_code);
        log.error("获取到支付宝用户信息：[{}]", JsonUtil.objToJson(response).toJSONString());
        if(response==null){
            log.error("支付宝获取用户信息失败，加盟商：[{}]未授",id);
            return PageEntity.page("/open/default/fail");
        }
        if(!response.isSuccess()){
            log.error("支付宝获取用户信息失败,支付宝信息：[{}]",response.getSubMsg());
            return PageEntity.page("/open/default/fail");
        }
        WxAppEm app= WxAppEm.ZFB_WB;
        TWxUser wxUser=wxUserService.getUserByType(id,type,app);
        if (wxUser==null){
            wxUser=new TWxUser();
            wxUser.setUserType(type);
            wxUser.setUserId(Long.valueOf(id));
            wxUser.setWxAppType(app.getType());
            wxUser.setWxAppId(app.getAppId());
            wxUser.setWxAppName(app.getName());
            wxUser.setWxUserOpenid(response.getUserId());
            wxUserService.save(wxUser);
        }
        return PageEntity.page("/open/default/success");
    }


    /***
     * 订单h5补充支付页面
     * @return
     */
    @GetMapping("/order/supply")
    public PageEntity orderSupply(String key){
        return PageEntity.page("/open/order/supply").add("orderNum",key);
    }


}
