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.enums.OrderStatusEnum;
import com.miaxis.common.exception.CustomException;
import com.miaxis.common.utils.uuid.CommonUtils;
import com.miaxis.newgzpt.domain.GzptSchActivation;
import com.miaxis.newgzpt.domain.GzptSchPayConfig;
import com.miaxis.newgzpt.domain.GzptUserInfo;
import com.miaxis.newgzpt.service.IGzptSchActivationService;
import com.miaxis.newgzpt.service.IGzptSchPayConfigService;
import com.miaxis.newgzpt.service.IGzptSchPayLogService;
import com.miaxis.newgzpt.service.IGzptUserInfoService;
import com.miaxis.system.service.ISysDictDataService;
import com.miaxis.wx.domain.WxJsOrder;
import com.miaxis.wx.dto.WxOrderDTO;
import com.miaxis.wx.service.IWxJsOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
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 java.util.Date;

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

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

    @Autowired
    private HttpClient httpClient;

    @Autowired
    private WxpayConfig wxpayConfig;

    @Autowired
    private IWxJsOrderService wxJsOrderService;

    @Autowired
    private ISysDictDataService dictDataService;

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

    @Autowired
    private IGzptUserInfoService userInfoService;

    @Autowired
    private IGzptSchPayConfigService schPayConfigService;

    @Autowired
    private IGzptSchActivationService schActivationService;
    /**
     * 微信支付获取预订单id
     */
    @PostMapping(value = "/prepareOrder")
    @ApiOperation("极速驾培微信下单")
    public Response<WxParamEntity> getPrepareOrder(@RequestBody WxOrderDTO wxOrderDTO) throws Exception{
        SysDictData sysDictData = dictDataService.selectDictDataById(wxOrderDTO.getDictCode());
        String[] values = sysDictData.getDictValue().split(",");

        int isShare = 0;

        //创建本地微信订单
        WxJsOrder order = new WxJsOrder();
        GzptUserInfo gzptUserInfo = userInfoService.getInfoById(wxOrderDTO.getUserId());
        //获取合作驾校配置表
        isShare = schPayConfigService.isSchPayConfigByJxbh(gzptUserInfo.getSchool());

        String orderCode = getOrderCode(gzptUserInfo.getId());
        order.setOutTradeNo(orderCode);
        order.setUserId(gzptUserInfo.getId());
        order.setUserName(gzptUserInfo.getUserName());
        order.setGoodsName(sysDictData.getDictLabel());
        order.setPhoneType(wxOrderDTO.getPhoneType());  //手机类型
        order.setDqbh(gzptUserInfo.getCity());
        order.setDqmc(gzptUserInfo.getCityName());
        order.setSchool(gzptUserInfo.getSchool());
        order.setSchoolName(gzptUserInfo.getSchoolName());
        order.setIsShare(isShare);
        Double price = Double.parseDouble(values[0])*100;   //订单总额
        int b =  new Double(price).intValue();
        order.setTotal(b);

        if(isShare==1) {
            //如果是合作驾校
            if (wxOrderDTO.getPhoneType() == 1) { //苹果
                Double priceApple = Double.parseDouble(values[3]) * 100;   //订单总额
                int priceAppleInt = new Double(priceApple).intValue();
                order.setSchoolCommission(priceAppleInt);
            } else if (wxOrderDTO.getPhoneType() == 2) { //安卓
                Double priceAndroid = Double.parseDouble(values[2]) * 100;   //订单总额
                int priceAndroidInt = new Double(priceAndroid).intValue();
                order.setSchoolCommission(priceAndroidInt);
            }
        }

       if("理论计时".equals(sysDictData.getDictLabel())){
           int isActiva = schActivationService.isSchActivation(gzptUserInfo.getOutId());
           if(isActiva==1){
               throw new CustomException(JSONObject.parseObject("该学员已激活，请勿重复操作").get("message").toString());
           }
       }

        order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
        wxJsOrderService.save(order);
        return Response.success(placeWxOrder(order, sysDictData.getDictLabel()));

    }


    //下单
    private WxParamEntity placeWxOrder(WxJsOrder order, String goodsName) 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());
        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 WxParamEntity getWxParamJson(String bodyAsString, String orderCode) throws Exception {
        WxParamEntity wxParamEntity = new WxParamEntity();
        wxParamEntity.setAppId(appid);
        wxParamEntity.setPartnerId(wxpayConfig.getMerchantId());
        String  prepayId = JSONObject.parseObject(bodyAsString).getString("prepay_id");
        wxParamEntity.setPrepayId(prepayId);
        wxParamEntity.setPackageValue("Sign=WXPay");
        String nonce_str = RandomStringUtils.randomAlphanumeric(32);
        wxParamEntity.setNonceStr(nonce_str);
        String timestamp = String.valueOf(System.currentTimeMillis()/1000);
        wxParamEntity.setTimeStamp(timestamp);

        StringBuffer sb = new StringBuffer();
        sb.append(appid + "\n");
        sb.append(timestamp + "\n");
        sb.append(nonce_str + "\n");
        sb.append(prepayId+ "\n");


        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);
        wxParamEntity.setSign(base64Str);

        return wxParamEntity;
    }

    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;
    }

    @Data
    class  WxParamEntity{
        String appId;
        String partnerId;
        String prepayId;
        String packageValue;
        String nonceStr;
        String timeStamp;
        String sign;

    }










}
