package com.jeeplus.psimanage.dishManage.controller;

import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import com.jeeplus.psimanage.dishManage.domain.PsiDishType;
import com.jeeplus.psimanage.dishManage.service.PsiDishTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜品分类管理
 */
@RestController
@Api(tags = "菜品分类管理")
@RequestMapping(value = "/psi/dishType")
public class PsiDishTypeController {

    @Resource
    private PsiDishTypeService service;

    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    public ResponseEntity<List<PsiDishType>> list(PsiDishType info) {
        return ResponseEntity.ok(service.list(info));
    }

    @ApiOperation(value = "分类树")
    @GetMapping("/treeData")
    public ResponseEntity<List<PsiDishType>> treeData(@RequestParam(required = false) String extId) {
        return ResponseEntity.ok(service.treeData(extId));
    }

    @ApiLog(value = "保存菜品分类", type = LogTypeEnum.SAVE)
    @ApiOperation(value = "新增/修改")
    @PreAuthorize("hasAnyAuthority('psi:dishType:add','psi:dishType:edit')")
    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody PsiDishType info) {
        return ResponseEntity.ok(service.saveDishType(info));
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/findById")
    public ResponseEntity<PsiDishType> findById(String id) {
        return ResponseEntity.ok(service.findById(id));
    }

    @ApiLog(value = "删除菜品分类", type = LogTypeEnum.SAVE)
    @ApiOperation(value = "根据id删除")
    @PreAuthorize("hasAuthority('psi:dishType:del')")
    @GetMapping("/deleteById")
    public ResponseEntity<String> deleteById(String id) {
        return ResponseEntity.ok(service.remove(id));
    }

    @ApiLog(value = "修改菜品分类状态", type = LogTypeEnum.SAVE)
    @ApiOperation(value = "启用/停用")
    @PreAuthorize("hasAuthority('psi:dishType:edit')")
    @GetMapping("/changeStatus")
    public ResponseEntity<String> changeStatus(@RequestParam String id, @RequestParam String status) {
        return ResponseEntity.ok(service.changeStatus(id, status));
    }
}
