package ink.wgink.service.group.controller.api;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.pojo.dtos.group.GroupDTO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
import ink.wgink.service.group.pojo.vos.GroupVO;
import ink.wgink.service.group.service.IGroupService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName: GroupController
 * @Description: 组
 * @Author: wenc
 * @Date: 2019/1/6 3:03 PM
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "组")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/group")
public class GroupController extends DefaultBaseController {

    @Autowired
    private IGroupService groupService;

    @ApiOperation(value = "组新增", notes = "组新增接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult saveGroup(@RequestBody GroupVO groupVO) {
        groupService.save(groupVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "组删除", notes = "通过id列表批量删除组接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "组ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{ids}")
    public SuccessResult removeGroup(@PathVariable("ids") String ids) {
        groupService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "组修改", notes = "组修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "组ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{groupId}")
    @CheckRequestBodyAnnotation
    public SuccessResult update(@PathVariable("groupId") String groupId, @RequestBody GroupVO groupVO) {
        groupService.update(groupId, groupVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "组列表", notes = "组列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupParentId", value = "组上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listallbyparentid/{groupParentId}")
    public List<GroupDTO> listAllByParentId(@PathVariable("groupParentId") String groupParentId) {
        return groupService.listAllByParentId(groupParentId);
    }

    @ApiOperation(value = "组详情", notes = "组详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "组ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{groupId}")
    public GroupDTO get(@PathVariable("groupId") String groupId) {
        return groupService.get(groupId);
    }

    @ApiOperation(value = "组zTree列表", notes = "组zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "父ID", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listztree")
    public List<ZTreeDTO> listZTree() {
        Map<String, Object> params = requestParams();
        String groupParentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_ID) == null ? null : params.get(ISystemConstant.PARAMS_ID).toString())) {
            groupParentId = params.get(ISystemConstant.PARAMS_ID).toString();
        }
        params.put("groupParentId", groupParentId);
        return groupService.listZTree(params);
    }

    @ApiOperation(value = "组分页列表", notes = "组分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "上级ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<GroupDTO>> listPage(ListPage page) {
        Map<String, Object> params = requestParams();
        String groupParentId = "0";
        if (!StringUtils.isBlank(params.get("parentId") == null ? null : params.get("parentId").toString())) {
            groupParentId = params.get("parentId").toString();
        }
        params.put("groupParentId", groupParentId);
        page.setParams(params);
        return groupService.listPage(page);
    }

    @ApiOperation(value = "通过ID列表获取组列表", notes = "通过ID列表获取用户组接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("listbyids")
    public List<GroupDTO> listByIds(@RequestBody IdsVO idsVO) {
        return groupService.list(idsVO.getIds());
    }

}
