/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.param.ProdCommParam;
import com.yami.shop.bean.enums.OrderStatus;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.ProdComm;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.OrderService;
import com.yami.shop.service.ProdCommService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/p/prodComm")
//@RequestMapping("/prodComm")
@Api(tags = "评论接口")
@AllArgsConstructor
public class ProdCommController {

    private final ProdCommService prodCommService;

    private final OrderService orderService;

    @PostMapping
    @ApiOperation(value = "添加评论")
    public ResponseEntity<Void> saveProdCommPage(@Valid @RequestBody ProdCommParam prodCommParam) {
        List<ProdComm> commList = prodCommService.list(
                new LambdaQueryWrapper<ProdComm>().eq(ProdComm::getOrderNumber, prodCommParam.getOrderNumber()));
        if (!commList.isEmpty()) {
            throw new GlobalException("该订单已评论...");
        }
        Order order = orderService.getOrderByOrderNumber(prodCommParam.getOrderNumber());
        if (order == null) {
            throw new GlobalException("订单不存在");
        }
        if (!Objects.equals(order.getHbOrderStatus(), OrderStatus.SUCCESS.value())) {
            throw new GlobalException("请确认收货后再进行评论");
        }
        prodCommService.comm(order, prodCommParam);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/commList")
    @ApiOperation(value = "商品详情-查看商品评价(0好评 1中评 2差评 3有图)")
    public ResponseEntity<IPage<ProdComm>> commList(PageParam<ProdComm> page, Long prodId,Long shopId, Integer commStatus,Long channelId) {
        shopId = shopId == null ? 1 : shopId;
        channelId = channelId == null ? 1 : channelId;
        return ResponseEntity.ok(prodCommService.commList(page,prodId,shopId,commStatus,channelId));
    }

    @GetMapping("/commentCount")
    @ApiOperation(value = "商品详情-评价数量-好评率")
    public ResponseEntity<Map<String, Object>> commentCount(Long prodId,Long shopId,Long channelId) {
        shopId = shopId == null ? 1 : shopId;
        channelId = channelId == null ? 1 : channelId;
        return ResponseEntity.ok(prodCommService.commentCount(prodId,shopId,channelId));
    }

    @GetMapping("/myCommList")
    @ApiOperation(value = "小程序-我的评价")
    public ResponseEntity<IPage<ProdComm>> myCommList(PageParam<ProdComm> page) {
        return ResponseEntity.ok(prodCommService.myCommList(page, SecurityUtils.getUser().getUserId()));
    }
}
