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

package com.yami.shop.api.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.ProdCommDto;
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.OrderItem;
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.OrderItemService;
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.Objects;

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

    private final ProdCommService prodCommService;

    private final OrderItemService orderItemService;

    private final OrderService orderService;


    @GetMapping("/prodCommPageByUser")
    @ApiOperation(value = "根据用户返回评论分页数据", notes = "传入页码")
    public ResponseEntity<IPage<ProdCommDto>> getProdCommPage(PageParam page) {
        return ResponseEntity.ok(prodCommService.getProdCommDtoPageByUserId(page, SecurityUtils.getUser().getUserId()));
    }



    @PostMapping
    @ApiOperation(value = "添加评论")
    public ResponseEntity<Void> saveProdCommPage(@Valid @RequestBody ProdCommParam prodCommParam) {
        Long orderItemId = prodCommParam.getOrderItemId();

        OrderItem orderItem = orderItemService.getById(orderItemId);
        if (orderItem == null) {
            throw new GlobalException("订单项不存在");
        }
        if (Objects.equals(orderItem.getCommSts(), 1)) {
            throw new GlobalException("改订单项已评论，请勿重复评论");
        }

        String userId = SecurityUtils.getUser().getUserId();

        Order order = orderService.getOrderByOrderNumberAndUserId(orderItem.getOrderNumber(),userId, true);

        if (!Objects.equals(order.getHbOrderStatus(), OrderStatus.SUCCESS.value())) {
            throw new GlobalException("请确认收货后再进行评论");
        }

        prodCommService.comm(orderItem, prodCommParam);

        return ResponseEntity.ok().build();
    }

    @GetMapping("/getProdComment")
    @ApiOperation(value = "根据itemId获取评论", notes = "根据itemId获取评论")
    public ResponseEntity<ProdComm> getProdComment(Long orderItemId) {

        ProdComm prodComm = prodCommService.getOne(new LambdaUpdateWrapper<ProdComm>().eq(ProdComm::getOrderItemId, orderItemId).eq(ProdComm::getUserId,SecurityUtils.getUser().getUserId()));
        return ResponseEntity.ok(prodComm);
    }
}
