package com.ruoyi.web.controller.back;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.domain.ChangesDto;
import com.ruoyi.web.service.BackChangesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@Api(tags = "变更, 变更通知")
@RequestMapping("/voluntary/changes")
public class BackChangesController {
    private final BackChangesService backChangesService;

    public BackChangesController(BackChangesService backChangesService) {
        this.backChangesService = backChangesService;
    }

    @ApiOperation("1. 分数位次变更")
    @RequestMapping(value = "/scoreRankNoToken", method = {RequestMethod.POST})
    public AjaxResult postScoreRank(@RequestBody ChangesDto.PageData<ChangesDto.ScoreRank> pageData) {
        if (CollectionUtils.isEmpty(pageData.getRows())) {
            return AjaxResult.error("无数据");
        }
        return AjaxResult.success(backChangesService.postScoreRank(pageData));
    }

    @ApiOperation("2. 分数位次重算")
    @RequestMapping(value = "/scoreRankCalNoToken", method = {RequestMethod.POST})
    public AjaxResult postScoreRank(@ApiParam @RequestParam String version, @ApiParam @RequestParam String location, @ApiParam @RequestParam Integer planYear) {
        backChangesService.processScoreRankChange(version, planYear, Arrays.asList(location.split(",")));
        return AjaxResult.success();
    }

    @ApiOperation("3. 删除分数位次变更版本")
    @RequestMapping(value = "/scoreRankDelNoToken", method = {RequestMethod.POST})
    public AjaxResult scoreRankDelNoToken(@ApiParam @RequestParam String version, @ApiParam @RequestParam String location) {
        backChangesService.deleteUpdateVersion(version, location);
        return AjaxResult.success();
    }

    @ApiOperation("4. 指定省份年度重算")
    @RequestMapping(value = "/statsCalNoToken", method = {RequestMethod.POST})
    public AjaxResult statCalNoToken(@ApiParam @RequestParam String location, @ApiParam @RequestParam Integer planYear) {
        backChangesService.processStats(planYear, Arrays.asList(location.split(",")));
        return AjaxResult.success();
    }
}
