123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- declare (strict_types=1);
- namespace app\model\api;
- use app\model\api\User as UserModel;
- use library\basic\BaseModel;
- use think\facade\Db;
- use think\Model;
- /**
- * @mixin \think\Model
- */
- class UserScoreDetail extends BaseModel
- {
- private $config = [
- 'admin_score_add' => ["code" => "后台补加积分", "content" => "后台补加积分:{money}。"],
- 'admin_score_jdd' => ["code" => "后台补扣积分", "content" => "后台补扣积分:{money}。"],
- 'income_score' => ["code" => "商城下单赠送积分", "content" => "用户收入{money}积分"],
- 'outcome_score' => ["code" => "商城购买积分抵扣支出", "content" => "{mono},支出{money}积分"],
- ];
-
- /**
- * 后台充值积分余额
- * @param $uid
- * @param $money
- * @param $admin_id
- * @return bool
- * @throws \think\db\exception\DbException
- */
- public function adminAddMoney($uid, $money,$admin_id,$mono="")
- {
- try {
- self::beginTrans();
- $post['uid'] = $uid;
- $post['v'] = floatval($money);
- $post['code'] = 'admin_score_add';
- $post['title'] = $this->config['admin_score_add']['code'];
- $post['content'] = $this->TplParam($this->config['admin_score_add']['content'],compact('money'));
- $post['type'] = 1;
- $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('score') + floatval($money);
- $post['time'] = time();
- $post['admin_id'] = $admin_id;
- $post['othen'] = $mono;
- $this->insert($post);
- $bool = Db::name("user")->where('uid', $uid)->inc('score', floatval($money))->update();
- self::commitTrans();
- if ($bool > 0) {
- return true;
- } else {
- return false;
- }
- } catch (DbException $db) {
- self::rollbackTrans();
- return false;
- }
- }
- /**
- * 后台扣除积分余额
- * @param $uid
- * @param $money
- * @param $admin_id
- * @return bool
- * @throws \think\db\exception\DbException
- */
- public function adminCutMoney($uid, $money,$admin_id,$mono="")
- {
- try {
- self::beginTrans();
- $post['uid'] = $uid;
- $post['v'] = floatval($money);
- $post['code'] = 'admin_score_jdd';
- $post['title'] = $this->config['admin_score_jdd']['code'];
- $post['content'] = $this->TplParam($this->config['admin_score_jdd']['content'],compact('money'));
- $post['type'] = -1;
- $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('score') - floatval($money);
- $post['time'] = time();
- $post['admin_id'] = $admin_id;
- $post['othen'] = $mono;
- $this->insert($post);
- $bool = Db::name("user")->where('uid', $uid)->dec('score', floatval($money))->update();
- self::commitTrans();
- if ($bool > 0) {
- return true;
- } else {
- return false;
- }
- } catch (DbException $db) {
- self::rollbackTrans();
- return false;
- }
- }
- /**
- * 增加用户积分
- * @param $uid
- * @param $money
- * @param $orderSn
- * @return bool
- * @throws \think\db\exception\DbException
- */
- public function incomeScore($uid, $money, $orderSn="",$code="income_score",$parms=array())
- {
- try {
- $content = $this->TplParam($this->config[$code]['content'],compact('money'));
- $post['uid'] = $uid;
- $post['v'] = $money;
- $post['code'] = $code;
- $post['title'] = $this->config[$code]['code'];
- $post['content'] = $content;
- $post['type'] = 1;
- $post['time'] = time();
- $post['order_sn'] = $orderSn;
- $post['money'] = (float)(new User)->where('uid', $uid)->value('score') + $money;
- $post['o_id'] = empty($parms["o_id"])?0:$parms["o_id"];
- $this->insert($post);
- $bool = Db::name("user")->where('uid', $uid)->inc('score', floatval($money))->inc('score_in', floatval($money))->update();
- if ($bool > 0) {
- return true;
- } else {
- return false;
- }
- } catch (DbException $db) {
- return false;
- }
- }
-
- /**
- * 消耗积分
- * @param type $uid
- * @param type $money
- * @param type $mono
- */
- public function payScore($uid,$money,$code,$mono,$parms=array()){
- try {
- $content = $this->TplParam($this->config[$code]['content'],compact('money', 'mono'));
- $post['uid'] = $uid;
- $post['v'] = $money;
- $post['code'] = $code;
- $post['title'] = $this->config[$code]['code'];
- $post['content'] = $content;
- $post['type'] = -1;
- $post['time'] = time();
- $post['money'] = (float)(new User)->where('uid', $uid)->value('score') - $money;
- $post['o_id'] = empty($parms["o_id"])?0:$parms["o_id"];
- $this->insert($post);
- $bool = Db::name("user")->where('uid', $uid)->dec('score', floatval($money))->update();
- if ($bool > 0) {
- return true;
- } else {
- return false;
- }
- } catch (DbException $db) {
- return false;
- }
- }
- /**
- * 消耗积分,从冻结账户扣除
- * @param type $uid
- * @param type $money
- * @param type $mono
- */
- public function payFreezeScore($uid,$money,$code,$mono,$parms=[]){
- $content = $this->TplParam($this->config[$code]['content'],compact('money', 'mono'));
- $post['uid'] = $uid;
- $post['v'] = $money;
- $post['code'] = $code;
- $post['title'] = $this->config[$code]['code'];
- $post['content'] = $content;
- $post['type'] = -1;
- $post['time'] = time();
- $post['money'] = (float)(new User)->where('uid', $uid)->value('score');
- $this->insert($post);
- $bool = Db::name("user")->where('uid', $uid)->dec('score_freeze', floatval($money))->update();
- if ($bool > 0) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * 转义的模板信息
- */
- private function TplParam($content)
- {
- $data = func_get_args();
- foreach ($data[1] as $k => $v) {
- $content = str_replace('{' . $k . '}', $v, $content);
- }
- return $content;
- }
- }
|