package com.fs.app.controller.store;


import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.fs.app.annotation.Login;
import com.fs.app.controller.AppBaseController;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.utils.IpUtil;
import com.fs.core.config.WxMaConfiguration;
import com.fs.hisStore.domain.FsUserScrm;
import com.fs.hisStore.param.FsStorePaymentGetWxaCodeParam;
import com.fs.hisStore.param.FsStorePaymentPayParam;
import com.fs.hisStore.service.IFsStorePaymentScrmService;
import com.fs.hisStore.service.IFsUserScrmService;
import com.fs.wx.miniapp.config.WxMaProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api("支付接口")
@RestController
@RequestMapping("/store/app/payment")
public class PaymentScrmController extends AppBaseController {
    @Autowired
    private WxMaProperties properties;
    protected final Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    private IFsStorePaymentScrmService paymentService;
    @Autowired
    private IFsUserScrmService userService;

    @ApiOperation("支付")
    @PostMapping("/payment")
    @RepeatSubmit
    public R payment(@Validated @RequestBody FsStorePaymentPayParam payment, HttpServletRequest request){
        String appId = null;
        if (payment.getAppId()!=null && !"".equals(payment.getAppId())){
            appId= payment.getAppId();
        }else {
            appId=properties.getConfigs().get(0).getAppid();
        }

        final WxMaService wxService = WxMaConfiguration.getMaService(appId);
        try {
            String ip = IpUtil.getRequestIp();
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(payment.getCode());
            FsUserScrm user=userService.selectFsUserByMaOpenId(session.getOpenid());
            if(user==null){
                return R.error("用户不存在，返回主页授权登录！");
            }
            payment.setUserId(user.getUserId());
            return paymentService.payment(payment);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }



    }

    @Login
    @ApiOperation("收款订单支付")
    @PostMapping("/paymentByWxaCode")
    @RepeatSubmit
    public R paymentByWxaCode(@Validated @RequestBody FsStorePaymentPayParam payment)
    {
        payment.setUserId(Long.parseLong(getUserId()));
        return paymentService.paymentByWxaCode(payment);
    }

    @ApiOperation("生成收款页面小程序码")
    @GetMapping("/getWxaCodeByPayment")
    public R getWxaCodeByPayment(FsStorePaymentGetWxaCodeParam param)
    {
        return paymentService.getWxaCodeByPayment(param);
    }




}
