package com.cweb.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.model.order.OrderModel;
import com.hfkj.service.order.BsOrderService;
import com.hfkj.sysenum.order.OrderPayChannelEnum;
import com.hfkj.sysenum.order.OrderPayTypeEnum;
import com.hfkj.sysenum.order.OrderStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
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.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;

/**
 * @className: OrderPayNotifyController
 * @author: HuRui
 * @date: 2024/5/7
 **/
@Controller
@RequestMapping(value="/notify")
@Api(value="通知业务")
public class OrderPayNotifyController {
    Logger log = LoggerFactory.getLogger(OrderPayNotifyController.class);
    @Resource
    private BsOrderService orderService;

    @RequestMapping(value="/huipay",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "【惠支付】主扫通知")
    public void huipay(@RequestBody String body, HttpServletResponse response) {
        try {
            log.info("===============惠支付回调start==================");
            JSONObject dataObject = JSONObject.parseObject(body);
            // 处理业务
            log.info("开始处理业务");
            log.info("惠支付-回调参数: " + dataObject);

            // 查询交易订单
            OrderModel order = orderService.getDetail(dataObject.getString("outTradeNo"));
            if (order != null && order.getOrderStatus().equals(OrderStatusEnum.status1.getCode())) {
                // 支付方式 微信：WECHAT 支付宝：ALIPAY 银联：UQRCODEPAY
                String payMode = dataObject.getString("payMode");
                if ("WECHAT".equals(payMode)) {
                    order.setPayType(OrderPayTypeEnum.type2.getCode());

                } else if ("ALIPAY".equals(payMode)) {
                    order.setPayType(OrderPayTypeEnum.type1.getCode());
                }
                order.setPayChannel(OrderPayChannelEnum.type1.getCode());
                order.setPayTime(new Date(dataObject.getLong("payTime")));
                order.setPaySerialNo(dataObject.getString("accTradeNo"));
                order.setPayChannelOrderNo(dataObject.getString("platformTradeNo"));
                orderService.orderPaySuccessHandle(order);
            }
            log.info("处理业务完成");
            log.info("============回调任务End=============");

            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer= response.getWriter();

            JSONObject postJson = new JSONObject();
            postJson.put("code" ,"SUCCESS");
            postJson.put("message" ,"执行成功");
            writer.write(postJson.toJSONString());
            writer.flush();
            writer.close();

        } catch (Exception e) {
            log.error("订单处理异常", e);
        } finally {
            log.info("===============微信支付回调end==================");
        }
    }

}
