LabelRuleRepository.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\LabelRuleDao;
  13. use app\common\repositories\BaseRepository;
  14. use FormBuilder\Factory\Elm;
  15. use think\facade\Db;
  16. /**
  17. * Class LabelRuleRepository
  18. * @package app\common\repositories\user
  19. * @author xaboy
  20. * @day 2020/10/20
  21. * @mixin LabelRuleDao
  22. */
  23. class LabelRuleRepository extends BaseRepository
  24. {
  25. /**
  26. * LabelRuleRepository constructor.
  27. * @param LabelRuleDao $dao
  28. */
  29. public function __construct(LabelRuleDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. public function getList(array $where, $page, $limit)
  34. {
  35. $query = $this->dao->search($where);
  36. $count = $query->count();
  37. $list = $query->with(['label'])->order('label_rule_id DESC')->page($page, $limit)->select()->toArray();
  38. return compact('count', 'list');
  39. }
  40. /**
  41. * @param $data
  42. * @return mixed
  43. * @author xaboy
  44. * @day 2020/10/21
  45. */
  46. public function create($data)
  47. {
  48. return Db::transaction(function () use ($data) {
  49. $labelName = $data['label_name'];
  50. unset($data['label_name']);
  51. $label = app()->make(UserLabelRepository::class)->create([
  52. 'label_name' => $labelName,
  53. 'mer_id' => $data['mer_id'],
  54. 'type' => 1
  55. ]);
  56. $data['label_id'] = $label->label_id;
  57. return $this->dao->create($data);
  58. });
  59. }
  60. /**
  61. * @param $id
  62. * @param $data
  63. * @return mixed
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. * @author xaboy
  68. * @day 2020/10/21
  69. */
  70. public function update($id, $data)
  71. {
  72. $rule = $this->dao->get($id);
  73. return Db::transaction(function () use ($data, $rule) {
  74. $labelName = $data['label_name'];
  75. unset($data['mer_id'], $data['label_name']);
  76. app()->make(UserLabelRepository::class)->update($rule->label_id, ['label_name' => $labelName]);
  77. return $rule->save($data);
  78. });
  79. }
  80. /**
  81. * @param $id
  82. * @return mixed
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. * @author xaboy
  87. * @day 2020/10/21
  88. */
  89. public function delete($id)
  90. {
  91. $rule = $this->dao->get($id);
  92. return Db::transaction(function () use ($rule) {
  93. app()->make(UserLabelRepository::class)->delete($rule->label_id);
  94. app()->make(UserRepository::class)->rmLabel($rule->label_id);
  95. return $rule->delete();
  96. });
  97. }
  98. /**
  99. * @param $id
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. * @author xaboy
  104. * @day 2020/10/21
  105. */
  106. public function syncUserNum($id)
  107. {
  108. $rule = $this->dao->get($id);
  109. $rule->update_time = date('Y-m-d H:i:s');
  110. $ids = [];
  111. $userMerchantRepository = app()->make(UserMerchantRepository::class);
  112. //订单金额
  113. if ($rule->type == 1) {
  114. $ids = $userMerchantRepository->priceUserIds($rule->mer_id, $rule->min, $rule->max);
  115. //订单数
  116. } else if ($rule->type == 0) {
  117. $ids = $userMerchantRepository->numUserIds($rule->mer_id, $rule->min, $rule->max);
  118. }
  119. $rule->user_num = count($ids);
  120. $idList = array_chunk($ids, 50);
  121. Db::transaction(function () use ($rule, $idList, $userMerchantRepository) {
  122. $userMerchantRepository->rmLabel($rule->label_id);
  123. foreach ($idList as $ids) {
  124. $userMerchantRepository->search(['uids' => $ids])->update([
  125. 'A.label_id' => Db::raw('trim(BOTH \',\' FROM CONCAT(IFNULL(A.label_id,\'\'),\',' . $rule->label_id . '\'))')
  126. ]);
  127. }
  128. $rule->save();
  129. });
  130. }
  131. }