package com.younike.member;

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.param.GoodsCommentParam;
import com.younike.system.service.IGoodsCommentDimensionService;
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("/member/comment")
public class MemberGoodsCommentController extends BaseController {
    @Autowired
    private IGoodsCommentService goodsCommentService;

    @Autowired
    private IGoodsCommentDimensionService goodsCommentDimensionService;


    /**
     * 查询商品评价列表
     */
    @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("删除商品评价")
    @PreAuthorize("@ss.hasPermi('system:comment:remove')")
    @Log(title = "商品评价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commontIds}")
    public AjaxResult remove(@PathVariable Long[] commontIds) {
        return toAjax(goodsCommentService.deleteGoodsCommentByCommontIds(commontIds));
    }


    /**
     * 查询我的商品评价列表
     */
    @ApiOperation("查询我的评价列表")
//    @PreAuthorize("@ss.hasPermi('system:comment:list')")
    @GetMapping("/commentlist")
    @AnonymousAccess
    public Object commentlist(@RequestParam("memberId") Long memberId) {
        startPage();
        if(memberId==null){
            return AjaxResult.error("参数错误");
        }
        List<GoodsCommentVo> list = goodsCommentService.getCommentlist(memberId);
        return getDataTable(list);
    }



    /**
     * 添加追评
     */
    @ApiOperation("添加追评")
    @PutMapping("/backhanderComment")
    @AnonymousAccess
    public AjaxResult backhanderComment(@RequestBody GoodsComment goodsComment) {

        //参数验证
        if(ObjectUtils.isEmpty(goodsComment)){
            return AjaxResult.error("参数错误");
        }
        //验证评价id是否为空
        if(goodsComment.getCommontId()==null){
            return AjaxResult.error("参数错误(评价id不能为空)");
        }
        //验证追评内容是否为空
        if(StringUtils.isNull(goodsComment.getReContent())){
            return AjaxResult.error("参数错误(追评内容不能为空)");
        }
        //追评时间
        goodsComment.setReContentTime(LocalDateTime.now());
        goodsComment.setUpdateTime(LocalDateTime.now());
        return toAjax(goodsCommentService.updateGoodsComment(goodsComment));
    }


    /**
     * 删除商品追评价
     */
    @ApiOperation("删除商品追评价")
    @DeleteMapping("/traceRemove/{commontId}")
    @AnonymousAccess
    public AjaxResult traceRemove(@PathVariable("commontId") Long commontId){
        //验证追评内容是否为空
        if(!StringUtils.isNotNull(commontId)){
            return AjaxResult.error("参数错误(商品评价id不能为空)");
        }
        return toAjax(goodsCommentService.traceRemove(commontId));
    }

    /**
     * 删除商品评价
     */
    @ApiOperation("删除商品评价")
    @DeleteMapping("/commentRemove/{commontId}")
    @AnonymousAccess
    public AjaxResult commentRemove(@PathVariable("commontId") Long commontId){
        //验证追评内容是否为空
        if(!StringUtils.isNotNull(commontId)){
            return AjaxResult.error("参数错误(商品评价id不能为空)");
        }
        return toAjax(goodsCommentService.commentRemove(commontId));
    }

    /**
     * 查询商品评价维度信息
     */
    @ApiOperation("查询商品评价维度信息")
    @AnonymousAccess
//    @PreAuthorize("@ss.hasPermi('member:comment:commentDimension')")
    @GetMapping(value = "/getCommentDimensionInfo")
    public AjaxResult getCommentDimensionInfo(@RequestParam("categoryId") Long categoryId, @RequestParam("goodsId") Long goodsId) {
        if (categoryId == null || goodsId == null) {
            return AjaxResult.error("参数错误(商品分类id和商品id都不能为空)");
        }
        return AjaxResult.success(goodsCommentDimensionService.getCommentDimensionInfo(categoryId, goodsId));
    }

    /**
     * 会员订单评价
     */
    @ApiOperation("会员订单评价")
    @AnonymousAccess
//    @PreAuthorize("@ss.hasPermi('member:comment:goodsComment')")
    @PostMapping(value = "/saveMemberGoodsComment")
    public AjaxResult saveMemberGoodsComment(@RequestBody GoodsCommentParam goodsCommentParam) {
        if (goodsCommentParam == null) {
            return AjaxResult.error("参数错误");
        }
        return toAjax(goodsCommentService.saveMemberGoodsComment(goodsCommentParam));
    }

    /**
     * 查询订单评价详情
     */
    @ApiOperation("查询订单评价详情")
    @AnonymousAccess
//    @PreAuthorize("@ss.hasPermi('member:comment:detail')")
    @GetMapping(value = "/getDetail/{commontId}")
    public AjaxResult getMemberGoodsCommentDetail(@PathVariable("commontId") Long commontId) {
        return AjaxResult.success(goodsCommentService.getMemberGoodsCommentDetail(commontId));
    }

}
