package com.rj.controller;

import com.rj.entity.Tenant;
import com.rj.service.ITenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 租户表 前端控制器
 * </p>
 *
 * @author 系统生成
 * @since 2025-01-XX
 */
@RestController
@RequestMapping("/api/tenant")
@Tag(name = "租户管理", description = "租户管理相关接口")
public class TenantController {

    @Autowired
    private ITenantService tenantService;

    /**
     * 分页查询租户列表
     */
    @PostMapping("/list")
    @Operation(summary = "分页查询租户列表", description = "根据条件分页查询租户信息列表")
    public ResponseEntity<Map<String, Object>> getTenantList(
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "租户编码（精确查询）")
            @RequestParam(required = false) String tenantCode,
            @Parameter(description = "租户名称（模糊查询）")
            @RequestParam(required = false) String tenantName,
            @Parameter(description = "状态：1-启用，0-禁用")
            @RequestParam(required = false) Integer status,
            @Parameter(description = "联系人姓名（模糊查询）")
            @RequestParam(required = false) String contactName,
            @Parameter(description = "联系人电话（模糊查询）")
            @RequestParam(required = false) String contactPhone,
            @Parameter(description = "过期时间开始（格式：yyyy-MM-dd HH:mm:ss）")
            @RequestParam(required = false) String expireStartTime,
            @Parameter(description = "过期时间结束（格式：yyyy-MM-dd HH:mm:ss）")
            @RequestParam(required = false) String expireEndTime) {
        Map<String, Object> result = tenantService.getTenantList(current, size, tenantCode, tenantName, 
                                                                 status, contactName, contactPhone, 
                                                                 expireStartTime, expireEndTime);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据条件查询租户列表（不分页）
     */
    @GetMapping("/listAll")
    @Operation(summary = "查询租户列表（不分页）", description = "根据条件查询所有租户信息，不分页")
    public ResponseEntity<Map<String, Object>> getTenantListWithoutPage(
            @Parameter(description = "租户编码（精确查询）")
            @RequestParam(required = false) String tenantCode,
            @Parameter(description = "租户名称（模糊查询）")
            @RequestParam(required = false) String tenantName,
            @Parameter(description = "状态：1-启用，0-禁用")
            @RequestParam(required = false) Integer status,
            @Parameter(description = "联系人姓名（模糊查询）")
            @RequestParam(required = false) String contactName,
            @Parameter(description = "联系人电话（模糊查询）")
            @RequestParam(required = false) String contactPhone,
            @Parameter(description = "过期时间开始（格式：yyyy-MM-dd HH:mm:ss）")
            @RequestParam(required = false) String expireStartTime,
            @Parameter(description = "过期时间结束（格式：yyyy-MM-dd HH:mm:ss）")
            @RequestParam(required = false) String expireEndTime) {
        Map<String, Object> result = tenantService.getTenantListWithoutPage(tenantCode, tenantName, 
                                                                           status, contactName, contactPhone, 
                                                                           expireStartTime, expireEndTime);
        return ResponseEntity.ok(result);
    }

    /**
     * 新增租户
     */
    @PostMapping("/add")
    @Operation(summary = "新增租户", description = "添加新的租户信息")
    public ResponseEntity<Map<String, Object>> addTenant(
            @Parameter(description = "租户信息", required = true)
            @RequestBody Tenant tenant) {
        tenant.setTenantCode("TENANT_ID_"+tenant.getTenantCode());
        tenant.setId(tenant.getTenantCode());
        Map<String, Object> result = tenantService.addTenant(tenant);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 更新租户信息
     */
    @PutMapping("/update")
    @Operation(summary = "更新租户信息", description = "更新租户详细信息")
    public ResponseEntity<Map<String, Object>> updateTenant(
            @Parameter(description = "租户信息", required = true)
            @RequestBody Tenant tenant) {
        Map<String, Object> result = tenantService.updateTenant(tenant);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 根据ID删除租户
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除租户", description = "根据租户ID删除租户信息")
    public ResponseEntity<Map<String, Object>> deleteTenant(
            @Parameter(description = "租户ID", required = true)
            @PathVariable String id) {
        Map<String, Object> result = tenantService.deleteTenant(id);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body(result);
        }
    }
}

