package com.backendsys.modules.system.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
import com.backendsys.modules.common.config.security.utils.SecurityUtil;
import com.backendsys.modules.system.dao.SysUserIntegralDao;
import com.backendsys.modules.system.dao.SysUserIntegralLogDao;
import com.backendsys.modules.system.entity.SysUserIntegral;
import com.backendsys.modules.system.entity.SysUserIntegralLog;
import com.backendsys.modules.system.service.SysUserIntegralService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

@Service
public class SysUserIntegralServiceImpl implements SysUserIntegralService {

    @Autowired
    private HttpRequestUtil httpRequestUtil;

    @Autowired
    private SysUserIntegralDao sysUserIntegralDao;
    @Autowired
    private SysUserIntegralLogDao sysUserIntegralLogDao;

    /**
     * 初始化用户积分
     */
    @Override
    public void init(Long user_id) {
        SysUserIntegral entity = new SysUserIntegral();
        entity.setUser_id(user_id);
        sysUserIntegralDao.insert(entity);
    }

    /**
     * 查询积分 (如果不存在记录，则创建)
     */
    @Override
    public Integer selectIntegralByUserId(Long user_id) {
        LambdaQueryWrapper<SysUserIntegral> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserIntegral::getUser_id, user_id);
        SysUserIntegral detail = sysUserIntegralDao.selectOne(wrapper);
        if (detail == null) {
            // 如果不存在，则创建记录
            SysUserIntegral entity = new SysUserIntegral();
            entity.setUser_id(user_id);
            sysUserIntegralDao.insert(entity);
            return 0;
        } else {
            return detail.getIntegral();
        }
    }

    /**
     * 增加积分
     */
    @Override
    public Map<String, Object> increase(Long user_id, Integer integral) {

        // 查询当前积分 (如果不存在记录，则创建)
        Integer current_integral = selectIntegralByUserId(user_id);
        // 计算结果积分 (当前 + 增量)
        int target_integral = NumberUtil.add(current_integral, integral).intValue();

        // 更新用户积分
        SysUserIntegral entity = new SysUserIntegral();
        entity.setIntegral(target_integral);
        sysUserIntegralDao.update(entity, new LambdaQueryWrapper<SysUserIntegral>().eq(SysUserIntegral::getUser_id, user_id));

        // 创建用户积分日志
        SysUserIntegralLog entityLog = new SysUserIntegralLog();
        entityLog.setUser_id(user_id);
        entityLog.setContent("手动增加积分");
        entityLog.setIntegral(integral);
        entityLog.setOperation(1);                                  // 操作 (1:增加, -1:减少)
        entityLog.setOperation_user_id(SecurityUtil.getUserId());   // 操作人 (手动操作必填)
        entityLog.setOrigin(1);                                     // 来源 (1:手动操作, 2:..)
        entityLog.setOperation_ip(httpRequestUtil.getIpAddr());               // IP
        sysUserIntegralLogDao.insert(entityLog);

        return Map.of("user_id", user_id);
    }

    /**
     * 减少积分
     */
    @Override
    public Map<String, Object> decrease(Long user_id, Integer integral) {

        // 查询当前积分 (如果不存在记录，则创建)
        Integer current_integral = selectIntegralByUserId(user_id);
        // 计算结果积分 (当前 - 增量)
        int target_integral = NumberUtil.sub(current_integral, integral).intValue();

        // 更新用户积分
        SysUserIntegral entity = new SysUserIntegral();
        entity.setIntegral(target_integral);
        sysUserIntegralDao.update(entity, new LambdaQueryWrapper<SysUserIntegral>().eq(SysUserIntegral::getUser_id, user_id));

        // 创建用户积分日志
        SysUserIntegralLog entityLog = new SysUserIntegralLog();
        entityLog.setUser_id(user_id);
        entityLog.setContent("手动减少积分");
        entityLog.setIntegral(integral);
        entityLog.setOperation(-1);                                 // 操作 (1:增加, -1:减少)
        entityLog.setOperation_user_id(SecurityUtil.getUserId());   // 操作人 (手动操作必填)
        entityLog.setOrigin(1);                                     // 来源 (1:手动操作, 2:..)
        entityLog.setOperation_ip(httpRequestUtil.getIpAddr());     // IP
        sysUserIntegralLogDao.insert(entityLog);

        return Map.of("user_id", user_id);
    }
}
