LabelRuleRepository.php 4.0 KB

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