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

import com.jeesharp.business.modules.food.application.FoodMealRuleApplicationService;
import com.jeesharp.business.modules.food.dto.FoodMealRuleSaveDTO;
import com.jeesharp.business.modules.food.dto.FoodStatusUpdateDTO;
import com.jeesharp.business.modules.food.entity.FoodMealRule;
import com.jeesharp.business.modules.food.vo.FoodMealRuleInfoVO;
import com.jeesharp.business.modules.food.vo.FoodMealRuleListVO;
import com.jeesharp.business.modules.food.vo.FoodMealRuleQueryVO;
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/mealRule/v1")
@Tag(name = "订餐规则管理")
@AllArgsConstructor
public class FoodMealRuleController extends BaseController {

    private final FoodMealRuleApplicationService foodMealRuleApplicationService;

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

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

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

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

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