package com.ruoyi.web.controller.coffee;

import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.wxpay.sdk.WXPayUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.framework.web.service.WeixinServiceImpl;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.mapper.GoodsSkuMapper;
import com.ruoyi.system.mapper.OrderMapper;
import com.ruoyi.system.service.impl.AppServiceImpl;
import com.ruoyi.system.service.impl.GoodsServiceImpl;
import com.ruoyi.system.service.impl.OrderTakingServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/weixin")
@Slf4j
public class WeixinController {

    @Autowired
    WeixinServiceImpl weixinService;
    @Autowired
    private AppServiceImpl appService;
    @Autowired
    private OrderTakingServiceImpl orderTakingService;
    @Autowired
    private OrderMapper orderMapper;

    //这个就是那个使用传code进来的接口
    @PostMapping("/login")
    public AjaxResult login(@RequestBody String body){
        JSONObject param = JSONObject.parseObject(body);
        String code =param.getString("code");
        String encryptedData = param.getString("encryptedData");
        String iv = param.getString("iv");
        String source = param.getString("source");

        AjaxResult ajax = AjaxResult.success();
        String token = weixinService.login(code,encryptedData,iv,source);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 支付通知
     * 微信支付通过支付通知接口将用户支付成功消息通知给商户
     */
    @PostMapping("/native/notify")
    public String wxNotify(HttpServletRequest request) throws Exception {

        System.out.println("微信发送的回调");
        return weixinService.wxNotify(request);

    }
    @RequestMapping("/test")
    public Map<String, String> test(HttpServletRequest request) throws Exception{
        Order order = orderMapper.selectOrderById("8");
        int amount = order.getAmount().movePointRight(2).intValue();
        return null;
    }

    @RequestMapping("/getShop")
    public AjaxResult getShop(@RequestParam(required = false) String lng,@RequestParam(required = false)String lat){
        return AjaxResult.success(appService.getShop(lng,lat));
    }


    @GetMapping(value = "/infoByShop")
    public AjaxResult infoByShop(String shopId)
    {
        return AjaxResult.success(orderTakingService.getInfoByShop(shopId));
    }

    /**
     * 退款回调
     *
     * @param xmlData
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/refundNotify")
    public String refundNotify(@RequestBody String xmlData) {
        return weixinService.refundNotify(xmlData);

    }
}
