package cn.iocoder.yudao.module.system.controller.admin.restaurant;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.dify.vo.DifyWorkFlowRunReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.restaurant.RestaurantManagementDO;
import cn.iocoder.yudao.module.system.service.dify.DifyWorkFlowRecordService;
import cn.iocoder.yudao.module.system.service.restaurant.RestaurantManagementService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 餐厅管理")
@RestController
@RequestMapping("/system/restaurantManagement")
@Validated
public class RestaurantManagementController {

    @Resource
    private RestaurantManagementService restaurantManagementService;

    @Autowired
    private DifyWorkFlowRecordService difyWorkFlowRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建餐厅")
    public CommonResult<Long> createRestaurantManagement(@Valid @RequestBody RestaurantManagementDO createReqVO) {
        return success(restaurantManagementService.createRestaurantManagement(createReqVO));
    }


    @PutMapping("/update")
    @Operation(summary = "更新餐厅管理")
    public CommonResult<Boolean> updateRestaurantManagement(@Valid @RequestBody RestaurantManagementDO updateReqVO) {
        restaurantManagementService.updateRestaurantManagement(updateReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除餐厅")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteRestaurantManagement(@RequestParam("id") Long id) {
        restaurantManagementService.deleteRestaurantManagement(id);
        return success(true);
    }

    //
//    @GetMapping("/get")
//    @Operation(summary = "获得餐厅管理")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('system:restaurant-management:query')")
//    public CommonResult<RestaurantManagementRespVO> getRestaurantManagement(@RequestParam("id") Long id) {
//        RestaurantManagementDO restaurantManagement = restaurantManagementService.getRestaurantManagement(id);
//        return success(BeanUtils.toBean(restaurantManagement, RestaurantManagementRespVO.class));
//    }
//
    @GetMapping("/getList")
    @Operation(summary = "获得餐厅列表")
    @TenantIgnore
    public CommonResult<List<RestaurantManagementDO>> getList(@RequestParam(value = "orgType",required = false) Integer orgType,
                                                              @RequestParam(value = "tenantId",required = false) Long tenantId) {
        tenantId = Objects.isNull(tenantId) ? TenantContextHolder.getTenantId() : tenantId;
        return success(restaurantManagementService.getList(orgType,tenantId));
    }
//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出餐厅管理 Excel")
//    @PreAuthorize("@ss.hasPermission('system:restaurant-management:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportRestaurantManagementExcel(@Valid RestaurantManagementPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<RestaurantManagementDO> list = restaurantManagementService.getRestaurantManagementPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "餐厅管理.xls", "数据", RestaurantManagementRespVO.class,
//                        BeanUtils.toBean(list, RestaurantManagementRespVO.class));
//    }
        @GetMapping("/getAiSuggest")
        @Operation(summary = "获取ai建议")
        @TenantIgnore
        public CommonResult<JSONObject> getAiSuggest(@RequestParam(value = "tenantId",required = false) Long tenantId) {
            return success(difyWorkFlowRecordService.workflowRun(new DifyWorkFlowRunReqVO().setTenantId(tenantId).setWorkNumber("restaurantManagementGetAiSuggest")));
        }
}