BalanceTransfer.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/2
  6. * Time: 下午2:25
  7. */
  8. namespace app\common\models\finance;
  9. use app\common\models\BaseModel;
  10. use Illuminate\Database\Eloquent\Builder;
  11. /*
  12. * 余额转让记录
  13. *
  14. * */
  15. class BalanceTransfer extends BaseModel
  16. {
  17. public $table = 'yz_balance_transfer';
  18. protected $guarded = [''];
  19. const TRANSFER_STATUS_SUCCES = 1;
  20. const TRANSFER_STATUS_ERROR =-1;
  21. /**
  22. * 设置全局作用域 拼接 uniacid()
  23. */
  24. public static function boot()
  25. {
  26. parent::boot();
  27. static::addGlobalScope(
  28. function(Builder $builder){
  29. return $builder->uniacid();
  30. }
  31. );
  32. }
  33. /**
  34. * 关联会员数据表,一对一
  35. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  36. */
  37. public function transferInfo()
  38. {
  39. return $this->hasOne('app\common\models\Member', 'uid', 'transferor');
  40. }
  41. /**
  42. * 关联会员数据表,一对一
  43. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  44. */
  45. public function recipientInfo()
  46. {
  47. return $this->hasOne('app\common\models\Member', 'uid', 'recipient');
  48. }
  49. /**
  50. * 检索条件 单号/流水号
  51. * @param $query
  52. * @param $orderSn
  53. * @return mixed
  54. */
  55. public function scopeOfOrderSn($query,$orderSn)
  56. {
  57. return $query->where('order_sn',$orderSn);
  58. }
  59. /**
  60. * @param $query
  61. * @return mixed
  62. */
  63. public function scopeWithTransfer($query)
  64. {
  65. return $query->with(['transferInfo' => function($transferInfo) {
  66. return $transferInfo->select('uid', 'nickname', 'realname', 'avatar', 'mobile');
  67. }]);
  68. }
  69. /**
  70. * @param $query
  71. * @return mixed
  72. */
  73. public function scopeWithRecipient($query)
  74. {
  75. return $query->with(['recipientInfo' => function($recipientInfo) {
  76. return $recipientInfo->select('uid', 'nickname', 'realname', 'avatar', 'mobile');
  77. }]);
  78. }
  79. /**
  80. * 记录检索
  81. * @param $query
  82. * @return mixed
  83. */
  84. public function scopeRecords($query)
  85. {
  86. return $query->withTransfer()->withRecipient();
  87. }
  88. /**
  89. * 条件检索
  90. * @param $query
  91. * @param $search
  92. * @return mixed
  93. */
  94. public function scopeSearch($query,$search)
  95. {
  96. if ($search['transfer']) {
  97. $query = $query->whereHas('transferInfo',function($query)use($search) {
  98. $query->select('uid', 'nickname', 'realname', 'avatar', 'mobile')
  99. ->where('uid',$search['transfer'])
  100. ->orWhere('nickname','like', '%'.$search['transfer']. '%')
  101. ->orWhere('mobile','like','%'.$search['transfer']. '%')
  102. ->orWhere('realname','like','%'.$search['transfer']. '%');
  103. });
  104. }
  105. if ($search['recipient']) {
  106. $query = $query->whereHas('recipientInfo',function($query)use($search) {
  107. $query->select('uid', 'nickname', 'realname', 'avatar', 'mobile')
  108. ->where('uid',$search['recipient'])
  109. ->orWhere('nickname','like', '%'.$search['recipient']. '%')
  110. ->orWhere('mobile','like','%'.$search['recipient']. '%')
  111. ->orWhere('realname','like','%'.$search['recipient']. '%');
  112. });
  113. }
  114. return $query;
  115. }
  116. /////////////////////////////////
  117. // 以下废弃使用, 慢慢移除
  118. /**
  119. * 关联会员数据表,一对一
  120. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  121. */
  122. public function transferorInfo()
  123. {
  124. return $this->hasOne('app\common\models\Member', 'uid', 'transferor');
  125. }
  126. /**
  127. * @param $recordId
  128. *
  129. * @return mixed */
  130. public static function getTransferRecordByRecordId($recordId)
  131. {
  132. return self::where('id', $recordId)->first();
  133. }
  134. /*
  135. * 获取会员余额转让记录
  136. *
  137. * @params int $transferId
  138. *
  139. * @return object
  140. * @Author yitian */
  141. public static function getMemberTransferRecord($transferId) {
  142. return self::uniacid()
  143. ->select('recipient', 'money', 'created_at', 'status')
  144. ->where('transferor', $transferId)
  145. ->with(['recipientInfo' => function($query) {
  146. return $query->select('uid', 'nickname', 'realname');
  147. }])
  148. ->get();
  149. }
  150. /*
  151. * 获取会员被转让记录
  152. *
  153. * @params int $recipientId
  154. *
  155. * @return object
  156. * @Author yitian */
  157. public static function getMemberRecipientRecord($recipientId) {
  158. return self::uniacid()
  159. ->select('transferor', 'money', 'created_at', 'status')
  160. ->where('recipient', $recipientId)
  161. ->with(['transferorInfo' => function($query) {
  162. return $query->select('uid', 'nickname', 'realname');
  163. }])
  164. ->get();
  165. }
  166. /**
  167. * 定义字段名
  168. *
  169. * @return array */
  170. public function atributeNames() {
  171. return [
  172. 'uniacid' => "公众号ID不能为空",
  173. 'transferor'=> "转让者ID不能为空",
  174. 'recipient' => '被转让者ID不能为空',
  175. 'money' => '转让金额必须是有效的数字,允许两位小数',
  176. 'status' => '状态不能为空'
  177. ];
  178. }
  179. /**
  180. * 字段规则
  181. *
  182. * @return array */
  183. public function rules()
  184. {
  185. return [
  186. 'uniacid' => "required",
  187. 'transferor'=> "required",
  188. 'recipient' => 'required',
  189. 'money' => 'numeric|regex:/^[0-9]+(.[0-9]{1,2})?$/',
  190. 'status' => 'required'
  191. ];
  192. }
  193. }