package com.boco.nbd.wios.manage.controller;

import cn.hutool.core.lang.Assert;
import com.boco.nbd.cams.core.constant.MessageConstant;
import com.boco.nbd.wios.manage.contants.WiosConstant;
import com.boco.nbd.wios.manage.entity.bo.Region;
import com.boco.nbd.wios.manage.entity.bo.RegionVo;
import com.boco.nbd.wios.manage.service.impl.RegionService;
import com.ihidea.component.api.v2.BaseResponse;
import com.ihidea.core.support.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 区域管理接口
 *
 * @author xgl
 * @version [版本号, 2020年9月21日]
 */
@RestController
@RequestMapping("api")
@Api(tags = "区域管理接口")
@ApiIgnore
public class RegionController {

    @Autowired
    private RegionService regionService;

    /**
     * 查询单个区域信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "region/detail")
    @ApiOperation(value = "查询区域信息")
    @ApiImplicitParam(paramType = "query", name = "id", value = "区域id", required = true, dataType = "int")
    public BaseResponse<Region> get(Integer id) {
        Assert.notNull(id, WiosConstant.EMPTY_ID);
        Region region = regionService.getById(id);
        return new BaseResponse<>(region);
    }

    /**
     * 查询子区域列表信息
     *
     * @param parentId
     * @return
     */
    @GetMapping(value = "region/children")
    @ApiOperation(value = "查询子区域列表信息")
    @ApiImplicitParam(paramType = "query", name = "parentId", value = "父级区域id", required = true, dataType = "int")
    public BaseResponse<List<Region>> getChildren(Integer parentId) {
        if (parentId == null) {
            throw new ServiceException(MessageConstant.MISSING_PARAM);
        }
        List<Region> region = regionService.getByParentId(parentId);
        return new BaseResponse<>(region);
    }

    /**
     * 查询所有区域
     *
     * @param status
     * @return
     */
    @GetMapping(value = "region/all")
    @ApiOperation(value = "查询区域列表")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "status", value = "状态", required = false, dataType = "int"),})
    public BaseResponse<List<RegionVo>> getAll(Integer status) {
        return new BaseResponse<>(regionService.selectAll(status));
    }

    /**
     * 添加区域
     *
     * @param region
     * @return
     */
    @PostMapping(value = "region/add")
    @ApiOperation(value = "添加区域信息")
    public BaseResponse<Object> add(Region region) {
        if (region.getParentId() == null) {
            throw new ServiceException(MessageConstant.MISSING_PARAM);
        }
        regionService.add(region);
        return new BaseResponse<>();
    }

    /**
     * 更新区域
     *
     * @param region
     */
    @PostMapping(value = "region/update")
    @ApiOperation(value = "更新区域信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "int", paramType = "query", required = true),
        @ApiImplicitParam(name = "status", value = "1:正常 2:删除", dataType = "int", paramType = "query", required = false),})
    public BaseResponse<Object> update(Region region) {
        Assert.notNull(region.getId(), WiosConstant.EMPTY_ID);
        if (region.getId() == 1) {
            throw new ServiceException("此数据不可修改");
        }
        regionService.update(region.getId(), region);
        return new BaseResponse<>();
    }
}