UserExtractRepository.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\user;
  12. use app\common\repositories\BaseRepository;
  13. use app\common\dao\user\UserExtractDao as dao;
  14. use crmeb\jobs\SendTemplateMessageJob;
  15. use crmeb\services\SwooleTaskService;
  16. use think\facade\Db;
  17. use think\facade\Queue;
  18. class UserExtractRepository extends BaseRepository
  19. {
  20. /**
  21. * @var dao
  22. */
  23. protected $dao;
  24. /**
  25. * UserExtractRepository constructor.
  26. * @param dao $dao
  27. */
  28. public function __construct(dao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * TODO
  34. * @param $id
  35. * @return bool
  36. * @author Qinii
  37. * @day 2020-06-16
  38. */
  39. public function getWhereCount($id)
  40. {
  41. $where['extract_id'] = $id;
  42. $where['status'] = 0;
  43. return $this->dao->getWhereCount($where) > 0;
  44. }
  45. /**
  46. * TODO
  47. * @param array $where
  48. * @param $page
  49. * @param $limit
  50. * @return array
  51. * @author Qinii
  52. * @day 2020-06-16
  53. */
  54. public function search(array $where, $page, $limit)
  55. {
  56. $query = $this->dao->search($where);
  57. $count = $query->count();
  58. $list = $query->page($page,$limit)->select();
  59. return compact('count','list');
  60. }
  61. /**
  62. * @param $uid
  63. * @return mixed
  64. * @author xaboy
  65. * @day 2020/6/22
  66. */
  67. public function userTotalExtract($uid)
  68. {
  69. return $this->dao->search(['status' => 1, 'uid' => $uid])->sum('extract_price');
  70. }
  71. /**
  72. * TODO
  73. * @param $user
  74. * @param $data
  75. * @author Qinii
  76. * @day 2020-06-16
  77. */
  78. public function create($user,$data)
  79. {
  80. $data = Db::transaction(function()use($user,$data){
  81. $brokerage_price = bcsub($user['brokerage_price'],$data['extract_price'],2);
  82. $user->brokerage_price = $brokerage_price;
  83. $user->save();
  84. $data['status'] = 0;
  85. $data['uid'] = $user['uid'];
  86. $data['balance'] = $brokerage_price;
  87. return $this->dao->create($data);
  88. });
  89. SwooleTaskService::admin('notice', [
  90. 'type' => 'extract',
  91. 'title' => '您有一条新的提醒申请',
  92. 'id' => $data->extract_id
  93. ]);
  94. }
  95. public function switchStatus($id,$data)
  96. {
  97. Db::transaction(function()use($id,$data){
  98. if($data['status'] == '-1'){
  99. $extract = $this->dao->getWhere(['extract_id' => $id]);
  100. $user = app()->make(UserRepository::class)->get($extract['uid']);
  101. $brokerage_price = bcadd($user['brokerage_price'] ,$extract['extract_price'],2);
  102. $user->brokerage_price = $brokerage_price;
  103. $user->save();
  104. }
  105. $this->dao->update($id,$data);
  106. });
  107. Queue::push(SendTemplateMessageJob::class,[
  108. 'tempCode' => 'ORDER_DELIVER_SUCCESS',
  109. 'id' =>$id
  110. ]);
  111. }
  112. }