package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import com.gxwebsoft.shop.entity.UserComment;
import com.gxwebsoft.shop.param.UserCommentParam;
import com.gxwebsoft.shop.service.UserCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户评价记录表控制器
 *
 * @author 科技小王子
 * @since 2023-07-06 13:55:16
 */
@Api(tags = "用户评价记录表管理")
@RestController
@RequestMapping("/api/shop/user-comment")
public class UserCommentController extends BaseController {
    @Resource
    private UserCommentService userCommentService;
    @Resource
    private UserService userService;

    @OperationLog
    @ApiOperation("分页查询用户评价记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<UserComment>> page(UserCommentParam param) {
        User loginUser = getLoginUser();
        // 使用关联查询
        if(loginUser != null){
          param.setLoginUserId(getLoginUserId());
        }
        return success(userCommentService.pageRel(param));
    }

    @OperationLog
    @ApiOperation("查询全部用户评价记录表")
    @GetMapping()
    public ApiResult<List<UserComment>> list(UserCommentParam param) {
        // 使用关联查询
        return success(userCommentService.listRel(param));
    }

    @OperationLog
    @ApiOperation("根据id查询用户评价记录表")
    @GetMapping("/{id}")
    public ApiResult<UserComment> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(userCommentService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:userComment:save')")
    @OperationLog
    @ApiOperation("添加用户评价记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody UserComment userComment) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          userComment.setUserId(loginUser.getUserId());
        }
        if (userCommentService.save(userComment)) {
            // 追加评论数
          final User toUser = userService.getById(userComment.getToUserId());
          toUser.setCommentNumbers(toUser.getCommentNumbers() + 1);
          userService.updateById(toUser);
          return success("发表成功",toUser);
        }
        return fail("发表失败");
    }

    @PreAuthorize("hasAuthority('shop:userComment:update')")
    @OperationLog
    @ApiOperation("修改用户评价记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody UserComment userComment) {
        if (userCommentService.updateById(userComment)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:userComment:remove')")
    @OperationLog
    @ApiOperation("删除用户评价记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (userCommentService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:userComment:save')")
    @OperationLog
    @ApiOperation("批量添加用户评价记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<UserComment> list) {
        if (userCommentService.saveBatch(list)) {
            return success("发表成功");
        }
        return fail("发表失败");
    }

    @PreAuthorize("hasAuthority('shop:userComment:update')")
    @OperationLog
    @ApiOperation("批量修改用户评价记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<UserComment> batchParam) {
        if (batchParam.update(userCommentService, "comment_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:userComment:remove')")
    @OperationLog
    @ApiOperation("批量删除用户评价记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (userCommentService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
