/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtygis.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.xrtygis.entity.Themelayer;
import com.tofly.xrtygis.service.ThemelayerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 自定义专题图层信息表
 *
 * @author admin
 * @date Wed Mar 10 00:00:00 CST 2021
 */
@RestController
@AllArgsConstructor
@RequestMapping("/themelayer")
@Api(tags = "自定义专题图层信息表接口")
public class ThemelayerController {

    private final ThemelayerService themelayerService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getThemelayerPage(Page page) {
        return ResultRespone.success(themelayerService.page(page));
    }

    @GetMapping("/getMeThemelayer")
    @ApiOperation("查看自己创建专题图")
    public ResultRespone getMeThemelayer(Page page) {
        return ResultRespone.success(themelayerService.page(page, Wrappers.<Themelayer>lambdaQuery()
                .eq(Themelayer::getCreateUser, SecurityUtils.getUserId())));
    }


    /**
     * 通过id查询自定义专题图层信息表
     *
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id") Long id) {
        return ResultRespone.success(themelayerService.getById(id));
    }

    /**
     * 新增自定义专题图层信息表
     *
     * @param themelayer 自定义专题图层信息表
     * @return ResultRespone
     */
    @ApiOperation(value = "新增自定义专题图层信息表")
    @PostMapping("/save")
    public ResultRespone save(@RequestBody Themelayer themelayer) {
        themelayerService.save(themelayer);
        List<Themelayer> list = themelayerService.list(Wrappers.<Themelayer>lambdaQuery()
                .eq(Themelayer::getName, themelayer.getName())
                .eq(Themelayer::getLayerName, themelayer.getLayerName())
                .orderByDesc(Themelayer::getCreateTime));
        Long id = list.get(0).getId();
        return ResultRespone.success(id);
    }

    /**
     * 修改自定义专题图层信息表
     *
     * @param themelayer 自定义专题图层信息表
     * @return ResultRespone
     */
    @ApiOperation(value = "修改自定义专题图层信息表")
    @PutMapping("/updateById")
    public ResultRespone updateById(@RequestBody Themelayer themelayer) {
        return ResultRespone.success(themelayerService.updateById(themelayer));
    }

    /**
     * 通过id删除自定义专题图层信息表
     *
     * @param id id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID删除自定义专题图层信息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable Long id) {
        return ResultRespone.success(themelayerService.removeById(id));
    }

    /**
     * 通过id批量删除自定义专题图层信息表
     *
     * @param ids id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID批量删除自定义专题图层信息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(themelayerService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
