package com.fs.app.controller;


import com.fs.common.core.controller.BaseController;
import com.fs.company.service.ICompanyRechargeService;
import com.fs.core.config.WxPayProperties;
import com.fs.course.service.IFsCourseProductOrderService;
import com.fs.course.service.IFsCourseRedPacketLogService;
import com.fs.course.service.IFsUserCourseOrderService;
import com.fs.course.service.IFsUserVipOrderService;
import com.fs.his.service.*;
import com.fs.sop.service.ISopUserLogsInfoService;
import com.fs.system.service.ISysConfigService;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.service.WxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api("微信支付接口")
@RestController
@RequestMapping("/app/wxpay")
public class WxPayController {
    protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
    @Autowired
    private WxPayService wxPayService;
    @Autowired
    private IFsInquiryOrderService inquiryOrderService;
    @Autowired
    private IFsStoreOrderService storeOrderService;
    @Autowired
    private IFsPackageOrderService packageOrderService;

    @Autowired
    private IFsUserCourseOrderService courseOrderService;

    @Autowired
    private IFsUserVipOrderService userVipOrderService;
    @Autowired
    private IFsStorePaymentService  paymentService;
    @Autowired
    private IFsCourseRedPacketLogService redPacketLogService;
    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISopUserLogsInfoService iSopUserLogsInfoService;
    @Autowired
    private IFsIntegralOrderService integralOrderService;

    @Autowired
    private IFsCourseProductOrderService courseProductOrderService;

    @Autowired
    ICompanyRechargeService rechargeService;

    @Autowired
    private WxPayProperties wxPayProperties;
    /**
     * 微信回调
     * 回调接口代码内部自动校验结果签名和业务代码
     * 经测试这个方法不可用 2025/12/10 请用 /store/app/wxpay/wxPayNotify
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation("微信回调")
    @PostMapping("/wxPayNotify")
    public String wxPayNotify(HttpServletRequest request) throws Exception {

        try {
            String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
            logger.info("微信回调结果{}", xmlResult);
            WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(xmlResult);
            if("SUCCESS".equals(result.getReturnCode())){
                //订单号
                String outtradeno = result.getOutTradeNo();
                String tradeNo = result.getTransactionId();
                String[] orderId=outtradeno.split("-");
                // 每个 case 必须 break，避免 fall-through 导致一笔支付重复入账多个业务线
                switch (orderId[0]){
                    case "inquiry":
                        inquiryOrderService.payConfirm("",orderId[1],tradeNo,"",1,tradeNo,null);
                        break;
                    case "store":
                        storeOrderService.payConfirm("",orderId[1],tradeNo,"",1,tradeNo,null);
                        break;
                    case "package":
                        packageOrderService.payConfirm("",orderId[1],tradeNo,"",1,tradeNo,null);
                        break;
                    case "course":
                        courseOrderService.payConfirm("",orderId[1],tradeNo,"",1,tradeNo,null);
                        break;
                    case "appvip":
                        userVipOrderService.payConfirm("",orderId[1],tradeNo,"",1,tradeNo,null);
                        break;
                    case "integral":
                        integralOrderService.payConfirm("",orderId[1],tradeNo,"",1,tradeNo,null);
                        break;
                    case "product":
                        courseProductOrderService.payConfirm("",orderId[1],tradeNo,"",1,tradeNo,null);
                        break;
                    case "payment":
                        paymentService.payConfirm(orderId[1], outtradeno,tradeNo,null);
                        break;
                    default:
                        logger.warn("未知订单前缀: {}", orderId[0]);
                        break;
                }
                return WxPayNotifyResponse.success("处理成功!");
            }else{
                return WxPayNotifyResponse.fail("");
            }

        } catch (Exception e) {
            logger.error("微信回调结果异常", e);
            return WxPayNotifyResponse.fail(e.getMessage());
        }
    }


}
