package com.fzm.mall.server.admin.goods_center.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.goods_center.entity.dto.CommentReplyDTO;
import com.fzm.mall.server.admin.goods_center.entity.dto.GoodsCheckDTO;
import com.fzm.mall.server.admin.goods_center.entity.vo.*;
import com.fzm.mall.server.admin.goods_center.service.IGoodsCommentAppendService;
import com.fzm.mall.server.admin.goods_center.service.IGoodsCommentService;
import com.fzm.mall.server.admin.home_page.enums.type.AdminTypeEnum;
import com.fzm.mall.server.admin.home_page.model.Admin;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wangp
 * @date 2021/5/12 18:14
 * @description 商品评论
 * @since JDK 1.8
 */
@Api(tags = "商品中心-商品评论")
@RestController
@RequestMapping("/goodsComment")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GoodsCommentController {

    private final IAdminService adminService;
    private final ResponseFactory resFac;
    private final IGoodsCommentService goodsCommentService;
    private final IGoodsCommentAppendService goodsCommentAppendService;

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public ResponseVO page(
            @ApiParam(name = "goodsId", value = "商品编号")
            @RequestParam(required = false) String goodsId,
            @ApiParam(name = "goodsName", value = "商品名称")
            @RequestParam(required = false) String goodsName,
            @ApiParam(name = "evaluateType", value = "评价类型")
            @RequestParam(required = false) Integer evaluateType,
            @ApiParam(name = "replyStatus", value = "回复状态")
            @RequestParam(required = false) Integer replyStatus,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getMerchantTypeArr());
        Admin admin = adminService.getByAdminId(adminId);
        IPage<GoodsCommentVO> page = goodsCommentService.page(admin.getMerchantId(), goodsId, goodsName, evaluateType,
                replyStatus, pageNum, pageSize);
        return resFac.getSuccessResponseWithData(page);
    }

    @ApiOperation(value = "评价详情")
    @GetMapping("/commentDetail")
    public ResponseVO commentDetail(@ApiParam(name = "commentId", value = "评论id") @RequestParam String commentId, @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getMerchantTypeArr());
        GoodsCommentDetailVO vo = goodsCommentService.queryCommentDetailByCommentId(commentId);
        return resFac.getSuccessResponseWithData(vo);
    }

    @ApiOperation(value = "回复")
    @PutMapping("/reply")
    public ResponseVO reply(@RequestBody @Valid CommentReplyDTO dto, @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getMerchantTypeArr());
        goodsCommentService.reply(dto);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{commentId}")
    public ResponseVO delete(@PathVariable String commentId, @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getMerchantTypeArr());
        goodsCommentService.delete(commentId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "追评详情")
    @GetMapping("/appendCommentDetail")
    public ResponseVO appendCommentDetail(@ApiParam(name = "commentId", value = "评论id") @RequestParam String commentId, @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getMerchantTypeArr());
        GoodsCommentAppendDetailVO vo = goodsCommentAppendService.queryAppendCommentDetailByCommentId(commentId);
        return resFac.getSuccessResponseWithData(vo);
    }

    @ApiOperation(value = "追评回复")
    @PutMapping("/appendReply")
    public ResponseVO appendReply(@RequestBody @Valid CommentReplyDTO dto, @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getMerchantTypeArr());
        goodsCommentAppendService.appendReply(dto);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "追评删除")
    @DeleteMapping("/appendDelete/{commentId}")
    public ResponseVO appendDelete(@PathVariable String commentId, @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getMerchantTypeArr());
        goodsCommentAppendService.appendDelete(commentId);
        return resFac.getSimpleSuccessResponse();
    }

}
