UserMerchantRepository.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\dao\user\UserMerchantDao;
  13. use app\common\repositories\BaseRepository;
  14. use FormBuilder\Factory\Elm;
  15. use think\facade\Db;
  16. use think\facade\Route;
  17. /**
  18. * Class UserMerchantRepository
  19. * @package app\common\repositories\user
  20. * @author xaboy
  21. * @day 2020/10/20
  22. * @mixin UserMerchantDao
  23. */
  24. class UserMerchantRepository extends BaseRepository
  25. {
  26. /**
  27. * UserMerchantRepository constructor.
  28. * @param UserMerchantDao $dao
  29. */
  30. public function __construct(UserMerchantDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. public function getList(array $where, $page, $limit)
  35. {
  36. $query = $this->dao->search($where);
  37. $count = $query->count();
  38. $make = app()->make(UserLabelRepository::class);
  39. $list = $query->setOption('field', [])->field('A.uid,A.user_merchant_id,B.avatar,B.nickname,B.user_type,A.last_pay_time,A.first_pay_time,A.label_id,A.create_time,A.last_time,A.pay_num,A.pay_price')
  40. ->page($page, $limit)->order('A.user_merchant_id DESC')->select()->each(function ($item) use ($where, $make) {
  41. return $item->label = count($item['label_id']) ? $make->labels($item['label_id'], $where['mer_id']) : [];
  42. });
  43. return compact('count', 'list');
  44. }
  45. /**
  46. * @param $uid
  47. * @param $merId
  48. * @return \app\common\dao\BaseDao|\think\Model
  49. * @author xaboy
  50. * @day 2020/10/20
  51. */
  52. public function create($uid, $merId)
  53. {
  54. return $this->dao->create([
  55. 'uid' => $uid,
  56. 'mer_id' => $merId,
  57. ]);
  58. }
  59. /**
  60. * @param $uid
  61. * @param $mer_id
  62. * @return \app\common\dao\BaseDao|array|\think\Model
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @author xaboy
  67. * @day 2020/10/20
  68. */
  69. public function getInfo($uid, $mer_id)
  70. {
  71. $user = $this->dao->getWhere(compact('uid', 'mer_id'));
  72. if (!$user) $user = $this->create($uid, $mer_id);
  73. return $user;
  74. }
  75. /**
  76. * @param $uid
  77. * @param $merId
  78. * @param $pay_price
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @author xaboy
  83. * @day 2020/10/21
  84. */
  85. public function updatePayTime($uid, $merId, $pay_price, $flag = true)
  86. {
  87. $user = $this->getInfo($uid, $merId);
  88. $time = date('Y-m-d H:i:s');
  89. $user->last_pay_time = $time;
  90. if ($flag)
  91. $user->pay_num++;
  92. $user->pay_price = bcadd($user->pay_price, $pay_price, 2);
  93. if (!$user->first_pay_time) $user->first_pay_time = $time;
  94. $user->save();
  95. }
  96. public function rmLabel($id)
  97. {
  98. return $this->dao->search(['label_id' => $id])->update([
  99. 'A.label_id' => Db::raw('(trim(BOTH \',\' FROM replace(CONCAT(\',\',A.label_id,\',\'),\',' . $id . ',\',\',\')))')
  100. ]);
  101. }
  102. public function changeLabelForm($merId, $id)
  103. {
  104. $user = $this->dao->get($id);
  105. /** @var UserLabelRepository $make */
  106. $userLabelRepository = app()->make(UserLabelRepository::class);
  107. $data = $userLabelRepository->allOptions($merId);
  108. return Elm::createForm(Route::buildUrl('merchantUserChangeLabel', compact('id'))->build(), [
  109. Elm::selectMultiple('label_id', '用户标签', $userLabelRepository->intersection($user->label_id, $merId, 0))->options(function () use ($data) {
  110. $options = [];
  111. foreach ($data as $value => $label) {
  112. $options[] = compact('value', 'label');
  113. }
  114. return $options;
  115. })
  116. ])->setTitle('修改用户标签');
  117. }
  118. }