belongsTo("app\\{$module}\\model\\user\\Grade"); } /** * 关联收货地址表 * @return \think\model\relation\HasMany */ public function address() { return $this->hasMany('UserAddress'); } /** * 关联收货地址表 (默认地址) * @return \think\model\relation\BelongsTo */ public function addressDefault() { return $this->belongsTo('UserAddress', 'address_id'); } /** * 显示性别 * @param $value * @return mixed */ public function getGenderAttr($value) { return $this->gender[$value]; } /** * 获取用户信息 * @param $where * @param $with * @return null|static * @throws \think\exception\DbException */ public static function detail($where, $with = ['address', 'addressDefault']) { $filter = ['is_delete' => 0]; if (is_array($where)) { $filter = array_merge($filter, $where); } else { $filter['user_id'] = (int)$where; } return static::get($filter, $with); } /** * 编辑记录 * @param $data * @return false|int */ public function edit($data) { if (!$this->validateForm($data, 'edit')) { return false; } return $this->allowField(true)->save($data) !== false; } /** * 表单验证 * @param $data * @param string $scene * @return bool */ private function validateForm($data, $scene = 'edit') { if ($scene === 'edit') { // 需要判断等级权重是否已存在 $checkId = self::where(['mobile'=> $data['mobile']])->whereNotIn('user_id',[$data['user_id']])->value('user_id'); if ($data['mobile'] && $checkId && $data['user_id'] != $checkId) { $this->error = '用户手机号已存在'; return false; } } return true; } /** * 累积用户的实际消费金额 * @param $userId * @param $expendMoney * @return int|true * @throws \think\Exception */ public function setIncUserExpend($userId, $expendMoney) { return $this->where(['user_id' => $userId])->setInc('expend_money', $expendMoney); } /** * 指定会员等级下是否存在用户 * @param $gradeId * @return bool */ public static function checkExistByGradeId($gradeId) { $model = new static; return !!$model->where('grade_id', '=', (int)$gradeId) ->where('is_delete', '=', 0) ->value('user_id'); } /** * 累积用户总消费金额 * @param $money * @return int|true * @throws \think\Exception */ public function setIncPayMoney($money) { return $this->setInc('pay_money', $money); } /** * 累积用户升级总消费金额 * @param $money * @return int|true * @throws \think\Exception */ public function setIncUpgradePayMoney($money) { return $this->setInc('expend_upgrade_money', $money); } /** * 累积用户实际消费的金额 (批量) * @param $data * @return array|false * @throws \Exception */ public function onBatchIncExpendMoney($data) { foreach ($data as $userId => $expendMoney) { $this->where(['user_id' => $userId])->setInc('expend_money', $expendMoney); } return true; } /** * 累积用户实际升级消费的金额 (批量) * @param $data * @return array|false * @throws \Exception */ public function onBatchIncUpgradeMoney($data) { foreach ($data as $userId => $money) { if($money>0){ $this->where(['user_id' => $userId])->setInc('expend_upgrade_money', $money); } } return true; } /** * 累积用户的可用积分数量 (批量) * @param $data * @return array|false * @throws \Exception */ public function onBatchIncPoints($data) { foreach ($data as $userId => $expendMoney) { $this->where(['user_id' => $userId])->setInc('points', $expendMoney); } return true; } /** * 累积用户的可用积分 * @param $points * @param $describe * @return int|true * @throws \think\Exception */ public function setIncPoints($points, $describe) { // 新增积分变动明细 PointsLogModel::add([ 'user_id' => $this['user_id'], 'value' => $points, 'describe' => $describe, ]); // 更新用户可用积分 return $this->setInc('points', $points); } }