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.QuotaAdjustmentSettingRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaAdjustmentSettingSaveReqVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaAdjustmentSettingSwapSortReqVO;
import com.yhy.module.core.dal.dataobject.quota.QuotaAdjustmentSettingDO;
import com.yhy.module.core.service.quota.QuotaAdjustmentSettingService;
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-setting")
@Validated
public class QuotaAdjustmentSettingController {

    @Resource
    private QuotaAdjustmentSettingService quotaAdjustmentSettingService;

    @PostMapping("/create")
    @Operation(summary = "创建定额调整设置")
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-setting:create')")
    public CommonResult<Long> createQuotaAdjustmentSetting(@Valid @RequestBody QuotaAdjustmentSettingSaveReqVO createReqVO) {
        return success(quotaAdjustmentSettingService.createQuotaAdjustmentSetting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新定额调整设置")
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-setting:update')")
    public CommonResult<Boolean> updateQuotaAdjustmentSetting(@Valid @RequestBody QuotaAdjustmentSettingSaveReqVO updateReqVO) {
        quotaAdjustmentSettingService.updateQuotaAdjustmentSetting(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得定额调整设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-setting:query')")
    public CommonResult<QuotaAdjustmentSettingRespVO> getQuotaAdjustmentSetting(@RequestParam("id") Long id) {
        QuotaAdjustmentSettingDO setting = quotaAdjustmentSettingService.getQuotaAdjustmentSetting(id);
        return success(BeanUtils.toBean(setting, QuotaAdjustmentSettingRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得定额调整设置列表")
    @Parameter(name = "quotaItemId", description = "定额子目ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:adjustment-setting:query')")
    public CommonResult<List<QuotaAdjustmentSettingRespVO>> getQuotaAdjustmentSettingList(@RequestParam("quotaItemId") Long quotaItemId) {
        List<QuotaAdjustmentSettingDO> list = quotaAdjustmentSettingService.getQuotaAdjustmentSettingList(quotaItemId);
        return success(BeanUtils.toBean(list, QuotaAdjustmentSettingRespVO.class));
    }

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

}
