package com.miaxis.app.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.miaxis.common.config.WxpayConfig;
import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.domain.entity.SysDictData;
import com.miaxis.common.core.domain.entity.UserInfo;
import com.miaxis.common.enums.OrderStatusEnum;
import com.miaxis.common.exception.CustomException;
import com.miaxis.common.utils.SecurityUtils;
import com.miaxis.common.utils.uuid.CommonUtils;
import com.miaxis.system.service.ISysDictDataService;
import com.miaxis.wx.domain.WxOrder;
import com.miaxis.wx.dto.WxOrderDTO;
import com.miaxis.wx.service.IWxOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.security.PrivateKey;
import java.security.Signature;
import java.util.Base64;

import static com.miaxis.common.utils.OrderCodeFactory.getOrderCode;

/**
 * 【小程序-微信支付】Controller
 *
 * @author miaxis
 * @date 2021-03-10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(Constants.STUDENT_PREFIX+"/wx")
@Api(tags = {"【H5-微信支付】"})
@Slf4j
public class WxController extends BaseController {

    @Autowired
    private HttpClient httpClient;

    @Autowired
    private WxpayConfig wxpayConfig;

    @Autowired
    private IWxOrderService wxOrderService;


    @Autowired
    private ISysDictDataService dictDataService;

    @Value("${app.appid}")
    private String appid;
    @Value("${wxpay.notifyUrl}")
    private  String notifyUrl ;






    /**
     * 微信支付获取预订单id
     */
    @PostMapping(value = "/prepareOrder")
    @ApiOperation("微信支付下单")
    public Response<JSONObject> getPrepareOrder(@RequestBody WxOrderDTO wxOrderDTO) throws Exception{


        SysDictData sysDictData = dictDataService.selectDictDataById(wxOrderDTO.getDictCode());
        String[] values = sysDictData.getDictValue().split(",");
        //创建本地微信订单
        WxOrder order = createOrder(values);
        wxOrderService.save(order);
        return Response.success(placeWxOrder(order, sysDictData.getDictLabel(),notifyUrl));


    }

    private WxOrder createOrder(String[] values) {
        WxOrder order = new WxOrder();
        UserInfo student = SecurityUtils.getLoginUser().getStudent();
        String orderCode = getOrderCode(student.getId());
        order.setOutTradeNo(orderCode);
        order.setOpenid(student.getOpenid());
        order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
        Double dprice = Double.valueOf(values[0]);
        Double v =dprice* 100;
        order.setTotal(v.intValue());
        order.setOrderDataJson(values[2]);
        return order;
    }

    /**
     * 微信支付获取预订单id
     */
    @PostMapping(value = "/prepareOrder-test")
    @ApiOperation("微信支付下单-测试")
    public Response<JSONObject> getPrepareOrderTest() throws Exception{


        //创建本地微信订单
        WxOrder order = new WxOrder();
        UserInfo student = SecurityUtils.getLoginUser().getStudent();
        String orderCode = getOrderCode(student.getId());
        order.setOutTradeNo(orderCode);
        order.setOpenid(student.getOpenid());
        order.setGoodsType("1");
        wxOrderService.createVipOrder(order);
        //wxOrderService.save(order);
        return Response.success(placeWxOrder(order, "学车vip充值", "http://jpcj-h5.zzxcx.net.zzxcx.net/prod-api/open-api/wx/notify/wxpay1"));


    }


    //下单
    private JSONObject placeWxOrder(WxOrder order, String goodsName, String notifyUrl) throws Exception {
        HttpPost httpPost = initHttpPost();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode rootNode = objectMapper.createObjectNode();
        rootNode.put("mchid",wxpayConfig.getMerchantId())
                .put("appid", appid)
                .put("description", goodsName)
                .put("notify_url", notifyUrl)
                .put("out_trade_no", order.getOutTradeNo());
        rootNode.putObject("amount")
                .put("total", order.getTotal());
        rootNode.putObject("payer")
                .put("openid", order.getOpenid());
        objectMapper.writeValue(bos, rootNode);
        httpPost.setEntity(new StringEntity(rootNode.toString(), "utf-8"));
        HttpResponse response = httpClient.execute(httpPost);
        String bodyAsString = EntityUtils.toString(response.getEntity());
        if (JSONObject.parseObject(bodyAsString).get("prepay_id") == null){
            throw new CustomException(JSONObject.parseObject(bodyAsString).get("message").toString());
        }
        return getWxParamJson(bodyAsString,order.getOutTradeNo());

    }

    // 生成调用调起微信支付所需参数
    private JSONObject getWxParamJson(String bodyAsString, String orderCode) throws Exception {
        String  packageStr = "prepay_id="+JSONObject.parseObject(bodyAsString).get("prepay_id");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("package",packageStr);
        String nonce_str = RandomStringUtils.randomAlphanumeric(32);
        jsonObject.put("nonceStr",nonce_str);
        long timestamp =  System.currentTimeMillis()/1000;
        jsonObject.put("timeStamp",String.valueOf(timestamp));
        jsonObject.put("signType","RSA");
        StringBuffer sb = new StringBuffer();
        sb.append(appid + "\n");
        sb.append(timestamp + "\n");
        sb.append(nonce_str + "\n");
        sb.append(packageStr+ "\n");
        System.out.println(sb);

        File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
        String realPath =file.getAbsolutePath();
        PrivateKey privateKey = CommonUtils.getPrivateKey(realPath);
        // 进行签名服务
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initSign(privateKey);
        signature.update(sb.toString().getBytes("UTF-8"));
        byte[] signedData = signature.sign();
        String base64Str =  Base64.getEncoder().encodeToString(signedData);
        jsonObject.put("paySign",base64Str);
        jsonObject.put("appId",appid);
        return jsonObject;
    }

    private HttpPost initHttpPost() {
        HttpPost httpPost = new HttpPost(wxpayConfig.getV3url());
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Content-type","application/json; charset=utf-8");
        return httpPost;
    }











}
