package com.backendsys.modules.system.controller;

import com.backendsys.exception.CustException;
import com.backendsys.modules.common.aspect.SysLog;
import com.backendsys.modules.common.config.security.enums.SecurityEnum;
import com.backendsys.modules.common.config.security.utils.SecurityUtil;
import com.backendsys.modules.common.utils.Result;
import com.backendsys.modules.system.dao.SysUserDao;
import com.backendsys.modules.system.entity.SysUser;
import com.backendsys.modules.system.entity.SysUserIntegral;
import com.backendsys.modules.system.entity.SysUserIntegralLog;
import com.backendsys.modules.system.service.SysUserIntegralLogService;
import com.backendsys.modules.system.service.SysUserIntegralService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Validated
@RestController
@Tag(name = "系统用户积分")
public class SysUserIntegralController {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserIntegralService sysUserIntegralService;
    @Autowired
    private SysUserIntegralLogService sysUserIntegralLogService;

    @SysLog("增加系统用户积分")
    @Operation(summary = "增加系统用户积分")
    @PreAuthorize("@sr.hasPermission('3.3.1')")
    @PutMapping("/api/system/user/increaseUserIntegral")
    public Result increaseUserIntegral(@Validated(SysUserIntegral.Increase.class) @RequestBody SysUserIntegral sysUserIntegral) {
        Long user_id = sysUserIntegral.getUser_id();
        Integer integral = sysUserIntegral.getIntegral();
        return Result.success().put("data", sysUserIntegralService.increase(user_id, integral));
    }

    @SysLog("减少系统用户积分")
    @Operation(summary = "减少系统用户积分")
    @PreAuthorize("@sr.hasPermission('3.3.2')")
    @PutMapping("/api/system/user/decreaseUserIntegral")
    public Result decreaseUserIntegral(@Validated(SysUserIntegral.Decrease.class) @RequestBody SysUserIntegral sysUserIntegral) {
        Long user_id = sysUserIntegral.getUser_id();
        Integer integral = sysUserIntegral.getIntegral();
        return Result.success().put("data", sysUserIntegralService.decrease(user_id, integral));
    }

    @Operation(summary = "获取系统用户积分详情")
    @PreAuthorize("@sr.hasPermission('3.3.3')")
    @GetMapping("/api/system/user/getUserIntegral")
    public Result getUserIntegral(Long user_id) {
        if (user_id == null) user_id = SecurityUtil.getUserId();

        SysUser sysUser = sysUserDao.selectById(user_id);
        if (sysUser == null) throw new CustException("用户不存在");

        // 获取用户积分
        Integer integral = sysUserIntegralService.selectIntegralByUserId(user_id);

        Map<String, Object> resp = new LinkedHashMap<>();
        resp.put("user_id", user_id);
        resp.put("integral", integral);
        return Result.success().put("data", resp);
    }

    @Operation(summary = "获取系统用户积分日志列表")
    @PreAuthorize("@sr.hasPermission('3.3.4')")
    @GetMapping("/api/system/user/getUserIntegralLogList")
    public Result getUserIntegralLogList(SysUserIntegralLog sysUserIntegralLog) {
        return Result.success().put("data", sysUserIntegralLogService.selectIntegralLogList(sysUserIntegralLog));
    }

}
