package com.fs.app.controller;

import com.alibaba.fastjson.JSON;
import com.fs.course.service.IFsUserCourseOrderService;
import com.fs.course.service.IFsUserVipOrderService;
import com.fs.his.service.IFsInquiryOrderService;
import com.fs.his.service.IFsPackageOrderService;
import com.fs.his.service.IFsStoreOrderService;
import com.fs.huifuPay.domain.HuiFuOnlinePay;
import com.fs.huifuPay.domain.HuiFuOnlineRefund;
import com.fs.huifuPay.domain.HuiFuResult;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value="/app/hfPay")
public class HuifuPayController {
    @Autowired
    private IFsInquiryOrderService inquiryOrderService;
    @Autowired
    private IFsStoreOrderService storeOrderService;
    @Autowired
    private IFsPackageOrderService packageOrderService;
    @Autowired
    private IFsUserCourseOrderService courseOrderService;
    @Autowired
    private IFsUserVipOrderService vipOrderService;

    org.slf4j.Logger logger= LoggerFactory.getLogger(getClass());
    @RequestMapping("/payNotifyUrl")
    public String asyncMessage(@RequestParam String resp_desc,@RequestParam String resp_code,@RequestParam String sign,@RequestParam String resp_data) {
        HuiFuResult huiFuResult = JSON.parseObject(resp_data, HuiFuResult.class);
        logger.info("汇付支付回调:"+huiFuResult);
        if(huiFuResult.getResp_code().equals("00000000")){
            String[] orderId=huiFuResult.getReq_seq_id().split("-");
            switch (orderId[0]){
                case "inquiry":
                    inquiryOrderService.payConfirm("",orderId[1],huiFuResult.getHf_seq_id(),"",1);
                    break;
                case "store":
                    storeOrderService.payConfirm("",orderId[1],huiFuResult.getHf_seq_id(),"",1);
                    break;
                case "package":
                    packageOrderService.payConfirm("",orderId[1],huiFuResult.getHf_seq_id(),"",1);
                    break;
                case "course":
                    courseOrderService.payConfirm("",orderId[1],huiFuResult.getHf_seq_id(),"",1);
                    break;
                case "appvip":
                    vipOrderService.payConfirm("",orderId[1],huiFuResult.getHf_seq_id(),"",1);
                    break;
            }
        }

        return "ok";
    }


    @RequestMapping("/payOnlineNotifyUrl")
    public String asyncHuFuOnlinePay(@RequestParam String resp_desc,@RequestParam String resp_code,@RequestParam String sign,@RequestParam String resp_data) {
        HuiFuOnlinePay huFuResult = JSON.parseObject(resp_data, HuiFuOnlinePay.class);
        return "ok";
    }
    @RequestMapping("/onlineRefund")
    public String onlineRefund(@RequestParam String resp_desc,@RequestParam String resp_code,@RequestParam String sign,@RequestParam String resp_data) {
        HuiFuOnlineRefund huFuResult = JSON.parseObject(resp_data, HuiFuOnlineRefund.class);
        return "ok";
    }

    @RequestMapping("/refundNotifyUrl")
    public String refundMessage(@RequestParam String resp_desc,@RequestParam String resp_code,@RequestParam String sign,@RequestParam String resp_data) {
        System.out.println(resp_data);
        return "ok";
    }
}
