package com.yhy.module.core.controller.admin.quota;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.quota.vo.QuotaUnifiedFeeResourceRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaUnifiedFeeResourceSaveReqVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaUnifiedFeeSettingRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaUnifiedFeeSettingSaveReqVO;
import com.yhy.module.core.dal.dataobject.quota.QuotaUnifiedFeeResourceDO;
import com.yhy.module.core.dal.dataobject.quota.QuotaUnifiedFeeSettingDO;
import com.yhy.module.core.service.quota.QuotaUnifiedFeeResourceService;
import com.yhy.module.core.service.quota.QuotaUnifiedFeeSettingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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 static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 统一取费设置")
@RestController
@RequestMapping("/core/quota/unified-fee-setting")
@Validated
public class QuotaUnifiedFeeSettingController {

    @Resource
    private QuotaUnifiedFeeSettingService unifiedFeeSettingService;

    @Resource
    private QuotaUnifiedFeeResourceService unifiedFeeResourceService;

    @PostMapping("/create")
    @Operation(summary = "创建统一取费设置")
    public CommonResult<Long> createUnifiedFeeSetting(@Valid @RequestBody QuotaUnifiedFeeSettingSaveReqVO createReqVO) {
        return success(unifiedFeeSettingService.createUnifiedFeeSetting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新统一取费设置")
    public CommonResult<Boolean> updateUnifiedFeeSetting(@Valid @RequestBody QuotaUnifiedFeeSettingSaveReqVO updateReqVO) {
        unifiedFeeSettingService.updateUnifiedFeeSetting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除统一取费设置")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteUnifiedFeeSetting(@RequestParam("id") Long id) {
        unifiedFeeSettingService.deleteUnifiedFeeSetting(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取统一取费设置详情")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<QuotaUnifiedFeeSettingRespVO> getUnifiedFeeSetting(@RequestParam("id") Long id) {
        QuotaUnifiedFeeSettingDO unifiedFeeSetting = unifiedFeeSettingService.getUnifiedFeeSetting(id);
        return success(BeanUtil.copyProperties(unifiedFeeSetting, QuotaUnifiedFeeSettingRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获取统一取费设置列表")
    @Parameter(name = "catalogItemId", description = "模式节点ID", required = true)
    public CommonResult<List<QuotaUnifiedFeeSettingRespVO>> getUnifiedFeeSettingList(
            @RequestParam("catalogItemId") Long catalogItemId) {
        List<QuotaUnifiedFeeSettingDO> list = unifiedFeeSettingService.getUnifiedFeeSettingList(catalogItemId);
        return success(list.stream()
                .map(item -> BeanUtil.copyProperties(item, QuotaUnifiedFeeSettingRespVO.class))
                .collect(Collectors.toList()));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取统一取费设置树")
    @Parameter(name = "catalogItemId", description = "模式节点ID", required = true)
    public CommonResult<List<QuotaUnifiedFeeSettingRespVO>> getUnifiedFeeSettingTree(
            @RequestParam("catalogItemId") Long catalogItemId) {
        return success(unifiedFeeSettingService.getUnifiedFeeSettingTree(catalogItemId));
    }

    @GetMapping("/parent-list")
    @Operation(summary = "获取父定额列表（用于工作台，包含子定额取费章节组合用于范围过滤）")
    @Parameter(name = "catalogItemId", description = "模式节点ID", required = true)
    public CommonResult<List<QuotaUnifiedFeeSettingRespVO>> getParentList(
            @RequestParam("catalogItemId") Long catalogItemId) {
        // 返回父定额列表，但每个父定额的feeChapter字段包含其所有子定额的取费章节组合
        return success(unifiedFeeSettingService.getParentListWithChildFeeChapters(catalogItemId));
    }

    @GetMapping("/child-list")
    @Operation(summary = "获取子定额列表")
    @Parameter(name = "parentId", description = "父定额ID", required = true)
    public CommonResult<List<QuotaUnifiedFeeSettingRespVO>> getChildList(
            @RequestParam("parentId") Long parentId) {
        List<QuotaUnifiedFeeSettingDO> list = unifiedFeeSettingService.getChildList(parentId);
        return success(list.stream()
                .map(item -> BeanUtil.copyProperties(item, QuotaUnifiedFeeSettingRespVO.class))
                .collect(Collectors.toList()));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    public CommonResult<Boolean> swapSort(@RequestParam("nodeId1") Long nodeId1,
                                          @RequestParam("nodeId2") Long nodeId2) {
        unifiedFeeSettingService.swapSort(nodeId1, nodeId2);
        return success(true);
    }

    // ==================== 子目工料机相关接口 ====================

    @PostMapping("/resource/create")
    @Operation(summary = "创建子目工料机")
    public CommonResult<Long> createUnifiedFeeResource(@Valid @RequestBody QuotaUnifiedFeeResourceSaveReqVO createReqVO) {
        return success(unifiedFeeResourceService.createUnifiedFeeResource(createReqVO));
    }

    @PutMapping("/resource/update")
    @Operation(summary = "更新子目工料机")
    public CommonResult<Boolean> updateUnifiedFeeResource(@Valid @RequestBody QuotaUnifiedFeeResourceSaveReqVO updateReqVO) {
        unifiedFeeResourceService.updateUnifiedFeeResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/resource/delete")
    @Operation(summary = "删除子目工料机")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteUnifiedFeeResource(@RequestParam("id") Long id) {
        unifiedFeeResourceService.deleteUnifiedFeeResource(id);
        return success(true);
    }

    @GetMapping("/resource/list")
    @Operation(summary = "获取子目工料机列表")
    @Parameter(name = "unifiedFeeSettingId", description = "子定额ID", required = true)
    public CommonResult<List<QuotaUnifiedFeeResourceRespVO>> getUnifiedFeeResourceList(
            @RequestParam("unifiedFeeSettingId") Long unifiedFeeSettingId) {
        return success(unifiedFeeResourceService.getUnifiedFeeResourceList(unifiedFeeSettingId));
    }

    @PostMapping("/resource/swap-sort")
    @Operation(summary = "交换子目工料机排序")
    public CommonResult<Boolean> swapResourceSort(@RequestParam("nodeId1") Long nodeId1,
                                                   @RequestParam("nodeId2") Long nodeId2) {
        unifiedFeeResourceService.swapSort(nodeId1, nodeId2);
        return success(true);
    }

    @GetMapping("/resource/get-by-code")
    @Operation(summary = "根据编码查询工料机")
    @Parameter(name = "unifiedFeeSettingId", description = "统一取费设置ID", required = true)
    @Parameter(name = "code", description = "工料机编码", required = true)
    public CommonResult<com.yhy.module.core.controller.admin.resource.vo.ResourceItemRespVO> getResourceItemByCode(
            @RequestParam("unifiedFeeSettingId") Long unifiedFeeSettingId,
            @RequestParam("code") String code) {
        return success(unifiedFeeResourceService.getResourceItemByCode(unifiedFeeSettingId, code));
    }

    @GetMapping("/resource/available-list")
    @Operation(summary = "获取可选工料机列表（支持模糊查询）")
    @Parameter(name = "unifiedFeeSettingId", description = "统一取费设置ID", required = true)
    @Parameter(name = "code", description = "编码（模糊查询）", required = false)
    @Parameter(name = "name", description = "名称（模糊查询）", required = false)
    @Parameter(name = "spec", description = "型号规格（模糊查询）", required = false)
    public CommonResult<List<com.yhy.module.core.controller.admin.resource.vo.ResourceItemRespVO>> getAvailableResourceItems(
            @RequestParam("unifiedFeeSettingId") Long unifiedFeeSettingId,
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "spec", required = false) String spec) {
        return success(unifiedFeeResourceService.getAvailableResourceItemsWithFilter(
            unifiedFeeSettingId, code, name, spec));
    }
}
