nk-lihaink-cn/app/admin/model/user/UserMerchant.php
2023-03-11 18:12:45 +08:00

158 lines
5.1 KiB
PHP

<?php
namespace app\common\dao\user;
// use app\common\model\user\UserLabel;
use think\db\BaseQuery;
use think\Model;
/**
* Class UserMerchant
*/
class UserMerchant extends Model
{
/**
* @return string|null
* @author xaboy
*/
public static function tablePk(): ?string
{
return 'user_merchant_id';
}
/**
* @return string
* @author xaboy
* @day 2020/10/20
*/
public static function tableName(): string
{
return 'user_merchant';
}
public function user()
{
return $this->hasOne(User::class, 'uid', 'uid');
}
/**
* @param $value
* @return array
* @author xaboy
* @day 2020-05-09
*/
public function getLabelIdAttr($value)
{
return $value ? explode(',', $value) : [];
}
/**
* @param $value
* @return string
* @author xaboy
* @day 2020-05-09
*/
public function setLabelIdAttr($value)
{
return implode(',', $value);
}
public function getAuthLabelAttr()
{
return app()->make(UserLabel::class)->whereIn('label_id', $this->label_id)->where('mer_id', $this->mer_id)->where('type', 1)->column('label_id');
}
/**
* @return string
* @author xaboy
* @day 2020/10/20
*/
protected function getModel(): string
{
return UserMerchant::class;
}
/**
* @param $uid
* @param $mer_id
* @return bool
* @author xaboy
* @day 2020/10/20
*/
public function isMerUser($uid, $mer_id)
{
return $this->existsWhere(compact('uid', 'mer_id'));
}
/**
* @param $uid
* @param $mer_id
* @return int
* @throws \think\db\exception\DbException
* @author xaboy
* @day 2020/10/20
*/
public function updateLastTime($uid, $mer_id)
{
return UserMerchant::getDB()->where(compact('uid', 'mer_id'))->update([
'last_time' => date('Y-m-d H:i:s')
]);
}
/**
* @param array $where
* @return mixed
* @author xaboy
* @day 2020/10/20
*/
public function search(array $where)
{
return UserMerchant::getDB()->alias('A')->leftJoin('User B', 'A.uid = B.uid')
->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
$query->where('A.mer_id', $where['mer_id']);
})->when(isset($where['nickname']) && $where['nickname'], function (BaseQuery $query) use ($where) {
return $query->where('B.nickname', 'like', '%' . $where['nickname'] . '%');
})->when(isset($where['sex']) && $where['sex'] !== '', function (BaseQuery $query) use ($where) {
return $query->where('B.sex', intval($where['sex']));
})->when(isset($where['is_promoter']) && $where['is_promoter'] !== '', function (BaseQuery $query) use ($where) {
return $query->where('B.is_promoter', $where['is_promoter']);
})->when(isset($where['uids']), function (BaseQuery $query) use ($where) {
return $query->whereIn('A.uid', $where['uids']);
})->when(isset($where['user_time_type']) && $where['user_time_type'] !== '' && $where['user_time'] != '', function ($query) use ($where) {
if ($where['user_time_type'] == 'visit') {
getModelTime($query, $where['user_time'], 'A.last_time');
}
if ($where['user_time_type'] == 'add_time') {
getModelTime($query, $where['user_time'], 'A.create_time');
}
})->when(isset($where['pay_count']) && $where['pay_count'] !== '', function ($query) use ($where) {
if ($where['pay_count'] == -1) {
$query->where('A.pay_num', 0);
} else {
$query->where('A.pay_num', '>', $where['pay_count']);
}
})->when(isset($where['label_id']) && $where['label_id'] !== '', function (BaseQuery $query) use ($where) {
return $query->whereRaw('CONCAT(\',\',A.label_id,\',\') LIKE \'%,' . $where['label_id'] . ',%\'');
})->when(isset($where['user_type']) && $where['user_type'] !== '', function (BaseQuery $query) use ($where) {
return $query->where('B.user_type', $where['user_type']);
})->where('A.status', 1);
}
public function numUserIds($mer_id, $min, $max = null)
{
return UserMerchant::getDB()->where('mer_id', $mer_id)->where('pay_num', '>=', $min)->when(!is_null($max), function ($query) use ($max) {
$query->where('pay_num', '<=', $max);
})->group('uid')->column('uid');
}
public function priceUserIds($mer_id, $min, $max = null)
{
return UserMerchant::getDB()->where('mer_id', $mer_id)->where('pay_price', '>=', $min)->when(!is_null($max), function ($query) use ($max, $min) {
$query->where('pay_price', $min == $max ? '<=' : '<', $max);
})->group('uid')->column('uid');
}
}