package com.bonus.core.menu.controller;

import com.bonus.common.core.domain.R;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.core.common.page.PageVO;
import com.bonus.core.menu.dto.MenuEvaluaOrderAddDTO;
import com.bonus.core.menu.dto.MenuEvaluaOrderPageDTO;
import com.bonus.core.menu.service.MenuEvaluaOrderService;
import com.bonus.core.menu.vo.MenuEvaluaOrderPageMealDTO;
import com.bonus.core.menu.vo.MenuEvaluateOrderPageVO;
import com.bonus.framework.annotation.RequiresGuest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping({"/api/v1/applet/menuevaluaorder"})
@Api(
        value = "menuevaluaorder",
        tags = {"pfy_订单评价"}
)
public class MenuEvaluaOrderController {
    private static final Logger log = LoggerFactory.getLogger(MenuEvaluaOrderController.class);
    @Lazy
    @Resource
    private MenuEvaluaOrderService menuEvaluaOrderService;

    @GetMapping({"/page/meal"})
    @RequiresGuest
    @ApiOperation("获取指定餐品的评价列表")
    public AjaxResult getMenuEvaluaOrderPageByMeal(@RequestBody @Valid MenuEvaluaOrderPageMealDTO dto) {
        try {
            if (StringUtils.isBlank(String.valueOf(dto.getMealId()))) {
                return AjaxResult.error("餐品ID不能为空");
            }
            if (StringUtils.isBlank(String.valueOf(dto.getCurrent()))) {
                return AjaxResult.error("页码不能为空");
            }
            if (StringUtils.isBlank(String.valueOf(dto.getSize()))) {
                return AjaxResult.error("每页数量不能为空");
            }
            if (StringUtils.isBlank(String.valueOf(dto.getShopstallId()))) {
                return AjaxResult.error("档口ID不能为空");
            }
            if (StringUtils.isBlank(String.valueOf(dto.getMealType()))){
                return AjaxResult.error("餐品类型不能为空");
            }
            return AjaxResult.success(this.menuEvaluaOrderService.getMenuEvaluaOrderPageByMeal(dto));
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("获取指定餐品的评价列表失败");
        }
    }

    @PostMapping({"/add"})
    @RequiresGuest
    @ApiOperation("新增订单评价")
    public R<Void> addMenuEvaluaOrder(@RequestBody @Valid MenuEvaluaOrderAddDTO request) {
        this.menuEvaluaOrderService.addMenuEvaluaOrder(request);
        return R.ok();
    }

    @PostMapping({"/page/evaluate/order"})
    @ApiOperation("获取评价列表")
    public PageVO<MenuEvaluateOrderPageVO> getMenuEvaluateOrderPagePc(@RequestBody MenuEvaluaOrderPageDTO request) {
        return this.menuEvaluaOrderService.getMenuEvaluateOrderPagePc(request);
    }
}
