package com.cweb.controller.pay;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.security.AESEncodeUtil;
import com.hai.config.CommonSysConst;
import com.hai.config.WxOrderConfig;
import com.hai.dao.HighGasOrderRefundMapper;
import com.hai.entity.HighGasOrderRefund;
import com.hai.entity.HighOrder;
import com.hai.entity.OutRechargeOrder;
import com.hai.model.OrderRefundModel;
import com.hai.service.OutRechargeOrderService;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;

@Controller
@RequestMapping(value = "/czOrder")
@Api(value = "充值回调")
public class CzOrderController {

    private static Logger log = LoggerFactory.getLogger(TuanYouController.class);

    @Resource
    private HighGasOrderRefundMapper highGasOrderRefundMapper;

    @Resource
    private OutRechargeOrderService outRechargeOrderService;

    @RequestMapping(value = "/orderRefundNotify", method = RequestMethod.POST)
    @ApiOperation(value = "充值回调")
    @ResponseBody
    public void orderRefundNotify(@RequestBody String reqBodyStr, HttpServletRequest request, HttpServletResponse response) {
        try {
            log.info(reqBodyStr);
            HighGasOrderRefund highGasOrderRefund = new HighGasOrderRefund();
            highGasOrderRefund.setCreateTime(new Date());
            highGasOrderRefund.setReturnContent(reqBodyStr);
            highGasOrderRefundMapper.insert(highGasOrderRefund);

            JSONObject dataObject = JSONObject.parseObject(reqBodyStr, JSONObject.class);
            String dataStr = AESEncodeUtil.aesDecryptByBytes(AESEncodeUtil.base64Decode(dataObject.getString("data")), CommonSysConst.getSysConfig().getTuanYouAppSecret());
            JSONObject object = JSONObject.parseObject(dataStr);

            // 查询订单
            OutRechargeOrder order = outRechargeOrderService.findByOrderNo(object.getString("orderNumber"));

            // 订单状态 ： 1.待支付 2.已支付 3.已完成 4.已取消 5.已退款 6.充值失败
            if (order != null && order.getStatus() == 5) {
                // 退单结果 true:成功  false:失败
                if (object.getInteger("rechargeState") == 1 && order != null) {
                     order.setStatus(3);
                     outRechargeOrderService.updateOrder(order);
                } else if (object.getInteger("rechargeState") == 0 && order != null) {
                    order.setStatus(6);
                    outRechargeOrderService.updateOrder(order);
                }
            }
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer= response.getWriter();
            writer.write("success");

        } catch (Exception e) {
            log.error("WechatPayController --> wechatNotify() error!", e);
        }
    }

}
