package com.fs.app.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
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.IFsIntegralOrderService;
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.tzBankPay.config.TzBankConfig;
import com.fs.tzBankPay.doman.TzBankOrderDecrypt;
import com.fs.tzBankPay.doman.TzBankRefundOrderDecrypt;
import com.google.gson.Gson;
import com.tzbank.util.CallBackUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/app/tzPay")
public class TzPayController extends BaseController
{
    @Autowired
    private IFsInquiryOrderService inquiryOrderService;
    @Autowired
    private IFsStoreOrderService storeOrderService;
    @Autowired
    private IFsPackageOrderService packageOrderService;
    @Autowired
    private IFsUserCourseOrderService courseOrderService;

    @Autowired
    private IFsUserVipOrderService userVipOrderService;
    @Autowired
    private IFsIntegralOrderService integralOrderService;

    @RequestMapping(value = "/payNotify",method = RequestMethod.POST)
    public String payNotify(@RequestBody String  decryptForm) throws Exception {
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String decrypt = CallBackUtil.decrypt( fsPayConfig.getTzAppSecret(),decryptForm);
        String filteredString = decrypt.replaceAll("[^\\x20-\\x7E]", "");
        Gson gson = new Gson();
        TzBankOrderDecrypt tzBankOrderDecrypt = gson.fromJson(filteredString, TzBankOrderDecrypt.class);
        logger.info("台州银行支付回调:"+tzBankOrderDecrypt.getOrderFlowNo()+":"+tzBankOrderDecrypt);
        if(tzBankOrderDecrypt.getStatus().equals("90")){
            if(tzBankOrderDecrypt.getOrderNo().indexOf("inquiry")>-1){
                inquiryOrderService.payConfirm("",tzBankOrderDecrypt.getOrderNo().substring(7),tzBankOrderDecrypt.getOrderFlowNo(),tzBankOrderDecrypt.getPayType(),1,null,null);
            }
            else if(tzBankOrderDecrypt.getOrderNo().indexOf("store")>-1){
                storeOrderService.payConfirm("",tzBankOrderDecrypt.getOrderNo().substring(5),tzBankOrderDecrypt.getOrderFlowNo(),tzBankOrderDecrypt.getPayType(),1,null,null);
            }
            else if(tzBankOrderDecrypt.getOrderNo().indexOf("package")>-1){
                packageOrderService.payConfirm("",tzBankOrderDecrypt.getOrderNo().substring(7),tzBankOrderDecrypt.getOrderFlowNo(),tzBankOrderDecrypt.getPayType(),1,null,null);
            }
            else if(tzBankOrderDecrypt.getOrderNo().indexOf("course")>-1){
                courseOrderService.payConfirm("",tzBankOrderDecrypt.getOrderNo().substring(6),tzBankOrderDecrypt.getOrderFlowNo(),tzBankOrderDecrypt.getPayType(),1,null,null);
            }
            else if(tzBankOrderDecrypt.getOrderNo().indexOf("appvip")>-1){
                userVipOrderService.payConfirm("",tzBankOrderDecrypt.getOrderNo().substring(6),tzBankOrderDecrypt.getOrderFlowNo(),tzBankOrderDecrypt.getPayType(),1,null,null);
            }
            else if (tzBankOrderDecrypt.getOrderNo().indexOf("integral")>-1) {
                integralOrderService.payConfirm("",tzBankOrderDecrypt.getOrderNo().substring(8),tzBankOrderDecrypt.getOrderFlowNo(),tzBankOrderDecrypt.getPayType(),1,null,null);
            }
        }
        return decrypt;
    }


    @RequestMapping(value = "/refundNotify",method = RequestMethod.POST)
    public String refundNotify(@RequestBody String  decryptForm) throws Exception {
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String decrypt = CallBackUtil.decrypt(fsPayConfig.getTzAppSecret(),decryptForm);
        String filteredString = decrypt.replaceAll("[^\\x20-\\x7E]", "");
        logger.info("台州银行退款回调:"+filteredString);
        TzBankRefundOrderDecrypt o = new Gson().fromJson(filteredString, TzBankRefundOrderDecrypt.class);
        return filteredString;
    }
}
