// +---------------------------------------------------------------------- namespace app\common\repositories\user; use app\common\dao\user\UserAssetsDao; use app\common\repositories\BaseRepository; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; /** * @mixin UserAssetsDao */ class UserAssetsRepository extends BaseRepository { public function __construct(UserAssetsDao $dao) { $this->dao = $dao; } /** * notes 获取用户资产 * @param $uid * @return array * @create 2024/3/18 14:48 * @update 2024/3/18 14:48 * @author zhangkxiang * @editor */ public function assets($uid) { $item = $this->dao->get($uid); if ($item) { return $item->toArray(); } return array( 'uid' => 0, 'consume' => 0.00, 'consume_frozen' => 0.00, 'share_point' => 0.00, 'welfare' => 0.00, 'welfare_frozen' => 0.00, 'huitong' => 0.00, 'huitong_frozen' => 0.00, 'contribution' => 0.00, 'contribution_frozen' => 0.00, ); } /** * notes 订单状态发生变更,影响用户资产 * @param $uid * @param $status * @param $data * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @create 2024/3/15 17:23 * @update 2024/3/15 17:23 * @author zhangkxiang * @editor */ public function orderEvent($uid, $status, $data = array()) { $assets = array(); $info = $this->dao->get($uid); $data['consume'] = $data['consume'] ?? 0; $data['welfare'] = $data['welfare'] ?? 0; $data['huitong'] = $data['huitong'] ?? 0; $data['contribution'] = $data['contribution'] ?? 0; if ($info) { // 购买订单获得冻结资产 if ($status == UserAssetsLogRepository::STATUS_FROZEN) { $assets = array( 'consume_frozen' => $info['consume_frozen'] + $data['consume'], 'welfare_frozen' => $info['welfare_frozen'] + $data['welfare'], 'huitong_frozen' => $info['huitong_frozen'] + $data['huitong'], 'contribution_frozen' => $info['contribution_frozen'] + $data['contribution'], ); } elseif ($status == UserAssetsLogRepository::STATUS_SUCCESS) { $assets = array( 'consume' => $info['consume'] + $data['consume'], 'welfare' => $info['welfare'] + $data['welfare'], 'huitong' => $info['huitong'] + $data['huitong'], 'contribution' => $info['contribution'] + $data['contribution'], 'consume_frozen' => $info['consume_frozen'] - $data['consume'], 'welfare_frozen' => $info['welfare_frozen'] - $data['welfare'], 'huitong_frozen' => $info['huitong_frozen'] - $data['huitong'], 'contribution_frozen' => $info['contribution_frozen'] - $data['contribution'], ); } elseif ($status == UserAssetsLogRepository::STATUS_REFUND) { $assets = array( 'consume_frozen' => $info['consume_frozen'] - $data['consume'], 'welfare_frozen' => $info['welfare_frozen'] - $data['welfare'], 'huitong_frozen' => $info['huitong_frozen'] - $data['huitong'], 'contribution_frozen' => $info['contribution_frozen'] - $data['contribution'], ); } elseif ($status == UserAssetsLogRepository::STATUS_USED) { $assets = array( 'welfare' => $info['welfare'] - $data['welfare'], ); } $this->dao->update($uid, $assets); } else { $assets = array( 'uid' => $uid, 'consume_frozen' => $data['consume'] ?? 0, 'welfare_frozen' => $data['welfare'] ?? 0, 'huitong_frozen' => $data['huitong'] ?? 0, 'contribution_frozen' => $data['contribution'] ?? 0, ); $this->dao->create($assets); } } }