package com.wteam.controller.system;

import com.wteam.framework.common.aop.annotation.DemoSite;
import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.system.entity.dos.Region;
import com.wteam.framework.modules.system.service.RegionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 管理端,行政地区管理接口
 *
 * @author Chopper
 * @since 2020/12/2 10:40
 */
@RestController
@Tag(name = "管理端,行政地区管理接口")
@RequestMapping("/manager/setting/region")
public class RegionManagerController {
    @Autowired
    private RegionService regionService;

    @DemoSite
    @PostMapping(value = "/sync")
    @Operation(summary = "同步高德行政地区数据")
    public void synchronizationData(String url) {
        regionService.synchronizationData(url);
    }

    @GetMapping(value = "/{id}")
    @Parameter(name = "id", description = "地区ID", required = true, schema = @Schema(type = "string"), in = ParameterIn.PATH)
    @Operation(summary = "通过id获取地区详情")
    public ResultMessage<Region> get(@PathVariable String id) {
        return ResultUtil.data(regionService.getById(id));
    }

    @GetMapping(value = "/item/{id}")
    @Parameter(name = "id", description = "地区ID", required = true, schema = @Schema(type = "string"), in = ParameterIn.PATH)
    @Operation(summary = "通过id获取子地区")
    public ResultMessage<List<Region>> getItem(@PathVariable String id) {
        return ResultUtil.data(regionService.getItem(id));
    }

    @PutMapping(value = "/{id}")
    @Parameter(name = "id", description = "地区ID", required = true, schema = @Schema(type = "string"), in = ParameterIn.PATH)
    @Operation(summary = "更新地区")
    public ResultMessage<Region> update(@PathVariable String id, @Valid Region region) {
        region.setId(id);
        regionService.updateById(region);
        return ResultUtil.data(region);
    }


    @PostMapping
    @Operation(summary = "增加地区")
    public ResultMessage<Region> save(@Valid Region region) {
        regionService.save(region);
        return ResultUtil.data(region);
    }

    @DeleteMapping(value = "{ids}")
    @Parameter(name = "id", description = "地区ID", required = true,schema = @Schema(type = "array",implementation = String.class), in = ParameterIn.PATH)
    @Operation(summary = "批量通过id删除")
    public ResultMessage<Object> delAllByIds(@PathVariable List<String> ids) {
        regionService.removeByIds(ids);
        return ResultUtil.success();
    }
}
