package com.fs.app.controller;

import com.fs.common.core.controller.BaseController;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.spring.SpringUtils;
import com.fs.course.service.IFsUserCourseOrderService;
import com.fs.course.service.IFsUserVipOrderService;
import com.fs.his.domain.FsPayConfig;
import com.fs.his.service.IFsInquiryOrderService;
import com.fs.his.service.IFsPackageOrderService;
import com.fs.his.service.IFsStoreOrderService;

import com.fs.system.domain.SysConfig;
import com.fs.system.mapper.SysConfigMapper;
import com.fs.ybPay.domain.OrderCallback;
import com.fs.ybPay.util.PayUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 *
 *
 * @author fs
 * @date 2023-07-12
 */
@RestController
@RequestMapping("/app/pay")
public class YbPayController extends BaseController
{
    @Autowired
    private IFsInquiryOrderService inquiryOrderService;
    @Autowired
    private IFsStoreOrderService storeOrderService;
    @Autowired
    private IFsPackageOrderService packageOrderService;
    @Autowired
    private IFsUserCourseOrderService courseOrderService;
    @Autowired
    private IFsUserVipOrderService userVipOrderService;
    @PostMapping("/payNotify")
    public String payNotify(@RequestBody Map<String, String> params, @RequestBody OrderCallback o) {
        System.out.println("支付回调");
        //验证
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String sign = PayUtil.sign(params,fsPayConfig.getYbKey());
        String sg = params.get("sign");
        if (!sg.equals(sign)){
            throw new CustomException("验签错误");
        }
        String[] orderId=o.getLowOrderId().split("-");
        switch (orderId[0]){
            case "inquiry":
                inquiryOrderService.payConfirm("",orderId[1],o.getUpOrderId(),"",1);
                break;
            case "store":
                storeOrderService.payConfirm("",orderId[1],o.getUpOrderId(),"",1);
                break;
            case "package":
                packageOrderService.payConfirm("",orderId[1],o.getUpOrderId(),"",1);
                break;
            case "course":
                courseOrderService.payConfirm("",orderId[1],o.getUpOrderId(),"",1);
                break;
            case "appvip":
                userVipOrderService.payConfirm("",orderId[1],o.getUpOrderId(),"",1);
                break;
        }
        System.out.println(o);
        return "SUCCESS";
    }




}
