package io.modules.sys.controller;


import io.common.annotation.LogOperation;
import io.common.constant.Constant;
import io.common.page.PageData;
import io.common.utils.Result;
import io.common.validator.AssertUtils;
import io.modules.item.dto.CommentDTO;
import io.modules.item.dto.HouseDTO;
import io.modules.item.dto.UserDTO;
import io.modules.item.service.CommentService;
import io.modules.item.service.HouseService;
import io.modules.item.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
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.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 评论表
 */
@RestController
@RequestMapping("/sys/comment")
@Tag(name="评论表")
@CrossOrigin
public class CommentController {

    @Autowired
    private  CommentService commentService;
    @Autowired
    private  UserService userService;
    @Autowired
    private HouseService itemService;

    @Value("${upload.url}")
    private String uploadUrl;

    @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"),
        @Parameter(name = "itemId", description = "关联编号", in = ParameterIn.QUERY, ref="String")
    })
    public Result<PageData<CommentDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        PageData<CommentDTO> page = commentService.page(params);
        List<CommentDTO> updatedPage = page.getList().stream()
                .map(comment -> {
                    Long userId = comment.getUserId();
                    // 如果缓存中已经存在该用户的信息，直接使用
                    UserDTO user = userService.get(userId);
                    if (user != null) {
                        // 否则调用 userService 获取用户信息并缓存
                        comment.setNickName(user.getNickName());  // 设置昵称
                    }
                    Long itemId = comment.getItemId();
                    HouseDTO itemDTO = itemService.get(itemId);
                   if (itemDTO != null){
                       itemDTO.setImage(uploadUrl + itemDTO.getImage());;
                   }
                    comment.setItemDTO(itemDTO);
                    return comment;
                })
                .collect(Collectors.toList());
        page.setList(updatedPage);
        return new Result<PageData<CommentDTO>>().ok(page);
    }



    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @LogOperation("删除")
    public Result delete(@PathVariable Long id){
        Long[] ids = new Long[] { id };
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        commentService.delete(ids);
        return new Result();
    }

}
