package io.controller;


import io.annotation.Login;
import io.annotation.LoginUser;
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.entity.UserEntity;
import io.modules.item.dto.WallDTO;
import io.modules.item.service.DictService;
import io.modules.item.service.WallService;
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;


/**
 * 吐槽墙

 */
@RestController
@RequestMapping("/api/wall")
@CrossOrigin
@Tag(name="吐槽墙")
public class WallController {
    @Autowired
    private WallService wallService;

    @Autowired
    private DictService dictService;

    @GetMapping("list")
    @Operation(summary = "分页")
    public Result<List<WallDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        List<WallDTO> page = wallService.list(params);
        return new Result<List<WallDTO>>().ok(page);
    }

    @Login
    @PostMapping
    @Operation(summary = "保存")
    public Result save(@RequestBody WallDTO dto,@Parameter(hidden = true) @LoginUser UserEntity user){
        //效验数据
        dto.setUserId(user.getId());
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        List<String> contentExistsList = dictService.isExistsList(dto.getContent());
        if (!contentExistsList.isEmpty()){
            return new Result().error("内容出现敏感词："+ contentExistsList);
        }
        wallService.save(dto);
        return new Result();
    }


}
