package com.wgb.system.service.service;

import com.tool.data.external.client.WechatClient;
import com.tool.data.external.constant.WechatTemplatMessage;
import com.tool.data.external.domain.model.WechatGetUserInfo;
import com.tool.data.external.domain.model.WechatModel;
import com.tool.data.external.domain.request.ReceiveWechatPayResultRequest;
import com.tool.data.external.domain.request.WechatPayUnifiedorder;
import com.tool.data.external.domain.request.wechat.Send;
import com.tool.data.external.domain.request.wechat.SendData;
import com.tool.data.external.domain.request.wechat.SendDataValue;
import com.tool.data.external.domain.response.WechatPayUnifiedorderResponse;
import com.wgb.system.service.component.WXPayUtil;
import com.wgb.system.service.constant.WXPayConstants;
import com.wgb.system.service.domain.mapper.SysPayInfoMapper;
import com.wgb.system.service.domain.model.SysPayInfo;
import com.wgb.system.service.domain.request.SendWechatBeforehandPayRequest;
import com.wgb.system.service.domain.request.WechatModelProperties;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
public class WechatService extends ServiceBase{
    private final WechatClient wechatClient;
    private final SysPayInfoMapper sysPayInfoMapper;
    private final WechatModelProperties wechatModelProperties;

    public WechatService(WechatClient wechatClient,WechatModelProperties wechatModelProperties,SysPayInfoMapper sysPayInfoMapper) {
        this.wechatClient = wechatClient;
        this.wechatModelProperties=wechatModelProperties;
        this.sysPayInfoMapper=sysPayInfoMapper;
    }

    public WechatGetUserInfo getUserInfo(String code){
        return wechatClient.getUserInfo(code);
    }


    public String getTicket(){
        return wechatClient.getTicket();
    }


    //微信预支付
    public WechatPayUnifiedorderResponse sendWechatBeforehandPay(SendWechatBeforehandPayRequest request)throws Exception{
        WechatPayUnifiedorder unifiedorder= new WechatPayUnifiedorder(
                wechatModelProperties.getAppid(),
                request.getOpenId(),
                wechatModelProperties.getMchId(),
                RandomStringUtils.random(4, true, true),
                WXPayConstants.BODY,
                "WGB"+LocalDate.now().toString().replace("-","")+RandomStringUtils.random(4, false, true),
                request.getTotalfee(),
                InetAddress.getLocalHost().getHostAddress(),
                wechatModelProperties.getTradeType(),
                wechatModelProperties.getNotifyUrl(),
                String.valueOf(System.currentTimeMillis()/1000),
                StringUtils.isNoneEmpty(request.getDeviceInfo())?request.getDeviceInfo():"WEB"
        );
        String body= wechatClient.sendWechatBeforehandPay(WXPayUtil.generateSignedXml(unifiedorder,wechatModelProperties.getAppkey()));
        WechatPayUnifiedorderResponse response= (WechatPayUnifiedorderResponse)WXPayUtil.map2Object(WXPayUtil.xmlToMap(body),WechatPayUnifiedorderResponse.class);
        handlerSendWechatBeforehandPay(response);
        sysPayInfoMapper.insert(new SysPayInfo(unifiedorder.getOut_trade_no(),request.getOpenId(),LocalDateTime.now().toString(),request.getTotalfee()));
        response.setAppid(null);
        response.setMch_id(null);
        return response;

    }

    //处理微信与支付结果  并且通知微信
    public void responseToWechatPay(ReceiveWechatPayResultRequest receiveWechatPayResultRequest){
        wechatClient.responseToWechatPay(new ReceiveWechatPayResultRequest());
        wechatClient.noticeWechatPay(new WechatModel());
    }

    public void sendMessage(){
        String openid="oBFwb1XjHoyGX9Zp9_n7WupZxDqM";
        String templateId= WechatTemplatMessage.WECHAT_TEMPLATE_SERVICE;
        SendData sendData=new SendData(
            new SendDataValue("您好！您的订单已被锁匠接收。"),
            new SendDataValue(LocalDateTime.now().toString()),
            new SendDataValue("98元"),
            new SendDataValue("王二五"),
            new SendDataValue("请保持电话畅通，锁匠到达时将会与您取得联系。！"));
        Send send=new Send(openid,templateId,sendData);
        wechatClient.sendMessage(send);
    }

}
