package com.younike.server.controller;

import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.core.page.TableDataInfo;
import com.younike.common.enums.BusinessType;
import com.younike.common.utils.StringUtils;
import com.younike.common.utils.poi.ExcelUtil;
import com.younike.framework.config.AnonymousAccess;
import com.younike.system.domain.GoodsComment;
import com.younike.system.domain.vo.GoodsCommentVo;
import com.younike.system.service.IGoodsCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 商品评价Controller
 *
 * @author elev
 * @date 2022-01-05
 */
@Api("商品评价接口")
@RestController
@RequestMapping("/server/goods/comment")
public class ServerGoodsCommentController extends BaseController {
    @Autowired
    private IGoodsCommentService goodsCommentService;

    /**
     * 查询商品评价列表
     */
    @ApiOperation("查询商品评价列表")
    @PreAuthorize("@ss.hasPermi('system:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsComment goodsComment) {
        startPage();
        List<GoodsComment> list = goodsCommentService.selectGoodsCommentList(goodsComment);
        return getDataTable(list);
    }

    /**
     * 导出商品评价列表
     */
    @ApiOperation("导出商品评价列表")
    @PreAuthorize("@ss.hasPermi('system:comment:export')")
    @Log(title = "商品评价", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GoodsComment goodsComment) {
        List<GoodsComment> list = goodsCommentService.selectGoodsCommentList(goodsComment);
        ExcelUtil<GoodsComment> util = new ExcelUtil<GoodsComment>(GoodsComment.class);
        return util.exportExcel(list, "商品评价数据");
    }

    /**
     * 获取商品评价详细信息
     */
    @ApiOperation("获取商品评价详细信息")
    @PreAuthorize("@ss.hasPermi('system:comment:query')")
    @GetMapping(value = "/{commontId}")
    public AjaxResult getInfo(@PathVariable("commontId") Long commontId) {
        return AjaxResult.success(goodsCommentService.selectGoodsCommentByCommontId(commontId));
    }

    /**
     * 新增商品评价
     */
    @ApiOperation("新增商品评价")
    @PreAuthorize("@ss.hasPermi('system:comment:add')")
    @Log(title = "商品评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsComment goodsComment) {
        return toAjax(goodsCommentService.insertGoodsComment(goodsComment));
    }

    /**
     * 修改商品评价
     */
    @ApiOperation("修改商品评价")
    @PreAuthorize("@ss.hasPermi('system:comment:edit')")
    @Log(title = "商品评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsComment goodsComment) {
        return toAjax(goodsCommentService.updateGoodsComment(goodsComment));
    }

    /**
     * 查询我的商品评价列表
     */
    @ApiOperation("查询商品评价列表")
    @GetMapping("/commentList")
    @AnonymousAccess
    public TableDataInfo commentList(GoodsComment goodsComment) {
        startPage();
        List<GoodsCommentVo> list = goodsCommentService.getCommentlistByServerId(goodsComment);
        return getDataTable(list);
    }
}
