Income.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/27
  6. * Time: 下午1:52
  7. */
  8. namespace app\common\models;
  9. use app\common\observers\income\IncomeObserver;
  10. /**
  11. * @property int status
  12. * @property float amount
  13. * @property string incometable_type
  14. *
  15. * Class Income
  16. * @package app\common\models
  17. */
  18. class Income extends BaseModel
  19. {
  20. protected $table = 'yz_member_income';
  21. protected $guarded = [''];
  22. protected $appends = ['status_name', 'pay_status_name'];
  23. /**
  24. * @var Withdraw withdraw
  25. */
  26. private $withdraw;
  27. /**
  28. * 提现状态:未提现
  29. */
  30. const STATUS_INITIAL = 0;
  31. /**
  32. * 提现状态:已提现
  33. */
  34. const STATUS_WITHDRAW = 1;
  35. /**
  36. * 打款状态:无效
  37. */
  38. const PAY_STATUS_INVALID = -1;
  39. /**
  40. * 打款状态:未审核
  41. */
  42. const PAY_STATUS_INITIAL = 0;
  43. /**
  44. * 打款状态:未打款
  45. */
  46. const PAY_STATUS_WAIT = 1;
  47. /**
  48. * 打款状态:已打款
  49. */
  50. const PAY_STATUS_FINISH = 2;
  51. /**
  52. * 打款状态:已驳回
  53. */
  54. const PAY_STATUS_REJECT = 3;
  55. /**
  56. * 分帐插件使用,['order_id' => '', 'mark' => '']
  57. *
  58. * @var array
  59. */
  60. public $separate = [];
  61. /**
  62. * 提现状态名称集合
  63. *
  64. * @var array
  65. */
  66. public static $statusComment = [
  67. self::STATUS_INITIAL => '未提现',
  68. self::STATUS_WITHDRAW => '已提现',
  69. ];
  70. /**
  71. * 打款状态名称集合
  72. *
  73. * @var array
  74. */
  75. public static $payStatusComment = [
  76. self::PAY_STATUS_INVALID => '无效',
  77. self::PAY_STATUS_INITIAL => '未审核',
  78. self::PAY_STATUS_WAIT => '未打款',
  79. self::PAY_STATUS_FINISH => '已打款',
  80. self::PAY_STATUS_REJECT => '已驳回',
  81. ];
  82. public static function boot()
  83. {
  84. parent::boot();
  85. self::observe(IncomeObserver::class);
  86. }
  87. /**
  88. * 通过 $status 值获取 $status 名称
  89. *
  90. * @param $status
  91. * @return mixed|string
  92. */
  93. public static function getStatusComment($status)
  94. {
  95. return isset(static::$statusComment[$status]) ? static::$statusComment[$status] : '';
  96. }
  97. /**
  98. * 通过 $pay_way 值获取 $pay_status 名称
  99. *
  100. * @param $pay_status
  101. * @return mixed|string
  102. */
  103. public static function getPayWayComment($pay_status)
  104. {
  105. return isset(static::$payStatusComment[$pay_status]) ? static::$payStatusComment[$pay_status] : '';
  106. }
  107. /**
  108. * 通过字段 status 输出 status_name ;
  109. *
  110. * @return string
  111. */
  112. public function getStatusNameAttribute()
  113. {
  114. return static::getStatusComment($this->attributes['status']);
  115. }
  116. /**
  117. * 通过字段 pay_status 输出 pay_status_name ;
  118. *
  119. * @return string
  120. */
  121. public function getPayStatusNameAttribute()
  122. {
  123. return static::getPayWayComment($this->attributes['pay_status']);
  124. }
  125. /**
  126. * 可提现收入检索条件
  127. *
  128. * @param $query
  129. * @return mixed
  130. */
  131. public function scopeCanWithdraw($query)
  132. {
  133. return $query->where('status', static::STATUS_INITIAL);
  134. }
  135. /**
  136. * 关联会员数据表
  137. *
  138. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  139. */
  140. public function member()
  141. {
  142. return $this->belongsTo('app\common\models\Member', 'member_id', 'uid');
  143. }
  144. //todo 以下代码未检查 yitian :: 2017-11-14
  145. /**
  146. * @param $id
  147. * @return mixed
  148. */
  149. public static function getIncomeFindId($id)
  150. {
  151. return self::find($id);
  152. }
  153. /**
  154. * @param $id
  155. * @return mixed
  156. */
  157. public static function getIncomeById($id)
  158. {
  159. return self::uniacid()
  160. ->where('id', $id);
  161. }
  162. /**
  163. * @param $ids
  164. * @return mixed
  165. */
  166. public static function getIncomeByIds($ids)
  167. {
  168. return self::uniacid()
  169. ->whereIn('id', explode(',', $ids));
  170. }
  171. public function incometable()
  172. {
  173. return $this->morphTo();
  174. }
  175. /**
  176. * @return mixed
  177. */
  178. public static function getIncomes()
  179. {
  180. return self::uniacid();
  181. }
  182. public static function getIncomeInMonth($search)
  183. {
  184. $model = self::select('create_month');
  185. $model->uniacid();
  186. $model->with(['hasManyIncome' => function ($query) use ($search) {
  187. $query->select('id', 'create_month', 'incometable_type', 'type_name', 'amount', 'created_at');
  188. if ($search['type']) {
  189. $query->where('incometable_type', $search['type']);
  190. }
  191. $query->where('member_id', \YunShop::app()->getMemberId());
  192. $query->orderBy('id', 'desc');
  193. return $query->get();
  194. }]);
  195. $model->groupBy('create_month');
  196. $model->orderBy('create_month', 'desc');
  197. return $model;
  198. }
  199. public static function getDetailById($id)
  200. {
  201. $model = self::uniacid();
  202. $model->select('detail');
  203. $model->where('id', $id);
  204. return $model;
  205. }
  206. public static function getWithdraw($type, $typeId, $status)
  207. {
  208. return self::where('type', 'commission')
  209. ->where('member_id', \YunShop::app()->getMemberId())
  210. ->whereIn('id', explode(',', $typeId))
  211. ->update(['status' => $status]);
  212. }
  213. public static function updatedWithdraw($type, $typeId, $status)
  214. {
  215. return self::where('member_id', \YunShop::app()->getMemberId())
  216. ->whereIn('id', explode(',', $typeId))
  217. ->update(['status' => $status]);
  218. }
  219. public static function updatedIncomeStatus($type, $typeId, $status)
  220. {
  221. return self::where('member_id', \YunShop::app()->getMemberId())
  222. ->whereIn('id', explode(',', $typeId))
  223. ->update(['status' => $status]);
  224. }
  225. public function hasManyIncome()
  226. {
  227. return $this->hasMany(self::class, "create_month", "create_month");
  228. }
  229. public function hasManyOrder()
  230. {
  231. return $this->hasOne(Order::class, "order_sn", "order_sn");
  232. }
  233. public static function updatedIncomePayStatus($id, $updatedData)
  234. {
  235. return self::where('id', $id)
  236. ->update($updatedData);
  237. }
  238. public static function getIncomesList($search)
  239. {
  240. if($search['select']){
  241. $selects = ['id', 'create_month', 'incometable_type', 'type_name', 'amount', 'created_at','detail','incometable_id'];
  242. }else{
  243. $selects = ['id', 'create_month', 'incometable_type', 'type_name', 'amount', 'created_at'];
  244. }
  245. $model = self::uniacid();
  246. $model->select($selects);
  247. if ($search['type']) {
  248. $model->where('incometable_type', $search['type']);
  249. }
  250. $model->where('member_id', \YunShop::app()->getMemberId());
  251. $model->orderBy('id', 'desc');
  252. return $model;
  253. }
  254. /**
  255. * @return Withdraw
  256. */
  257. public function withdraw()
  258. {
  259. if (!isset($this->withdraw)) {
  260. $this->withdraw = Withdraw::where('type_id', 'like', "%{$this->id}%")->where('type', $this->incometable_type)->first();
  261. }
  262. return $this->withdraw;
  263. }
  264. }