package com.icloud.server.scala.engine.service

import cn.hutool.core.util.RandomUtil
import com.icloud.server.payutils.dto.PayOrderDO
import com.icloud.server.payutils.{PayEntiy, PayService}
import com.icloud.server.payutils.enums.PayTypeEnum
import com.icloud.server.payutils.pojo.CommonResult
import com.icloud.server.scala.engine.contect.Context
import com.icloud.server.scala.paynotify.{PayNotifyEntity, PayNotifyRepository}
import com.icloud.server.scala.payorder.PayOrderRepository
import com.icloud.server.utils.IdWorker
import org.springframework.stereotype.Component

import java.util.UUID
import javax.annotation.Resource
import scala.beans.BeanProperty

@Component
class PayOrderServer {
  @Resource
  @BeanProperty
  var ps: PayService = _

  @Resource
  @BeanProperty
  var por: PayOrderRepository = _
  @Resource
  @BeanProperty
  var pnr: PayNotifyRepository = _


  def aa(): Unit ={
    println("aaa")
  }
  /**
   * @Description:
   * @Author: yang
   * @Date: 2022-07-12 18:33
   *
   *
   * */
  def getAliPayLink(context:Context): CommonResult[_] = {
    //获取商品信息基础信息
    val goodspay = context.goodspaye
    val subject = goodspay.subject
    val shortname = goodspay.shortname
    val ip = context.request.ip
    val channelId = RandomUtil.randomLong
    val alipayid = context.alipayid
    val phone = if (context.phone != null) context.phone else null
    var uid=context.uid

    //    val url = ""
//    val url = context.goodspaye.payrealmname.url+"/unitePay/notify/aliorder"
    val url = context.goodspaye.payrealmname.url + s"/pay/aliorder/alinotify/${channelId}"
    println("回调地址:"+url)
    val aliappid = context.aliappid
    val privatekey = context.privatekey
    val alipaypublickey = context.alipaypublickey

    val str = UUID.randomUUID.toString.replace("-", "")
    val OrderId = String.valueOf(System.currentTimeMillis)

    val e = new PayNotifyEntity()
    e.setId( new IdWorker().nextId())
    e.setMerchantorderid(OrderId)
    e.setNotifyurl(url)
    e.setOuttradeno(str)
    e.setStatus(0)
    pnr.save(e)
    //todo 订单保存,接收订单,检索订单-------1.0.3
    val id = new IdWorker().nextId()
    val o = new PayOrderDO()
    o.setId(id)
    o.setMerchantid(uid)

    o.setMerchantorderid(OrderId)
    o.setAmount(goodspay.payment)
    o.setAppid(alipayid)
    o.setBody(shortname)
    o.setCreator(phone)
    o.setSubject(subject)
    o.setUserip(ip)
    o.setChannelid(channelId)
    if(context.request.openid!=null){
      o.setOpenid(context.request.openid)
      o.setNickname(context.request.nickname)
      o.setHeadimgurl(context.request.headimgurl)
    }
    por.save(o)

    val pe = new PayEntiy()
    pe.setAmount(goodspay.payment)
    pe.setSubject(subject)
    pe.setBody(shortname)
    pe.setMerchantOrderId(OrderId)
    pe.setUserIp(ip)
    //    pe.setUserIp("127.0.0.1")
    pe.setNotifyUrl(url)
    pe.setOutTradeNo(str)
    pe.setPayType(PayTypeEnum.ALIPAY.getStatus)
    pe.setAppid(aliappid)
    pe.setPrivateKey(privatekey)
    pe.setAlipayPublicKey(alipaypublickey)
    pe.setExpireTime(goodspay.getExpireendtime)
    pe.setChannelId(channelId)
    pe.setReturnUrl(context.goodspaye.payrealmname.url )

    ps.getPayUrl(pe)
  }



  /**
   * @Description:
   * @Author: yang
   * @Date: 2022-07-12 18:33
   *
   *
   * */
  def getWxPayLink(context:Context): CommonResult[_] = {
    //获取商品信息基础信息
    val goodspay = context.goodspaye
    val subject = goodspay.subject
    val shortname = goodspay.shortname
    val ip = context.request.ip
//    val url = ""
    val channelId = RandomUtil.randomLong
    var uid=context.uid

//    val url = context.goodspaye.payrealmname.url+"/unitePay/notify/wxorder"
//val url = context.goodspaye.payrealmname.url + s"/pay/order/wxnotify/${channelId}"
    val url = context.goodspaye.payrealmname.url + s"/pay/order2/notify/order/${channelId}"
    println("回调地址:"+url)
    val OrderId = String.valueOf(System.currentTimeMillis)
    context.request.ip
    var wxappid = context.wxappid
    val mchid = context.mchid
    val mchkey = context.mchkey
    val wxpayid = context.wxpayid
    val phone = if (context.phone != null) context.phone else null
    val str = UUID.randomUUID.toString.replace("-", "")
    val e = new PayNotifyEntity()
    e.setId( new IdWorker().nextId())
    e.setMerchantorderid(OrderId)
    e.setNotifyurl(url)
    e.setOuttradeno(str)
    e.setStatus(0)
    pnr.save(e)
    //String.valueOf(System.currentTimeMillis)
    val o = new PayOrderDO()
    val id = new IdWorker().nextId()

    o.setMerchantid(uid)
    o.setId(id)
    o.setMerchantorderid(OrderId)
    o.setAmount(goodspay.payment)
    o.setAppid(wxpayid)
    o.setBody(shortname)
    o.setCreator(phone)
    o.setSubject(subject)
    o.setUserip(ip)
    o.setChannelid(channelId)
    if(context.request.openid!=null){
      o.setOpenid(context.request.openid)
      o.setNickname(context.request.nickname)
      o.setHeadimgurl(context.request.headimgurl)
    }
    por.save(o)
    val pe = new PayEntiy()
    pe.setBody(subject)
    pe.setExpireTime(goodspay.getExpireendtime)
    pe.setPayType(PayTypeEnum.WXPAY.getStatus)
    pe.setAppid(wxappid)
    if(context.request.openid!=null){
      pe.setOpenid(context.request.openid)
      pe.setAppid(context.wxappid2)
    }
    pe.setMchId(mchid)
    pe.setMchKey(mchkey)
    pe.setUserIp(ip)
    pe.setNotifyUrl(url)
    pe.setAmount(goodspay.payment)
    pe.setMerchantOrderId(OrderId)
    pe.setChannelId(channelId)
    pe.setOutTradeNo(str)
    ps.getPayUrl(pe)

  }

  def getWxJSPayLink(context:Context): CommonResult[_] = {
    //获取商品信息基础信息
    val goodspay = context.goodspaye
    val subject = goodspay.subject
    val shortname = goodspay.shortname
    val ip = context.request.ip
    val wxpayid = context.wxpayid
    val phone = if (context.phone != null) context.phone else null
    var uid=context.uid

    //    val url = ""
    val channelId = RandomUtil.randomLong

//    val url = context.goodspaye.payrealmname.url+"/unitePay/notify/wxorder"
//    val url = context.goodspaye.payrealmname.url + s"/pay/order/wxnotify/${channelId}"
    val url = context.goodspaye.payrealmname.url + s"/pay/order2/notify/order/${channelId}"

    println("回调地址:"+url)

    context.request.ip
    var wxappid = context.wxappid2
    val mchid = context.mchid
    val mchkey = context.mchkey

    val str = UUID.randomUUID.toString.replace("-", "")
    val OrderId = String.valueOf(System.currentTimeMillis)
    val e = new PayNotifyEntity()
    e.setId( new IdWorker().nextId())
    e.setMerchantorderid(OrderId)
    e.setNotifyurl(url)
    e.setOuttradeno(str)
    e.setStatus(0)
    pnr.save(e)
    //String.valueOf(System.currentTimeMillis)
    val id = new IdWorker().nextId()
    val o = new PayOrderDO()
    o.setId(id)
    o.setMerchantid(uid)
    o.setMerchantorderid(OrderId)
    o.setAmount(goodspay.payment)
    o.setAppid(wxpayid)
    o.setBody(shortname)
    o.setCreator(phone)
    o.setSubject(subject)
    o.setUserip(ip)
    if(context.request.openid!=null){
      o.setOpenid(context.request.openid)
      o.setNickname(context.request.nickname)
      o.setHeadimgurl(context.request.headimgurl)
    }
    o.setChannelid(channelId)
    por.save(o)
    val pe = new PayEntiy()
    pe.setBody(subject)
    pe.setExpireTime(goodspay.getExpireendtime)
    pe.setPayType(PayTypeEnum.WXJSPAY.getStatus)
    pe.setAppid(wxappid)
    pe.setOpenid(context.request.openid)

    pe.setMchId(mchid)
    pe.setMchKey(mchkey)
    pe.setUserIp(ip)
    pe.setNotifyUrl(url)
    pe.setAmount(goodspay.payment)
    pe.setMerchantOrderId(OrderId)
    pe.setChannelId(channelId)
    pe.setOutTradeNo(str)
    ps.getPayUrl(pe)

  }

}
