UserMerchantRepository.php 3.8 KB

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