// +---------------------------------------------------------------------- namespace app\common\repositories\user; use app\common\dao\user\UserAssetsDao; use app\common\repositories\BaseRepository; use app\common\repositories\store\order\StoreOrderBaseRepository; use app\common\repositories\system\groupData\GroupDataRepository; use app\common\repositories\system\HuitongRepository; 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; } public function getConfig() { // 配置信息 /** * @var GroupDataRepository $groupDataRepository */ $groupDataRepository = app()->make(GroupDataRepository::class); $config = $groupDataRepository->getGroupDataLst(0, 100, 1, 100); return array_column($config['list'], "value", 'key'); } /** * 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); } } /** * notes 分红福利积分定时任务 * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @create 2024/3/19 21:03 * @update 2024/3/19 21:03 * @author zhangkxiang * @editor */ public function sharePointTask() { $config = $this->getConfig(); /** * @var StoreOrderBaseRepository $orderBase */ $orderBase = app()->make(StoreOrderBaseRepository::class); $count = $orderBase->getTotalBase(date('Y-m-d', strtotime('-1day'))); if (empty($count)) { return; } $shareData = $this->dao->getShareData(); if (!$shareData['total']) { return; } $base = round($config['share_award'] * $count / 100, 2); if ($base < 0.01) { $base = 0.01; } $logList = array(); foreach ($shareData['list'] as $item) { $welfare = $base / $shareData['total'] * $item['share_point']; if ($welfare < 0.01) { $welfare = 0.01; } $logList[] = array( 'uid' => $item['uid'], 'asset_type' => UserAssetsLogRepository::ASSET_TYPE_WELFARE, 'type' => UserAssetsLogRepository::CHANGE_TYPE_SHARE_GET, 'status' => UserAssetsLogRepository::STATUS_SUCCESS, 'count' => $welfare, ); // 增加福利积分 $assets = $this->assets($item['uid']); $this->update($item['uid'], array('welfare' => $assets['welfare'] + $welfare)); } // 增加记录 $logRepository = app()->make(UserAssetsLogRepository::class); $logRepository->addLog($logList); } /** * notes * @param $uid * @param $count * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @create 2024/3/19 22:33 * @update 2024/3/19 22:33 * @author zhangkxiang * @editor */ public function consumeToShare($uid, $count){ $config = $this->getConfig(); $assets = $this->assets($uid); if($assets['consume'] < $config['consume_to_share'] * $count){ throw new \Exception('消费积分不足'); } $logList = array(); $consume = -1 * $config['consume_to_share'] * $count; $logList[] = array( 'uid' => $uid, 'asset_type' => UserAssetsLogRepository::ASSET_TYPE_CONSUME, 'type' => UserAssetsLogRepository::CHANGE_TYPE_SHARE_EXCHANGE, 'status' => UserAssetsLogRepository::STATUS_SUCCESS, 'count' => $consume, ); $logList[] = array( 'uid' => $uid, 'asset_type' => UserAssetsLogRepository::ASSET_TYPE_SHARE_POINT, 'type' => UserAssetsLogRepository::CHANGE_TYPE_SHARE_EXCHANGE, 'status' => UserAssetsLogRepository::STATUS_SUCCESS, 'count' => $count, ); app()->make(UserAssetsLogRepository::class)->addLog($logList); $this->dao->update($uid, array('consume' => $assets['consume'] + $consume, 'share_point' => $assets['share_point'] + $count, 'share_point_time' => time())); } /** * notes 惠通宝转账 * @param $uid * @param $phone * @param $count * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @create 2024/3/20 11:01 * @update 2024/3/20 11:01 * @author zhangkxiang * @editor */ public function sendHuitong($uid, $phone, $count){ $assets = $this->assets($uid); if($assets['huitong'] < $count){ throw new \Exception('惠通宝不足'); } /** * @var UserRepository $userRepository */ $userRepository = app()->make(UserRepository::class); $toUser = $userRepository->getUserByPhone($phone); if(!$toUser){ throw new \Exception('用户不存在'); } $logList[] = array( 'uid' => $toUser['uid'], 'asset_type' => UserAssetsLogRepository::ASSET_TYPE_HUITONG, 'type' => UserAssetsLogRepository::CHANGE_TYPE_HUITONG_GET, 'status' => UserAssetsLogRepository::STATUS_SUCCESS, 'count' => $count, ); $logList[] = array( 'uid' => $uid, 'asset_type' => UserAssetsLogRepository::ASSET_TYPE_HUITONG, 'type' => UserAssetsLogRepository::CHANGE_TYPE_HUITONG_SEND, 'status' => UserAssetsLogRepository::STATUS_SUCCESS, 'count' => -1 * $count, ); app()->make(UserAssetsLogRepository::class)->addLog($logList); $this->dao->update($toUser['uid'], array('huitong' => $assets['huitong'] - $count)); $toAssets = $this->assets($toUser['uid']); $this->dao->update($toUser['uid'], array('huitong' => $toAssets['huitong'] + $count)); } /** * notes * @param $uid * @param $count * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @create 2024/3/19 22:33 * @update 2024/3/19 22:33 * @author zhangkxiang * @editor */ public function huitongToConsume($uid, $count){ $current = app(HuitongRepository::class)->getCurrent(); $assets = $this->assets($uid); if($assets['huitong'] < $count){ throw new \Exception('惠通宝不足'); } $logList = array(); $consume = $current * $count; $logList[] = array( 'uid' => $uid, 'asset_type' => UserAssetsLogRepository::ASSET_TYPE_CONSUME, 'type' => UserAssetsLogRepository::CHANGE_TYPE_HUITONG, 'status' => UserAssetsLogRepository::STATUS_SUCCESS, 'count' => $consume, ); $huitong = -1 * $count; $logList[] = array( 'uid' => $uid, 'asset_type' => UserAssetsLogRepository::ASSET_TYPE_HUITONG, 'type' => UserAssetsLogRepository::CHANGE_TYPE_HUITONG, 'status' => UserAssetsLogRepository::STATUS_SUCCESS, 'count' => $huitong, ); app()->make(UserAssetsLogRepository::class)->addLog($logList); $this->dao->update($uid, array('consume' => $assets['consume'] + $consume, 'huitong' => $assets['huitong'] + $huitong)); } /** * notes * @param $uid * @param $consume * @create 2024/3/20 16:36 * @update 2024/3/20 16:36 * @author zhangkxiang * @editor */ public function sign($uid, $consume){ $logList = array(); $logList[] = array( 'uid' => $uid, 'asset_type' => UserAssetsLogRepository::ASSET_TYPE_CONSUME, 'type' => UserAssetsLogRepository::CHANGE_TYPE_SIGN, 'status' => UserAssetsLogRepository::STATUS_SUCCESS, 'count' => $consume, ); app()->make(UserAssetsLogRepository::class)->addLog($logList); $assets = $this->assets($uid); $this->dao->update($uid, array('consume' => $assets['consume'] + $consume)); } }