package com.icloud.server.scala.pay

import com.fasterxml.jackson.annotation.JsonProperty
import com.icloud.server.scala.goods.{Goods, GoodsRepository, Goodspayditch}
import com.icloud.server.utils.IdWorker
import com.icloud.server.web.err.{GlobalErrorCodeConstants, ServiceException}
import com.icloud.server.web.json.JsonResult
import io.swagger.annotations.{Api, ApiImplicitParam, ApiImplicitParams, ApiModel, ApiModelProperty, ApiOperation}
import lombok.{AllArgsConstructor, Data, NoArgsConstructor}
import org.hibernate.annotations.{CreationTimestamp, UpdateTimestamp}
import org.springframework.data.jpa.repository.{JpaRepository, JpaSpecificationExecutor}
import org.springframework.data.repository.{CrudRepository, PagingAndSortingRepository}
import org.springframework.stereotype.Repository
import org.springframework.web.bind.annotation.{CrossOrigin, PostMapping, RequestBody, RequestMapping, ResponseBody, RestController}

import java.util.Date
import javax.annotation.Resource
import javax.persistence.{Column, Entity, Id, JoinColumn, ManyToOne, Table}
import scala.annotation.meta.field
import scala.beans.BeanProperty
@Api( tags = Array( "支付支付配制" ), value = "API",hidden = true )
@RestController
@RequestMapping(Array("/pay/"))
@CrossOrigin
class PayRealmNameCotroller {

  @Resource
  @BeanProperty
  var prnr:PayRealmNameRepository=_

  @ApiOperation( value = "创建支付域名", notes = "" )
  @ApiImplicitParams( Array(
    new ApiImplicitParam( name = "req", value = "支付域名", required = true, dataType = "CreatePayRealmNameReq" )
  ) )
  @PostMapping( Array( "createrealmname" ) )
  @ResponseBody
  def createrealmname(@RequestBody req:CreatePayRealmNameReq): JsonResult ={
    val id = new IdWorker().nextId()

    prnr.save(new PayRealmName().toEntity(id,req.uid: Long,null, req.url  ,req.remarks,0,req.appid,req.secret))
    new JsonResult("ok")
  }
  @ApiOperation( value = "查询所有支付域名", notes = "" )
  @ApiImplicitParams( Array(
    new ApiImplicitParam( name = "uid", value = "uid", required = true, dataType = "Long" )
  ) )
  @PostMapping( Array( "selectrealmname" ) )
  @ResponseBody
  def select(uid:Long): JsonResult ={

    new JsonResult(prnr.findByUseridAndStatus(uid,0))
  }
  @ApiOperation( value = "更新支付域名", notes = "" )
  @ApiImplicitParams( Array(
    new ApiImplicitParam( name = "req", value = "支付域名实体类", required = true, dataType = "UpdatePayRealmNameReq" )
  ) )
  @PostMapping( Array( "updaterealmname" ) )
  @ResponseBody
  def updaterealmname(@RequestBody req:UpdatePayRealmNameReq): JsonResult ={
    val id = req.id
    val uid = req.uid
    val e = prnr.findByUseridAndStatus(uid, 0)
    if(!e.isEmpty){
      prnr.save(new PayRealmName().toEntity(req.id,req.uid: Long,null, req.url  ,req.remarks,0,req.appid,req.secret))
    }else{
       throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST)
    }

    new JsonResult("ok")
  }
}
@Repository trait PayRealmNameRepository extends CrudRepository[PayRealmName, Integer] with JpaSpecificationExecutor[PayRealmName] with JpaRepository[PayRealmName, Integer] with PagingAndSortingRepository[PayRealmName, Integer] {
  def findByUseridAndStatus (userid: Long,status: Int): java.util.List[PayRealmName]
  def findByUrlAndAndStatus (url: String,status: Int): java.util.List[PayRealmName]


}
@Data
@Entity
@Table( name = "pay_realm_name" )
@NoArgsConstructor
@AllArgsConstructor
class PayRealmName{
  @Id
  @BeanProperty
  @Column
  var id: Long = _
  @BeanProperty
  @Column(name="user_id")
  var userid: Long = _
  @BeanProperty
  @Column(name = "tenant_id")
  var tenantid: String = _
  @BeanProperty
  @Column(name = "url")
  var url: String = _
  @BeanProperty
  @Column
  var remarks: String = _
  @BeanProperty
  @Column
  var appid: String = _
  @BeanProperty
  @Column
  var secret: String = _



  @BeanProperty
  @Column var status: Int = _
  @CreationTimestamp
  @BeanProperty
  @Column var CREATETIME: Date = _
  @UpdateTimestamp
  @BeanProperty
  @Column var UPDATETIME: Date = _

  def toEntity(id: Long,userid: Long,tenantid: String, url: String  ,remarks: String,status: Int,appid: String,secret: String ): PayRealmName ={
    this.id=id
    this.userid=userid
    this.tenantid=tenantid
    this.url=url
    this.remarks=remarks
    this.status=status
    this.appid=appid
    this.secret=secret
    this
  }
}
@ApiModel(description = "创建支付域名")
case class CreatePayRealmNameReq(@(ApiModelProperty@field)(position = 1, example = "1232", value = "uid") @JsonProperty("uid") uid:Long,
                                 @(ApiModelProperty@field)(position = 1, example = "1232", value = "url") @JsonProperty("url") url: String,
                                 @(ApiModelProperty@field)(position = 1, example = "1232", value = "remarks") @JsonProperty("remarks") remarks: String,
                                 @(ApiModelProperty@field)(position = 1, example = "1232", value = "appid") @JsonProperty("appid") appid: String,
                                 @(ApiModelProperty@field)(position = 1, example = "1232", value = "secret") @JsonProperty("secret") secret: String
                                )
@ApiModel(description = "更新支付域名")
case class UpdatePayRealmNameReq(@(ApiModelProperty@field)(position = 1, example = "1232", value = "id") @JsonProperty("id") id:Long,
                                 @(ApiModelProperty@field)(position = 1, example = "1232", value = "uid") @JsonProperty("uid") uid:Long,
                                 @(ApiModelProperty@field)(position = 1, example = "1232", value = "url") @JsonProperty("url") url: String,
                                 @(ApiModelProperty@field)(position = 1, example = "1232", value = "remarks") @JsonProperty("remarks") remarks: String,
                                 @(ApiModelProperty@field)(position = 1, example = "1232", value = "appid") @JsonProperty("appid") appid: String,
                                 @(ApiModelProperty@field)(position = 1, example = "1232", value = "secret") @JsonProperty("secret") secret: String
                                )
