//package com.backendsys.service.System;
//
//import com.backendsys.entity.System.SysUserPointsDTO;
//import com.backendsys.exception.CustException;
//import com.backendsys.mapper.System.SysUserMapper;
//import com.backendsys.mapper.System.SysUserPointsHistoryMapper;
//import com.backendsys.utils.response.PageInfoResult;
//import com.github.pagehelper.PageHelper;
//import org.redisson.api.RLock;
//import org.redisson.api.RedissonClient;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.stereotype.Service;
//
//import java.util.LinkedHashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//
//@Service
//public class SysUserPointHistoryServiceImpl implements SysUserPointHistoryService {
//
//    @Lazy
//    @Autowired
//    RedissonClient redissonClient;
//
//    @Autowired
//    private SysUserMapper sysUserMapper;
//
//    @Autowired
//    private SysUserPointsHistoryMapper sysUserPointsHistoryMapper;
//
//    /**
//     * 操作用户积分 (增加/减少) (其他操作扣除积分可使用此业务逻辑)
//     * SysUserPointsDTO sysUserPointsDTO = new SysUserPointsDTO();
//     * ..
//     * sysUserService.adjustmentUserPoint(sysUserPointsDTO);
//     */
//    public Map<String, Object> adjustmentUserPoint(SysUserPointsDTO sysUserPointsDTO) {
//        RLock lock = redissonClient.getLock("adjustmentPoint");
//        try {
//            lock.tryLock(3, TimeUnit.SECONDS);
//
//            // [查询] 用户当前信息
//            Map<String, Object> userDetail = sysUserMapper.queryUserDetail(sysUserPointsDTO.getTarget_user_id());
//            if (userDetail == null) {
//                throw new CustException("用户不存在");
//            }
//            if (!userDetail.get("audit_status").equals(2)) {
//                throw new CustException("用户未通过审核");
//            }
//            if (!userDetail.get("status").equals(1)) {
//                throw new CustException("用户未激活");
//            }
//
//            // -- 积分计算 (增加/减少) -------------------------------------------------
//            // 当前积分余额
//            Float point_balance = (Float) userDetail.get("point_balance");
//            // 积分变动额 (绝对值) (此处为增加积分)
//            Float point_adjustment = sysUserPointsDTO.getPoint_adjustment();
//            // 计算结果
//            Float point_blance_total = point_balance + point_adjustment;
//
//
//            if (point_blance_total < 0) {
//                // 判断：如果操作后积分小于0，则目标值等于0 (弃用)
//                // point_blance_total = (float) 0;
//
//                // 判断：如果操作后积分小于0，则抛出异常
//                throw new CustException("积分不足");
//            }
//
//            // 入参格式化
//            SysUserPointsDTO dto = sysUserPointsDTO;
//            dto.setPoint_balance(point_blance_total);
//            // ----------------------------------------------------------------------
//
//            // [编辑] 用户积分
//            sysUserMapper.updateUserPoint(dto);
//            Map<String, Object> resp = new LinkedHashMap<>();
//            resp.put("user_id", dto.getTarget_user_id());
//            resp.put("point_balance", point_balance);
//            resp.put("point_adjustment", point_adjustment);
//
//            return resp;
//
//        } catch (InterruptedException e) { throw new RuntimeException(e);
//        } finally { lock.unlock(); }
//    }
//
////    /**
////     * 创建 [操作用户积分] 的操作记录
////     */
////    public Map<String, Object> insertUserPointHistory(SysUserPointsDTO sysUserPointsDTO) {
////        RLock lock = redissonClient.getLock("insertUserPointHistory");
////        try { lock.tryLock(3, TimeUnit.SECONDS);
////
////            System.out.println(sysUserPointsDTO);
////            sysUserPointsHistoryMapper.insertUserPointsHistory(sysUserPointsDTO);
////
////            return Map.of("target_user_id", sysUserPointsDTO.getTarget_user_id());
////        } catch (InterruptedException e) { throw new RuntimeException(e);
////        } finally { lock.unlock(); }
////    }
//
//    /**
//     * 查询单个用户积分 (积分余额、消耗积分、充值积分）
//     */
//    public Map<String, Object> queryUserPoint(SysUserPointsDTO sysUserPointsDTO) {
//
//        System.out.println(sysUserPointsDTO);
//
//        // 获得余额
//        Map<String, Object> respUser = sysUserMapper.queryUserPoint(sysUserPointsDTO);
//        Float point_balance = (Float) respUser.get("point_balance");
//
//        // 获得消耗的积分、购买/获赠的积分
//        List<Map<String, Object>> pointsHistory = sysUserPointsHistoryMapper.queryUserPointsHistoryList(sysUserPointsDTO);
//        double point_receive = 0;
//        double point_used = 0;
//        if (pointsHistory.size() > 0) {
//            // 计算正数的总和 (购买、获赠）
//            point_receive = pointsHistory.stream()
//                    .filter(map -> (Float) map.get("point_adjustment") > 0)
//                    .mapToDouble(map -> (Float) map.get("point_adjustment"))
//                    .sum();
//            // 计算负数的总和 (消耗)
//            point_used = pointsHistory.stream()
//                    .filter(map -> (Float) map.get("point_adjustment") < 0)
//                    .mapToDouble(map -> (Float) map.get("point_adjustment") * -1) // 将负数转换为正数进行求和
//                    .sum();
//        }
//
//        Map<String, Object> result = new LinkedHashMap<>();
//        result.put("point_balance", point_balance);
//        result.put("point_receive", point_receive);
//        result.put("point_used", point_used > 0 ? -(point_used) : 0);
//
//        return result;
//    }
//
//    /**
//     * 查询用户积分操作记录列表
//     */
//    public Map<String, Object> queryUserPointHistoryList(Integer pageNum, Integer pageSize, SysUserPointsDTO sysUserPointsDTO) {
//        // 分页查询
//        if (pageNum != null && pageSize != null) {
//            PageHelper.startPage(pageNum, pageSize);
//        }
//        // 分页输出 (自定义)
//        List<Map<String, Object>> list = sysUserPointsHistoryMapper.queryUserPointsHistoryList(sysUserPointsDTO);
//        PageInfoResult pageInfoResult = new PageInfoResult(list);
//        return pageInfoResult.toMap();
//    }
//
//}
