package com.jeeplus.hotelorder.dish.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.hotelorder.dish.domain.HotelDishAddition;
import com.jeeplus.hotelorder.dish.domain.HotelDishSpec;
import com.jeeplus.hotelorder.dish.domain.HotelDishSpecOption;
import com.jeeplus.hotelorder.dish.service.HotelDishService;
import com.jeeplus.hotelorder.dish.service.dto.HotelDishDTO;
import com.jeeplus.hotelorder.dto.DishBatchOperationDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "菜品管理")
@RestController
@RequestMapping(value = "/dish")
public class HotelDishController {

    @Resource
    private HotelDishService hotelDishService;

    /**
     * 管理后台-菜品分页查询
     * @param queryDTO 查询条件
     * @param page 分页参数
     * @return
     */
    @ApiOperation(value = "管理后台-菜品分页查询")
    @GetMapping("page")
    public ResponseEntity<IPage<HotelDishDTO>> page(HotelDishDTO queryDTO, Page<HotelDishDTO> page) {
        Page<com.jeeplus.hotelorder.dish.domain.HotelDish> entityPage = new Page<>(page.getCurrent(), page.getSize());
        IPage<HotelDishDTO> result = hotelDishService.findAdminPage(entityPage, queryDTO);
        return ResponseEntity.ok(result);
    }

    /**
     * 客户端-菜品分页查询(在售且未售罄)
     * @param queryDTO 查询条件
     * @param page 分页参数
     * @return
     */
    @ApiOperation(value = "客户端-菜品分页查询(在售且未售罄)")
    @GetMapping("clientPage")
    public ResponseEntity<IPage<HotelDishDTO>> clientPage(HotelDishDTO queryDTO, Page<HotelDishDTO> page) {
        Page<com.jeeplus.hotelorder.dish.domain.HotelDish> entityPage = new Page<>(page.getCurrent(), page.getSize());
        IPage<HotelDishDTO> result = hotelDishService.findClientPage(entityPage, queryDTO);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询菜品详情(含规格和加料)
     * @param id 菜品ID
     * @return
     */
    @ApiOperation(value = "查询菜品详情(含规格和加料)")
    @GetMapping("detail/{id}")
    public ResponseEntity<HotelDishDTO> getDetail(@PathVariable String id) {
        HotelDishDTO dto = hotelDishService.getDetail(id);
        return ResponseEntity.ok(dto);
    }

    /**
     * 新增/编辑菜品
     * @param dto 菜品信息
     * @return
     */
    @ApiOperation(value = "新增/编辑菜品")
    @PostMapping("save")
    public ResponseEntity<String> saveDish(@RequestBody HotelDishDTO dto) {
        String result = hotelDishService.saveDish(dto);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除菜品(含级联删除规格和加料)
     * @param id 菜品ID
     * @return
     */
    @ApiOperation(value = "删除菜品(含级联删除规格和加料)")
    @PostMapping("delete/{id}")
    public ResponseEntity<String> deleteDish(@PathVariable String id) {
        String result = hotelDishService.deleteDish(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 更新菜品状态(上架/下架)
     * @param dto 包含id和status
     * @return
     */
    @ApiOperation(value = "更新菜品状态(上架/下架)")
    @PostMapping("updateStatus")
    public ResponseEntity<String> updateStatus(@RequestBody HotelDishDTO dto) {
        String result = hotelDishService.updateStatus(dto.getId(), dto.getStatus());
        return ResponseEntity.ok(result);
    }

    /**
     * 标记售罄
     * @param id 菜品ID
     * @return
     */
    @ApiOperation(value = "标记售罄")
    @PostMapping("soldOut/{id}")
    public ResponseEntity<String> markSoldOut(@PathVariable String id) {
        String result = hotelDishService.markSoldOut(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 恢复上架(取消售罄)
     * @param id 菜品ID
     * @return
     */
    @ApiOperation(value = "恢复上架(取消售罄)")
    @PostMapping("restore/{id}")
    public ResponseEntity<String> restoreDish(@PathVariable String id) {
        String result = hotelDishService.restoreDish(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 批量操作(上架/下架/售罄/恢复/删除)
     * @param dto 批量操作参数
     * @return
     */
    @ApiOperation(value = "批量操作(上架/下架/售罄/恢复/删除)")
    @PostMapping("batch")
    public ResponseEntity<String> batch(@RequestBody DishBatchOperationDTO dto) {
        String result = hotelDishService.batchOperate(dto.getIds(), dto.getOperation());
        return ResponseEntity.ok(result);
    }

    /**
     * 保存菜品规格组
     * @param spec 规格组信息
     * @return
     */
    @ApiOperation(value = "保存菜品规格组")
    @PostMapping("spec/save")
    public ResponseEntity<String> saveSpec(@RequestBody HotelDishSpec spec) {
        String result = hotelDishService.saveSpec(spec);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除菜品规格组(含级联删除选项)
     * @param id 规格组ID
     * @return
     */
    @ApiOperation(value = "删除菜品规格组(含级联删除选项)")
    @PostMapping("spec/delete/{id}")
    public ResponseEntity<String> deleteSpec(@PathVariable String id) {
        String result = hotelDishService.deleteSpec(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存规格选项
     * @param option 规格选项信息
     * @return
     */
    @ApiOperation(value = "保存规格选项")
    @PostMapping("specOption/save")
    public ResponseEntity<String> saveSpecOption(@RequestBody HotelDishSpecOption option) {
        String result = hotelDishService.saveSpecOption(option);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存加料
     * @param addition 加料信息
     * @return
     */
    @ApiOperation(value = "保存加料")
    @PostMapping("addition/save")
    public ResponseEntity<String> saveAddition(@RequestBody HotelDishAddition addition) {
        String result = hotelDishService.saveAddition(addition);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除加料
     * @param id 加料ID
     * @return
     */
    @ApiOperation(value = "删除加料")
    @PostMapping("addition/delete/{id}")
    public ResponseEntity<String> deleteAddition(@PathVariable String id) {
        String result = hotelDishService.deleteAddition(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 销量排行
     * @param limit 数量限制
     * @return
     */
    @ApiOperation(value = "销量排行")
    @GetMapping("ranking")
    public ResponseEntity<List<HotelDishDTO>> salesRanking(@RequestParam(defaultValue = "10") int limit) {
        List<HotelDishDTO> list = hotelDishService.salesRanking(limit);
        return ResponseEntity.ok(list);
    }
}
