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

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.yhy.module.core.controller.admin.quota.vo.QuotaAdjustmentCombinedRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaAdjustmentDetailRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaAdjustmentDetailSaveReqVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaAdjustmentDetailSwapSortReqVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaAdjustmentSettingRespVO;
import com.yhy.module.core.dal.dataobject.quota.QuotaAdjustmentDetailDO;
import com.yhy.module.core.dal.dataobject.quota.QuotaAdjustmentSettingDO;
import com.yhy.module.core.service.quota.QuotaAdjustmentDetailService;
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 javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
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;

@Tag(name = "管理后台 - 定额调整明细")
@RestController
@RequestMapping("/core/quota/adjustment-detail")
@Validated
public class QuotaAdjustmentDetailController {

    @Resource
    private QuotaAdjustmentDetailService quotaAdjustmentDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建定额调整明细")
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-detail:create')")
    public CommonResult<Long> createQuotaAdjustmentDetail(@Valid @RequestBody QuotaAdjustmentDetailSaveReqVO createReqVO) {
        return success(quotaAdjustmentDetailService.createQuotaAdjustmentDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新定额调整明细")
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-detail:update')")
    public CommonResult<Boolean> updateQuotaAdjustmentDetail(@Valid @RequestBody QuotaAdjustmentDetailSaveReqVO updateReqVO) {
        quotaAdjustmentDetailService.updateQuotaAdjustmentDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除定额调整明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-detail:delete')")
    public CommonResult<Boolean> deleteQuotaAdjustmentDetail(@RequestParam("id") Long id) {
        quotaAdjustmentDetailService.deleteQuotaAdjustmentDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得定额调整明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-detail:query')")
    public CommonResult<QuotaAdjustmentDetailRespVO> getQuotaAdjustmentDetail(@RequestParam("id") Long id) {
        QuotaAdjustmentDetailDO detail = quotaAdjustmentDetailService.getQuotaAdjustmentDetail(id);
        return success(BeanUtils.toBean(detail, QuotaAdjustmentDetailRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得定额调整明细列表")
    @Parameter(name = "quotaItemId", description = "定额子目ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-detail:query')")
    public CommonResult<List<QuotaAdjustmentDetailRespVO>> getQuotaAdjustmentDetailList(@RequestParam("quotaItemId") Long quotaItemId) {
        List<QuotaAdjustmentDetailDO> list = quotaAdjustmentDetailService.getQuotaAdjustmentDetailListByQuotaItem(quotaItemId);
        return success(BeanUtils.toBean(list, QuotaAdjustmentDetailRespVO.class));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-detail:update')")
    public CommonResult<Boolean> swapSort(@Valid @RequestBody QuotaAdjustmentDetailSwapSortReqVO reqVO) {
        quotaAdjustmentDetailService.swapSort(reqVO.getId1(), reqVO.getId2());
        return success(true);
    }

    @GetMapping("/available-settings")
    @Operation(summary = "获取可引用的调整设置列表")
    @Parameter(name = "adjustmentSettingId", description = "当前调整设置ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-detail:query')")
    public CommonResult<List<QuotaAdjustmentSettingRespVO>> getAvailableSettings(@RequestParam("adjustmentSettingId") Long adjustmentSettingId) {
        List<QuotaAdjustmentSettingDO> list = quotaAdjustmentDetailService.getAvailableSettings(adjustmentSettingId);
        return success(BeanUtils.toBean(list, QuotaAdjustmentSettingRespVO.class));
    }

    @GetMapping("/combined-list")
    @Operation(summary = "获取调整设置与明细的组合列表")
    @Parameter(name = "quotaItemId", description = "定额子目ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-detail:query')")
    public CommonResult<List<QuotaAdjustmentCombinedRespVO>> getCombinedList(@RequestParam("quotaItemId") Long quotaItemId) {
        return success(quotaAdjustmentDetailService.getCombinedList(quotaItemId));
    }

}
