| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace app\common\model;
- use think\Exception;
- use think\Model;
- class MoneyLog extends Model
- {
- // 表名
- protected $name = 'money_log';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'create_time';
- protected $updateTime = false;
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'create_time_text'
- ];
- //订单状态: 0未支付 1完成 2冻结 3取消
- const Recharge = 1;
- const Withdraw = 2;
- const Pay = 3;
- const PayBack = 4;
- const OrderBonus = 5;
- const Commission = 6;
- const SystemChange = 7;
- protected function getStatusNamesArr(){
- $status_names = [
- '-1' => __('全部状态'),
- self::Recharge => __('充值'),
- self::Withdraw => __('提现'),
- self::Pay => __('交易'),
- self::PayBack => __('交易'),
- self::OrderBonus => __('佣金'),
- self::Commission => __('奖励'),
- self::SystemChange => __('系统'),
- ];
- return $status_names;
- }
- /*
- * 订单状态
- * 0未支付 1完成 2冻结 3取消
- */
- public function getStatusNames($type = -1){
- $status_names = $this->getStatusNamesArr();
- if($type == -1){
- return $status_names;
- }
- if(isset($status_names[$type])){
- return $status_names[$type];
- }
- return __('未定义');
- }
- /**
- * 变更会员余额
- * @param int $money 余额
- * @param int $user_id 会员ID
- * @param string $memo 备注
- */
- public function change($user_id, $amount, $action, $from_id, $memo)
- {
- $user = (new Users())->lock(true)->find($user_id);
- if(empty($user)){
- throw new Exception(__('会员信息不存在'));
- }
- if($amount == 0){
- throw new Exception(__('参数有误'));
- }
- if (!array_key_exists($action, $this->getStatusNamesArr())) {
- throw new Exception(__('账变类型不存在'));
- }
- $balance = function_exists('bcadd') ? bcadd($user->balance, $amount, 2) : $user->balance + $amount;
- //更新会员信息
- $user->save(['balance' => $balance]);
- //写入日志
- MoneyLog::create([
- 'user_id' => $user_id,
- 'amount' => $amount,
- 'balance' => $balance,
- 'action' => $action,
- 'from_id' => $from_id,
- 'note' => $memo
- ]);
- }
- //
- public function users()
- {
- return $this->hasOne('Users','id','user_id',[],'LEFT')->setEagerlyType(0);
- }
- public function fromuser()
- {
- return $this->hasOne('Users','id','from_id',[],'LEFT')->setEagerlyType(0);
- }
- public function getCreateTimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- protected function setCreateTimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- }
|