package io.controller;


import io.annotation.Login;
import io.common.constant.Constant;
import io.common.page.PageData;
import io.common.utils.Result;
import io.common.validator.AssertUtils;
import io.common.validator.ValidatorUtils;
import io.common.validator.group.AddGroup;
import io.common.validator.group.DefaultGroup;
import io.common.validator.group.UpdateGroup;
import io.modules.item.dto.UserBehaviorDTO;
import io.modules.item.service.UserBehaviorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.enums.ParameterIn;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 用户行为表
 *
 * @author Mark #
 * @since 1.0.0 2024-12-16
 */
@RestController
@RequestMapping("/api/behavior")
@CrossOrigin
@Tag(name="用户行为表")
public class UserBehaviorController {
    @Autowired
    private UserBehaviorService userBehaviorService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
        @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref="int") ,
        @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY,required = true, ref="int") ,
        @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref="String") ,
        @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref="String")
    })
    public Result<List<UserBehaviorDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        List<UserBehaviorDTO> page = userBehaviorService.list(params);
        return new Result<List<UserBehaviorDTO>>().ok(page);
    }

    @Login
    @PostMapping("exists")
    @Operation(summary = "查看是否收藏")
    public Result<Boolean> get(@RequestBody UserBehaviorDTO dto,@Parameter(hidden = true) @RequestAttribute("userId") Long userId){
        dto.setUserId(userId);
        return new Result<Boolean>().ok(userBehaviorService.isExists(dto));
    }
    @Login
    @PostMapping
    @Operation(summary = "添加")
    public Result save(@RequestBody UserBehaviorDTO dto,@Parameter(hidden = true) @RequestAttribute("userId") Long userId){
        //效验数据
        dto.setUserId(userId);
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        userBehaviorService.save(dto);
        return new Result();
    }

    @Login
    @PostMapping("delete")
    @Operation(summary = "删除")
    public Result delete(@RequestBody UserBehaviorDTO dto,@Parameter(hidden = true) @RequestAttribute("userId") Long userId){
        dto.setUserId(userId);
        //效验数据
        userBehaviorService.deleteUserBehavior(dto);
        return new Result();
    }

}
