| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace app\common\model;
- use app\common\model\user\PointsLog as PointsLogModel;
- /**
- * 用户模型类
- * Class User
- * @package app\common\model
- */
- class User extends BaseModel
- {
- protected $name = 'user';
- // 性别
- private $gender = ['未知', '男', '女'];
- /**
- * 关联会员等级表
- * @return \think\model\relation\BelongsTo
- */
- public function grade()
- {
- $module = self::getCalledModule() ?: 'common';
- return $this->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);
- }
- }
|