
package com.tmerclub.cloud.product.controller.app;

import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.product.dto.SpuCommDTO;
import com.tmerclub.cloud.product.service.SpuCommService;
import com.tmerclub.cloud.product.vo.SpuCommStatisticsStarVO;
import com.tmerclub.cloud.product.vo.SpuCommStatisticsVO;
import com.tmerclub.cloud.product.vo.SpuCommVO;
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.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品评论
 *
 * @author YXF
 * @date 2021-01-11 13:47:33
 */
@RestController("appSpuCommController")
@RequestMapping("/spu_comm")
@Tag(name = "商品评论")
public class SpuCommController {

    @Autowired
    private SpuCommService spuCommService;

    @GetMapping("/ua/page")
    @Operation(summary = "获取商品评论列表(商品)", description = "分页获取商品评论列表")
    @Parameters({
            @Parameter(name = "spuId", description = "商品id", required = true),
            @Parameter(name = "evaluate", description = "评论类型  null/0 全部，1好评 2中评 3差评 4有图")
    })
    public ServerResponseEntity<PageVO<SpuCommVO>> page(@Valid PageDTO pageDTO, @RequestParam("spuId") Long spuId, @RequestParam("evaluate") Integer evaluate) {
        PageVO<SpuCommVO> spuCommPage = spuCommService.spuCommPage(pageDTO, spuId, evaluate);
        return ServerResponseEntity.success(spuCommPage);
    }

    @GetMapping("/ua/prod_comm_data")
    @Operation(summary = "返回商品评论数据(好评率 好评数量 中评数 差评数)(商品)", description = "根据商品id获取")
    @Parameter(name = "spuId", description = "商品id", required = true)
    public ServerResponseEntity<SpuCommStatisticsVO> getProdCommData(Long spuId) {
        return ServerResponseEntity.success(spuCommService.getProdCommDataBySpuId(spuId));
    }

    @GetMapping("/ua/prod_comm_data_by_star")
    @Operation(summary = "返回商品评论数据(按星数划分数量)(商品)", description = "根据商品id获取")
    @Parameter(name = "spuId", description = "商品id", required = true)
    public ServerResponseEntity<SpuCommStatisticsStarVO> getProdCommDataByStar(Long spuId) {
        return ServerResponseEntity.success(spuCommService.getProdCommDataByStar(spuId));
    }

    @GetMapping("/prod_comm_page_by_user")
    @Operation(summary = "根据用户返回评论分页数据(该用户发表的评论特殊显示)", description = "传入页码")
    public ServerResponseEntity<PageVO<SpuCommVO>> getProdCommPageByUser(@Valid PageDTO pageDTO) {
        return ServerResponseEntity.success(spuCommService.spuCommPageByUserId(pageDTO, AuthUserContext.get().getUserId()));
    }

    @GetMapping("/ua/prod_comm_pag")
    @Operation(summary = "返回评论分页数据(未登陆时请求)", description = "传入页码")
    public ServerResponseEntity<PageVO<SpuCommVO>> getProdCommPage(@Valid PageDTO pageDTO) {
        return ServerResponseEntity.success(spuCommService.spuCommPageByUserId(pageDTO, null));
    }

    @PostMapping("/comment")
    @Operation(summary = "添加评论")
    public ServerResponseEntity<Void> saveProdCommPage(@Valid @RequestBody SpuCommDTO spuCommDTO) {
        spuCommService.comm(spuCommDTO);
        return ServerResponseEntity.success();
    }

    @PostMapping("/comments")
    @Operation(summary = "批量添加评论")
    public ServerResponseEntity<Void> saveBatchComm(@RequestBody List<SpuCommDTO> spuCommDTOList) {
        spuCommService.comms(spuCommDTOList);
        return ServerResponseEntity.success();
    }

    @GetMapping("/get_prod_comment")
    @Operation(summary = "根据itemIds获取评论", description = "根据itemIds获取评论")
    public ServerResponseEntity<List<SpuCommVO>> getProdComment(@RequestParam("orderItemIds") List<Long> orderItemIds) {
        return ServerResponseEntity.success(spuCommService.getSpuCommByOrderItemId(orderItemIds, AuthUserContext.get().getUserId()));
    }
}
