package com.jeesharp.business.modules.food.controller;

import com.jeesharp.business.modules.food.application.FoodDishApplicationService;
import com.jeesharp.business.modules.food.dto.FoodDishSaveDTO;
import com.jeesharp.business.modules.food.dto.FoodStatusUpdateDTO;
import com.jeesharp.business.modules.food.entity.FoodDish;
import com.jeesharp.business.modules.food.vo.FoodDishInfoVO;
import com.jeesharp.business.modules.food.vo.FoodDishListVO;
import com.jeesharp.business.modules.food.vo.FoodDishQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/food/dish/v1")
@Tag(name = "订餐菜品管理")
@AllArgsConstructor
public class FoodDishController extends BaseController {

    private final FoodDishApplicationService foodDishApplicationService;

    @Operation(summary = "获取详情")
    @PreAuthorize("@ss.hasPermission('manager:business:food:dish:info')")
    @GetMapping("info")
    public AjaxResult<FoodDishInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(foodDishApplicationService.get(id));
    }

    @Operation(summary = "获取列表（分页）")
    @PreAuthorize("@ss.hasPermission('manager:business:food:dish:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<FoodDishListVO>> list(@RequestBody FoodDishQueryVO query) {
        return AjaxResult.okData(foodDishApplicationService.findPage(new Page<FoodDish>(query), query));
    }

    @Operation(summary = "保存")
    @PreAuthorize("@ss.hasPermission('manager:business:food:dish:save') or @ss.hasPermission('manager:business:food:dish:create') or @ss.hasPermission('manager:business:food:dish:update')")
    @PostMapping("save")
    public AjaxResult<Void> save(@RequestBody @Valid FoodDishSaveDTO dto) {
        foodDishApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @Operation(summary = "启停")
    @PreAuthorize("@ss.hasPermission('manager:business:food:dish:update')")
    @PostMapping("status")
    public AjaxResult<Void> status(@RequestBody @Valid FoodStatusUpdateDTO dto) {
        foodDishApplicationService.updateStatus(dto.getId(), dto.getStatus());
        return AjaxResult.ok("操作成功");
    }

    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('manager:business:food:dish:delete')")
    @GetMapping("delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        foodDishApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }
}
