123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- declare (strict_types=1);
- namespace app\services\user\label;
- use app\services\BaseServices;
- use app\dao\user\label\UserLabelRelationDao;
- use crmeb\exceptions\AdminException;
- class UserLabelRelationServices extends BaseServices
- {
-
- public function __construct(UserLabelRelationDao $dao)
- {
- $this->dao = $dao;
- }
-
- public function getUserLabels(int $uid, int $type = 0, int $relation_id = 0)
- {
- return $this->dao->getColumn(['uid' => $uid, 'type' => $type, 'relation_id' => $relation_id], 'label_id', '');
- }
-
- public function setUserLable($uids, array $labels, int $type = 0, int $relation_id = 0, bool $group = false)
- {
- if (!$uids) {
- return true;
- }
- if (!is_array($uids)) {
- $uids = [$uids];
- }
-
- $data = [];
- foreach ($uids as $uid) {
-
- $user_label_ids = $this->dao->getColumn([
- ['uid', '=', $uid],
- ['type', '=', $type],
- ['relation_id', '=', $relation_id]
- ], 'label_id');
-
- if ($group) {
- $del_labels = array_diff($user_label_ids, $labels);
- $this->dao->delete([
- ['uid', '=', $uid],
- ['label_id', 'in', $del_labels],
- ['type', '=', $type],
- ['relation_id', '=', $relation_id]
- ]);
- }
- $add_labels = array_diff($labels, $user_label_ids);
- if ($add_labels) {
- foreach ($add_labels as $label) {
- $label = (int)$label;
- if (!$label) continue;
- $data[] = ['uid' => $uid, 'label_id' => $label, 'type' => $type, 'relation_id' => $relation_id];
- }
- }
- }
- if ($data) {
- if (!$this->dao->saveAll($data))
- throw new AdminException('设置标签失败');
- }
- return true;
- }
-
- public function unUserLabel(int $uid, array $labels = [], int $type = 0, int $relation_id = 0)
- {
- $where = [
- ['uid', '=', $uid],
- ['type', '=', $type],
- ['relation_id', '=', $relation_id]
- ];
-
- if (count($labels)) {
- $where[] = ['label_id', 'in', $labels];
- }
- $this->dao->delete($where);
- return true;
- }
-
- public function getUserLabelList(array $uids, int $type = 0, int $relation_id = 0)
- {
- return $this->dao->getLabelList($uids, $type, $relation_id);
- }
- }
|